@alcorexchange/alcor-swap-sdk 1.0.38 → 1.0.391

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/.babelrc +20 -0
  2. package/build/entities/baseCurrency.js +45 -21
  3. package/build/entities/currency.js +4 -1
  4. package/build/entities/fractions/currencyAmount.js +158 -96
  5. package/build/entities/fractions/fraction.js +130 -88
  6. package/build/entities/fractions/index.js +32 -10
  7. package/build/entities/fractions/percent.js +70 -31
  8. package/build/entities/fractions/price.js +98 -52
  9. package/build/entities/index.js +114 -25
  10. package/build/entities/pool.js +315 -256
  11. package/build/entities/position.js +443 -313
  12. package/build/entities/route.js +142 -121
  13. package/build/entities/tick.js +72 -43
  14. package/build/entities/tickDataProvider.js +28 -9
  15. package/build/entities/tickListDataProvider.js +38 -19
  16. package/build/entities/token.js +67 -43
  17. package/build/entities/trade.js +431 -294
  18. package/build/errors.js +48 -22
  19. package/build/index.js +36 -17
  20. package/build/internalConstants.js +43 -40
  21. package/build/utils/common.js +56 -39
  22. package/build/utils/computeAllRoutes.js +62 -66
  23. package/build/utils/encodeSqrtRatioX64.js +12 -13
  24. package/build/utils/fullMath.js +26 -19
  25. package/build/utils/getBestSwapRoute.js +148 -138
  26. package/build/utils/index.js +191 -32
  27. package/build/utils/isSorted.js +11 -9
  28. package/build/utils/liquidityMath.js +27 -20
  29. package/build/utils/maxLiquidityForAmounts.js +38 -41
  30. package/build/utils/mostSignificantBit.js +20 -24
  31. package/build/utils/nearestUsableTick.js +14 -19
  32. package/build/utils/positionLibrary.js +28 -19
  33. package/build/utils/priceTickConversions.js +27 -33
  34. package/build/utils/sortedInsert.js +33 -33
  35. package/build/utils/sqrt.js +29 -27
  36. package/build/utils/sqrtPriceMath.js +85 -79
  37. package/build/utils/swapMath.js +60 -81
  38. package/build/utils/tickLibrary.js +56 -55
  39. package/build/utils/tickList.js +130 -99
  40. package/build/utils/tickMath.js +81 -95
  41. package/examples/utils/rpc.js +114 -0
  42. package/package.json +11 -4
  43. package/build/entities/baseCurrency.d.ts +0 -32
  44. package/build/entities/currency.d.ts +0 -2
  45. package/build/entities/fractions/currencyAmount.d.ts +0 -38
  46. package/build/entities/fractions/fraction.d.ts +0 -24
  47. package/build/entities/fractions/index.d.ts +0 -4
  48. package/build/entities/fractions/percent.d.ts +0 -14
  49. package/build/entities/fractions/price.d.ts +0 -40
  50. package/build/entities/index.d.ts +0 -10
  51. package/build/entities/pool.d.ts +0 -118
  52. package/build/entities/position.d.ts +0 -170
  53. package/build/entities/route.d.ts +0 -47
  54. package/build/entities/tick.d.ts +0 -25
  55. package/build/entities/tickDataProvider.d.ts +0 -27
  56. package/build/entities/tickListDataProvider.d.ts +0 -13
  57. package/build/entities/token.d.ts +0 -30
  58. package/build/entities/trade.d.ts +0 -198
  59. package/build/errors.d.ts +0 -16
  60. package/build/index.d.ts +0 -3
  61. package/build/internalConstants.d.ts +0 -37
  62. package/build/utils/common.d.ts +0 -14
  63. package/build/utils/computeAllRoutes.d.ts +0 -5
  64. package/build/utils/encodeSqrtRatioX64.d.ts +0 -9
  65. package/build/utils/fullMath.d.ts +0 -8
  66. package/build/utils/getBestSwapRoute.d.ts +0 -9
  67. package/build/utils/index.d.ts +0 -17
  68. package/build/utils/isSorted.d.ts +0 -7
  69. package/build/utils/liquidityMath.d.ts +0 -8
  70. package/build/utils/maxLiquidityForAmounts.d.ts +0 -14
  71. package/build/utils/mostSignificantBit.d.ts +0 -2
  72. package/build/utils/nearestUsableTick.d.ts +0 -6
  73. package/build/utils/positionLibrary.d.ts +0 -8
  74. package/build/utils/priceTickConversions.d.ts +0 -16
  75. package/build/utils/sortedInsert.d.ts +0 -1
  76. package/build/utils/sqrt.d.ts +0 -7
  77. package/build/utils/sqrtPriceMath.d.ts +0 -13
  78. package/build/utils/swapMath.d.ts +0 -9
  79. package/build/utils/tickLibrary.d.ts +0 -15
  80. package/build/utils/tickList.d.ts +0 -23
  81. package/build/utils/tickMath.d.ts +0 -30
