Skip to content

Commit

Permalink
only set max if no quotes are loading (#3543)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonator authored Jul 17, 2024
1 parent 21bf5d0 commit 09f2224
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
5 changes: 5 additions & 0 deletions packages/web/components/bridge/amount-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,11 @@ export const AmountScreen = observer(
isInsufficientBal={Boolean(isInsufficientBal)}
isInsufficientFee={Boolean(isInsufficientFee)}
transferGasCost={selectedQuote?.gasCost}
/** Wait for all quotes to resolve before modifying input amount.
* This helps reduce thrash while the best quote is being determined.
* Only once we get the best quote, we can modify the input amount
* to account for gas then restart the quote search process. */
canSetMax={!quote.isLoadingAnyBridgeQuote}
setFiatAmount={setFiatAmount}
setCryptoAmount={setCryptoAmount}
setInputUnit={setInputUnit}
Expand Down
10 changes: 6 additions & 4 deletions packages/web/components/bridge/crypto-fiat-input.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export const CryptoFiatInput: FunctionComponent<{
isInsufficientFee: boolean;
fromChain: BridgeChainWithDisplayInfo;
transferGasCost: CoinPretty | undefined;
canSetMax?: boolean;
setFiatAmount: (amount: string) => void;
setCryptoAmount: (amount: string) => void;
setInputUnit: (unit: "fiat" | "crypto") => void;
Expand All @@ -50,6 +51,7 @@ export const CryptoFiatInput: FunctionComponent<{
isInsufficientBal,
isInsufficientFee,
transferGasCost,
canSetMax = true,
setFiatAmount: setFiatAmountProp,
setCryptoAmount: setCryptoAmountProp,
setInputUnit,
Expand Down Expand Up @@ -152,7 +154,7 @@ export const CryptoFiatInput: FunctionComponent<{

// Subtract gas cost and adjust input when selecting max amount
useEffect(() => {
if (isMax && transferGasCost) {
if (isMax && transferGasCost && canSetMax) {
let maxTransferAmount = new Dec(0);

const gasFeeMatchesInputDenom =
Expand All @@ -175,14 +177,14 @@ export const CryptoFiatInput: FunctionComponent<{
onInput("crypto")(trimPlaceholderZeros(maxTransferAmount.toString()));
}
}
}, [isMax, transferGasCost, asset.amount, inputCoin, onInput]);
}, [isMax, canSetMax, transferGasCost, asset.amount, inputCoin, onInput]);

// Apply max amount if asset changes
useEffect(() => {
if (isMax) {
if (isMax && canSetMax) {
onInput("crypto")(trimPlaceholderZeros(asset.amount.toDec().toString()));
}
}, [asset, isMax, onInput]);
}, [asset, isMax, canSetMax, onInput]);

const fiatCurrentValue = `${assetPrice.symbol}${fiatInputRaw}`;
const fiatInputFontSize = calcTextSizeClass(
Expand Down
20 changes: 15 additions & 5 deletions packages/web/components/bridge/use-bridge-quotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,8 @@ export const useBridgeQuotes = ({

const numSucceeded = successfulQuotes.length;
const isOneSuccessful = Boolean(numSucceeded);
const amountOfErrors = erroredQuotes.length;
const isOneErrored = Boolean(amountOfErrors);
const isAllSuccessful = numSucceeded === bridges.length;
const isOneErrored = Boolean(erroredQuotes.length);

// if none have returned a resulting quote, find some error
const someError = useMemo(
Expand Down Expand Up @@ -653,17 +653,25 @@ export const useBridgeQuotes = ({
(!isOneSuccessful ||
quoteResults.every((quoteResult) => quoteResult.isLoading)) &&
quoteResults.some((quoteResult) => quoteResult.fetchStatus !== "idle");
const isLoadingAnyBridgeQuote = quoteResults.some(
(quoteResult) => quoteResult.isLoading && quoteResult.fetchStatus !== "idle"
);
const isLoadingBridgeTransaction =
bridgeTransaction.isLoading && bridgeTransaction.fetchStatus !== "idle";
const isWithdrawReady = isWithdraw && !isTxPending;
const isWalletConnected =
const isWithdrawReady =
isWithdraw && !isTxPending && !isLoadingBridgeTransaction;
const isFromWalletConnected =
fromChain?.chainType === "evm"
? isEvmWalletConnected
: fromChain?.chainType === "cosmos"
? accountStore.getWallet(fromChain.chainId)?.isWalletConnected ?? false
: false;
const isDepositReady =
isDeposit && isWalletConnected && !isLoadingBridgeQuote && !isTxPending;
isDeposit &&
isFromWalletConnected &&
!isLoadingBridgeQuote &&
!isTxPending &&
!isLoadingBridgeTransaction;
const userCanAdvance =
(isDepositReady || isWithdrawReady) &&
!isInsufficientFee &&
Expand Down Expand Up @@ -711,13 +719,15 @@ export const useBridgeQuotes = ({
warnUserOfPriceImpact,

successfulQuotes,
isAllQuotesSuccessful: isAllSuccessful,
selectedBridgeProvider,
setSelectedBridgeProvider: onChangeBridgeProvider,

selectedQuote,
selectedQuoteUpdatedAt: selectedQuoteQuery?.dataUpdatedAt,
refetchInterval,
isLoadingBridgeQuote,
isLoadingAnyBridgeQuote,
isLoadingBridgeTransaction,
isRefetchingQuote: selectedQuoteQuery?.isRefetching ?? false,
};
Expand Down

0 comments on commit 09f2224

Please sign in to comment.