UNPKG

@cks-systems/manifest-sdk

Version:
41 lines (40 loc) 967 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.toNum = toNum; exports.convertU128 = convertU128; const bn_js_1 = require("bn.js"); /** * Converts a beet.bignum to a number. * * @param n The number to convert */ function toNum(n) { let target; if (typeof n === 'number') { target = n; } else { target = n.toString(); } return target; } const BN_NUMBER_MAX = new bn_js_1.BN(2 ** 48 - 1); const BN_10 = new bn_js_1.BN(10); /** * Converts a beet.bignum to a number after dividing by 10**18 * * @param n The number to convert */ function convertU128(n) { if (typeof n === 'number') { return n; } let mantissa = n.clone(); for (let exponent = -18; exponent < 20; exponent += 1) { if (mantissa.lte(BN_NUMBER_MAX)) { return mantissa.toNumber() * 10 ** exponent; } mantissa = mantissa.div(BN_10); } throw 'unreachable'; }