From 8a1d3e3f4a340af295654e3f9d8452081a4df2ad Mon Sep 17 00:00:00 2001 From: DMY <147dmy@gmail.com> Date: Wed, 4 Dec 2024 17:33:18 +0800 Subject: [PATCH 1/2] fix: Bignumer Rounding modes --- src/ui/utils/number.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ui/utils/number.ts b/src/ui/utils/number.ts index 4b23ccc50c4..1f63c5f2e7c 100644 --- a/src/ui/utils/number.ts +++ b/src/ui/utils/number.ts @@ -68,7 +68,7 @@ export const formatNumber = ( num: string | number, decimal = 2, opt = {} as BigNumber.Format, - roundingMode = BigNumber.ROUND_UP as BigNumber.RoundingMode + roundingMode = BigNumber.ROUND_HALF_UP as BigNumber.RoundingMode ) => { const n = new BigNumber(num); const format = { @@ -119,7 +119,7 @@ export const intToHex = (n: number) => { export const formatUsdValue = ( value: string | number, - roundingMode = BigNumber.ROUND_UP as BigNumber.RoundingMode + roundingMode = BigNumber.ROUND_HALF_UP as BigNumber.RoundingMode ) => { const bnValue = new BigNumber(value); if (bnValue.lt(0)) { @@ -212,7 +212,7 @@ export const formatGasCostUsd = (gasCostUsd: BigNumber) => { export const formatGasHeaderUsdValue = ( value: string | number, - roundingMode = BigNumber.ROUND_UP as BigNumber.RoundingMode + roundingMode = BigNumber.ROUND_HALF_UP as BigNumber.RoundingMode ) => { const bnValue = new BigNumber(value); if (bnValue.lt(0)) { From 89e09e5d9bd938bef7954de977f34de65a993d38 Mon Sep 17 00:00:00 2001 From: DMY <147dmy@gmail.com> Date: Fri, 6 Dec 2024 11:59:00 +0800 Subject: [PATCH 2/2] fix: adjust gasaccout decimal --- src/ui/utils/number.ts | 6 ++++++ .../Approval/components/TxComponents/GasSelectorHeader.tsx | 7 ++++--- src/ui/views/GasAccount/components/History.tsx | 5 ++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/ui/utils/number.ts b/src/ui/utils/number.ts index 1f63c5f2e7c..8f7aed096d4 100644 --- a/src/ui/utils/number.ts +++ b/src/ui/utils/number.ts @@ -225,3 +225,9 @@ export const formatGasHeaderUsdValue = ( return `$${formatNumber(value, 4, undefined, roundingMode)}`; }; + +export const formatGasAccountUSDValue = (value: string | number) => { + const bnValue = new BigNumber(value); + if (bnValue.lt(0.0001)) return '<$0.0001'; + return `$${formatNumber(value, 4)}`; +}; diff --git a/src/ui/views/Approval/components/TxComponents/GasSelectorHeader.tsx b/src/ui/views/Approval/components/TxComponents/GasSelectorHeader.tsx index e3d5ec7af43..6c679cd4309 100644 --- a/src/ui/views/Approval/components/TxComponents/GasSelectorHeader.tsx +++ b/src/ui/views/Approval/components/TxComponents/GasSelectorHeader.tsx @@ -25,6 +25,7 @@ import { formatGasCostUsd, formatTokenAmount, formatGasHeaderUsdValue, + formatGasAccountUSDValue, } from '@/ui/utils/number'; import { calcMaxPriorityFee } from '@/utils/transaction'; import styled, { css } from 'styled-components'; @@ -729,7 +730,7 @@ const GasSelectorHeader = ({ if (!Number.isNaN(v) && v < 0.0001) { return `$${n}`; } - return formatGasHeaderUsdValue(n || '0'); + return formatGasAccountUSDValue(n || '0'); }, []); const [isGasHovering, gasHoverProps] = useHover(); @@ -816,7 +817,7 @@ const GasSelectorHeader = ({ >
- {formatGasHeaderUsdValue( + {formatGasAccountUSDValue( (gasAccountCost?.gas_account_cost.estimate_tx_cost || 0) + (gasAccountCost?.gas_account_cost.gas_cost || 0) )} @@ -831,7 +832,7 @@ const GasSelectorHeader = ({
diff --git a/src/ui/views/GasAccount/components/History.tsx b/src/ui/views/GasAccount/components/History.tsx index 83a46ec5984..6b4844ee328 100644 --- a/src/ui/views/GasAccount/components/History.tsx +++ b/src/ui/views/GasAccount/components/History.tsx @@ -1,14 +1,13 @@ import React from 'react'; import { ReactComponent as RcIconEmptyCC } from '@/ui/assets/empty-cc.svg'; import { useTranslation } from 'react-i18next'; -import { formatGasHeaderUsdValue, sinceTime } from '@/ui/utils'; +import { formatGasAccountUSDValue, sinceTime } from '@/ui/utils'; import clsx from 'clsx'; import { useGasAccountHistory } from '../hooks'; import { Skeleton } from 'antd'; import { ReactComponent as RcIconPendingCC } from '@/ui/assets/pending-cc.svg'; import { ReactComponent as RcIconOpenExternalCC } from '@/ui/assets/open-external-cc.svg'; import { findChainByServerID } from '@/utils/chain'; -import BigNumber from 'bignumber.js'; const HistoryItem = ({ time, @@ -74,7 +73,7 @@ const HistoryItem = ({ )}
{sign} - {formatGasHeaderUsdValue(value, BigNumber.ROUND_DOWN)}{' '} + {formatGasAccountUSDValue(value)}{' '}
);