|
| 1 | +import { type UseMutationResult, useMutation } from "@tanstack/react-query"; |
| 2 | +import type { GaslessOptions } from "../../../../transaction/actions/gasless/types.js"; |
| 3 | +import type { SendTransactionOptions } from "../../../../transaction/actions/send-transaction.js"; |
| 4 | +import { waitForReceipt } from "../../../../transaction/actions/wait-for-tx-receipt.js"; |
| 5 | +import type { TransactionReceipt } from "../../../../transaction/types.js"; |
| 6 | +import type { SendTransactionPayModalConfig } from "../../../core/hooks/transaction/useSendTransaction.js"; |
| 7 | +import { useSendTransaction } from "./useSendTransaction.js"; |
| 8 | + |
| 9 | +/** |
| 10 | + * Configuration for the `useSendAndConfirmTransaction` hook. |
| 11 | + */ |
| 12 | +type SendAndConfirmTransactionConfig = { |
| 13 | + /** |
| 14 | + * Refer to [`SendTransactionPayModalConfig`](https://portal.thirdweb.com/references/typescript/v5/SendTransactionPayModalConfig) for more details. |
| 15 | + */ |
| 16 | + payModal?: SendTransactionPayModalConfig; |
| 17 | + |
| 18 | + /** |
| 19 | + * Configuration for gasless transactions. |
| 20 | + * Refer to [`GaslessOptions`](https://portal.thirdweb.com/references/typescript/v5/GaslessOptions) for more details. |
| 21 | + */ |
| 22 | + gasless?: GaslessOptions; |
| 23 | +}; |
| 24 | + |
| 25 | +/** |
| 26 | + * A hook to send a transaction and confirm the transaction with users's connected wallet |
| 27 | + * @returns A mutation object to send a transaction. |
| 28 | + * @example |
| 29 | + * ```jsx |
| 30 | + * import { useSendAndConfirmTransaction } from "thirdweb/react"; |
| 31 | + * const { mutate: sendAndConfirmTx, data: transactionReceipt } = useSendAndConfirmTransaction(); |
| 32 | + * |
| 33 | + * // later |
| 34 | + * sendAndConfirmTx(tx); |
| 35 | + * ``` |
| 36 | + * |
| 37 | + * |
| 38 | + * ### Gasless usage with [thirdweb Engine](https://portal.thirdweb.com/engine) |
| 39 | + * ```tsx |
| 40 | + * import { useSendAndConfirmTransaction } from "thirdweb/react"; |
| 41 | + * const mutation = useSendAndConfirmTransaction({ |
| 42 | + * gasless: { |
| 43 | + * provider: "engine", |
| 44 | + * relayerUrl: "https://thirdweb.engine-***.thirdweb.com/relayer/***", |
| 45 | + * relayerForwarderAddress: "0x...", |
| 46 | + * } |
| 47 | + * }); |
| 48 | + * ``` |
| 49 | + * |
| 50 | + * ### Gasless usage with OpenZeppelin |
| 51 | + * ```tsx |
| 52 | + * import { useSendAndConfirmTransaction } from "thirdweb/react"; |
| 53 | + * const mutation = useSendAndConfirmTransaction({ |
| 54 | + * gasless: { |
| 55 | + * provider: "openzeppelin", |
| 56 | + * relayerUrl: "https://...", |
| 57 | + * relayerForwarderAddress: "0x...", |
| 58 | + * } |
| 59 | + * }); |
| 60 | + * ``` |
| 61 | + * |
| 62 | + * ### Configuring the Pay Modal |
| 63 | + * |
| 64 | + * When the wallet does not have enough funds to send the transaction, a modal is shown to the user to buy the required funds and then continue with the transaction. |
| 65 | + * |
| 66 | + * You can configure the pay modal by passing a [`SendTransactionPayModalConfig`](https://portal.thirdweb.com/references/typescript/v5/SendTransactionPayModalConfig) object to the `payModal` config. |
| 67 | + * |
| 68 | + * ```tsx |
| 69 | + * import { useSendAndConfirmTransaction } from "thirdweb/react"; |
| 70 | + * |
| 71 | + * const sendAndConfirmTx = useSendAndConfirmTransaction({ |
| 72 | + * payModal: { |
| 73 | + * theme: "light", |
| 74 | + * }, |
| 75 | + * }); |
| 76 | + * ``` |
| 77 | + * |
| 78 | + * By default, the pay modal is enabled. You can disable it by passing `payModal: false` to the config. |
| 79 | + * |
| 80 | + * ```tsx |
| 81 | + * import { useSendAndConfirmTransaction } from "thirdweb/react"; |
| 82 | + * |
| 83 | + * const sendAndConfirmTx = useSendAndConfirmTransaction({ |
| 84 | + * payModal: false, |
| 85 | + * }); |
| 86 | + * ``` |
| 87 | + * @transaction |
| 88 | + */ |
| 89 | +export function useSendAndConfirmTransaction( |
| 90 | + config: SendAndConfirmTransactionConfig = {}, |
| 91 | +): UseMutationResult< |
| 92 | + TransactionReceipt, |
| 93 | + Error, |
| 94 | + SendTransactionOptions["transaction"] |
| 95 | +> { |
| 96 | + const sendTx = useSendTransaction(config); |
| 97 | + return useMutation({ |
| 98 | + mutationFn: async (transaction) => { |
| 99 | + const receipt = await sendTx.mutateAsync(transaction); |
| 100 | + const confirmedReceipt = await waitForReceipt(receipt); |
| 101 | + return confirmedReceipt; |
| 102 | + }, |
| 103 | + }); |
| 104 | +} |
0 commit comments