35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
TypeScript
export function abbrev(pk: string | null | undefined): string {
|
|
if (!pk) return '—'
|
|
return `${pk.slice(0, 4)}…${pk.slice(-4)}`
|
|
}
|
|
|
|
export function fmtSol(lamports: bigint | number): string {
|
|
const v = Number(lamports) / 1e9
|
|
return v.toLocaleString(undefined, { maximumFractionDigits: 4 }) + ' SOL'
|
|
}
|
|
|
|
export function fmtTok(raw: bigint | number, decimals: number, symbol: string): string {
|
|
const v = Number(raw) / Math.pow(10, decimals)
|
|
return v.toLocaleString(undefined, { maximumFractionDigits: Math.min(decimals, 2) }) + ' ' + symbol
|
|
}
|
|
|
|
export function relTime(ts: number): string {
|
|
const d = Math.floor(Date.now() / 1000) - ts
|
|
if (d < 60) return `${d}s ago`
|
|
if (d < 3600) return `${Math.floor(d / 60)}m ago`
|
|
if (d < 86400) return `${Math.floor(d / 3600)}h ago`
|
|
return `${Math.floor(d / 86400)}d ago`
|
|
}
|
|
|
|
export function exact(ts: number): string {
|
|
return new Date(ts * 1000).toUTCString()
|
|
}
|
|
|
|
export function priceStr(
|
|
currency: { __kind: 'Sol' } | { __kind: 'Spl'; decimals: number; symbol?: string },
|
|
price: bigint | number,
|
|
): string {
|
|
if (currency.__kind === 'Sol') return fmtSol(price)
|
|
return fmtTok(price, currency.decimals, (currency as { symbol?: string }).symbol ?? 'SPL')
|
|
}
|