package/.babelrc ADDED
@@ -0,0 +1,20 @@
1
+ {
2
+ "presets": [
3
+ [
4
+ "@babel/preset-env",
5
+ {
6
+ "targets": {
7
+ "browsers": "defaults",
8
+ "node": "8"
9
+ },
10
+ "modules": "commonjs"
11
+ }
12
+ ],
13
+ "@babel/preset-typescript"
14
+ ],
15
+ "plugins": [
16
+ ["transform-jsbi-to-bigint", { "debug": true }],
17
+ "@babel/plugin-proposal-class-properties",
18
+ "@babel/plugin-transform-classes"
19
+ ]
20
+ }
@@ -1,27 +1,51 @@
1
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 });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
6
  exports.BaseCurrency = void 0;
7
- const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
7
+ var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));
8
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
9
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
10
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
11
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
12
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
13
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
14
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
8
15
  /**
9
16
  * A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
10
17
  */
11
- class BaseCurrency {
12
- /**
13
- * Constructs an instance of the base class `BaseCurrency`.
14
- * @param chainId the chain ID on which this currency resides
15
- * @param decimals decimals of the currency
16
- * @param symbol symbol of the currency
17
- * @param name of the currency
18
- */
19
- constructor(contract, decimals, symbol) {
20
- (0, tiny_invariant_1.default)(decimals >= 0 && decimals < 19 && Number.isInteger(decimals), "DECIMALS");
21
- this.contract = contract;
22
- this.decimals = decimals;
23
- this.symbol = symbol;
24
- this.id = symbol.toLowerCase() + '-' + contract;
25
- }
18
+ let BaseCurrency = exports.BaseCurrency = /*#__PURE__*/_createClass(
19
+ /**
20
+ * Constructs an instance of the base class `BaseCurrency`.
21
+ * @param chainId the chain ID on which this currency resides
22
+ * @param decimals decimals of the currency
23
+ * @param symbol symbol of the currency
24
+ * @param name of the currency
25
+ */
26
+ function BaseCurrency(contract, decimals, symbol) {
27
+ _classCallCheck(this, BaseCurrency);
28
+ /**
29
+ * The contract address of the currency
30
+ */
31
+ _defineProperty(this, "contract", void 0);
32
+ /**
33
+ * The decimals used in representing currency amounts
34
+ */
35
+ _defineProperty(this, "decimals", void 0);
36
+ /**
37
+ * The symbol of the currency, i.e. a short textual non-unique identifier
38
+ */
39
+ _defineProperty(this, "symbol", void 0);
40
+ _defineProperty(this, "id", void 0);
41
+ (0, _tinyInvariant.default)(decimals >= 0 && decimals < 19 && Number.isInteger(decimals), "DECIMALS");
42
+ this.contract = contract;
43
+ this.decimals = decimals;
44
+ this.symbol = symbol;
45
+ this.id = symbol.toLowerCase() + '-' + contract;
26
46
  }
27
- exports.BaseCurrency = BaseCurrency;
47
+
48
+ /**
49
+ * Returns whether this currency is functionally equivalent to the other currency
50
+ * @param other the other currency
51
+ */);
@@ -1,2 +1,5 @@
1
1
  "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -1,110 +1,172 @@
1
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 });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
6
  exports.CurrencyAmount = void 0;
