UNPKG

rainbow-swap-sdk

Version:

SDK for building applications on top of Rainbow.ag - Swap Aggregator on TON 💎.

45 lines (44 loc) • 1.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromNano = exports.toNano = void 0; const toNano = (src, decimals) => { const precision = 10n ** BigInt(decimals); // Check sign let neg = false; while (src.startsWith('-')) { neg = !neg; src = src.slice(1); } // Split string if (src === '.') { throw Error('Invalid number'); } const parts = src.split('.'); if (parts.length > 2) { throw Error('Invalid number'); } // Prepare parts let whole = parts[0]; let frac = parts[1]; if (!whole) { whole = '0'; } if (!frac) { frac = ''; } if (frac.length > decimals) { throw Error('Invalid number'); } while (frac.length < decimals) { frac += '0'; } // Convert let r = BigInt(whole) * precision + BigInt(frac); if (neg) { r = -r; } return r; }; exports.toNano = toNano; const fromNano = (value, decimals) => String(Number(value) / 10 ** decimals); exports.fromNano = fromNano;