Skip to content

Commit

Permalink
Keep logic in useDepositBTCTransaction
Browse files Browse the repository at this point in the history
  • Loading branch information
kkosiorowska committed Nov 7, 2024
1 parent 7d77844 commit 1440e07
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import {
useActionFlowPause,
useActionFlowTokenAmount,
useAppDispatch,
useConnector,
useDepositBTCTransaction,
useInvalidateQueries,
useStakeFlowContext,
useVerifyDepositAddress,
useWallet,
} from "#/hooks"
import { eip1193, logPromiseFailure } from "#/utils"
import { PROCESS_STATUSES } from "#/types"
Expand All @@ -20,8 +19,6 @@ import WalletInteractionModal from "../WalletInteractionModal"
const { userKeys } = queryKeysFactory

export default function DepositBTCModal() {
const connector = useConnector()
const { address } = useWallet()
const tokenAmount = useActionFlowTokenAmount()
const { btcAddress, depositReceipt, stake } = useStakeFlowContext()
const verifyDepositAddress = useVerifyDepositAddress()
Expand Down Expand Up @@ -70,32 +67,7 @@ export default function DepositBTCModal() {
[onError, handlePause],
)

const { mutate: sendBitcoinTransaction, status } = 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()

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
},
const { mutate: sendBitcoinTransaction, status } = useDepositBTCTransaction({
onSuccess: onDepositBTCSuccess,
onError: onDepositBTCError,
})
Expand Down
1 change: 1 addition & 0 deletions dapp/src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ export { default as useSignMessageAndCreateSession } from "./useSignMessageAndCr
export { default as useScrollbarVisibility } from "./useScrollbarVisibility"
export { default as useAccessCode } from "./useAccessCode"
export { default as useFormField } from "./useFormField"
export { default as useDepositBTCTransaction } from "./useDepositBTCTransaction"
42 changes: 42 additions & 0 deletions dapp/src/hooks/useDepositBTCTransaction.ts
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,
})
}

0 comments on commit 1440e07

Please sign in to comment.