@@ -6,7 +6,7 @@ import type { ThirdwebClient } from "../../../../../../../client/client.js";
6
6
import { getContract } from "../../../../../../../contract/contract.js" ;
7
7
import { allowance } from "../../../../../../../extensions/erc20/__generated__/IERC20/read/allowance.js" ;
8
8
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" ;
10
10
import type { BuyWithCryptoStatus } from "../../../../../../../pay/buyWithCrypto/getStatus.js" ;
11
11
import type { BuyWithFiatQuote } from "../../../../../../../pay/buyWithFiat/getQuote.js" ;
12
12
import {
@@ -17,6 +17,7 @@ import {
17
17
type OnRampStep ,
18
18
getOnRampSteps ,
19
19
} from "../../../../../../../pay/buyWithFiat/isSwapRequiredPostOnramp.js" ;
20
+ import type { PayTokenInfo } from "../../../../../../../pay/utils/commonTypes.js" ;
20
21
import { sendBatchTransaction } from "../../../../../../../transaction/actions/send-batch-transaction.js" ;
21
22
import { sendTransaction } from "../../../../../../../transaction/actions/send-transaction.js" ;
22
23
import type { WaitForReceiptOptions } from "../../../../../../../transaction/actions/wait-for-tx-receipt.js" ;
@@ -28,7 +29,6 @@ import type { Wallet } from "../../../../../../../wallets/interfaces/wallet.js";
28
29
import { isSmartWallet } from "../../../../../../../wallets/smart/is-smart-wallet.js" ;
29
30
import { spacing } from "../../../../../../core/design-system/index.js" ;
30
31
import { useChainName } from "../../../../../../core/hooks/others/useChainQuery.js" ;
31
- import { useBuyWithCryptoQuote } from "../../../../../../core/hooks/pay/useBuyWithCryptoQuote.js" ;
32
32
import { useBuyWithCryptoStatus } from "../../../../../../core/hooks/pay/useBuyWithCryptoStatus.js" ;
33
33
import { useBuyWithFiatStatus } from "../../../../../../core/hooks/pay/useBuyWithFiatStatus.js" ;
34
34
import { useConnectedWallets } from "../../../../../../core/hooks/wallets/useConnectedWallets.js" ;
@@ -319,20 +319,6 @@ function useOnRampScreenState(props: {
319
319
// Get quote for current swap/bridge step if needed
320
320
const previousStep = onRampSteps [ currentStepIndex - 1 ] ;
321
321
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
- ) ;
336
322
337
323
// Handle swap execution
338
324
const swapMutation = useSwapMutation ( {
@@ -375,9 +361,9 @@ function useOnRampScreenState(props: {
375
361
status = "completed" ;
376
362
} else if ( index === currentStepIndex ) {
377
363
// Current step - could be swap or bridge
378
- if ( swapQuoteQuery . isLoading || swapMutation . isPending ) {
364
+ if ( swapMutation . isPending ) {
379
365
status = "pending" ;
380
- } else if ( swapQuoteQuery . error || swapMutation . error ) {
366
+ } else if ( swapMutation . error ) {
381
367
status = "failed" ;
382
368
} else if ( swapTxHash ) {
383
369
status = swapStatus ;
@@ -418,11 +404,13 @@ function useOnRampScreenState(props: {
418
404
type : "fiat" ,
419
405
intentId : props . quote . intentId ,
420
406
} ) ;
421
- } else if ( swapQuoteQuery . data && ! swapTxHash ) {
407
+ } else if ( previousStep && currentStep && ! swapTxHash ) {
422
408
// Execute swap/bridge
423
409
try {
424
410
const result = await swapMutation . mutateAsync ( {
425
- quote : swapQuoteQuery . data ,
411
+ fromToken : previousStep . token ,
412
+ toToken : currentStep . token ,
413
+ amount : currentStep . amount ,
426
414
} ) ;
427
415
setSwapTxHash ( {
428
416
hash : result . transactionHash ,
@@ -435,23 +423,22 @@ function useOnRampScreenState(props: {
435
423
// retry the quote step
436
424
setSwapTxHash ( undefined ) ;
437
425
swapMutation . reset ( ) ;
438
- await swapQuoteQuery . refetch ( ) ;
439
426
}
440
427
} , [
441
428
isDone ,
442
429
currentStepIndex ,
443
- swapQuoteQuery . data ,
444
430
swapTxHash ,
445
431
props . quote ,
446
432
props . onDone ,
447
433
swapMutation ,
448
434
props . theme ,
449
435
isFailed ,
450
- swapQuoteQuery . refetch ,
451
436
swapMutation . reset ,
452
437
props . client ,
453
438
props . payer . account . address ,
454
439
props . payer . wallet . id ,
440
+ currentStep ,
441
+ previousStep ,
455
442
] ) ;
456
443
457
444
// Auto-progress effect
@@ -467,15 +454,13 @@ function useOnRampScreenState(props: {
467
454
! isFailed &&
468
455
currentStepIndex > 0 &&
469
456
currentStepIndex < onRampSteps . length &&
470
- swapQuoteQuery . data &&
471
457
! swapTxHash
472
458
) {
473
459
handleContinue ( ) ;
474
460
}
475
461
} , [
476
462
props . isAutoMode ,
477
463
currentStepIndex ,
478
- swapQuoteQuery . data ,
479
464
swapTxHash ,
480
465
onRampSteps . length ,
481
466
handleContinue ,
@@ -643,8 +628,24 @@ function useSwapMutation(props: {
643
628
} ) {
644
629
const queryClient = useQueryClient ( ) ;
645
630
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
+
648
649
const canBatch = props . payer . account . sendBatchTransaction ;
649
650
const tokenContract = getContract ( {
650
651
client : props . client ,
0 commit comments