Skip to content

Commit

Permalink
[NO CHANGELOG] [Add Funds Widget] Update to calculate from amount fro…
Browse files Browse the repository at this point in the history
…m route response when route to amou… (#2333)
  • Loading branch information
jiyounglee authored Oct 22, 2024
1 parent a4353ff commit b19ee40
Showing 1 changed file with 48 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>(0);

const { addFundsDispatch } = useContext(AddFundsContext);

const { track } = useAnalytics();

const setRoutes = (routes: RouteData[]) => {
addFundsDispatch({
payload: {
Expand Down Expand Up @@ -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();
};

Expand Down Expand Up @@ -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,
Expand All @@ -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 {};
Expand Down

0 comments on commit b19ee40

Please sign in to comment.