From b19ee40c83a988c0a4539293c5e0c2849d69aaf9 Mon Sep 17 00:00:00 2001 From: Ji Young Lee <641712+jiyounglee@users.noreply.github.com> Date: Wed, 23 Oct 2024 09:53:27 +1100 Subject: [PATCH] =?UTF-8?q?[NO=20CHANGELOG]=20[Add=20Funds=20Widget]=20Upd?= =?UTF-8?q?ate=20to=20calculate=20from=20amount=20from=20route=20response?= =?UTF-8?q?=20when=20route=20to=20amou=E2=80=A6=20(#2333)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/widgets/add-funds/hooks/useRoutes.ts | 61 +++++++++++++++---- 1 file changed, 48 insertions(+), 13 deletions(-) diff --git a/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useRoutes.ts b/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useRoutes.ts index 7a104160f5..f2c7d021fa 100644 --- a/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useRoutes.ts +++ b/packages/checkout/widgets-lib/src/widgets/add-funds/hooks/useRoutes.ts @@ -11,12 +11,17 @@ import { sortRoutesByFastestTime } from '../functions/sortRoutesByFastestTime'; import { AddFundsActions, AddFundsContext } from '../context/AddFundsContext'; import { retry } from '../../../lib/retry'; import { getFormattedNumber } from '../functions/getFormattedNumber'; +import { useAnalytics, UserJourney } from '../../../context/analytics-provider/SegmentAnalyticsProvider'; + +const BASE_SLIPPAGE = 0.02; export const useRoutes = () => { const latestRequestIdRef = useRef(0); const { addFundsDispatch } = useContext(AddFundsContext); + const { track } = useAnalytics(); + const setRoutes = (routes: RouteData[]) => { addFundsDispatch({ payload: { @@ -48,7 +53,16 @@ export const useRoutes = () => { const toAmountNumber = Number(toAmount); const toAmountInUsd = toAmountNumber * toToken.usdPrice; const baseFromAmount = toAmountInUsd / fromToken.usdPrice; - const fromAmountWithBuffer = baseFromAmount * (1.02 + additionalBuffer); + const fromAmountWithBuffer = baseFromAmount * (1 + BASE_SLIPPAGE + additionalBuffer); + return fromAmountWithBuffer.toString(); + }; + + const calculateFromAmountFromRoute = ( + exchangeRate: string, + toAmount: string, + ) => { + const fromAmount = Number(toAmount) / Number(exchangeRate); + const fromAmountWithBuffer = fromAmount * (1 + BASE_SLIPPAGE); return fromAmountWithBuffer.toString(); }; @@ -183,18 +197,11 @@ export const useRoutes = () => { if (isRouteToAmountGreaterThanToAmount(routeResponse, toAmount)) { return { route: routeResponse }; } - - const additionalBuffer = Math.abs( - Number(routeResponse?.route.estimate.aggregatePriceImpact), - ); - const newFromAmount = calculateFromAmount( - fromToken, - toToken, + const newFromAmount = calculateFromAmountFromRoute( + routeResponse.route.estimate.exchangeRate, toAmount, - additionalBuffer, ); - - const routeWithBufferResponse = await getRouteWithRetry( + const newRoute = await getRouteWithRetry( squid, fromToken, toToken, @@ -203,9 +210,37 @@ export const useRoutes = () => { fromAddress, quoteOnly, ); - if (isRouteToAmountGreaterThanToAmount(routeResponse, toAmount)) { - return { route: routeWithBufferResponse, additionalBuffer }; + if (!newRoute?.route) { + return {}; + } + if (isRouteToAmountGreaterThanToAmount(newRoute, toAmount)) { + return { route: newRoute }; } + track({ + userJourney: UserJourney.ADD_FUNDS, + screen: 'Routes', + action: 'Failed', + extras: { + fromToken: fromToken.symbol, + toToken: toToken.symbol, + fromChain: fromToken.chainId, + toChain: toToken.chainId, + initialRoute: { + fromAmount, + toAmount, + exchangeRate: routeResponse.route.estimate.exchangeRate, + routeFromAmount: routeResponse.route.estimate.fromAmount, + routeToAmount: routeResponse.route.estimate.toAmount, + }, + newRoute: { + fromAmount: newFromAmount, + toAmount, + exchangeRate: newRoute.route.estimate.exchangeRate, + routeFromAmount: newRoute.route.estimate.fromAmount, + routeToAmount: newRoute.route.estimate.toAmount, + }, + }, + }); return {}; } catch (error) { return {};