Skip to content

Commit c407bd1

Browse files
[SDK] fix: Fetch fresh quotes before post onramp step in PayEmbed (#6515)
1 parent e540dc1 commit c407bd1

File tree

3 files changed

+43
-27
lines changed

3 files changed

+43
-27
lines changed

.changeset/silly-times-shop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Always fetch fresh quotes before post onramp step in PayEmbed

apps/playground-web/src/components/pay/embed.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ export function StyledPayEmbedPreview() {
1818
<div className="flex flex-col items-center justify-center">
1919
<StyledConnectButton
2020
chains={[base, defineChain(466), arbitrum, treasure, arbitrumNova]}
21+
accountAbstraction={{
22+
sponsorGas: true,
23+
chain: base,
24+
}}
2125
supportedTokens={{
2226
466: [
2327
{
@@ -50,6 +54,12 @@ export function StyledPayEmbedPreview() {
5054
<PayEmbed
5155
client={THIRDWEB_CLIENT}
5256
theme={theme === "light" ? "light" : "dark"}
57+
connectOptions={{
58+
accountAbstraction: {
59+
sponsorGas: true,
60+
chain: base,
61+
},
62+
}}
5363
payOptions={{
5464
mode: "fund_wallet",
5565
metadata: {

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

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import type { ThirdwebClient } from "../../../../../../../client/client.js";
66
import { getContract } from "../../../../../../../contract/contract.js";
77
import { allowance } from "../../../../../../../extensions/erc20/__generated__/IERC20/read/allowance.js";
88
import { approve } from "../../../../../../../extensions/erc20/write/approve.js";
9-
import type { BuyWithCryptoQuote } from "../../../../../../../pay/buyWithCrypto/getQuote.js";
9+
import { getBuyWithCryptoQuote } from "../../../../../../../pay/buyWithCrypto/getQuote.js";
1010
import type { BuyWithCryptoStatus } from "../../../../../../../pay/buyWithCrypto/getStatus.js";
1111
import type { BuyWithFiatQuote } from "../../../../../../../pay/buyWithFiat/getQuote.js";
1212
import {
@@ -17,6 +17,7 @@ import {
1717
type OnRampStep,
1818
getOnRampSteps,
1919
} from "../../../../../../../pay/buyWithFiat/isSwapRequiredPostOnramp.js";
20+
import type { PayTokenInfo } from "../../../../../../../pay/utils/commonTypes.js";
2021
import { sendBatchTransaction } from "../../../../../../../transaction/actions/send-batch-transaction.js";
2122
import { sendTransaction } from "../../../../../../../transaction/actions/send-transaction.js";
2223
import type { WaitForReceiptOptions } from "../../../../../../../transaction/actions/wait-for-tx-receipt.js";
@@ -28,7 +29,6 @@ import type { Wallet } from "../../../../../../../wallets/interfaces/wallet.js";
2829
import { isSmartWallet } from "../../../../../../../wallets/smart/is-smart-wallet.js";
2930
import { spacing } from "../../../../../../core/design-system/index.js";
3031
import { useChainName } from "../../../../../../core/hooks/others/useChainQuery.js";
31-
import { useBuyWithCryptoQuote } from "../../../../../../core/hooks/pay/useBuyWithCryptoQuote.js";
3232
import { useBuyWithCryptoStatus } from "../../../../../../core/hooks/pay/useBuyWithCryptoStatus.js";
3333
import { useBuyWithFiatStatus } from "../../../../../../core/hooks/pay/useBuyWithFiatStatus.js";
3434
import { useConnectedWallets } from "../../../../../../core/hooks/wallets/useConnectedWallets.js";
@@ -319,20 +319,6 @@ function useOnRampScreenState(props: {
319319
// Get quote for current swap/bridge step if needed
320320
const previousStep = onRampSteps[currentStepIndex - 1];
321321
const currentStep = onRampSteps[currentStepIndex];
322-
const swapQuoteQuery = useBuyWithCryptoQuote(
323-
previousStep && currentStep
324-
? {
325-
fromChainId: previousStep.token.chainId,
326-
fromTokenAddress: previousStep.token.tokenAddress,
327-
toAmount: currentStep.amount,
328-
toChainId: currentStep.token.chainId,
329-
toTokenAddress: currentStep.token.tokenAddress,
330-
fromAddress: props.payer.account.address,
331-
toAddress: props.payer.account.address,
332-
client: props.client,
333-
}
334-
: undefined,
335-
);
336322

337323
// Handle swap execution
338324
const swapMutation = useSwapMutation({
@@ -375,9 +361,9 @@ function useOnRampScreenState(props: {
375361
status = "completed";
376362
} else if (index === currentStepIndex) {
377363
// Current step - could be swap or bridge
378-
if (swapQuoteQuery.isLoading || swapMutation.isPending) {
364+
if (swapMutation.isPending) {
379365
status = "pending";
380-
} else if (swapQuoteQuery.error || swapMutation.error) {
366+
} else if (swapMutation.error) {
381367
status = "failed";
382368
} else if (swapTxHash) {
383369
status = swapStatus;
@@ -418,11 +404,13 @@ function useOnRampScreenState(props: {
418404
type: "fiat",
419405
intentId: props.quote.intentId,
420406
});
421-
} else if (swapQuoteQuery.data && !swapTxHash) {
407+
} else if (previousStep && currentStep && !swapTxHash) {
422408
// Execute swap/bridge
423409
try {
424410
const result = await swapMutation.mutateAsync({
425-
quote: swapQuoteQuery.data,
411+
fromToken: previousStep.token,
412+
toToken: currentStep.token,
413+
amount: currentStep.amount,
426414
});
427415
setSwapTxHash({
428416
hash: result.transactionHash,
@@ -435,23 +423,22 @@ function useOnRampScreenState(props: {
435423
// retry the quote step
436424
setSwapTxHash(undefined);
437425
swapMutation.reset();
438-
await swapQuoteQuery.refetch();
439426
}
440427
}, [
441428
isDone,
442429
currentStepIndex,
443-
swapQuoteQuery.data,
444430
swapTxHash,
445431
props.quote,
446432
props.onDone,
447433
swapMutation,
448434
props.theme,
449435
isFailed,
450-
swapQuoteQuery.refetch,
451436
swapMutation.reset,
452437
props.client,
453438
props.payer.account.address,
454439
props.payer.wallet.id,
440+
currentStep,
441+
previousStep,
455442
]);
456443

457444
// Auto-progress effect
@@ -467,15 +454,13 @@ function useOnRampScreenState(props: {
467454
!isFailed &&
468455
currentStepIndex > 0 &&
469456
currentStepIndex < onRampSteps.length &&
470-
swapQuoteQuery.data &&
471457
!swapTxHash
472458
) {
473459
handleContinue();
474460
}
475461
}, [
476462
props.isAutoMode,
477463
currentStepIndex,
478-
swapQuoteQuery.data,
479464
swapTxHash,
480465
onRampSteps.length,
481466
handleContinue,
@@ -643,8 +628,24 @@ function useSwapMutation(props: {
643628
}) {
644629
const queryClient = useQueryClient();
645630
return useMutation({
646-
mutationFn: async (input: { quote: BuyWithCryptoQuote }) => {
647-
const { quote } = input;
631+
mutationFn: async (input: {
632+
fromToken: PayTokenInfo;
633+
toToken: PayTokenInfo;
634+
amount: string;
635+
}) => {
636+
const { fromToken, toToken, amount } = input;
637+
// always get a fresh quote before executing
638+
const quote = await getBuyWithCryptoQuote({
639+
fromChainId: fromToken.chainId,
640+
fromTokenAddress: fromToken.tokenAddress,
641+
toAmount: amount,
642+
toChainId: toToken.chainId,
643+
toTokenAddress: toToken.tokenAddress,
644+
fromAddress: props.payer.account.address,
645+
toAddress: props.payer.account.address,
646+
client: props.client,
647+
});
648+
648649
const canBatch = props.payer.account.sendBatchTransaction;
649650
const tokenContract = getContract({
650651
client: props.client,

0 commit comments

Comments
 (0)