-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.js
More file actions
31 lines (28 loc) · 1.17 KB
/
Copy pathformat.js
File metadata and controls
31 lines (28 loc) · 1.17 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
/** Gateway `pay_amount` is token minimal units (USDC/USDT = 6). */
const TOKEN_DECIMALS = { USDC: 6, USDT: 6, ETH: 18, POL: 18, BNB: 18, AVAX: 18 };
export function formatToken(minimal, symbol) {
if (minimal == null || minimal === '') return '';
const dec = TOKEN_DECIMALS[String(symbol || '').toUpperCase()] ?? 6;
try {
const n = BigInt(String(minimal));
const base = 10n ** BigInt(dec);
const whole = n / base;
const frac = (n % base).toString().padStart(dec, '0').replace(/0+$/, '');
return frac ? `${whole}.${frac}` : `${whole}`;
} catch {
return String(minimal);
}
}
/** SDK timestamps: RFC3339 string on Payment/Refund, unix ms on webhook events. */
export function msOf(ts) {
if (ts == null) return Date.now();
const n = typeof ts === 'number' ? ts : Date.parse(ts);
return Number.isFinite(n) ? n : Date.now();
}
/** BuyerRef snapshot: omitted = anonymous; olares / external otherwise. */
export function formatBuyer(buyer) {
if (buyer == null) return 'anonymous';
if (buyer.kind === 'olares') return `olares ${buyer.olaresId}`;
const name = buyer.display?.name;
return name ? `external ${buyer.ref} (${name})` : `external ${buyer.ref}`;
}