diff --git a/app/tips/library/format.ts b/app/tips/library/format.ts index 52ff588..1e9e36b 100644 --- a/app/tips/library/format.ts +++ b/app/tips/library/format.ts @@ -6,9 +6,11 @@ const WEI_PER_GWEI = 10n ** 9n; const WEI_PER_ETH = 10n ** 18n; function formatBigInt(value: bigint, decimals: number, scale: bigint): string { - const whole = value / scale; - const frac = ((value % scale) * 10n ** BigInt(decimals)) / scale; - return `${whole}.${frac.toString().padStart(decimals, '0')}`; + const sign = value < 0n ? '-' : ''; + const absValue = value < 0n ? -value : value; + const whole = absValue / scale; + const frac = ((absValue % scale) * 10n ** BigInt(decimals)) / scale; + return `${sign}${whole}.${frac.toString().padStart(decimals, '0')}`; } /** Render a `0x…` wei quantity as ETH / Gwei / Wei depending on magnitude. */ diff --git a/app/vibenet/library/explorer.ts b/app/vibenet/library/explorer.ts index bb21980..92b6935 100644 --- a/app/vibenet/library/explorer.ts +++ b/app/vibenet/library/explorer.ts @@ -42,12 +42,14 @@ export function hexToInt(hex: string | null | undefined): number | null { /** Format a raw integer token amount with `decimals` places, trimming zeros. */ export function fmtTokenAmount(raw: bigint, decimals: number): string { if (raw === 0n) return '0'; + const sign = raw < 0n ? '-' : ''; + const absRaw = raw < 0n ? -raw : raw; const divisor = 10n ** BigInt(decimals); - const whole = raw / divisor; - const frac = raw % divisor; - if (frac === 0n) return whole.toLocaleString(); + const whole = absRaw / divisor; + const frac = absRaw % divisor; + if (frac === 0n) return `${sign}${whole.toLocaleString()}`; const fracStr = frac.toString().padStart(decimals, '0').replace(/0+$/, ''); - return `${whole.toLocaleString()}.${fracStr}`; + return `${sign}${whole.toLocaleString()}.${fracStr}`; } export function weiToEth(hex: string | null | undefined): string {