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/dull-lamps-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Batch approvals and swaps if using smart wallets
2 changes: 2 additions & 0 deletions packages/thirdweb/src/pay/buyWithCrypto/getStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type BuyWithCryptoQuoteSummary = {
export type BuyWithCryptoTransaction = {
client: ThirdwebClient;
transactionHash: string;
chainId: number; // optional for backwards compatibility
};

type BuyWithCryptoStatuses = "NONE" | "PENDING" | "FAILED" | "COMPLETED";
Expand Down Expand Up @@ -133,6 +134,7 @@ export async function getBuyWithCryptoStatus(
}
const queryString = new URLSearchParams({
transactionHash: buyWithCryptoTransaction.transactionHash,
chainId: buyWithCryptoTransaction.chainId.toString(),
}).toString();
const url = `${getPayBuyWithCryptoStatusUrl()}?${queryString}`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function SwapDetailsScreen(props: {
? {
client: client,
transactionHash: initialStatus.source.transactionHash,
chainId: initialStatus.source.token.chainId,
}
: undefined,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export function useBuyTransactionsToShow(client: ThirdwebClient) {
const swapStatus = await getBuyWithCryptoStatus({
client: client,
transactionHash: tx.txHash,
chainId: tx.chainId,
});

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ import type { ThirdwebClient } from "../../../../../../../client/client.js";
import { getContract } from "../../../../../../../contract/contract.js";
import { approve } from "../../../../../../../extensions/erc20/write/approve.js";
import type { BuyWithCryptoQuote } from "../../../../../../../pay/buyWithCrypto/getQuote.js";
import { sendBatchTransaction } from "../../../../../../../transaction/actions/send-batch-transaction.js";
import { sendTransaction } from "../../../../../../../transaction/actions/send-transaction.js";
import { waitForReceipt } from "../../../../../../../transaction/actions/wait-for-tx-receipt.js";
import {
type WaitForReceiptOptions,
waitForReceipt,
} from "../../../../../../../transaction/actions/wait-for-tx-receipt.js";
import { shortenAddress } from "../../../../../../../utils/address.js";
import { formatNumber } from "../../../../../../../utils/formatNumber.js";
import { useCustomTheme } from "../../../../../../core/design-system/CustomThemeProvider.js";
Expand Down Expand Up @@ -55,11 +59,13 @@ export function SwapConfirmationScreen(props: {
payer: PayerInfo;
preApprovedAmount?: bigint;
}) {
const needsApproval =
const approveTxRequired =
props.quote.approvalData &&
props.preApprovedAmount !== undefined &&
props.preApprovedAmount < BigInt(props.quote.approvalData.amountWei);
const initialStep = needsApproval ? "approval" : "swap";
const needsApprovalStep =
approveTxRequired && !props.payer.account.sendBatchTransaction;
const initialStep = needsApprovalStep ? "approval" : "swap";

const [step, setStep] = useState<"approval" | "swap">(initialStep);
const [status, setStatus] = useState<
Expand Down Expand Up @@ -142,7 +148,7 @@ export function SwapConfirmationScreen(props: {
<Spacer y="xl" />

{/* Show 2 steps - Approve and confirm */}
{needsApproval && (
{needsApprovalStep && (
<>
<Spacer y="sm" />
<Container
Expand Down Expand Up @@ -251,8 +257,6 @@ export function SwapConfirmationScreen(props: {
if (step === "swap") {
setStatus("pending");
try {
const tx = props.quote.transactionRequest;

trackPayEvent({
event: "prompt_swap_execution",
client: props.client,
Expand All @@ -265,11 +269,31 @@ export function SwapConfirmationScreen(props: {
chainId: props.quote.swapDetails.fromToken.chainId,
dstChainId: props.quote.swapDetails.toToken.chainId,
});
const tx = props.quote.transactionRequest;
let _swapTx: WaitForReceiptOptions;
// check if we can batch approval and swap
const canBatch = props.payer.account.sendBatchTransaction;
if (canBatch && props.quote.approvalData && approveTxRequired) {
const approveTx = approve({
contract: getContract({
client: props.client,
address: props.quote.swapDetails.fromToken.tokenAddress,
chain: props.fromChain,
}),
spender: props.quote.approvalData.spenderAddress,
amountWei: BigInt(props.quote.approvalData.amountWei),
});

const _swapTx = await sendTransaction({
account: props.payer.account,
transaction: tx,
});
_swapTx = await sendBatchTransaction({
account: props.payer.account,
transactions: [approveTx, tx],
});
} else {
_swapTx = await sendTransaction({
account: props.payer.account,
transaction: tx,
});
}

await waitForReceipt({ ..._swapTx, maxBlocksWaitTime: 50 });

Expand All @@ -291,6 +315,7 @@ export function SwapConfirmationScreen(props: {
addPendingTx({
type: "swap",
txHash: _swapTx.transactionHash,
chainId: _swapTx.chain.id,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function FiatValue(
<Text {...props}>
{cryptoToFiatQuery.data?.result
? `$${formatNumber(cryptoToFiatQuery.data.result, 2)}`
: "$0.00"}
: "-"}
</Text>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export function SwapFlow(props: SwapFlowProps) {
onBack={props.onBack}
onTryAgain={props.onTryAgain}
swapTxHash={swapTxHash}
fromChain={fromChain}
client={props.client}
onDone={props.onDone}
transactionMode={props.transactionMode}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { CheckCircledIcon } from "@radix-ui/react-icons";
import { useQueryClient } from "@tanstack/react-query";
import { useEffect, useRef } from "react";
import type { Chain } from "../../../../../../../chains/types.js";
import type { ThirdwebClient } from "../../../../../../../client/client.js";
import type { BuyWithCryptoQuote } from "../../../../../../../pay/buyWithCrypto/getQuote.js";
import type { BuyWithCryptoStatus } from "../../../../../../../pay/buyWithCrypto/getStatus.js";
Expand All @@ -21,6 +22,7 @@ export function SwapStatusScreen(props: {
title: string;
onBack?: () => void;
swapTxHash: string;
fromChain: Chain;
client: ThirdwebClient;
onTryAgain: () => void;
onDone: () => void;
Expand All @@ -34,6 +36,7 @@ export function SwapStatusScreen(props: {
const swapStatus = useBuyWithCryptoStatus({
client: props.client,
transactionHash: props.swapTxHash,
chainId: props.fromChain.id,
});

let uiStatus: UIStatus = "pending";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function TransferFlow(props: TransferFlowProps) {
onBack={props.onBack}
onTryAgain={props.onTryAgain}
swapTxHash={transferTxHash}
fromChain={props.chain}
client={props.client}
onDone={props.onDone}
transactionMode={false}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ type PendingTxInfo =
| {
type: "swap";
txHash: string;
chainId: number;
}
| {
type: "fiat";
Expand Down
Loading