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

feat: format token amount #2713

Merged
merged 2 commits into from
Jan 13, 2025
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
52 changes: 29 additions & 23 deletions src/ui/utils/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@ export const splitNumberByStep = (
return n.toFormat(fmt);
};

export const formatLittleNumber = (num: string, minLen = 8) => {
const bn = new BigNumber(num);
if (bn.toFixed().length > minLen) {
const s = bn.precision(4).toFormat();
const ss = s.replace(/^0.(0*)?(?:.*)/, (l, z) => {
const zeroLength = z.length;

const sub = `${zeroLength}`
.split('')
.map((x) => Sub_Numbers[x as any])
.join('');

const end = s.slice(zeroLength + 2);
return `0.0${sub}${end}`;
});

return ss;
}
return num;
};

export const formatTokenAmount = (
amount: number | string,
decimals = 4,
Expand All @@ -37,6 +58,9 @@ export const formatTokenAmount = (
if (moreDecimalsWhenNotEnough && bn.lt(0.00000001)) {
return '<0.00000001';
}
if (bn.lte(0.0001)) {
return formatLittleNumber(bn.toFixed());
}
if (!split[1] || split[1].length < realDecimals) {
return splitNumberByStep(bn.toFixed());
}
Expand Down Expand Up @@ -105,24 +129,8 @@ export const formatPrice = (price: string | number) => {
}
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (price < 0.00001) {
if (price.toString().length > 10) {
const s = new BigNumber(price).precision(4).toFormat();
const ss = s.replace(/^0.(0*)?(?:.*)/, (l, z) => {
const zeroLength = z.length;

const sub = `${zeroLength}`
.split('')
.map((x) => Sub_Numbers[x as any])
.join('');

const end = s.slice(zeroLength + 2);
return `0.0${sub}${end}`;
});

return ss;
}
return price.toString();
if (price < 0.0001) {
return formatLittleNumber(new BigNumber(price).toFixed(), 6);
}
return formatNumber(price, 4);
};
Expand Down Expand Up @@ -165,11 +173,9 @@ export const formatAmount = (amount: string | number, decimals = 4) => {
if (amount > 1) return formatNumber(amount, 4);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
if (amount < 0.00001) {
if (amount.toString().length > 10) {
return Number(amount).toExponential(4);
}
return amount.toString();
if (amount < 0.0001) {
const str = new BigNumber(amount).toFixed();
return formatLittleNumber(str);
}
return formatNumber(amount, decimals);
};
Expand Down
2 changes: 0 additions & 2 deletions src/ui/views/Approval/components/FooterBar/SubmitActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export const SubmitActions: React.FC<Props> = ({
setIsSign(false);
}, []);

console.log('gasLess', gasLess);

return (
<ActionsContainer onCancel={onCancel}>
{isSign ? (
Expand Down
Loading