You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xrpConversion.js 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.xrpToDrops = exports.dropsToXrp = void 0;
  7. const bignumber_js_1 = __importDefault(require("bignumber.js"));
  8. const errors_1 = require("../errors");
  9. const DROPS_PER_XRP = 1000000.0;
  10. const MAX_FRACTION_LENGTH = 6;
  11. const BASE_TEN = 10;
  12. const SANITY_CHECK = /^-?[0-9.]+$/u;
  13. function dropsToXrp(dropsToConvert) {
  14. const drops = new bignumber_js_1.default(dropsToConvert).toString(BASE_TEN);
  15. if (typeof dropsToConvert === 'string' && drops === 'NaN') {
  16. throw new errors_1.ValidationError(`dropsToXrp: invalid value '${dropsToConvert}', should be a BigNumber or string-encoded number.`);
  17. }
  18. if (drops.includes('.')) {
  19. throw new errors_1.ValidationError(`dropsToXrp: value '${drops}' has too many decimal places.`);
  20. }
  21. if (!SANITY_CHECK.exec(drops)) {
  22. throw new errors_1.ValidationError(`dropsToXrp: failed sanity check -` +
  23. ` value '${drops}',` +
  24. ` does not match (^-?[0-9]+$).`);
  25. }
  26. return new bignumber_js_1.default(drops).dividedBy(DROPS_PER_XRP).toString(BASE_TEN);
  27. }
  28. exports.dropsToXrp = dropsToXrp;
  29. function xrpToDrops(xrpToConvert) {
  30. const xrp = new bignumber_js_1.default(xrpToConvert).toString(BASE_TEN);
  31. if (typeof xrpToConvert === 'string' && xrp === 'NaN') {
  32. throw new errors_1.ValidationError(`xrpToDrops: invalid value '${xrpToConvert}', should be a BigNumber or string-encoded number.`);
  33. }
  34. if (!SANITY_CHECK.exec(xrp)) {
  35. throw new errors_1.ValidationError(`xrpToDrops: failed sanity check - value '${xrp}', does not match (^-?[0-9.]+$).`);
  36. }
  37. const components = xrp.split('.');
  38. if (components.length > 2) {
  39. throw new errors_1.ValidationError(`xrpToDrops: failed sanity check - value '${xrp}' has too many decimal points.`);
  40. }
  41. const fraction = components[1] || '0';
  42. if (fraction.length > MAX_FRACTION_LENGTH) {
  43. throw new errors_1.ValidationError(`xrpToDrops: value '${xrp}' has too many decimal places.`);
  44. }
  45. return new bignumber_js_1.default(xrp)
  46. .times(DROPS_PER_XRP)
  47. .integerValue(bignumber_js_1.default.ROUND_FLOOR)
  48. .toString(BASE_TEN);
  49. }
  50. exports.xrpToDrops = xrpToDrops;
  51. //# sourceMappingURL=xrpConversion.js.map