7
- const msgpack_lite_1 = __importDefault(require("msgpack-lite"));
8
- const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
9
- const jsbi_1 = __importDefault(require("jsbi"));
10
- const token_1 = require("../token");
11
- const fraction_1 = require("./fraction");
12
- const big_js_1 = __importDefault(require("big.js"));
13
- const toformat_1 = __importDefault(require("toformat"));
14
- const internalConstants_1 = require("../../internalConstants");
15
- const Big = (0, toformat_1.default)(big_js_1.default);
16
- class CurrencyAmount extends fraction_1.Fraction {
7
+ var _msgpackLite = _interopRequireDefault(require("msgpack-lite"));
8
+ var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));
9
+ var _token = require("../token");
10
+ var _fraction = require("./fraction");
11
+ var _big = _interopRequireDefault(require("big.js"));
12
+ var _toformat = _interopRequireDefault(require("toformat"));
13
+ var _internalConstants = require("../../internalConstants");
14
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
15
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
16
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
17
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
18
+ function _callSuper(t, o, e) { return o = _getPrototypeOf(o), _possibleConstructorReturn(t, _isNativeReflectConstruct() ? Reflect.construct(o, e || [], _getPrototypeOf(t).constructor) : o.apply(t, e)); }
19
+ function _possibleConstructorReturn(t, e) { if (e && ("object" == typeof e || "function" == typeof e)) return e; if (void 0 !== e) throw new TypeError("Derived constructors may only return object or undefined"); return _assertThisInitialized(t); }
20
+ function _assertThisInitialized(e) { if (void 0 === e) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return e; }
21
+ function _isNativeReflectConstruct() { try { var t = !Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); } catch (t) {} return (_isNativeReflectConstruct = function () { return !!t; })(); }
22
+ function _superPropGet(t, o, e, r) { var p = _get(_getPrototypeOf(1 & r ? t.prototype : t), o, e); return 2 & r && "function" == typeof p ? function (t) { return p.apply(e, t); } : p; }
23
+ function _get() { return _get = "undefined" != typeof Reflect && Reflect.get ? Reflect.get.bind() : function (e, t, r) { var p = _superPropBase(e, t); if (p) { var n = Object.getOwnPropertyDescriptor(p, t); return n.get ? n.get.call(arguments.length < 3 ? e : r) : n.value; } }, _get.apply(null, arguments); }
24
+ function _superPropBase(t, o) { for (; !{}.hasOwnProperty.call(t, o) && null !== (t = _getPrototypeOf(t));); return t; }
25
+ function _getPrototypeOf(t) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function (t) { return t.__proto__ || Object.getPrototypeOf(t); }, _getPrototypeOf(t); }
26
+ function _inherits(t, e) { if ("function" != typeof e && null !== e) throw new TypeError("Super expression must either be null or a function"); t.prototype = Object.create(e && e.prototype, { constructor: { value: t, writable: !0, configurable: !0 } }), Object.defineProperty(t, "prototype", { writable: !1 }), e && _setPrototypeOf(t, e); }
27
+ function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function (t, e) { return t.__proto__ = e, t; }, _setPrototypeOf(t, e); }
28
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
29
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
30
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
31
+ const Big = (0, _toformat.default)(_big.default);
32
+ let CurrencyAmount = exports.CurrencyAmount = /*#__PURE__*/function (_Fraction) {
33
+ function CurrencyAmount(currency, numerator, denominator) {
34
+ var _this;
35
+ _classCallCheck(this, CurrencyAmount);
36
+ _this = _callSuper(this, CurrencyAmount, [numerator, denominator]);
37
+ _defineProperty(_this, "currency", void 0);
38
+ _defineProperty(_this, "decimalScale", void 0);
39
+ (0, _tinyInvariant.default)(_this.quotient <= _internalConstants.MaxUint256, "AMOUNT");
40
+ _this.currency = currency;
41
+ _this.decimalScale = 10n ** BigInt(currency.decimals);
42
+ return _this;
43
+ }
44
+ _inherits(CurrencyAmount, _Fraction);
45
+ return _createClass(CurrencyAmount, [{
46
+ key: "add",
47
+ value: function add(other) {
48
+ (0, _tinyInvariant.default)(this.currency.equals(other.currency), "CURRENCY");
49
+ const added = _superPropGet(CurrencyAmount, "add", this, 3)([other]);
50
+ return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
51
+ }
52
+ }, {
53
+ key: "subtract",
54
+ value: function subtract(other) {
55
+ (0, _tinyInvariant.default)(this.currency.equals(other.currency), "CURRENCY");
56
+ const subtracted = _superPropGet(CurrencyAmount, "subtract", this, 3)([other]);
57
+ return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
58
+ }
59
+ }, {
60
+ key: "multiply",
61
+ value: function multiply(other) {
62
+ const multiplied = _superPropGet(CurrencyAmount, "multiply", this, 3)([other]);
63
+ return CurrencyAmount.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
64
+ }
65
+ }, {
66
+ key: "divide",
67
+ value: function divide(other) {
68
+ const divided = _superPropGet(CurrencyAmount, "divide", this, 3)([other]);
69
+ return CurrencyAmount.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
70
+ }
71
+ }, {
72
+ key: "toSignificant",
73
+ value: function toSignificant() {
74
+ let significantDigits = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 6;
75
+ let format = arguments.length > 1 ? arguments[1] : undefined;
76
+ let rounding = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _internalConstants.Rounding.ROUND_DOWN;
77
+ return _superPropGet(CurrencyAmount, "divide", this, 3)([this.decimalScale]).toSignificant(significantDigits, format, rounding);
78
+ }
79
+ }, {
80
+ key: "toFixed",
81
+ value: function toFixed() {
82
+ let decimalPlaces = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.currency.decimals;
83
+ let format = arguments.length > 1 ? arguments[1] : undefined;
84
+ let rounding = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _internalConstants.Rounding.ROUND_DOWN;
85
+ (0, _tinyInvariant.default)(decimalPlaces <= this.currency.decimals, "DECIMALS");
86
+ return _superPropGet(CurrencyAmount, "divide", this, 3)([this.decimalScale]).toFixed(decimalPlaces, format, rounding);
87
+ }
88
+ }, {
89
+ key: "toExact",
90
+ value: function toExact() {
91
+ let format = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
92
+ groupSeparator: ""
93
+ };
94
+ Big.DP = this.currency.decimals;
95
+ return new Big(this.quotient.toString()).div(this.decimalScale.toString()).toFormat(format);
96
+ }
97
+ }, {
98
+ key: "toAsset",
99
+ value: function toAsset() {
100
+ return `${this.toFixed(...arguments)} ${this.currency.symbol}`;
101
+ }
102
+ }, {
103
+ key: "toExtendedAsset",
104
+ value: function toExtendedAsset() {
105
+ return `${this.toFixed(...arguments)} ${this.currency.symbol}@${this.currency.contract}`;
106
+ }
107
+ }, {
108
+ key: "toExtendedAssetObject",
109
+ value: function toExtendedAssetObject() {
110
+ return {
111
+ quantity: `${this.toFixed(...arguments)} ${this.currency.symbol}`,
112
+ contract: this.currency.contract
113
+ };
114
+ }
115
+ }], [{
116
+ key: "fromRawAmount",
117
+ value:
17
118
  /**
18
119
  * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
19
120
  * @param currency the currency in the amount
20
121
  * @param rawAmount the raw token or ether amount
21
122
  */
22
- static fromRawAmount(currency, rawAmount) {
23
- return new CurrencyAmount(currency, rawAmount);
123
+ function fromRawAmount(currency, rawAmount) {
124
+ return new CurrencyAmount(currency, rawAmount);
24
125
  }
126
+
25
127
  /**
26
128
  * Construct a currency amount with a denominator that is not equal to 1
27
129
  * @param currency the currency
28
130
  * @param numerator the numerator of the fractional token amount
29
131
  * @param denominator the denominator of the fractional token amount
30
132
  */
31
- static fromFractionalAmount(currency, numerator, denominator) {
32
- return new CurrencyAmount(currency, numerator, denominator);
33
- }
34
- constructor(currency, numerator, denominator) {
35
- super(numerator, denominator);
36
- (0, tiny_invariant_1.default)(jsbi_1.default.lessThanOrEqual(this.quotient, internalConstants_1.MaxUint256), "AMOUNT");
37
- this.currency = currency;
38
- this.decimalScale = jsbi_1.default.exponentiate(jsbi_1.default.BigInt(10), jsbi_1.default.BigInt(currency.decimals));
39
- }
40
- add(other) {
41
- (0, tiny_invariant_1.default)(this.currency.equals(other.currency), "CURRENCY");
42
- const added = super.add(other);
43
- return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
44
- }
45
- subtract(other) {
46
- (0, tiny_invariant_1.default)(this.currency.equals(other.currency), "CURRENCY");
47
- const subtracted = super.subtract(other);
48
- return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
49
- }
50
- multiply(other) {
51
- const multiplied = super.multiply(other);
52
- return CurrencyAmount.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
53
- }
54
- divide(other) {
55
- const divided = super.divide(other);
56
- return CurrencyAmount.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
57
- }
58
- toSignificant(significantDigits = 6, format, rounding = internalConstants_1.Rounding.ROUND_DOWN) {
59
- return super
60
- .divide(this.decimalScale)
61
- .toSignificant(significantDigits, format, rounding);
62
- }
63
- toFixed(decimalPlaces = this.currency.decimals, format, rounding = internalConstants_1.Rounding.ROUND_DOWN) {
64
- (0, tiny_invariant_1.default)(decimalPlaces <= this.currency.decimals, "DECIMALS");
65
- return super
66
- .divide(this.decimalScale)
67
- .toFixed(decimalPlaces, format, rounding);
68
- }
69
- toExact(format = { groupSeparator: "" }) {
70
- Big.DP = this.currency.decimals;
71
- return new Big(this.quotient.toString())
72
- .div(this.decimalScale.toString())
73
- .toFormat(format);
74
- }
75
- toAsset(...args) {
76
- return `${this.toFixed(...args)} ${this.currency.symbol}`;
77
- }
78
- toExtendedAsset(...args) {
79
- return `${this.toFixed(...args)} ${this.currency.symbol}@${this.currency.contract}`;
80
- }
81
- toExtendedAssetObject(...args) {
82
- return { quantity: `${this.toFixed(...args)} ${this.currency.symbol}`, contract: this.currency.contract };
83
- }
84
- static toJSON(amount) {
85
- return {
86
- currency: token_1.Token.toJSON(amount.currency),
87
- numerator: amount.numerator.toString(),
88
- denominator: amount.denominator.toString(),
89
- };
90
- }
91
- static fromJSON(json) {
92
- const currency = token_1.Token.fromJSON(json.currency);
93
- const numerator = jsbi_1.default.BigInt(json.numerator);
94
- const denominator = jsbi_1.default.BigInt(json.denominator);
95
- return new CurrencyAmount(currency, numerator, denominator);
96
- }
97
- static toBuffer(amount) {
98
- const json = {
99
- currency: token_1.Token.toJSON(amount.currency),
100
- numerator: amount.numerator.toString(),
101
- denominator: amount.denominator.toString(),
102
- };
103
- return msgpack_lite_1.default.encode(json);
104
- }
105
- static fromBuffer(buffer) {
106
- const json = msgpack_lite_1.default.decode(buffer);
107
- return this.fromJSON(json);
108
- }
109
- }
110
- exports.CurrencyAmount = CurrencyAmount;
133
+ }, {
134
+ key: "fromFractionalAmount",
135
+ value: function fromFractionalAmount(currency, numerator, denominator) {
136
+ return new CurrencyAmount(currency, numerator, denominator);
137
+ }
138
+ }, {
139
+ key: "toJSON",
140
+ value: function toJSON(amount) {
141
+ return {
142
+ currency: _token.Token.toJSON(amount.currency),
143
+ numerator: amount.numerator.toString(),
144
+ denominator: amount.denominator.toString()
145
+ };
146
+ }
147
+ }, {
148
+ key: "fromJSON",
149
+ value: function fromJSON(json) {
150
+ const currency = _token.Token.fromJSON(json.currency);
151
+ const numerator = BigInt(json.numerator);
152
+ const denominator = BigInt(json.denominator);
153
+ return new CurrencyAmount(currency, numerator, denominator);
154
+ }
155
+ }, {
156
+ key: "toBuffer",
157
+ value: function toBuffer(amount) {
158
+ const json = {
159
+ currency: _token.Token.toJSON(amount.currency),
160
+ numerator: amount.numerator.toString(),
161
+ denominator: amount.denominator.toString()
162
+ };
163
+ return _msgpackLite.default.encode(json);
164
+ }
165
+ }, {
166
+ key: "fromBuffer",
167
+ value: function fromBuffer(buffer) {
168
+ const json = _msgpackLite.default.decode(buffer);
169
+ return this.fromJSON(json);
170
+ }
171
+ }]);
172
+ }(_fraction.Fraction);
@@ -1,117 +1,159 @@
1
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 });
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
6
  exports.Fraction = void 0;
