-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor
useDepositBTCTransaction
and use useMutation
- Loading branch information
1 parent
8ceb6b4
commit 1717ea5
Showing
4 changed files
with
60 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,51 @@ | ||
import { useCallback, useEffect, useState } from "react" | ||
import { OnErrorCallback, OnSuccessCallback } from "#/types" | ||
import { useMutation } from "@tanstack/react-query" | ||
import { useWallet } from "./useWallet" | ||
import { useConnector } from "./orangeKit/useConnector" | ||
|
||
type SendBitcoinTransactionParams = Parameters< | ||
(recipient: string, amount: bigint) => void | ||
> | ||
|
||
type UseDepositBTCTransactionReturn = { | ||
inProgress: boolean | ||
transactionHash?: string | ||
sendBitcoinTransaction: ( | ||
...params: SendBitcoinTransactionParams | ||
) => Promise<void> | ||
} | ||
|
||
export function useDepositBTCTransaction( | ||
onSuccess?: OnSuccessCallback, | ||
onSuccess?: OnSuccessCallback<string>, | ||
onError?: OnErrorCallback, | ||
): UseDepositBTCTransactionReturn { | ||
) { | ||
const connector = useConnector() | ||
const { address } = useWallet() | ||
|
||
const [transactionHash, setTransactionHash] = useState<string | undefined>( | ||
undefined, | ||
) | ||
const [inProgress, setInProgress] = useState<boolean>(false) | ||
|
||
useEffect(() => { | ||
if (transactionHash && onSuccess) { | ||
onSuccess() | ||
} | ||
}, [onSuccess, transactionHash]) | ||
|
||
const sendBitcoinTransaction = useCallback( | ||
async (recipient: string, amount: bigint) => { | ||
const { | ||
mutate: sendBitcoinTransaction, | ||
status, | ||
data, | ||
} = useMutation({ | ||
mutationKey: ["send-bitcoin"], | ||
mutationFn: async ({ | ||
recipient, | ||
amount, | ||
}: { | ||
recipient: string | ||
amount: bigint | ||
}) => { | ||
if (!address) { | ||
throw new Error("Bitcoin account was not connected.") | ||
} | ||
|
||
if (!connector) { | ||
throw new Error("Connector was not defined.") | ||
} | ||
// @ts-expect-error adjust types to handle bitcoin wallet wrappers | ||
const client = await connector.getClient() | ||
|
||
try { | ||
// @ts-expect-error adjust types to handle bitcoin wallet wrappers | ||
const client = await connector.getClient() | ||
|
||
const satoshis = Number(amount) | ||
const satoshis = Number(amount) | ||
|
||
setInProgress(true) | ||
// @ts-expect-error adjust types to handle bitcoin wallet wrappers | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment | ||
const txhash: string = await client.sendBitcoin(recipient, satoshis) | ||
setTransactionHash(txhash) | ||
} catch (error) { | ||
if (onError) { | ||
onError(error) | ||
} | ||
console.error(error) | ||
} finally { | ||
setInProgress(false) | ||
} | ||
// @ts-expect-error adjust types to handle bitcoin wallet wrappers | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment | ||
const txhash: string = await client.sendBitcoin(recipient, satoshis) | ||
return txhash | ||
}, | ||
onSuccess: (responseData) => onSuccess?.(responseData), | ||
onError: (error) => { | ||
onError?.(error) | ||
console.error(error) | ||
}, | ||
[address, connector, onError], | ||
) | ||
}) | ||
|
||
return { sendBitcoinTransaction, transactionHash, inProgress } | ||
return { sendBitcoinTransaction, status, data } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export type OnSuccessCallback = () => void | ||
export type OnSuccessCallback<DataType = unknown> = (data: DataType) => void | ||
|
||
export type OnErrorCallback<ErrorType = unknown> = (error: ErrorType) => void |