UNPKG

1.52 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.quality = void 0;
7const types_1 = require("./types");
8const bignumber_js_1 = __importDefault(require("bignumber.js"));
9const utils_1 = require("@xrplf/isomorphic/utils");
10/**
11 * class for encoding and decoding quality
12 */
13class quality {
14 /**
15 * Encode quality amount
16 *
17 * @param arg string representation of an amount
18 * @returns Serialized quality
19 */
20 static encode(quality) {
21 const decimal = (0, bignumber_js_1.default)(quality);
22 const exponent = ((decimal === null || decimal === void 0 ? void 0 : decimal.e) || 0) - 15;
23 const qualityString = decimal.times(`1e${-exponent}`).abs().toString();
24 const bytes = types_1.coreTypes.UInt64.from(BigInt(qualityString)).toBytes();
25 bytes[0] = exponent + 100;
26 return bytes;
27 }
28 /**
29 * Decode quality amount
30 *
31 * @param arg hex-string denoting serialized quality
32 * @returns deserialized quality
33 */
34 static decode(quality) {
35 const bytes = (0, utils_1.hexToBytes)(quality).slice(-8);
36 const exponent = bytes[0] - 100;
37 const mantissa = new bignumber_js_1.default(`0x${(0, utils_1.bytesToHex)(bytes.slice(1))}`);
38 return mantissa.times(`1e${exponent}`);
39 }
40}
41exports.quality = quality;
42//# sourceMappingURL=quality.js.map
\No newline at end of file