quality.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.quality = void 0;
  4. var types_1 = require("./types");
  5. var decimal_js_1 = require("decimal.js");
  6. var bigInt = require("big-integer");
  7. var buffer_1 = require("buffer/");
  8. /**
  9. * class for encoding and decoding quality
  10. */
  11. var quality = /** @class */ (function () {
  12. function quality() {
  13. }
  14. /**
  15. * Encode quality amount
  16. *
  17. * @param arg string representation of an amount
  18. * @returns Serialized quality
  19. */
  20. quality.encode = function (quality) {
  21. var decimal = new decimal_js_1.Decimal(quality);
  22. var exponent = decimal.e - 15;
  23. var qualityString = decimal.times("1e".concat(-exponent)).abs().toString();
  24. var 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. quality.decode = function (quality) {
  35. var bytes = buffer_1.Buffer.from(quality, 'hex').slice(-8);
  36. var exponent = bytes[0] - 100;
  37. var mantissa = new decimal_js_1.Decimal("0x".concat(bytes.slice(1).toString('hex')));
  38. return mantissa.times("1e".concat(exponent));
  39. };
  40. return quality;
  41. }());
  42. exports.quality = quality;
  43. //# sourceMappingURL=quality.js.map