From fea4414ab8579c38138837d9844b04d1996b86fc Mon Sep 17 00:00:00 2001 From: zugdev Date: Mon, 14 Oct 2024 17:35:58 -0300 Subject: [PATCH 1/2] feat: handle large numbers in details table --- .../render-transaction/insert-table-data.ts | 28 +++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/static/scripts/rewards/render-transaction/insert-table-data.ts b/static/scripts/rewards/render-transaction/insert-table-data.ts index 59cf3c22..fc8f9cb1 100644 --- a/static/scripts/rewards/render-transaction/insert-table-data.ts +++ b/static/scripts/rewards/render-transaction/insert-table-data.ts @@ -6,6 +6,24 @@ function shortenAddress(address: string): string { return `${address.slice(0, 10)}...${address.slice(-8)}`; } +function formatLargeNumber(value: BigNumber, decimals: number): string { + const num = parseFloat(ethers.utils.formatUnits(value, decimals)); + + if (num >= 1_000_000_000_000_000) { + return "INFINITE"; // we can consider quintillion and above basically infinite + } else if (num >= 1_000_000_000_000) { + return `${(num / 1_000_000_000_000).toFixed(1)}T`; // i.e: 1.2T + } else if (num >= 1_000_000_000) { + return `${(num / 1_000_000_000).toFixed(1)}B`; // i.e: 3.5B + } else if (num >= 1_000_000) { + return `${(num / 1_000_000).toFixed(1)}M`; // i.e: 1.2M + } else if (num >= 1_000) { + return `${(num / 1_000).toFixed(1)}K`; // i.e: 341.1K + } else { + return num.toFixed(2); // keep two decimals for smaller numbers + } +} + export function insertErc20PermitTableData( app: AppState, table: Element, @@ -24,8 +42,14 @@ export function insertErc20PermitTableData( return deadline.lte(Number.MAX_SAFE_INTEGER.toString()) ? new Date(deadline.toNumber()).toLocaleString() : undefined; })(), }, - { name: "Balance", value: treasury.balance.gte(0) ? `${ethers.utils.formatUnits(treasury.balance, treasury.decimals)} ${treasury.symbol}` : "N/A" }, - { name: "Allowance", value: treasury.allowance.gte(0) ? `${ethers.utils.formatUnits(treasury.allowance, treasury.decimals)} ${treasury.symbol}` : "N/A" }, + { + name: "Balance", + value: treasury.balance.gte(0) ? `${formatLargeNumber(treasury.balance, treasury.decimals)} ${treasury.symbol}` : "N/A", + }, + { + name: "Allowance", + value: treasury.allowance.gte(0) ? `${formatLargeNumber(treasury.allowance, treasury.decimals)} ${treasury.symbol}` : "N/A", + }, ]); table.setAttribute(`data-make-claim-rendered`, "true"); return requestedAmountElement; From 4c442e8bc22d022617739bcc44202a93fb1fa581 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=A2=E3=83=AC=E3=82=AF=E3=82=B5=E3=83=B3=E3=83=80?= =?UTF-8?q?=E3=83=BC=2Eeth?= <4975670+0x4007@users.noreply.github.com> Date: Tue, 15 Oct 2024 05:52:25 +0900 Subject: [PATCH 2/2] Update insert-table-data.ts --- static/scripts/rewards/render-transaction/insert-table-data.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/static/scripts/rewards/render-transaction/insert-table-data.ts b/static/scripts/rewards/render-transaction/insert-table-data.ts index fc8f9cb1..e6a65b65 100644 --- a/static/scripts/rewards/render-transaction/insert-table-data.ts +++ b/static/scripts/rewards/render-transaction/insert-table-data.ts @@ -10,7 +10,7 @@ function formatLargeNumber(value: BigNumber, decimals: number): string { const num = parseFloat(ethers.utils.formatUnits(value, decimals)); if (num >= 1_000_000_000_000_000) { - return "INFINITE"; // we can consider quintillion and above basically infinite + return "Unlimited"; // we can consider quintillion and above basically unlimited } else if (num >= 1_000_000_000_000) { return `${(num / 1_000_000_000_000).toFixed(1)}T`; // i.e: 1.2T } else if (num >= 1_000_000_000) {