7
- const jsbi_1 = __importDefault(require("jsbi"));
8
- const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
9
- const toformat_1 = __importDefault(require("toformat"));
10
- const decimal_js_light_1 = __importDefault(require("decimal.js-light"));
11
- const big_js_1 = __importDefault(require("big.js"));
12
- const internalConstants_1 = require("../../internalConstants");
13
- const Decimal = (0, toformat_1.default)(decimal_js_light_1.default);
14
- const Big = (0, toformat_1.default)(big_js_1.default);
7
+ var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));
8
+ var _toformat = _interopRequireDefault(require("toformat"));
9
+ var _decimal = _interopRequireDefault(require("decimal.js-light"));
10
+ var _big = _interopRequireDefault(require("big.js"));
11
+ var _internalConstants = require("../../internalConstants");
12
+ function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
13
+ function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
14
+ function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
15
+ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
16
+ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
17
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
18
+ function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
19
+ const Decimal = (0, _toformat.default)(_decimal.default);
20
+ const Big = (0, _toformat.default)(_big.default);
15
21
  const toSignificantRounding = {
16
- [internalConstants_1.Rounding.ROUND_DOWN]: Decimal.ROUND_DOWN,
17
- [internalConstants_1.Rounding.ROUND_HALF_UP]: Decimal.ROUND_HALF_UP,
18
- [internalConstants_1.Rounding.ROUND_UP]: Decimal.ROUND_UP,
22
+ [_internalConstants.Rounding.ROUND_DOWN]: Decimal.ROUND_DOWN,
23
+ [_internalConstants.Rounding.ROUND_HALF_UP]: Decimal.ROUND_HALF_UP,
24
+ [_internalConstants.Rounding.ROUND_UP]: Decimal.ROUND_UP
19
25
  };
