Skip to content

Commit

Permalink
add more checks to query enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jonator committed Jun 4, 2024
1 parent 10725b2 commit 67d8b87
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions packages/web/hooks/use-swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export function useSwap(
const { isOneClickTradingEnabled, oneClickTradingInfo } =
useOneClickTradingSession();
const { t } = useTranslation();
const { isLoading: isWalletLoading } = useWalletSelect();

const swapAssets = useSwapAssets({
initialFromDenom,
Expand All @@ -119,14 +120,15 @@ export function useSwap(
// load flags
const isToFromAssets =
Boolean(swapAssets.fromAsset) && Boolean(swapAssets.toAsset);
const canLoadQuote =
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 &&
!Boolean(account?.txTypeInProgress);
!Boolean(account?.txTypeInProgress) &&
!isWalletLoading;

const {
data: quote,
Expand All @@ -140,11 +142,11 @@ export function useSwap(
forcePoolId: forceSwapInPoolId,
maxSlippage,
},
canLoadQuote
quoteQueryEnabled
);
/** If a query is not enabled, it is considered loading.
* Work around this by checking if the query is enabled and if the query is loading to be considered loading. */
const isQuoteLoading = isQuoteLoading_ && canLoadQuote;
const isQuoteLoading = isQuoteLoading_ && quoteQueryEnabled;

const {
data: spotPriceQuote,
Expand Down Expand Up @@ -196,6 +198,7 @@ export function useSwap(
const networkFeeQueryEnabled =
featureFlags.swapToolSimulateFee &&
!Boolean(precedentError) &&
// includes check for quoteQueryEnabled
!isQuoteLoading &&
Boolean(quote) &&
Boolean(account?.address);
Expand All @@ -211,10 +214,7 @@ export function useSwap(
useOneClickTrading: isOneClickTradingEnabled,
},
});
const isLoadingNetworkFee =
featureFlags.swapToolSimulateFee &&
isLoadingNetworkFee_ &&
networkFeeQueryEnabled;
const isLoadingNetworkFee = isLoadingNetworkFee_ && networkFeeQueryEnabled;

const hasExceededOneClickTradingGasLimit = useMemo(() => {
if (
Expand Down Expand Up @@ -774,6 +774,7 @@ function useSwapAmountInput({
}) {
const { chainStore, accountStore } = useStore();
const account = accountStore.getWallet(chainStore.osmosis.chainId);
const { isLoading: isLoadingWallet } = useWalletSelect();

const featureFlags = useFeatureFlags();

Expand All @@ -785,10 +786,15 @@ function useSwapAmountInput({
});

const balanceQuoteQueryEnabled =
!!inAmountInput.balance &&
!inAmountInput.balance?.toDec().isZero() &&
!isLoadingWallet &&
Boolean(swapAssets.fromAsset) &&
Boolean(swapAssets.toAsset);
Boolean(swapAssets.toAsset) &&
inAmountInput.debouncedInAmount?.currency.coinMinimalDenom ===
swapAssets.fromAsset!.coinMinimalDenom &&
!!inAmountInput.balance &&
!inAmountInput.balance.toDec().isZero() &&
inAmountInput.balance.currency.coinMinimalDenom ===
swapAssets.fromAsset?.coinMinimalDenom;
const {
data: quoteForCurrentBalance,
isLoading: isQuoteForCurrentBalanceLoading_,
Expand All @@ -806,8 +812,9 @@ function useSwapAmountInput({
const isQuoteForCurrentBalanceLoading =
isQuoteForCurrentBalanceLoading_ && balanceQuoteQueryEnabled;

const networkQueryEnabled =
const networkFeeQueryEnabled =
featureFlags.swapToolSimulateFee &&
// includes check for balanceQuoteQueryEnabled
!isQuoteForCurrentBalanceLoading &&
Boolean(quoteForCurrentBalance) &&
!Boolean(account?.txTypeInProgress);
Expand All @@ -818,10 +825,10 @@ function useSwapAmountInput({
} = useEstimateTxFees({
chainId: chainStore.osmosis.chainId,
messages: quoteForCurrentBalance?.messages,
enabled: networkQueryEnabled,
enabled: networkFeeQueryEnabled,
});
const isLoadingCurrentBalanceNetworkFee =
networkQueryEnabled && isLoadingCurrentBalanceNetworkFee_;
networkFeeQueryEnabled && isLoadingCurrentBalanceNetworkFee_;

const hasErrorWithCurrentBalanceQuote = useMemo(() => {
return !!currentBalanceNetworkFeeError || !!quoteForCurrentBalanceError;
Expand Down

0 comments on commit 67d8b87

Please sign in to comment.