Skip to content

Commit

Permalink
avoid sending gas fee queries while tx is pending (#3324)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonator committed Jun 12, 2024
1 parent 3c225f8 commit 96a804f
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions packages/web/hooks/use-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,18 @@ export function useSwap(
// load flags
const isToFromAssets =
Boolean(swapAssets.fromAsset) && Boolean(swapAssets.toAsset);

const quoteQueryEnabled =
isToFromAssets &&
Boolean(inAmountInput.debouncedInAmount?.toDec().isPositive()) &&
// since input is debounced there could be the wrong asset associated
// 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_,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 ===
Expand All @@ -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_,
Expand Down

0 comments on commit 96a804f

Please sign in to comment.