-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathformat.ts
More file actions
47 lines (41 loc) · 1.62 KB
/
Copy pathformat.ts
File metadata and controls
47 lines (41 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Output helpers shared by the runnable examples.
*
* Amounts in the Pump protocol are integer base units: lamports for SOL
* (1 SOL = 1e9 lamports) and 6-decimal base units for Pump tokens
* (1 token = 1e6 units). These helpers render them for humans without
* ever doing financial math in floating point: BN stays authoritative,
* floats appear only at the final display step.
*/
import BN from "bn.js";
const LAMPORTS = new BN(1_000_000_000);
const TOKEN_UNITS = new BN(1_000_000);
export function formatSol(lamports: BN, decimals = 4): string {
return `${divToDecimalString(lamports, LAMPORTS, decimals)} SOL`;
}
export function formatTokens(units: BN, decimals = 2): string {
return `${divToDecimalString(units, TOKEN_UNITS, decimals)} tokens`;
}
/** Integer division with a fixed number of decimal places, no floats. */
export function divToDecimalString(
amount: BN,
divisor: BN,
decimals: number,
): string {
const scale = new BN(10).pow(new BN(decimals));
const scaled = amount.mul(scale).div(divisor);
const text = scaled.toString().padStart(decimals + 1, "0");
const whole = text.slice(0, text.length - decimals);
const frac = text.slice(text.length - decimals);
const grouped = whole.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return decimals > 0 ? `${grouped}.${frac}` : grouped;
}
/** Section header for example output. */
export function heading(title: string): void {
console.log(`\n${title}`);
console.log("-".repeat(title.length));
}
/** Aligned key/value line for example output. */
export function row(label: string, value: unknown): void {
console.log(`${label.padEnd(28)} ${String(value)}`);
}