Skip to content

Commit

Permalink
[NO CHANGELOG] [Checkout Widget] Fix routes sorting and formatted pri…
Browse files Browse the repository at this point in the history
…ce calculation (#2319)
  • Loading branch information
jhesgodi authored Oct 16, 2024
1 parent adb6fe5 commit 65223a8
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ import { DEFAULT_TOKEN_FORMATTING_DECIMALS } from '../../../lib/constants';
* removing trailing zeros
*/
export const getFormattedAmounts = (
amount: string | number,
value: string | number,
maxDecimals = DEFAULT_TOKEN_FORMATTING_DECIMALS,
) => tokenValueFormat(amount, maxDecimals).replace(/\.?0+$/, '');
) => {
const amount = typeof value === 'number' ? value : parseFloat(value);

if (amount > 0 && amount < 1) {
return tokenValueFormat(amount, maxDecimals).replace(/\.?0+$/, '');
}

return tokenValueFormat(amount, maxDecimals);
};

/**
* Converts a crypto amount to a formatted string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ export function getRouteAndTokenBalances(routeData?: RouteData): RouteBalance {
return emptyRouteBalance;
}

const { fromToken } = routeData.amountData;

const usdPrice = routeData?.route.route.estimate.fromToken.usdPrice;
if (!usdPrice) {
return emptyRouteBalance;
Expand All @@ -31,8 +29,8 @@ export function getRouteAndTokenBalances(routeData?: RouteData): RouteBalance {

const routeBalance = getFormattedNumber(
balance.balance,
fromToken?.decimals,
fromToken?.decimals, // preserve precision for usd conversion down below
balance?.decimals,
balance?.decimals, // preserve precision for usd conversion down below
);
const routeBalanceUsd = (parseFloat(routeBalance) * usdPrice).toString();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,14 @@ export const useRoutes = () => {
}),
);

const sortedRoutes = sortRoutesByFastestTime(allRoutes);

// Only update routes if the request is the latest one
if (currentRequestId === latestRequestIdRef.current) {
const sortedRoutes = sortRoutesByFastestTime(allRoutes);
setRoutes(sortedRoutes);
}

return allRoutes;
return sortedRoutes;
};

return {
Expand Down

0 comments on commit 65223a8

Please sign in to comment.