Skip to content
Open
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
8 changes: 5 additions & 3 deletions app/tips/library/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down
10 changes: 6 additions & 4 deletions app/vibenet/library/explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down