From 6d86dba39feb91a2abb2304f15d368adf149466a Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 26 Mar 2024 12:11:18 -0600 Subject: [PATCH] Revert "Fix: update swap to match SQS return (#2991)" (#2997) (#2998) --- packages/pools/src/router/types.ts | 2 +- packages/web/.gitignore | 2 +- packages/web/components/swap-tool/index.tsx | 113 +++++++++----------- packages/web/hooks/use-swap.ts | 71 +++++++++--- packages/web/integrations/sidecar/router.ts | 2 +- 5 files changed, 113 insertions(+), 77 deletions(-) diff --git a/packages/pools/src/router/types.ts b/packages/pools/src/router/types.ts index 66b6749c629..0b71ce426fa 100644 --- a/packages/pools/src/router/types.ts +++ b/packages/pools/src/router/types.ts @@ -62,7 +62,7 @@ export type SplitTokenInQuote = Quote & { /** In amount after fees paid are subtracted. */ tokenInFeeAmount?: Int; swapFee?: Dec; - inBaseOutQuoteSpotPrice?: Dec; + inOutSpotPrice?: Dec; }; export type Logger = { diff --git a/packages/web/.gitignore b/packages/web/.gitignore index 06da521bb65..b12a8ac9d06 100644 --- a/packages/web/.gitignore +++ b/packages/web/.gitignore @@ -46,4 +46,4 @@ generated robots.txt sitemap-*.xml -sitemap.xml +sitemap.xml \ No newline at end of file diff --git a/packages/web/components/swap-tool/index.tsx b/packages/web/components/swap-tool/index.tsx index 6da954a7260..10cdf40f8d5 100644 --- a/packages/web/components/swap-tool/index.tsx +++ b/packages/web/components/swap-tool/index.tsx @@ -153,13 +153,6 @@ export const SwapTool: FunctionComponent = observer( // to & from box switch animation const [isHoveringSwitchButton, setHoveringSwitchButton] = useState(false); - // true if the spot price from quote result is available - const isSwapAssetsSpotPriceAvailable = - swapState && - !swapState.isQuoteLoading && - swapState.quote && - swapState.quote.inBaseOutQuoteSpotPrice; - // user action const sendSwapTx = () => { // prompt to select wallet insteaad of swapping @@ -658,7 +651,7 @@ export const SwapTool: FunctionComponent = observer( isLoaded={ Boolean(swapState.toAsset) && Boolean(swapState.fromAsset) && - !swapState.isQuoteLoading + !swapState.isSpotPriceQuoteLoading } > {/* TODO - move this custom button to our own button component */} @@ -674,45 +667,46 @@ export const SwapTool: FunctionComponent = observer( setShowEstimateDetails((show) => !show); }} > - {swapState.toAsset && isSwapAssetsSpotPriceAvailable && ( -
- - 1{" "} - - {ellipsisText( - swapState.fromAsset?.coinDenom ?? "", - isMobile ? 11 : 20 - )} - {" "} - {`≈ ${formatPretty( - new CoinPretty( - swapState.toAsset, - // asserting quote and inBaseOutQuoteSpotPrice is acceptable - // as this is guarded by a isSwapAssetsSpotPriceAvailable check - // that checks for quote and inBaseOutQuoteSpotPrice being available - swapState.quote!.inBaseOutQuoteSpotPrice!.mul( - DecUtils.getTenExponentN( - swapState.fromAsset.coinDecimals - ) - ) - ), - { - maxDecimals: Math.min( - swapState.toAsset.coinDecimals, - 8 - ), - } - )}`} - -
- )} + + 1{" "} + + {ellipsisText( + swapState.fromAsset?.coinDenom ?? "", + isMobile ? 11 : 20 + )} + {" "} + {`≈ ${ + swapState.toAsset + ? formatPretty( + (swapState.quote?.inOutSpotPrice + ? new CoinPretty( + swapState.toAsset, + swapState.quote.inOutSpotPrice.mul( + DecUtils.getTenExponentN( + swapState.toAsset.coinDecimals + ) + ) + ) + : null) ?? + swapState.spotPriceQuote?.amount ?? + new Dec(0), + { + maxDecimals: Math.min( + swapState.toAsset.coinDecimals, + 8 + ), + } + ) + : "0" + }`} +
= observer( showPriceImpactWarning ? "opacity-100" : "opacity-0" )} /> - { - // if unavailalble, hide the icon - isSwapAssetsSpotPriceAvailable && ( - - ) - } +
{ let error = quoteError; + // only show spot price error if there's no quote + if ( + (quote && + !quote.inOutSpotPrice && + !quote.amount.toDec().isPositive() && + !error) || + (!quote && spotPriceQuoteError) + ) + error = spotPriceQuoteError; + const errorFromTrpc = makeRouterErrorFromTrpcError(error)?.error; if (errorFromTrpc) return errorFromTrpc; // prioritize router errors over user input errors if (!inAmountInput.isEmpty && inAmountInput.error) return inAmountInput.error; - }, [quoteError, inAmountInput.error, inAmountInput.isEmpty]); + }, [ + quoteError, + quote, + spotPriceQuoteError, + inAmountInput.error, + inAmountInput.isEmpty, + ]); const getSwapTxParameters = useCallback( ({ @@ -350,10 +385,23 @@ export function useSwap({ ] ); + const positivePrevQuote = usePreviousWhen( + quote, + useCallback( + () => Boolean(quote?.amount.toDec().isPositive()) && !quoteError, + [quote, quoteError] + ) + ); + return { ...swapAssets, inAmountInput, - quote, + quote: + isQuoteLoading || inAmountInput.isTyping + ? positivePrevQuote + : !Boolean(quoteError) + ? quote + : undefined, totalFee: sum([ quote?.tokenInFeeAmountFiatValue?.toDec() ?? new Dec(0), networkFee?.gasUsdValueToPay?.toDec() ?? new Dec(0), @@ -361,8 +409,13 @@ export function useSwap({ networkFee, isLoadingNetworkFee, error: precedentError, + spotPriceQuote, + isSpotPriceQuoteLoading, + spotPriceQuoteError, isQuoteLoading, - isLoading: isQuoteLoading, + /** Spot price or user input quote. */ + isAnyQuoteLoading: isQuoteLoading || isSpotPriceQuoteLoading, + isLoading: isQuoteLoading || isSpotPriceQuoteLoading, sendTradeTokenInTx, }; } @@ -560,7 +613,7 @@ function useSwapAsset( asset.coinDenom === minDenomOrSymbol || asset.coinMinimalDenom === minDenomOrSymbol ); - + !existingAsset; const asset = useMemo(() => { if (existingAsset) return existingAsset; @@ -570,19 +623,13 @@ function useSwapAsset( (asset.symbol === minDenomOrSymbol || asset.coinMinimalDenom === minDenomOrSymbol) ); - if (!asset) { - console.warn( - "useSwap: asset not found for in/out swap:", - minDenomOrSymbol - ); - return; - } + if (!asset) return; return makeMinimalAsset(asset); }, [minDenomOrSymbol, existingAsset]); return { - asset: asset as TAsset, + asset: existingAsset ?? (asset as TAsset), }; } diff --git a/packages/web/integrations/sidecar/router.ts b/packages/web/integrations/sidecar/router.ts index d0d6bb45b4e..04e5fd714c8 100644 --- a/packages/web/integrations/sidecar/router.ts +++ b/packages/web/integrations/sidecar/router.ts @@ -53,7 +53,7 @@ export class OsmosisSidecarRemoteRouter implements TokenOutGivenInRouter { swapFee, priceImpactTokenOut: priceImpact, tokenInFeeAmount: tokenIn.amount.toDec().mul(swapFee).truncate(), - inBaseOutQuoteSpotPrice: new Dec(in_base_out_quote_spot_price), + inOutSpotPrice: new Dec(in_base_out_quote_spot_price), split: routes.map(({ pools, in_amount }) => ({ initialAmount: new Int(in_amount), pools: pools.map(({ id, spread_factor, type, code_id }) => ({