Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rounding large numbers #1612

Merged
merged 5 commits into from
Jul 13, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/entries/popup/pages/swap/SwapReviewSheet/SwapAssetCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useMemo } from 'react';

import { useCurrentCurrencyStore } from '~/core/state';
import { ParsedSearchAsset } from '~/core/types/assets';
import { formatCurrency } from '~/core/utils/formatNumber';
import {
convertRawAmountToBalance,
convertRawAmountToNativeDisplay,
Expand Down Expand Up @@ -49,6 +50,18 @@ export const SwapAssetCard = ({
).display,
[asset.decimals, asset.price?.value, assetAmount, currentCurrency],
);
const cleanNumber = (n: number | string | null | undefined): number => {
if (typeof n === 'string') {
return parseFloat(n.replace(/,/g, ''));
}
return n || 0;
};

const formatNumber = (n?: number | string | null) =>
formatCurrency(cleanNumber(n), {
notation: 'compact',
maximumSignificantDigits: 4,
});
BrodyHughes marked this conversation as resolved.
Show resolved Hide resolved

return (
<AccentColorProvider
Expand All @@ -75,7 +88,7 @@ export const SwapAssetCard = ({
<Columns space="4px" alignVertical="center">
<Column>
<TextOverflow color="label" size="14pt" weight="bold">
{`${amount}`}
{`${formatNumber(amount)}`}
</TextOverflow>
</Column>
<Column width="content">
Expand Down
30 changes: 29 additions & 1 deletion src/entries/popup/pages/swap/SwapReviewSheet/SwapReviewSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { useSwapAssetsToRefreshStore } from '~/core/state/swapAssetsToRefresh';
import { ParsedSearchAsset } from '~/core/types/assets';
import { ChainId } from '~/core/types/chains';
import { truncateAddress } from '~/core/utils/address';
import { formatCurrency } from '~/core/utils/formatNumber';
import { isLowerCaseMatch } from '~/core/utils/strings';
import { isUnwrapEth, isWrapEth } from '~/core/utils/swaps';
import {
Expand Down Expand Up @@ -243,6 +244,33 @@ const SwapReviewSheetWithQuote = ({
const { minimumReceived, swappingRoute, includedFee, exchangeRate } =
useSwapReviewDetails({ quote, assetToBuy, assetToSell });

const cleanNumber = (n: string): number => {
return parseFloat(n.replace(/,/g, ''));
};

const formatNumber = (n: string) =>
formatCurrency(cleanNumber(n), {
notation: 'compact',
maximumSignificantDigits: 4,
});

const processArray = (arr: string[]): string[] => {
return arr.map((item) => {
const parts = item.split(' ');
if (parts.length === 5) {
const formattedAmount = formatNumber(parts[3]);
return `${parts[0]} ${parts[1]} ${parts[2]} ${formattedAmount} ${parts[4]}`;
}
return item;
});
};
BrodyHughes marked this conversation as resolved.
Show resolved Hide resolved

const formattedExchangeRate = useMemo(
() => processArray(exchangeRate),
// eslint-disable-next-line react-hooks/exhaustive-deps
[exchangeRate],
);

const { explainerSheetParams, showExplainerSheet, hideExplainerSheet } =
useExplainerSheetParams();

Expand Down Expand Up @@ -615,7 +643,7 @@ const SwapReviewSheetWithQuote = ({
<CarrouselButton
testId="exchange-rate"
symbol="arrow.2.squarepath"
textArray={exchangeRate}
textArray={formattedExchangeRate}
/>
</ReviewDetailsRow>
{!assetToSell.isNativeAsset && (
Expand Down
Loading