26
+
20
27
  // const toFixedRounding = {
21
28
  // [Rounding.ROUND_DOWN]: RoundingMode.RoundDown,
22
29
  // [Rounding.ROUND_HALF_UP]: RoundingMode.RoundHalfUp,
23
30
  // [Rounding.ROUND_UP]: RoundingMode.RoundUp
24
31
  // }
25
32
  const toFixedRounding = {
26
- [internalConstants_1.Rounding.ROUND_DOWN]: 0,
27
- [internalConstants_1.Rounding.ROUND_HALF_UP]: 1,
28
- [internalConstants_1.Rounding.ROUND_UP]: 3,
33
+ [_internalConstants.Rounding.ROUND_DOWN]: 0,
34
+ [_internalConstants.Rounding.ROUND_HALF_UP]: 1,
35
+ [_internalConstants.Rounding.ROUND_UP]: 3
29
36
  };
30
- class Fraction {
31
- constructor(numerator, denominator = jsbi_1.default.BigInt(1)) {
32
- this.numerator = jsbi_1.default.BigInt(numerator);
33
- this.denominator = jsbi_1.default.BigInt(denominator);
34
- }
35
- static tryParseFraction(fractionish) {
36
- if (fractionish instanceof jsbi_1.default ||
37
- typeof fractionish === "number" ||
38
- typeof fractionish === "string")
39
- return new Fraction(fractionish);
40
- if ("numerator" in fractionish && "denominator" in fractionish)
41
- return fractionish;
42
- throw new Error("Could not parse fraction");
43
- }
37
+ let Fraction = exports.Fraction = /*#__PURE__*/function () {
38
+ function Fraction(numerator) {
39
+ let denominator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1n;
40
+ _classCallCheck(this, Fraction);
41
+ _defineProperty(this, "numerator", void 0);
42
+ _defineProperty(this, "denominator", void 0);
43
+ this.numerator = BigInt(numerator);
44
+ this.denominator = BigInt(denominator);
45
+ }
46
+ return _createClass(Fraction, [{
47
+ key: "quotient",
48
+ get:
44
49
  // performs floor division
45
- get quotient() {
46
- return jsbi_1.default.divide(this.numerator, this.denominator);
50
+ function () {
51
+ return this.numerator / this.denominator;
47
52
  }
53
+
48
54
  // remainder after floor division
49
- get remainder() {
50
- return new Fraction(jsbi_1.default.remainder(this.numerator, this.denominator), this.denominator);
55
+ }, {
56
+ key: "remainder",
57
+ get: function () {
58
+ return new Fraction(this.numerator % this.denominator, this.denominator);
51
59
  }
52
- invert() {
53
- return new Fraction(this.denominator, this.numerator);
60
+ }, {
61
+ key: "invert",
62
+ value: function invert() {
63
+ return new Fraction(this.denominator, this.numerator);
54
64
  }
55
- add(other) {
56
- const otherParsed = Fraction.tryParseFraction(other);
57
- if (jsbi_1.default.equal(this.denominator, otherParsed.denominator)) {
58
- return new Fraction(jsbi_1.default.add(this.numerator, otherParsed.numerator), this.denominator);
59
- }
60
- return new Fraction(jsbi_1.default.add(jsbi_1.default.multiply(this.numerator, otherParsed.denominator), jsbi_1.default.multiply(otherParsed.numerator, this.denominator)), jsbi_1.default.multiply(this.denominator, otherParsed.denominator));
65
+ }, {
66
+ key: "add",
67
+ value: function add(other) {
68
+ const otherParsed = Fraction.tryParseFraction(other);
69
+ if (this.denominator === otherParsed.denominator) {
70
+ return new Fraction(this.numerator + otherParsed.numerator, this.denominator);
71
+ }
72
+ return new Fraction(this.numerator * otherParsed.denominator + otherParsed.numerator * this.denominator, this.denominator * otherParsed.denominator);
61
73
  }
62
- subtract(other) {
63
- const otherParsed = Fraction.tryParseFraction(other);
64
- if (jsbi_1.default.equal(this.denominator, otherParsed.denominator)) {
65
- return new Fraction(jsbi_1.default.subtract(this.numerator, otherParsed.numerator), this.denominator);
66
- }
67
- return new Fraction(jsbi_1.default.subtract(jsbi_1.default.multiply(this.numerator, otherParsed.denominator), jsbi_1.default.multiply(otherParsed.numerator, this.denominator)), jsbi_1.default.multiply(this.denominator, otherParsed.denominator));
74
+ }, {
75
+ key: "subtract",
76
+ value: function subtract(other) {
77
+ const otherParsed = Fraction.tryParseFraction(other);
78
+ if (this.denominator === otherParsed.denominator) {
79
+ return new Fraction(this.numerator - otherParsed.numerator, this.denominator);
80
+ }
81
+ return new Fraction(this.numerator * otherParsed.denominator - otherParsed.numerator * this.denominator, this.denominator * otherParsed.denominator);
68
82
  }
69
- lessThan(other) {
70
- const otherParsed = Fraction.tryParseFraction(other);
71
- return jsbi_1.default.lessThan(jsbi_1.default.multiply(this.numerator, otherParsed.denominator), jsbi_1.default.multiply(otherParsed.numerator, this.denominator));
83
+ }, {
84
+ key: "lessThan",
85
+ value: function lessThan(other) {
86
+ const otherParsed = Fraction.tryParseFraction(other);
87
+ return this.numerator * otherParsed.denominator < otherParsed.numerator * this.denominator;
72
88
  }
73
- equalTo(other) {
74
- const otherParsed = Fraction.tryParseFraction(other);
75
- return jsbi_1.default.equal(jsbi_1.default.multiply(this.numerator, otherParsed.denominator), jsbi_1.default.multiply(otherParsed.numerator, this.denominator));
89
+ }, {
90
+ key: "equalTo",
91
+ value: function equalTo(other) {
92
+ const otherParsed = Fraction.tryParseFraction(other);
93
+ return this.numerator * otherParsed.denominator === otherParsed.numerator * this.denominator;
76
94
  }
77
- greaterThan(other) {
78
- const otherParsed = Fraction.tryParseFraction(other);
79
- return jsbi_1.default.greaterThan(jsbi_1.default.multiply(this.numerator, otherParsed.denominator), jsbi_1.default.multiply(otherParsed.numerator, this.denominator));
95
+ }, {
96
+ key: "greaterThan",
97
+ value: function greaterThan(other) {
98
+ const otherParsed = Fraction.tryParseFraction(other);
99
+ return this.numerator * otherParsed.denominator > otherParsed.numerator * this.denominator;
80
100
  }
81
- multiply(other) {
82
- const otherParsed = Fraction.tryParseFraction(other);
83
- return new Fraction(jsbi_1.default.multiply(this.numerator, otherParsed.numerator), jsbi_1.default.multiply(this.denominator, otherParsed.denominator));
101
+ }, {
102
+ key: "multiply",
103
+ value: function multiply(other) {
104
+ const otherParsed = Fraction.tryParseFraction(other);
105
+ return new Fraction(this.numerator * otherParsed.numerator, this.denominator * otherParsed.denominator);
84
106
  }
85
- divide(other) {
86
- const otherParsed = Fraction.tryParseFraction(other);
87
- return new Fraction(jsbi_1.default.multiply(this.numerator, otherParsed.denominator), jsbi_1.default.multiply(this.denominator, otherParsed.numerator));
107
+ }, {
108
+ key: "divide",
109
+ value: function divide(other) {
110
+ const otherParsed = Fraction.tryParseFraction(other);
111
+ return new Fraction(this.numerator * otherParsed.denominator, this.denominator * otherParsed.numerator);
88
112
  }
89
- toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = internalConstants_1.Rounding.ROUND_HALF_UP) {
90
- (0, tiny_invariant_1.default)(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
91
- (0, tiny_invariant_1.default)(significantDigits > 0, `${significantDigits} is not positive.`);
92
- Decimal.set({
93
- precision: significantDigits + 1,
94
- rounding: toSignificantRounding[rounding],
95
- });
96
- const quotient = new Decimal(this.numerator.toString())
97
- .div(this.denominator.toString())
98
- .toSignificantDigits(significantDigits);
99
- return quotient.toFormat(quotient.decimalPlaces(), format);
113
+ }, {
114
+ key: "toSignificant",
115
+ value: function toSignificant(significantDigits) {
116
+ let format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
117
+ groupSeparator: ""
118
+ };
119
+ let rounding = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _internalConstants.Rounding.ROUND_HALF_UP;
120
+ (0, _tinyInvariant.default)(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
121
+ (0, _tinyInvariant.default)(significantDigits > 0, `${significantDigits} is not positive.`);
122
+ Decimal.set({
123
+ precision: significantDigits + 1,
124
+ rounding: toSignificantRounding[rounding]
125
+ });
126
+ const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
127
+ return quotient.toFormat(quotient.decimalPlaces(), format);
100
128
  }
101
- toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = internalConstants_1.Rounding.ROUND_HALF_UP) {
102
- (0, tiny_invariant_1.default)(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
103
- (0, tiny_invariant_1.default)(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
104
- Big.DP = decimalPlaces;
105
- Big.RM = toFixedRounding[rounding];
106
- return new Big(this.numerator.toString())
107
- .div(this.denominator.toString())
108
- .toFormat(decimalPlaces, format);
129
+ }, {
130
+ key: "toFixed",
131
+ value: function toFixed(decimalPlaces) {
132
+ let format = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
133
+ groupSeparator: ""
134
+ };
135
+ let rounding = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : _internalConstants.Rounding.ROUND_HALF_UP;
136
+ (0, _tinyInvariant.default)(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
137
+ (0, _tinyInvariant.default)(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
138
+ Big.DP = decimalPlaces;
139
+ Big.RM = toFixedRounding[rounding];
140
+ return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
109
141
  }
142
+
110
143
  /**
111
144
  * Helper method for converting any super class back to a fraction
112
145
  */
113
- get asFraction() {
114
- return new Fraction(this.numerator, this.denominator);
146
+ }, {
147
+ key: "asFraction",
148
+ get: function () {
149
+ return new Fraction(this.numerator, this.denominator);
150
+ }
151
+ }], [{
152
+ key: "tryParseFraction",
153
+ value: function tryParseFraction(fractionish) {
154
+ if (typeof fractionish === "bigint" || typeof fractionish === "number" || typeof fractionish === "string") return new Fraction(fractionish);
155
+ if ("numerator" in fractionish && "denominator" in fractionish) return fractionish;
156
+ throw new Error("Could not parse fraction");
115
157
  }
116
- }
117
- exports.Fraction = Fraction;
158
+ }]);
159
+ }();