forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversion.js
More file actions
35 lines (27 loc) · 1.05 KB
/
Copy pathconversion.js
File metadata and controls
35 lines (27 loc) · 1.05 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
/**
* Conversion utilities for Nova Rewards
*/
const STROOPS_PER_XLM = 10_000_000;
/** Convert XLM to stroops (Stellar's smallest unit) */
const xlmToStroops = (xlm) => Math.round(xlm * STROOPS_PER_XLM);
/** Convert stroops to XLM */
const stroopsToXlm = (stroops) => stroops / STROOPS_PER_XLM;
/** Convert points to token amount using a given rate */
const pointsToTokens = (points, rate) => roundTo(points * rate);
/** Convert bytes to a human-readable string */
const bytesToHuman = (bytes) => {
const units = ['B', 'KB', 'MB', 'GB'];
let i = 0;
let value = bytes;
while (value >= 1024 && i < units.length - 1) { value /= 1024; i++; }
return `${value.toFixed(i === 0 ? 0 : 2)} ${units[i]}`;
};
/** Parse a string to a safe integer, returns null if invalid */
const toSafeInt = (value) => {
const n = parseInt(value, 10);
return isNaN(n) ? null : n;
};
function roundTo(value, decimals = 6) {
return Math.round(value * 10 ** decimals) / 10 ** decimals;
}
module.exports = { xlmToStroops, stroopsToXlm, pointsToTokens, bytesToHuman, toSafeInt };