UNPKG

fsl-js-sdk

Version:
81 lines (80 loc) 3.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DooarU64 = void 0; var spl_token_v0_1 = require("@solana/spl-token-v0"); var decimal_js_1 = __importDefault(require("decimal.js")); var decimal_utils_1 = require("./decimal-utils"); /** * Dooar's U64 wrapper class to help users convert to/from regular javascript number types * * * Examples: * DooarU64(value: 99999, decimal: 0) -> 99999 * DooarU64(value: 99999, decimal: 5) -> 0.99999 */ var DooarU64 = /** @class */ (function () { function DooarU64(value, scale) { if (scale === void 0) { scale = 0; } this.value = value; this.scale = Math.floor(scale); } /** * Create an DooarU64 from a Decimal * @param value - an object representing the value of the u64 in Decimal form * @param scale - the number of digits after the decimal point to keep account for in this u64 * @returns DooarU64 hosting a u64 representing the input value adjusted to the provided scale */ DooarU64.fromDecimal = function (value, scale) { if (scale === void 0) { scale = 0; } var dec = Math.floor(scale); return new DooarU64(decimal_utils_1.DecimalUtil.toU64(value, dec), dec); }; /** * Create an DooarU64 from a number * @param value - an object representing the value of the u64 in number form * @param scale - the number of digits after the decimal point to keep account for in this u64 * @returns DooarU64 hosting a u64 representing the input value adjusted to the provided scale */ DooarU64.fromNumber = function (value, scale) { if (scale === void 0) { scale = 0; } var dec = Math.floor(scale); return new DooarU64(decimal_utils_1.DecimalUtil.toU64(new decimal_js_1.default(value), dec), dec); }; /** * Create an DooarU64 from a u64 * @param value - an object representing the value of the u64 * @param scale - the number of digits after the decimal point represented in this u64 * @returns DooarU64 hosting the input u64 with the provided scale */ DooarU64.fromU64 = function (value, scale) { if (scale === void 0) { scale = 0; } var dec = Math.floor(scale); return new DooarU64(value, dec); }; /** * Convert this DooarU64 to Decimal. * @returns Decimal object that equals to the DooarU64's value & scale */ DooarU64.prototype.toDecimal = function () { return decimal_utils_1.DecimalUtil.fromU64(this.value, this.scale); }; /** * Convert this DooarU64 to number. * @returns number that equals to the DooarU64's value & scale */ DooarU64.prototype.toNumber = function () { return decimal_utils_1.DecimalUtil.fromU64(this.value, this.scale).toNumber(); }; /** * Convert this DooarU64 to u64. * @returns u64 that equals to the DooarU64 value */ DooarU64.prototype.toU64 = function () { return new spl_token_v0_1.u64(this.value.toString()); }; return DooarU64; }()); exports.DooarU64 = DooarU64;