UNPKG

@0xsplits/splits-sdk

Version:

SDK for the 0xSplits protocol

42 lines 2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.fromBigIntToTokenValue = exports.getBigIntTokenValue = exports.fromBigIntToPercent = exports.getNumberFromPercent = exports.getBigIntFromPercent = exports.roundToDecimals = void 0; var viem_1 = require("viem"); var constants_1 = require("../constants"); var roundToDecimals = function (num, decimals) { var multiplier = Math.pow(10, decimals); // Include Number.EPSILON to help with floating point precision (i.e. expected 1.325 but got 1.324999999999) return Math.round((num + Number.EPSILON) * multiplier) / multiplier; }; exports.roundToDecimals = roundToDecimals; var getBigIntFromPercent = function (value) { return BigInt((0, exports.getNumberFromPercent)(value)); }; exports.getBigIntFromPercent = getBigIntFromPercent; var getNumberFromPercent = function (value, scale) { if (!scale) scale = constants_1.PERCENTAGE_SCALE; return Math.round(Number(scale) * value) / 100; }; exports.getNumberFromPercent = getNumberFromPercent; var PERCENTAGE_SCALE_FACTOR = Math.pow(10, 4); // 4 decimal places var fromBigIntToPercent = function (value, scale) { var numberVal = BigInt(value); if (!scale) { var rawPercentage = parseFloat((0, viem_1.formatUnits)(numberVal, 6)) * 100; return (Math.round(rawPercentage * PERCENTAGE_SCALE_FACTOR) / PERCENTAGE_SCALE_FACTOR); } var percentage = (numberVal * BigInt(100) * BigInt(PERCENTAGE_SCALE_FACTOR)) / scale; return Number(percentage) / PERCENTAGE_SCALE_FACTOR; }; exports.fromBigIntToPercent = fromBigIntToPercent; var getBigIntTokenValue = function (value, decimals) { return (0, viem_1.parseUnits)(value.toString(), decimals); }; exports.getBigIntTokenValue = getBigIntTokenValue; var fromBigIntToTokenValue = function (amount, decimals) { return (0, viem_1.formatUnits)(amount, decimals); }; exports.fromBigIntToTokenValue = fromBigIntToTokenValue; //# sourceMappingURL=numbers.js.map