@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.
- package/.babelrc +20 -0
- package/build/entities/baseCurrency.js +45 -21
- package/build/entities/currency.js +4 -1
- package/build/entities/fractions/currencyAmount.js +158 -96
- package/build/entities/fractions/fraction.js +130 -88
- package/build/entities/fractions/index.js +32 -10
- package/build/entities/fractions/percent.js +70 -31
- package/build/entities/fractions/price.js +98 -52
- package/build/entities/index.js +114 -25
- package/build/entities/pool.js +315 -256
- package/build/entities/position.js +443 -313
- package/build/entities/route.js +142 -121
- package/build/entities/tick.js +72 -43
- package/build/entities/tickDataProvider.js +28 -9
- package/build/entities/tickListDataProvider.js +38 -19
- package/build/entities/token.js +67 -43
- package/build/entities/trade.js +431 -294
- package/build/errors.js +48 -22
- package/build/index.js +36 -17
- package/build/internalConstants.js +43 -40
- package/build/utils/common.js +56 -39
- package/build/utils/computeAllRoutes.js +62 -66
- package/build/utils/encodeSqrtRatioX64.js +12 -13
- package/build/utils/fullMath.js +26 -19
- package/build/utils/getBestSwapRoute.js +148 -138
- package/build/utils/index.js +191 -32
- package/build/utils/isSorted.js +11 -9
- package/build/utils/liquidityMath.js +27 -20
- package/build/utils/maxLiquidityForAmounts.js +38 -41
- package/build/utils/mostSignificantBit.js +20 -24
- package/build/utils/nearestUsableTick.js +14 -19
- package/build/utils/positionLibrary.js +28 -19
- package/build/utils/priceTickConversions.js +27 -33
- package/build/utils/sortedInsert.js +33 -33
- package/build/utils/sqrt.js +29 -27
- package/build/utils/sqrtPriceMath.js +85 -79
- package/build/utils/swapMath.js +60 -81
- package/build/utils/tickLibrary.js +56 -55
- package/build/utils/tickList.js +130 -99
- package/build/utils/tickMath.js +81 -95
- package/examples/utils/rpc.js +114 -0
- package/package.json +11 -4
- package/build/entities/baseCurrency.d.ts +0 -32
- package/build/entities/currency.d.ts +0 -2
- package/build/entities/fractions/currencyAmount.d.ts +0 -38
- package/build/entities/fractions/fraction.d.ts +0 -24
- package/build/entities/fractions/index.d.ts +0 -4
- package/build/entities/fractions/percent.d.ts +0 -14
- package/build/entities/fractions/price.d.ts +0 -40
- package/build/entities/index.d.ts +0 -10
- package/build/entities/pool.d.ts +0 -118
- package/build/entities/position.d.ts +0 -170
- package/build/entities/route.d.ts +0 -47
- package/build/entities/tick.d.ts +0 -25
- package/build/entities/tickDataProvider.d.ts +0 -27
- package/build/entities/tickListDataProvider.d.ts +0 -13
- package/build/entities/token.d.ts +0 -30
- package/build/entities/trade.d.ts +0 -198
- package/build/errors.d.ts +0 -16
- package/build/index.d.ts +0 -3
- package/build/internalConstants.d.ts +0 -37
- package/build/utils/common.d.ts +0 -14
- package/build/utils/computeAllRoutes.d.ts +0 -5
- package/build/utils/encodeSqrtRatioX64.d.ts +0 -9
- package/build/utils/fullMath.d.ts +0 -8
- package/build/utils/getBestSwapRoute.d.ts +0 -9
- package/build/utils/index.d.ts +0 -17
- package/build/utils/isSorted.d.ts +0 -7
- package/build/utils/liquidityMath.d.ts +0 -8
- package/build/utils/maxLiquidityForAmounts.d.ts +0 -14
- package/build/utils/mostSignificantBit.d.ts +0 -2
- package/build/utils/nearestUsableTick.d.ts +0 -6
- package/build/utils/positionLibrary.d.ts +0 -8
- package/build/utils/priceTickConversions.d.ts +0 -16
- package/build/utils/sortedInsert.d.ts +0 -1
- package/build/utils/sqrt.d.ts +0 -7
- package/build/utils/sqrtPriceMath.d.ts +0 -13
- package/build/utils/swapMath.d.ts +0 -9
- package/build/utils/tickLibrary.d.ts +0 -15
- package/build/utils/tickList.d.ts +0 -23
- 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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
6
|
exports.BaseCurrency = void 0;
|
|
7
|
-
|
|
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
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns whether this currency is functionally equivalent to the other currency
|
|
50
|
+
* @param other the other currency
|
|
51
|
+
*/);
|
|
@@ -1,110 +1,172 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
6
|
exports.CurrencyAmount = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
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
|
-
|
|
23
|
-
|
|
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
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
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
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
6
|
exports.Fraction = void 0;
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
[_internalConstants.Rounding.ROUND_DOWN]: 0,
|
|
34
|
+
[_internalConstants.Rounding.ROUND_HALF_UP]: 1,
|
|
35
|
+
[_internalConstants.Rounding.ROUND_UP]: 3
|
|
29
36
|
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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
|
-
|
|
46
|
-
|
|
50
|
+
function () {
|
|
51
|
+
return this.numerator / this.denominator;
|
|
47
52
|
}
|
|
53
|
+
|
|
48
54
|
// remainder after floor division
|
|
49
|
-
|
|
50
|
-
|
|
55
|
+
}, {
|
|
56
|
+
key: "remainder",
|
|
57
|
+
get: function () {
|
|
58
|
+
return new Fraction(this.numerator % this.denominator, this.denominator);
|
|
51
59
|
}
|
|
52
|
-
|
|
53
|
-
|
|
60
|
+
}, {
|
|
61
|
+
key: "invert",
|
|
62
|
+
value: function invert() {
|
|
63
|
+
return new Fraction(this.denominator, this.numerator);
|
|
54
64
|
}
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
return new Fraction(
|
|
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
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return new Fraction(
|
|
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
|
-
|
|
70
|
-
|
|
71
|
-
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
-
|
|
86
|
-
|
|
87
|
-
|
|
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
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
-
|
|
114
|
-
|
|
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
|
-
|
|
158
|
+
}]);
|
|
159
|
+
}();
|