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

Handle wallet chain mismatch on post onramp flow
10 changes: 0 additions & 10 deletions apps/playground-web/src/components/pay/embed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ export function StyledPayEmbedPreview() {
<div className="flex flex-col items-center justify-center">
<StyledConnectButton
chains={[base, defineChain(466), arbitrum, treasure, arbitrumNova]}
accountAbstraction={{
sponsorGas: true,
chain: base,
}}
supportedTokens={{
466: [
{
Expand Down Expand Up @@ -54,12 +50,6 @@ export function StyledPayEmbedPreview() {
<PayEmbed
client={THIRDWEB_CLIENT}
theme={theme === "light" ? "light" : "dark"}
connectOptions={{
accountAbstraction: {
sponsorGas: true,
chain: base,
},
}}
payOptions={{
mode: "fund_wallet",
metadata: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -634,19 +634,32 @@
amount: string;
}) => {
const { fromToken, toToken, amount } = input;
const wallet = props.payer.wallet;

Check warning on line 637 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L637

Added line #L637 was not covered by tests

// in case the wallet is not on the same chain as the fromToken, switch to it
if (wallet.getChain()?.id !== fromToken.chainId) {
await wallet.switchChain(getCachedChain(fromToken.chainId));
}

Check warning on line 642 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L640-L642

Added lines #L640 - L642 were not covered by tests

const account = wallet.getAccount();

Check warning on line 644 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L644

Added line #L644 was not covered by tests

if (!account) {
throw new Error("Payer wallet has no account");
}

Check warning on line 648 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L646-L648

Added lines #L646 - L648 were not covered by tests

// always get a fresh quote before executing
const quote = await getBuyWithCryptoQuote({
fromChainId: fromToken.chainId,
fromTokenAddress: fromToken.tokenAddress,
toAmount: amount,
toChainId: toToken.chainId,
toTokenAddress: toToken.tokenAddress,
fromAddress: props.payer.account.address,
toAddress: props.payer.account.address,
fromAddress: account.address,
toAddress: account.address,

Check warning on line 658 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L657-L658

Added lines #L657 - L658 were not covered by tests
client: props.client,
});

const canBatch = props.payer.account.sendBatchTransaction;
const canBatch = account.sendBatchTransaction;

Check warning on line 662 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L662

Added line #L662 was not covered by tests
const tokenContract = getContract({
client: props.client,
address: quote.swapDetails.fromToken.tokenAddress,
Expand All @@ -656,14 +669,14 @@
quote.approvalData &&
(await allowance({
contract: tokenContract,
owner: props.payer.account.address,
owner: account.address,

Check warning on line 672 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L672

Added line #L672 was not covered by tests
spender: quote.approvalData.spenderAddress,
})) < BigInt(quote.approvalData.amountWei);
if (approveTxRequired && quote.approvalData && !canBatch) {
trackPayEvent({
event: "prompt_swap_approval",
client: props.client,
walletAddress: props.payer.account.address,
walletAddress: account.address,

Check warning on line 679 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L679

Added line #L679 was not covered by tests
walletType: props.payer.wallet.id,
fromToken: quote.swapDetails.fromToken.tokenAddress,
fromAmount: quote.swapDetails.fromAmountWei,
Expand All @@ -680,7 +693,7 @@
});

const tx = await sendTransaction({
account: props.payer.account,
account,

Check warning on line 696 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L696

Added line #L696 was not covered by tests
transaction,
});

Expand All @@ -689,7 +702,7 @@
trackPayEvent({
event: "swap_approval_success",
client: props.client,
walletAddress: props.payer.account.address,
walletAddress: account.address,

Check warning on line 705 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L705

Added line #L705 was not covered by tests
walletType: props.payer.wallet.id,
fromToken: quote.swapDetails.fromToken.tokenAddress,
fromAmount: quote.swapDetails.fromAmountWei,
Expand All @@ -703,7 +716,7 @@
trackPayEvent({
event: "prompt_swap_execution",
client: props.client,
walletAddress: props.payer.account.address,
walletAddress: account.address,

Check warning on line 719 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L719

Added line #L719 was not covered by tests
walletType: props.payer.wallet.id,
fromToken: quote.swapDetails.fromToken.tokenAddress,
fromAmount: quote.swapDetails.fromAmountWei,
Expand All @@ -723,12 +736,12 @@
});

_swapTx = await sendBatchTransaction({
account: props.payer.account,
account,

Check warning on line 739 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L739

Added line #L739 was not covered by tests
transactions: [approveTx, tx],
});
} else {
_swapTx = await sendTransaction({
account: props.payer.account,
account,

Check warning on line 744 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L744

Added line #L744 was not covered by tests
transaction: tx,
});
}
Expand All @@ -738,7 +751,7 @@
trackPayEvent({
event: "swap_execution_success",
client: props.client,
walletAddress: props.payer.account.address,
walletAddress: account.address,

Check warning on line 754 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/fiat/OnRampScreen.tsx#L754

Added line #L754 was not covered by tests
walletType: props.payer.wallet.id,
fromToken: quote.swapDetails.fromToken.tokenAddress,
fromAmount: quote.swapDetails.fromAmountWei,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,15 +160,28 @@
fullWidth
disabled={status === "pending"}
onClick={async () => {
const wallet = props.payer.wallet;

Check warning on line 163 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L163 was not covered by tests

// in case the wallet is not on the same chain as the fromToken, switch to it
if (wallet.getChain()?.id !== props.fromChain.id) {
await wallet.switchChain(props.fromChain);
}

Check warning on line 168 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx#L166-L168

Added lines #L166 - L168 were not covered by tests

const account = wallet.getAccount();

Check warning on line 170 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L170 was not covered by tests

if (!account) {
throw new Error("Payer wallet has no account");
}

Check warning on line 174 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx#L172-L174

Added lines #L172 - L174 were not covered by tests

if (step === "approval" && props.quote.approvalData) {
try {
setStatus("pending");

trackPayEvent({
event: "prompt_swap_approval",
client: props.client,
walletAddress: props.payer.account.address,
walletType: props.payer.wallet.id,
walletAddress: account.address,
walletType: wallet.id,

Check warning on line 184 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx#L183-L184

Added lines #L183 - L184 were not covered by tests
fromToken: props.quote.swapDetails.fromToken.tokenAddress,
fromAmount: props.quote.swapDetails.fromAmountWei,
toToken: props.quote.swapDetails.toToken.tokenAddress,
Expand All @@ -188,7 +201,7 @@
});

const tx = await sendTransaction({
account: props.payer.account,
account: account,

Check warning on line 204 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L204 was not covered by tests
transaction,
});

Expand All @@ -197,8 +210,8 @@
trackPayEvent({
event: "swap_approval_success",
client: props.client,
walletAddress: props.payer.account.address,
walletType: props.payer.wallet.id,
walletAddress: account.address,
walletType: wallet.id,

Check warning on line 214 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx#L213-L214

Added lines #L213 - L214 were not covered by tests
fromToken: props.quote.swapDetails.fromToken.tokenAddress,
fromAmount: props.quote.swapDetails.fromAmountWei,
toToken: props.quote.swapDetails.toToken.tokenAddress,
Expand All @@ -221,8 +234,8 @@
trackPayEvent({
event: "prompt_swap_execution",
client: props.client,
walletAddress: props.payer.account.address,
walletType: props.payer.wallet.id,
walletAddress: account.address,
walletType: wallet.id,

Check warning on line 238 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx#L237-L238

Added lines #L237 - L238 were not covered by tests
fromToken: props.quote.swapDetails.fromToken.tokenAddress,
fromAmount: props.quote.swapDetails.fromAmountWei,
toToken: props.quote.swapDetails.toToken.tokenAddress,
Expand All @@ -233,7 +246,7 @@
const tx = props.quote.transactionRequest;
let _swapTx: WaitForReceiptOptions;
// check if we can batch approval and swap
const canBatch = props.payer.account.sendBatchTransaction;
const canBatch = account.sendBatchTransaction;

Check warning on line 249 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L249 was not covered by tests
if (
canBatch &&
props.quote.approvalData &&
Expand All @@ -250,21 +263,21 @@
});

_swapTx = await sendBatchTransaction({
account: props.payer.account,
account: account,

Check warning on line 266 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L266 was not covered by tests
transactions: [approveTx, tx],
});
} else {
_swapTx = await sendTransaction({
account: props.payer.account,
account: account,

Check warning on line 271 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L271 was not covered by tests
transaction: tx,
});
}

trackPayEvent({
event: "swap_execution_success",
client: props.client,
walletAddress: props.payer.account.address,
walletType: props.payer.wallet.id,
walletAddress: account.address,
walletType: wallet.id,

Check warning on line 280 in packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/react/web/ui/ConnectWallet/screens/Buy/swap/ConfirmationScreen.tsx#L279-L280

Added lines #L279 - L280 were not covered by tests
fromToken: props.quote.swapDetails.fromToken.tokenAddress,
fromAmount: props.quote.swapDetails.fromAmountWei,
toToken: props.quote.swapDetails.toToken.tokenAddress,
Expand Down
17 changes: 13 additions & 4 deletions packages/thirdweb/src/wallets/smart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,12 @@
executeOverride: options.overrides?.execute,
});

const chain = getCachedChain(transaction.chainId);
const result = await _sendUserOp({
executeTx,
options: {
...options,
chain: getCachedChain(transaction.chainId),
chain,
accountContract,
overrides: {
...options.overrides,
Expand All @@ -278,7 +279,7 @@
});
trackTransaction({
client: options.client,
chainId: options.chain.id,
chainId: chain.id,
transactionHash: result.transactionHash,
walletAddress: options.accountContract.address,
walletType: "smart",
Expand All @@ -292,17 +293,25 @@
transactions,
executeBatchOverride: options.overrides?.executeBatch,
});
if (transactions.length === 0) {
throw new Error("No transactions to send");
}

Check warning on line 298 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L297-L298

Added lines #L297 - L298 were not covered by tests
const firstTx = transactions[0];
if (!firstTx) {
throw new Error("No transactions to send");
}

Check warning on line 302 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L301-L302

Added lines #L301 - L302 were not covered by tests
const chain = getCachedChain(firstTx.chainId);
const result = await _sendUserOp({
executeTx,
options: {
...options,
chain: getCachedChain(transactions[0]?.chainId ?? options.chain.id),
chain,
accountContract,
},
});
trackTransaction({
client: options.client,
chainId: options.chain.id,
chainId: chain.id,
transactionHash: result.transactionHash,
walletAddress: options.accountContract.address,
walletType: "smart",
Expand Down