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.

quality.js 3.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.percentToQuality = exports.transferRateToDecimal = exports.qualityToDecimal = exports.decimalToQuality = exports.percentToTransferRate = exports.decimalToTransferRate = void 0;
  7. const bignumber_js_1 = __importDefault(require("bignumber.js"));
  8. const errors_1 = require("../errors");
  9. const BASE_TEN = 10;
  10. const ONE_BILLION = '1000000000';
  11. const TWO_BILLION = '2000000000';
  12. function percentToDecimal(percent) {
  13. if (!percent.endsWith('%')) {
  14. throw new errors_1.ValidationError(`Value ${percent} must end with %`);
  15. }
  16. const split = percent.split('%').filter((str) => str !== '');
  17. if (split.length !== 1) {
  18. throw new errors_1.ValidationError(`Value ${percent} contains too many % signs`);
  19. }
  20. return new bignumber_js_1.default(split[0]).dividedBy('100').toString(BASE_TEN);
  21. }
  22. function decimalToTransferRate(decimal) {
  23. const rate = new bignumber_js_1.default(decimal).times(ONE_BILLION).plus(ONE_BILLION);
  24. if (rate.isLessThan(ONE_BILLION) || rate.isGreaterThan(TWO_BILLION)) {
  25. throw new errors_1.ValidationError(`Decimal value must be between 0 and 1.00.`);
  26. }
  27. const billionths = rate.toString(BASE_TEN);
  28. if (billionths === ONE_BILLION) {
  29. return 0;
  30. }
  31. if (billionths === 'NaN') {
  32. throw new errors_1.ValidationError(`Value is not a number`);
  33. }
  34. if (billionths.includes('.')) {
  35. throw new errors_1.ValidationError(`Decimal exceeds maximum precision.`);
  36. }
  37. return Number(billionths);
  38. }
  39. exports.decimalToTransferRate = decimalToTransferRate;
  40. function percentToTransferRate(percent) {
  41. return decimalToTransferRate(percentToDecimal(percent));
  42. }
  43. exports.percentToTransferRate = percentToTransferRate;
  44. function decimalToQuality(decimal) {
  45. const rate = new bignumber_js_1.default(decimal).times(ONE_BILLION);
  46. const billionths = rate.toString(BASE_TEN);
  47. if (billionths === 'NaN') {
  48. throw new errors_1.ValidationError(`Value is not a number`);
  49. }
  50. if (billionths.includes('-')) {
  51. throw new errors_1.ValidationError('Cannot have negative Quality');
  52. }
  53. if (billionths === ONE_BILLION) {
  54. return 0;
  55. }
  56. if (billionths.includes('.')) {
  57. throw new errors_1.ValidationError(`Decimal exceeds maximum precision.`);
  58. }
  59. return Number(billionths);
  60. }
  61. exports.decimalToQuality = decimalToQuality;
  62. function qualityToDecimal(quality) {
  63. if (!Number.isInteger(quality)) {
  64. throw new errors_1.ValidationError('Quality must be an integer');
  65. }
  66. if (quality < 0) {
  67. throw new errors_1.ValidationError('Negative quality not allowed');
  68. }
  69. if (quality === 0) {
  70. return '1';
  71. }
  72. const decimal = new bignumber_js_1.default(quality).dividedBy(ONE_BILLION);
  73. return decimal.toString(BASE_TEN);
  74. }
  75. exports.qualityToDecimal = qualityToDecimal;
  76. function transferRateToDecimal(rate) {
  77. if (!Number.isInteger(rate)) {
  78. throw new errors_1.ValidationError('Error decoding, transfer Rate must be an integer');
  79. }
  80. if (rate === 0) {
  81. return '0';
  82. }
  83. const decimal = new bignumber_js_1.default(rate).minus(ONE_BILLION).dividedBy(ONE_BILLION);
  84. if (decimal.isLessThan(0)) {
  85. throw new errors_1.ValidationError('Error decoding, negative transfer rate');
  86. }
  87. return decimal.toString(BASE_TEN);
  88. }
  89. exports.transferRateToDecimal = transferRateToDecimal;
  90. function percentToQuality(percent) {
  91. return decimalToQuality(percentToDecimal(percent));
  92. }
  93. exports.percentToQuality = percentToQuality;
  94. //# sourceMappingURL=quality.js.map