Files
explore/ui/shared/value/utils.ts
T
tom goriunovandGitHub 932add9d96 Change zero to <0.001 value within different pages (#3150)
* basic components and address page refactoring

* second part of refactoring

* third part of refactoring

* NativeCoinValue component

* more refactoring

* rename TruncatedValue into TruncatedText

* minor fixes

* refactor block details

* fix layout of value and fee in tx table

* update screenshots

* different units for tooltip and stats widget small value fix

* update screenshots

* update tx table screenshot

* fix copy button vertical alignment
2025-12-05 16:04:27 +01:00

61 lines
1.6 KiB
TypeScript

import BigNumber from 'bignumber.js';
import config from 'configs/app';
import { currencyUnits } from 'lib/units';
import { ZERO } from 'toolkit/utils/consts';
import { thinsp } from 'toolkit/utils/htmlEntities';
export type Unit = 'wei' | 'gwei' | 'ether';
interface FormatBnValueParams {
value: BigNumber;
accuracy?: number;
prefix?: string;
postfix?: string;
overflowed?: boolean;
}
export const formatBnValue = ({ value, accuracy, prefix, postfix, overflowed }: FormatBnValueParams) => {
const fullPrefix = `${ overflowed ? `>${ thinsp }` : '' }${ prefix ?? '' }`;
if (!accuracy) {
return `${ fullPrefix }${ value.toFormat() }${ postfix ?? '' }`;
}
const formattedValue = value.dp(accuracy).toFormat();
return formattedValue === '0' && !value.isEqualTo(ZERO) && !overflowed ?
`<${ thinsp }${ prefix ?? '' }0.${ '0'.repeat(accuracy - 1) }1${ postfix ?? '' }` :
`${ fullPrefix }${ formattedValue }${ postfix ?? '' }`;
};
export const DEFAULT_ACCURACY = 8;
export const DEFAULT_ACCURACY_USD = 2;
export const WEI_DECIMALS = config.chain.currency.decimals;
// maybe we need to add customization for gwei decimals as well
export const GWEI_DECIMALS = 9;
export const WEI = new BigNumber(10 ** WEI_DECIMALS);
export const GWEI = new BigNumber(10 ** GWEI_DECIMALS);
export const getUnitDecimals = (units: Unit) => {
if (units === 'wei') {
return 0;
}
if (units === 'gwei') {
return GWEI_DECIMALS;
}
return WEI_DECIMALS;
};
export const getUnitName = (units: Unit) => {
if (units === 'wei') {
return currencyUnits.wei;
}
if (units === 'gwei') {
return currencyUnits.gwei;
}
return currencyUnits.ether;
};