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

fix: BigNumber Rounding modes #2657

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 9 additions & 3 deletions src/ui/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)}`;
};
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
formatGasCostUsd,
formatTokenAmount,
formatGasHeaderUsdValue,
formatGasAccountUSDValue,
} from '@/ui/utils/number';
import { calcMaxPriorityFee } from '@/utils/transaction';
import styled, { css } from 'styled-components';
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -816,7 +817,7 @@ const GasSelectorHeader = ({
>
<div className="truncate max-w-[170px] group text-r-neutral-body">
<span className="text-[16px] font-medium text-r-blue-default">
{formatGasHeaderUsdValue(
{formatGasAccountUSDValue(
(gasAccountCost?.gas_account_cost.estimate_tx_cost ||
0) + (gasAccountCost?.gas_account_cost.gas_cost || 0)
)}
Expand All @@ -831,7 +832,7 @@ const GasSelectorHeader = ({
</span>
</div>
<Tooltip
overlayClassName="rectangle"
overlayClassName="rectangle max-w-max"
visible={isGasAccountHovering}
title={
<>
Expand Down
5 changes: 2 additions & 3 deletions src/ui/views/GasAccount/components/History.tsx
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -74,7 +73,7 @@ const HistoryItem = ({
)}
<div className="text-14 font-medium text-r-neutral-title-1">
{sign}
{formatGasHeaderUsdValue(value, BigNumber.ROUND_DOWN)}{' '}
{formatGasAccountUSDValue(value)}{' '}
</div>
</div>
);
Expand Down
Loading