Skip to content

Commit 97a31f5

Browse files
feat: add useSendAndConfirmTransaction hook (#2674)
1 parent 319435a commit 97a31f5

File tree

11 files changed

+62
-16
lines changed

11 files changed

+62
-16
lines changed

.changeset/early-ants-repeat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": minor
3+
---
4+
5+
Add `useSendAndConfirmTransaction` hook

packages/thirdweb/src/exports/react-native.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export {
1616
} from "../react/core/hooks/wallets/wallet-hooks.js";
1717

1818
// contract related
19-
export { useReadContract } from "../react/core/hooks/contract/useRead.js";
20-
export { useSendTransaction } from "../react/core/hooks/contract/useSend.js";
19+
export { useReadContract } from "../react/core/hooks/contract/useReadContract.js";
20+
export { useSendTransaction } from "../react/core/hooks/contract/useSendTransaction.js";
2121
export { useEstimateGas } from "../react/core/hooks/contract/useEstimateGas.js";
2222
export { useWaitForReceipt } from "../react/core/hooks/contract/useWaitForReceipt.js";
2323
export { useContractEvents } from "../react/core/hooks/contract/useContractEvents.js";

packages/thirdweb/src/exports/react.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ export {
5151
} from "../react/core/hooks/wallets/wallet-hooks.js";
5252

5353
// contract related
54-
export { useReadContract } from "../react/core/hooks/contract/useRead.js";
55-
export { useSendTransaction } from "../react/core/hooks/contract/useSend.js";
54+
export { useReadContract } from "../react/core/hooks/contract/useReadContract.js";
55+
export { useSendTransaction } from "../react/core/hooks/contract/useSendTransaction.js";
56+
export { useSendAndConfirmTransaction } from "../react/core/hooks/contract/useSendAndConfirmTransaction.js";
5657
export { useEstimateGas } from "../react/core/hooks/contract/useEstimateGas.js";
5758
export { useEstimateGasCost } from "../react/core/hooks/contract/useEstimateGasCost.js";
5859
export { useWaitForReceipt } from "../react/core/hooks/contract/useWaitForReceipt.js";
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { useMutation, type UseMutationResult } from "@tanstack/react-query";
2+
import { useActiveAccount } from "../wallets/wallet-hooks.js";
3+
import type { PreparedTransaction } from "../../../../transaction/prepare-transaction.js";
4+
import type { TransactionReceipt } from "../../../../transaction/types.js";
5+
import { sendAndConfirmTransaction } from "../../../../transaction/actions/send-and-confirm-transaction.js";
6+
7+
/**
8+
* A hook to send a transaction.
9+
* @returns A mutation object to send a transaction.
10+
* @example
11+
* ```jsx
12+
* import { useSendAndConfirmTransaction } from "thirdweb/react";
13+
* const { mutate: sendAndConfirmTx, data: transactionReceipt } = useSendAndConfirmTransaction();
14+
*
15+
* // later
16+
* sendAndConfirmTx(tx);
17+
* ```
18+
* @transaction
19+
*/
20+
export function useSendAndConfirmTransaction(): UseMutationResult<
21+
TransactionReceipt,
22+
Error,
23+
PreparedTransaction
24+
> {
25+
const account = useActiveAccount();
26+
27+
return useMutation({
28+
mutationFn: async (transaction) => {
29+
if (!account) {
30+
throw new Error("No active account");
31+
}
32+
return await sendAndConfirmTransaction({
33+
transaction,
34+
account,
35+
});
36+
},
37+
});
38+
}

packages/thirdweb/src/react/core/hooks/contract/useSend.ts renamed to packages/thirdweb/src/react/core/hooks/contract/useSendTransaction.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ import type { PreparedTransaction } from "../../../../transaction/prepare-transa
1010
* @example
1111
* ```jsx
1212
* import { useSendTransaction } from "thirdweb/react";
13-
* const { mutate: sendTx, data: transactionHash } = useSendTransaction();
13+
* const { mutate: sendTx, data: transactionResult } = useSendTransaction();
1414
*
1515
* // later
16-
* const transactionHash = await sendTx(tx);
16+
* sendTx(tx);
1717
* ```
1818
* @transaction
1919
*/

packages/thirdweb/src/react/core/providers/thirdweb-provider.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function ThirdwebProvider(props: React.PropsWithChildren) {
3131
defaultOptions: {
3232
mutations: {
3333
onSettled: (data, error, variables) => {
34+
console.log("Mutation settled", data, error, variables);
3435
if (error) {
3536
// TODO: remove - but useful for debug now
3637
console.error("[Mutation Error]", error);
@@ -50,14 +51,14 @@ export function ThirdwebProvider(props: React.PropsWithChildren) {
5051
// invalidate any readContract queries for this chainId:contractAddress
5152
[
5253
"readContract",
53-
variables.contract.chain.id,
54-
variables.contract.address,
54+
variables.__contract?.chain.id,
55+
variables.__contract?.address,
5556
] as const,
5657
// invalidate any walletBalance queries for this chainId
5758
// TODO: add wallet address in here if we can get it somehow
5859
[
5960
"walletBalance",
60-
variables.contract.chain.id,
61+
variables.__contract?.chain.id,
6162
] as const,
6263
],
6364
});

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useState } from "react";
77
import type { Chain } from "../../../../../../../chains/types.js";
88
import type { BuyWithCryptoQuote } from "../../../../../../../pay/buyWithCrypto/actions/getQuote.js";
99
import type { Account } from "../../../../../../../wallets/interfaces/wallet.js";
10-
import { useSendTransaction } from "../../../../../../core/hooks/contract/useSend.js";
10+
import { useSendTransaction } from "../../../../../../core/hooks/contract/useSendTransaction.js";
1111
import { useChainQuery } from "../../../../../../core/hooks/others/useChainQuery.js";
1212
import {
1313
useBuyWithCryptoStatus,

packages/thirdweb/src/react/web/ui/TransactionButton/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
useActiveAccount,
99
useActiveWallet,
1010
} from "../../../core/hooks/wallets/wallet-hooks.js";
11-
import { useSendTransaction } from "../../../core/hooks/contract/useSend.js";
11+
import { useSendTransaction } from "../../../core/hooks/contract/useSendTransaction.js";
1212
import type { PreparedTransaction } from "../../../../transaction/prepare-transaction.js";
1313
import type { TransactionReceipt } from "../../../../transaction/types.js";
1414
import { useState } from "react";

packages/thirdweb/src/react/web/ui/hooks/useSendToken.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useMutation } from "@tanstack/react-query";
2-
import { useSendTransaction } from "../../../core/hooks/contract/useSend.js";
2+
import { useSendTransaction } from "../../../core/hooks/contract/useSendTransaction.js";
33
import { useActiveWalletChain } from "../../../core/hooks/wallets/wallet-hooks.js";
44
import { toWei } from "../../../../utils/units.js";
55
import { getContract } from "../../../../contract/contract.js";

packages/thirdweb/src/transaction/types.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type { ThirdwebContract } from "../contract/contract.js";
1010
import { isObjectWithKeys } from "../utils/type-guards.js";
1111
import type { Hex } from "../utils/encoding/hex.js";
1212
import type { TransactionReceipt as ViemTransactionReceipt } from "viem";
13+
import type { PreparedTransaction } from "./prepare-transaction.js";
1314

1415
export type SendTransactionResult = {
1516
readonly transactionHash: Hex;
@@ -44,11 +45,11 @@ export type BaseTransactionOptions<
4445
*/
4546
export function isBaseTransactionOptions(
4647
value: unknown,
47-
): value is BaseTransactionOptions {
48+
): value is PreparedTransaction {
4849
return (
49-
isObjectWithKeys(value, ["contract"]) &&
50-
isObjectWithKeys(value.contract, ["address", "chain"]) &&
51-
typeof value.contract.address === "string"
50+
isObjectWithKeys(value, ["__contract"]) &&
51+
isObjectWithKeys(value.__contract, ["address", "chain"]) &&
52+
typeof value.__contract.address === "string"
5253
);
5354
}
5455

0 commit comments

Comments
 (0)