Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pretty-geese-win.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Add Pay Modal integration in `useSendAndConfirmTransaction` hook similar to `useSendTransaction` hook
2 changes: 1 addition & 1 deletion packages/thirdweb/src/exports/react.native.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export {
export type { TransactionButtonProps } from "../react/core/hooks/transaction/transaction-button-utils.js";
export { useEstimateGas } from "../react/core/hooks/transaction/useEstimateGas.js";
export { useEstimateGasCost } from "../react/core/hooks/transaction/useEstimateGasCost.js";
export { useSendAndConfirmTransaction } from "../react/core/hooks/transaction/useSendAndConfirmTransaction.js";
export { useSendAndConfirmTransactionCore as useSendAndConfirmTransaction } from "../react/core/hooks/transaction/useSendAndConfirmTransaction.js";
export { useSendBatchTransaction } from "../react/core/hooks/transaction/useSendBatchTransaction.js";
// transaction
export type {
Expand Down
3 changes: 1 addition & 2 deletions packages/thirdweb/src/exports/react.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export {
export type { TransactionButtonProps } from "../react/core/hooks/transaction/transaction-button-utils.js";
export { useEstimateGas } from "../react/core/hooks/transaction/useEstimateGas.js";
export { useEstimateGasCost } from "../react/core/hooks/transaction/useEstimateGasCost.js";
export { useSendAndConfirmTransaction } from "../react/core/hooks/transaction/useSendAndConfirmTransaction.js";
export { useSendBatchTransaction } from "../react/core/hooks/transaction/useSendBatchTransaction.js";
// transaction
export type {
Expand Down Expand Up @@ -101,7 +100,6 @@ export { useConnectionManager } from "../react/core/providers/connection-manager
// Social
export { useSocialProfiles } from "../react/core/social/useSocialProfiles.js";
export type { AccountBalanceInfo } from "../react/core/utils/account.js";

// utils
export { createContractQuery } from "../react/core/utils/createQuery.js";
// tokens
Expand All @@ -126,6 +124,7 @@ export {
WalletProvider,
type WalletProviderProps,
} from "../react/core/wallet/provider.js";
export { useSendAndConfirmTransaction } from "../react/web/hooks/transaction/useSendAndConfirmTransaction.js";
export { useSendTransaction } from "../react/web/hooks/transaction/useSendTransaction.js";
export { useAutoConnect } from "../react/web/hooks/wallets/useAutoConnect.js";
export { useLinkProfile } from "../react/web/hooks/wallets/useLinkProfile.js";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ type SendAndConfirmTransactionConfig = {
* ```
* @transaction
*/
export function useSendAndConfirmTransaction(
export function useSendAndConfirmTransactionCore(
config: SendAndConfirmTransactionConfig = {},
): UseMutationResult<
TransactionReceipt,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { type UseMutationResult, useMutation } from "@tanstack/react-query";
import type { GaslessOptions } from "../../../../transaction/actions/gasless/types.js";
import type { SendTransactionOptions } from "../../../../transaction/actions/send-transaction.js";
import { waitForReceipt } from "../../../../transaction/actions/wait-for-tx-receipt.js";
import type { TransactionReceipt } from "../../../../transaction/types.js";
import type { SendTransactionPayModalConfig } from "../../../core/hooks/transaction/useSendTransaction.js";
import { useSendTransaction } from "./useSendTransaction.js";

/**
* Configuration for the `useSendAndConfirmTransaction` hook.
*/
type SendAndConfirmTransactionConfig = {
/**
* Refer to [`SendTransactionPayModalConfig`](https://portal.thirdweb.com/references/typescript/v5/SendTransactionPayModalConfig) for more details.
*/
payModal?: SendTransactionPayModalConfig;

/**
* Configuration for gasless transactions.
* Refer to [`GaslessOptions`](https://portal.thirdweb.com/references/typescript/v5/GaslessOptions) for more details.
*/
gasless?: GaslessOptions;
};

/**
* A hook to send a transaction and confirm the transaction with users's connected wallet
* @returns A mutation object to send a transaction.
* @example
* ```jsx
* import { useSendAndConfirmTransaction } from "thirdweb/react";
* const { mutate: sendAndConfirmTx, data: transactionReceipt } = useSendAndConfirmTransaction();
*
* // later
* sendAndConfirmTx(tx);
* ```
*
*
* ### Gasless usage with [thirdweb Engine](https://portal.thirdweb.com/engine)
* ```tsx
* import { useSendAndConfirmTransaction } from "thirdweb/react";
* const mutation = useSendAndConfirmTransaction({
* gasless: {
* provider: "engine",
* relayerUrl: "https://thirdweb.engine-***.thirdweb.com/relayer/***",
* relayerForwarderAddress: "0x...",
* }
* });
* ```
*
* ### Gasless usage with OpenZeppelin
* ```tsx
* import { useSendAndConfirmTransaction } from "thirdweb/react";
* const mutation = useSendAndConfirmTransaction({
* gasless: {
* provider: "openzeppelin",
* relayerUrl: "https://...",
* relayerForwarderAddress: "0x...",
* }
* });
* ```
*
* ### Configuring the Pay Modal
*
* 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.
*
* You can configure the pay modal by passing a [`SendTransactionPayModalConfig`](https://portal.thirdweb.com/references/typescript/v5/SendTransactionPayModalConfig) object to the `payModal` config.
*
* ```tsx
* import { useSendAndConfirmTransaction } from "thirdweb/react";
*
* const sendAndConfirmTx = useSendAndConfirmTransaction({
* payModal: {
* theme: "light",
* },
* });
* ```
*
* By default, the pay modal is enabled. You can disable it by passing `payModal: false` to the config.
*
* ```tsx
* import { useSendAndConfirmTransaction } from "thirdweb/react";
*
* const sendAndConfirmTx = useSendAndConfirmTransaction({
* payModal: false,
* });
* ```
* @transaction
*/
export function useSendAndConfirmTransaction(
config: SendAndConfirmTransactionConfig = {},
): UseMutationResult<
TransactionReceipt,
Error,
SendTransactionOptions["transaction"]
> {
const sendTx = useSendTransaction(config);
return useMutation({
mutationFn: async (transaction) => {
const receipt = await sendTx.mutateAsync(transaction);
const confirmedReceipt = await waitForReceipt(receipt);
return confirmedReceipt;
},

Check warning on line 102 in packages/thirdweb/src/react/web/hooks/transaction/useSendAndConfirmTransaction.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/hooks/transaction/useSendAndConfirmTransaction.tsx#L99-L102

Added lines #L99 - L102 were not covered by tests
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,32 @@ import { TransactionModal } from "../../ui/TransactionButton/TransactionModal.js
* };
* ```
*
* ### Configuring the Pay Modal
*
* 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.
*
* You can configure the pay modal by passing a [`SendTransactionPayModalConfig`](https://portal.thirdweb.com/references/typescript/v5/SendTransactionPayModalConfig) object to the `payModal` config.
*
* ```tsx
* import { useSendTransaction } from "thirdweb/react";
*
* const sendTx = useSendTransaction({
* payModal: {
* theme: "light",
* },
* });
* ```
*
* By default, the pay modal is enabled. You can disable it by passing `payModal: false` to the config.
*
* ```tsx
* import { useSendTransaction } from "thirdweb/react";
*
* const sendTx = useSendTransaction({
* payModal: false,
* });
* ```
*
* @transaction
*/
export function useSendTransaction(config: SendTransactionConfig = {}) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
import type { BaseTransactionOptions } from "../../../../../../transaction/types.js";
import { useReadContract } from "../../../../../core/hooks/contract/useReadContract.js";
import type { TransactionButtonProps } from "../../../../../core/hooks/transaction/transaction-button-utils.js";
import { useSendAndConfirmTransaction } from "../../../../../core/hooks/transaction/useSendAndConfirmTransaction.js";
import { useActiveAccount } from "../../../../../core/hooks/wallets/useActiveAccount.js";
import { useSendAndConfirmTransaction } from "../../../../hooks/transaction/useSendAndConfirmTransaction.js";
import { TransactionButton } from "../../../TransactionButton/index.js";

export type BuyDirectListingButtonProps = Omit<
Expand Down Expand Up @@ -104,7 +104,15 @@
},
});

const { mutateAsync } = useSendAndConfirmTransaction();
const { mutateAsync } = useSendAndConfirmTransaction({
payModal:
typeof payModal === "object"
? {
...payModal,
metadata: payModal.metadata || payMetadata,
}

Check warning on line 113 in packages/thirdweb/src/react/web/ui/prebuilt/thirdweb/BuyDirectListingButton/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/prebuilt/thirdweb/BuyDirectListingButton/index.tsx#L110-L113

Added lines #L110 - L113 were not covered by tests
: payModal,
});

const prepareBuyTransaction = useCallback(async () => {
if (!account) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import type { BaseTransactionOptions } from "../../../../../../transaction/types.js";
import type { Account } from "../../../../../../wallets/interfaces/wallet.js";
import { useReadContract } from "../../../../../core/hooks/contract/useReadContract.js";
import { useSendAndConfirmTransaction } from "../../../../../core/hooks/transaction/useSendAndConfirmTransaction.js";
import { useActiveAccount } from "../../../../../core/hooks/wallets/useActiveAccount.js";
import { useSendAndConfirmTransaction } from "../../../../hooks/transaction/useSendAndConfirmTransaction.js";
import { TransactionButton } from "../../../TransactionButton/index.js";
import type {
ClaimButtonProps,
Expand Down Expand Up @@ -126,7 +126,15 @@
tokenId: claimParams.type === "ERC1155" ? claimParams.tokenId : undefined,
});
const account = useActiveAccount();
const { mutateAsync } = useSendAndConfirmTransaction();
const { mutateAsync } = useSendAndConfirmTransaction({
payModal:
typeof payModal === "object"
? {
...payModal,
metadata: payModal.metadata || payMetadata,
}

Check warning on line 135 in packages/thirdweb/src/react/web/ui/prebuilt/thirdweb/ClaimButton/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/prebuilt/thirdweb/ClaimButton/index.tsx#L132-L135

Added lines #L132 - L135 were not covered by tests
: payModal,
});
return (
<TransactionButton
payModal={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
import type { BaseTransactionOptions } from "../../../../../../transaction/types.js";
import { useReadContract } from "../../../../../core/hooks/contract/useReadContract.js";
import type { TransactionButtonProps } from "../../../../../core/hooks/transaction/transaction-button-utils.js";
import { useSendAndConfirmTransaction } from "../../../../../core/hooks/transaction/useSendAndConfirmTransaction.js";
import { useActiveAccount } from "../../../../../core/hooks/wallets/useActiveAccount.js";
import { useSendAndConfirmTransaction } from "../../../../hooks/transaction/useSendAndConfirmTransaction.js";
import { TransactionButton } from "../../../TransactionButton/index.js";

export type CreateDirectListingButtonProps = Omit<
Expand Down Expand Up @@ -88,7 +88,15 @@
},
tokenId,
});
const { mutateAsync } = useSendAndConfirmTransaction();
const { mutateAsync } = useSendAndConfirmTransaction({
payModal:
typeof payModal === "object"
? {
...payModal,
metadata: payModal.metadata || payMetadata,
}

Check warning on line 97 in packages/thirdweb/src/react/web/ui/prebuilt/thirdweb/CreateDirectListingButton/index.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/prebuilt/thirdweb/CreateDirectListingButton/index.tsx#L94-L97

Added lines #L94 - L97 were not covered by tests
: payModal,
});

const prepareTransaction = useCallback(async () => {
if (!account) {
Expand Down
Loading