forked from SmartDropLabs/smartdrop-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstroops.js
More file actions
47 lines (39 loc) · 1.33 KB
/
Copy pathstroops.js
File metadata and controls
47 lines (39 loc) · 1.33 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
'use strict';
const STROOPS_PER_UNIT = 10_000_000n;
const MAX_DECIMALS = 7;
/**
* Convert a decimal amount (e.g. 1.5) to integer stroops using BigInt.
* Throws on NaN, Infinity, negative values, or exceeding 7 decimal places.
*/
function toStroops(amount) {
if (typeof amount !== 'number' || !Number.isFinite(amount) || amount < 0) {
throw new Error('amount must be a non-negative finite number');
}
// Convert to fixed-point string to avoid scientific notation (e.g. 1e-7)
// and to get a predictable decimal representation.
const fixed = amount.toFixed(MAX_DECIMALS);
// Verify no precision loss: the fixed string must round-trip to the same value.
if (Number(fixed) !== amount) {
throw new Error(`amount must have at most ${MAX_DECIMALS} decimal places`);
}
// fixed is always "XXXXXXXX.XXXXXXX" (7 decimal places, zero-padded)
const withoutDot = fixed.replace('.', '');
return BigInt(withoutDot);
}
/**
* Sum an array of recipient amounts as stroops.
*/
function sumStroops(recipients) {
let total = 0n;
for (const r of recipients) {
total += toStroops(r.amount);
}
return total;
}
/**
* Compare two decimal amounts in stroops for equality.
*/
function stroopsEqual(a, b) {
return toStroops(a) === toStroops(b);
}
module.exports = { toStroops, sumStroops, stroopsEqual, STROOPS_PER_UNIT, MAX_DECIMALS };