-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keep logic in
useDepositBTCTransaction
- Loading branch information
1 parent
7d77844
commit 1440e07
Showing
3 changed files
with
45 additions
and
30 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
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { useMutation, UseMutationOptions } from "@tanstack/react-query" | ||
import { useConnector } from "./orangeKit" | ||
import { useWallet } from "./useWallet" | ||
|
||
type MutationFnParams = { | ||
recipient: string | ||
amount: bigint | ||
} | ||
|
||
type UseDepositBTCTransactionOptions = Omit< | ||
UseMutationOptions<string, Error, MutationFnParams>, | ||
"mutationKey" | "mutationFn" | ||
> | ||
|
||
export default function useDepositBTCTransaction( | ||
options: UseDepositBTCTransactionOptions, | ||
) { | ||
const connector = useConnector() | ||
const { address } = useWallet() | ||
return useMutation({ | ||
mutationKey: ["send-bitcoin"], | ||
mutationFn: async ({ recipient, amount }: MutationFnParams) => { | ||
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() | ||
|
||
const satoshis = Number(amount) | ||
|
||
// @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 | ||
}, | ||
...options, | ||
}) | ||
} |