From 96a804fefe55505efc05073ebf3c0ba71da68aee Mon Sep 17 00:00:00 2001 From: Jon Ator Date: Wed, 12 Jun 2024 14:03:53 -0400 Subject: [PATCH] avoid sending gas fee queries while tx is pending (#3324) --- packages/web/hooks/use-swap.tsx | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/packages/web/hooks/use-swap.tsx b/packages/web/hooks/use-swap.tsx index b634cac848..fb90b5bd31 100644 --- a/packages/web/hooks/use-swap.tsx +++ b/packages/web/hooks/use-swap.tsx @@ -117,6 +117,7 @@ export function useSwap( // load flags const isToFromAssets = Boolean(swapAssets.fromAsset) && Boolean(swapAssets.toAsset); + const quoteQueryEnabled = isToFromAssets && Boolean(inAmountInput.debouncedInAmount?.toDec().isPositive()) && @@ -124,9 +125,10 @@ export function useSwap( // with the input amount when switching assets inAmountInput.debouncedInAmount?.currency.coinMinimalDenom === swapAssets.fromAsset?.coinMinimalDenom && + inAmountInput.amount?.currency.coinMinimalDenom === + swapAssets.fromAsset?.coinMinimalDenom && !account?.txTypeInProgress && !isWalletLoading; - const { data: quote, isLoading: isQuoteLoading_, @@ -195,13 +197,17 @@ export function useSwap( const networkFeeQueryEnabled = featureFlags.swapToolSimulateFee && !Boolean(precedentError) && - // includes check for quoteQueryEnabled !isQuoteLoading && - Boolean(quote) && + quoteQueryEnabled && + Boolean(quote?.messages) && Boolean(account?.address) && inAmountInput.debouncedInAmount !== null && inAmountInput.balance && - inAmountInput.debouncedInAmount.toDec().lte(inAmountInput.balance.toDec()); + inAmountInput.amount && + inAmountInput.debouncedInAmount + .toDec() + .lte(inAmountInput.balance.toDec()) && + inAmountInput.amount.toDec().lte(inAmountInput.balance.toDec()); const { data: networkFee, error: networkFeeError, @@ -790,12 +796,16 @@ function useSwapAmountInput({ }); const balanceQuoteQueryEnabled = + featureFlags.swapToolSimulateFee && !isLoadingWallet && + !account?.txTypeInProgress && Boolean(swapAssets.fromAsset) && Boolean(swapAssets.toAsset) && // since the in amount is debounced, the asset could be wrong when switching assets inAmountInput.debouncedInAmount?.currency.coinMinimalDenom === swapAssets.fromAsset!.coinMinimalDenom && + inAmountInput.amount?.currency.coinMinimalDenom === + swapAssets.fromAsset!.coinMinimalDenom && !!inAmountInput.balance && !inAmountInput.balance.toDec().isZero() && inAmountInput.balance.currency.coinMinimalDenom === @@ -818,11 +828,9 @@ function useSwapAmountInput({ isQuoteForCurrentBalanceLoading_ && balanceQuoteQueryEnabled; const networkFeeQueryEnabled = - featureFlags.swapToolSimulateFee && - // includes check for balanceQuoteQueryEnabled !isQuoteForCurrentBalanceLoading && - Boolean(quoteForCurrentBalance) && - !account?.txTypeInProgress; + balanceQuoteQueryEnabled && + Boolean(quoteForCurrentBalance); const { data: currentBalanceNetworkFee, isLoading: isLoadingCurrentBalanceNetworkFee_,