@alcorexchange/alcor-swap-sdk 1.0.0

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 (110) hide show
  1. package/.eslintignore +2 -0
  2. package/.eslintrc +12 -0
  3. package/LICENSE +21 -0
  4. package/build/entities/baseCurrency.js +27 -0
  5. package/build/entities/currency.js +2 -0
  6. package/build/entities/fractions/currencyAmount.js +80 -0
  7. package/build/entities/fractions/fraction.js +117 -0
  8. package/build/entities/fractions/index.js +11 -0
  9. package/build/entities/fractions/percent.js +46 -0
  10. package/build/entities/fractions/price.js +73 -0
  11. package/build/entities/index.js +26 -0
  12. package/build/entities/pool.js +248 -0
  13. package/build/entities/position.js +364 -0
  14. package/build/entities/route.js +70 -0
  15. package/build/entities/tick.js +23 -0
  16. package/build/entities/tickDataProvider.js +30 -0
  17. package/build/entities/tickListDataProvider.js +35 -0
  18. package/build/entities/token.js +53 -0
  19. package/build/entities/trade.js +464 -0
  20. package/build/index.js +19 -0
  21. package/build/internalConstants.js +54 -0
  22. package/build/utils/computeAllRoutes.js +39 -0
  23. package/build/utils/encodeSqrtRatioX64.js +21 -0
  24. package/build/utils/fullMath.js +22 -0
  25. package/build/utils/index.js +30 -0
  26. package/build/utils/isSorted.js +18 -0
  27. package/build/utils/liquidityMath.js +23 -0
  28. package/build/utils/maxLiquidityForAmounts.js +86 -0
  29. package/build/utils/mostSignificantBit.js +27 -0
  30. package/build/utils/nearestUsableTick.js +26 -0
  31. package/build/utils/positionLibrary.js +22 -0
  32. package/build/utils/priceTickConversions.js +51 -0
  33. package/build/utils/sortedInsert.js +39 -0
  34. package/build/utils/sqrt.js +33 -0
  35. package/build/utils/sqrtPriceMath.js +92 -0
  36. package/build/utils/swapMath.js +86 -0
  37. package/build/utils/tickLibrary.js +61 -0
  38. package/build/utils/tickList.js +110 -0
  39. package/build/utils/tickMath.js +122 -0
  40. package/jest.config.js +40 -0
  41. package/nodemon.json +6 -0
  42. package/package.json +51 -0
  43. package/src/entities/baseCurrency.ts +53 -0
  44. package/src/entities/currency.ts +3 -0
  45. package/src/entities/fractions/currencyAmount.ts +129 -0
  46. package/src/entities/fractions/fraction.ts +190 -0
  47. package/src/entities/fractions/index.ts +4 -0
  48. package/src/entities/fractions/percent.ts +54 -0
  49. package/src/entities/fractions/price.ts +127 -0
  50. package/src/entities/index.ts +11 -0
  51. package/src/entities/pool.ts +399 -0
  52. package/src/entities/position.ts +591 -0
  53. package/src/entities/route.ts +84 -0
  54. package/src/entities/tick.ts +48 -0
  55. package/src/entities/tickDataProvider.ts +43 -0
  56. package/src/entities/tickListDataProvider.ts +37 -0
  57. package/src/entities/token.ts +56 -0
  58. package/src/entities/trade.ts +650 -0
  59. package/src/index.ts +3 -0
  60. package/src/internalConstants.ts +58 -0
  61. package/src/utils/computeAllRoutes.ts +64 -0
  62. package/src/utils/encodeSqrtRatioX64.ts +20 -0
  63. package/src/utils/fullMath.ts +17 -0
  64. package/src/utils/index.ts +14 -0
  65. package/src/utils/isSorted.ts +17 -0
  66. package/src/utils/liquidityMath.ts +17 -0
  67. package/src/utils/maxLiquidityForAmounts.ts +127 -0
  68. package/src/utils/mostSignificantBit.ts +25 -0
  69. package/src/utils/nearestUsableTick.ts +23 -0
  70. package/src/utils/positionLibrary.ts +37 -0
  71. package/src/utils/priceTickConversions.ts +57 -0
  72. package/src/utils/sortedInsert.ts +35 -0
  73. package/src/utils/sqrt.ts +31 -0
  74. package/src/utils/sqrtPriceMath.ts +169 -0
  75. package/src/utils/swapMath.ts +175 -0
  76. package/src/utils/tickLibrary.ts +88 -0
  77. package/src/utils/tickList.ts +147 -0
  78. package/src/utils/tickMath.ts +166 -0
  79. package/test/bestTradeExactOut.test.ts +266 -0
  80. package/test/currencyAmount.test.js +92 -0
  81. package/test/currencyAmount.test.ts +114 -0
  82. package/test/encodeSqrtRatioX64.test.ts +33 -0
  83. package/test/fixtures/pools.json +276 -0
  84. package/test/fixtures/ticks.json +608 -0
  85. package/test/fraction.test.js +87 -0
  86. package/test/fraction.test.ts +176 -0
  87. package/test/isSorted.test.ts +52 -0
  88. package/test/maxLiquidityForAmounts.test copy.ts +256 -0
  89. package/test/mostSignificantBit.test.ts +32 -0
  90. package/test/nearestUsableTick.test.ts +54 -0
  91. package/test/percent.test.js +52 -0
  92. package/test/percent.test.ts +68 -0
  93. package/test/pool.test.ts +377 -0
  94. package/test/position.test.ts +579 -0
  95. package/test/positionLibrary.test.ts +31 -0
  96. package/test/price.test.js +57 -0
  97. package/test/price.test.ts +62 -0
  98. package/test/priceTickConversions.test.ts +137 -0
  99. package/test/sqrtPriceMath.test.ts +113 -0
  100. package/test/tick.test.js +22 -0
  101. package/test/tick.test.ts +28 -0
  102. package/test/tickDataProvider.test.ts +17 -0
  103. package/test/tickLibrary.test.ts +111 -0
  104. package/test/tickList.test.ts +215 -0
  105. package/test/tickListDataProvider.test.ts +64 -0
  106. package/test/tickMath.test.ts +66 -0
  107. package/test/token.test.ts +58 -0
  108. package/test/trade.test.ts +210 -0
  109. package/test2.ts +73 -0
  110. package/tsconfig.json +24 -0
package/.eslintignore ADDED
@@ -0,0 +1,2 @@
1
+ node_modules
2
+ dist
package/.eslintrc ADDED
@@ -0,0 +1,12 @@
1
+ {
2
+ "root": true,
3
+ "parser": "@typescript-eslint/parser",
4
+ "plugins": [
5
+ "@typescript-eslint"
6
+ ],
7
+ "extends": [
8
+ "eslint:recommended",
9
+ "plugin:@typescript-eslint/eslint-recommended",
10
+ "plugin:@typescript-eslint/recommended"
11
+ ]
12
+ }
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 ChimGoKien
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,27 @@
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.BaseCurrency = void 0;
7
+ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
8
+ /**
9
+ * A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
10
+ */
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, id) {
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 = id;
25
+ }
26
+ }
27
+ exports.BaseCurrency = BaseCurrency;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,80 @@
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.CurrencyAmount = void 0;
7
+ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
8
+ const jsbi_1 = __importDefault(require("jsbi"));
9
+ const fraction_1 = require("./fraction");
10
+ const big_js_1 = __importDefault(require("big.js"));
11
+ const toformat_1 = __importDefault(require("toformat"));
12
+ const internalConstants_1 = require("../../internalConstants");
13
+ const Big = (0, toformat_1.default)(big_js_1.default);
14
+ class CurrencyAmount extends fraction_1.Fraction {
15
+ /**
16
+ * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
17
+ * @param currency the currency in the amount
18
+ * @param rawAmount the raw token or ether amount
19
+ */
20
+ static fromRawAmount(currency, rawAmount) {
21
+ return new CurrencyAmount(currency, rawAmount);
22
+ }
23
+ /**
24
+ * Construct a currency amount with a denominator that is not equal to 1
25
+ * @param currency the currency
26
+ * @param numerator the numerator of the fractional token amount
27
+ * @param denominator the denominator of the fractional token amount
28
+ */
29
+ static fromFractionalAmount(currency, numerator, denominator) {
30
+ return new CurrencyAmount(currency, numerator, denominator);
31
+ }
32
+ constructor(currency, numerator, denominator) {
33
+ super(numerator, denominator);
34
+ (0, tiny_invariant_1.default)(jsbi_1.default.lessThanOrEqual(this.quotient, internalConstants_1.MaxUint256), "AMOUNT");
35
+ this.currency = currency;
36
+ this.decimalScale = jsbi_1.default.exponentiate(jsbi_1.default.BigInt(10), jsbi_1.default.BigInt(currency.decimals));
37
+ }
38
+ add(other) {
39
+ (0, tiny_invariant_1.default)(this.currency.equals(other.currency), "CURRENCY");
40
+ const added = super.add(other);
41
+ return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
42
+ }
43
+ subtract(other) {
44
+ (0, tiny_invariant_1.default)(this.currency.equals(other.currency), "CURRENCY");
45
+ const subtracted = super.subtract(other);
46
+ return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
47
+ }
48
+ multiply(other) {
49
+ const multiplied = super.multiply(other);
50
+ return CurrencyAmount.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
51
+ }
52
+ divide(other) {
53
+ const divided = super.divide(other);
54
+ return CurrencyAmount.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
55
+ }
56
+ toSignificant(significantDigits = 6, format, rounding = internalConstants_1.Rounding.ROUND_DOWN) {
57
+ return super
58
+ .divide(this.decimalScale)
59
+ .toSignificant(significantDigits, format, rounding);
60
+ }
61
+ toFixed(decimalPlaces = this.currency.decimals, format, rounding = internalConstants_1.Rounding.ROUND_DOWN) {
62
+ (0, tiny_invariant_1.default)(decimalPlaces <= this.currency.decimals, "DECIMALS");
63
+ return super
64
+ .divide(this.decimalScale)
65
+ .toFixed(decimalPlaces, format, rounding);
66
+ }
67
+ toExact(format = { groupSeparator: "" }) {
68
+ Big.DP = this.currency.decimals;
69
+ return new Big(this.quotient.toString())
70
+ .div(this.decimalScale.toString())
71
+ .toFormat(format);
72
+ }
73
+ toAsset(...args) {
74
+ return `${this.toFixed(...args)} ${this.currency.symbol}`;
75
+ }
76
+ toExtendedAsset(...args) {
77
+ return `${this.toFixed(...args)} ${this.currency.symbol}@${this.currency.contract}`;
78
+ }
79
+ }
80
+ exports.CurrencyAmount = CurrencyAmount;
@@ -0,0 +1,117 @@
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.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);
15
+ 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,
19
+ };
20
+ // const toFixedRounding = {
21
+ // [Rounding.ROUND_DOWN]: RoundingMode.RoundDown,
22
+ // [Rounding.ROUND_HALF_UP]: RoundingMode.RoundHalfUp,
23
+ // [Rounding.ROUND_UP]: RoundingMode.RoundUp
24
+ // }
25
+ const toFixedRounding = {
26
+ [internalConstants_1.Rounding.ROUND_DOWN]: 0,
27
+ [internalConstants_1.Rounding.ROUND_HALF_UP]: 1,
28
+ [internalConstants_1.Rounding.ROUND_UP]: 3,
29
+ };
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
+ }
44
+ // performs floor division
45
+ get quotient() {
46
+ return jsbi_1.default.divide(this.numerator, this.denominator);
47
+ }
48
+ // remainder after floor division
49
+ get remainder() {
50
+ return new Fraction(jsbi_1.default.remainder(this.numerator, this.denominator), this.denominator);
51
+ }
52
+ invert() {
53
+ return new Fraction(this.denominator, this.numerator);
54
+ }
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));
61
+ }
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));
68
+ }
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));
72
+ }
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));
76
+ }
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));
80
+ }
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));
84
+ }
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));
88
+ }
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);
100
+ }
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);
109
+ }
110
+ /**
111
+ * Helper method for converting any super class back to a fraction
112
+ */
113
+ get asFraction() {
114
+ return new Fraction(this.numerator, this.denominator);
115
+ }
116
+ }
117
+ exports.Fraction = Fraction;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Price = exports.Percent = exports.Fraction = exports.CurrencyAmount = void 0;
4
+ var currencyAmount_1 = require("./currencyAmount");
5
+ Object.defineProperty(exports, "CurrencyAmount", { enumerable: true, get: function () { return currencyAmount_1.CurrencyAmount; } });
6
+ var fraction_1 = require("./fraction");
7
+ Object.defineProperty(exports, "Fraction", { enumerable: true, get: function () { return fraction_1.Fraction; } });
8
+ var percent_1 = require("./percent");
9
+ Object.defineProperty(exports, "Percent", { enumerable: true, get: function () { return percent_1.Percent; } });
10
+ var price_1 = require("./price");
11
+ Object.defineProperty(exports, "Price", { enumerable: true, get: function () { return price_1.Price; } });
@@ -0,0 +1,46 @@
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.Percent = void 0;
7
+ const jsbi_1 = __importDefault(require("jsbi"));
8
+ const fraction_1 = require("./fraction");
9
+ const ONE_HUNDRED = new fraction_1.Fraction(jsbi_1.default.BigInt(100));
10
+ /**
11
+ * Converts a fraction to a percent
12
+ * @param fraction the fraction to convert
13
+ */
14
+ function toPercent(fraction) {
15
+ return new Percent(fraction.numerator, fraction.denominator);
16
+ }
17
+ class Percent extends fraction_1.Fraction {
18
+ constructor() {
19
+ super(...arguments);
20
+ /**
21
+ * This boolean prevents a fraction from being interpreted as a Percent
22
+ */
23
+ this.isPercent = true;
24
+ }
25
+ add(other) {
26
+ return toPercent(super.add(other));
27
+ }
28
+ subtract(other) {
29
+ return toPercent(super.subtract(other));
30
+ }
31
+ multiply(other) {
32
+ return toPercent(super.multiply(other));
33
+ }
34
+ divide(other) {
35
+ return toPercent(super.divide(other));
36
+ }
37
+ toSignificant(significantDigits = 5, format, rounding) {
38
+ return super
39
+ .multiply(ONE_HUNDRED)
40
+ .toSignificant(significantDigits, format, rounding);
41
+ }
42
+ toFixed(decimalPlaces = 2, format, rounding) {
43
+ return super.multiply(ONE_HUNDRED).toFixed(decimalPlaces, format, rounding);
44
+ }
45
+ }
46
+ exports.Percent = Percent;
@@ -0,0 +1,73 @@
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.Price = void 0;
7
+ const jsbi_1 = __importDefault(require("jsbi"));
8
+ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
9
+ const fraction_1 = require("./fraction");
10
+ const currencyAmount_1 = require("./currencyAmount");
11
+ class Price extends fraction_1.Fraction {
12
+ /**
13
+ * Construct a price, either with the base and quote currency amount, or the
14
+ * @param args
15
+ */
16
+ constructor(...args) {
17
+ let baseCurrency, quoteCurrency, denominator, numerator;
18
+ if (args.length === 4) {
19
+ [baseCurrency, quoteCurrency, denominator, numerator] = args;
20
+ }
21
+ else {
22
+ const result = args[0].quoteAmount.divide(args[0].baseAmount);
23
+ [baseCurrency, quoteCurrency, denominator, numerator] = [
24
+ args[0].baseAmount.currency,
25
+ args[0].quoteAmount.currency,
26
+ result.denominator,
27
+ result.numerator,
28
+ ];
29
+ }
30
+ super(numerator, denominator);
31
+ this.baseCurrency = baseCurrency;
32
+ this.quoteCurrency = quoteCurrency;
33
+ this.scalar = new fraction_1.Fraction(jsbi_1.default.exponentiate(jsbi_1.default.BigInt(10), jsbi_1.default.BigInt(baseCurrency.decimals)), jsbi_1.default.exponentiate(jsbi_1.default.BigInt(10), jsbi_1.default.BigInt(quoteCurrency.decimals)));
34
+ }
35
+ /**
36
+ * Flip the price, switching the base and quote currency
37
+ */
38
+ invert() {
39
+ return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
40
+ }
41
+ /**
42
+ * Multiply the price by another price, returning a new price. The other price must have the same base currency as this price's quote currency
43
+ * @param other the other price
44
+ */
45
+ multiply(other) {
46
+ (0, tiny_invariant_1.default)(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
47
+ const fraction = super.multiply(other);
48
+ return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
49
+ }
50
+ /**
51
+ * Return the amount of quote currency corresponding to a given amount of the base currency
52
+ * @param currencyAmount the amount of base currency to quote against the price
53
+ */
54
+ quote(currencyAmount) {
55
+ (0, tiny_invariant_1.default)(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
56
+ const result = super.multiply(currencyAmount);
57
+ return currencyAmount_1.CurrencyAmount.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
58
+ }
59
+ /**
60
+ * Get the value scaled by decimals for formatting
61
+ * @private
62
+ */
63
+ get adjustedForDecimals() {
64
+ return super.multiply(this.scalar);
65
+ }
66
+ toSignificant(significantDigits = 6, format, rounding) {
67
+ return this.adjustedForDecimals.toSignificant(significantDigits, format, rounding);
68
+ }
69
+ toFixed(decimalPlaces = 4, format, rounding) {
70
+ return this.adjustedForDecimals.toFixed(decimalPlaces, format, rounding);
71
+ }
72
+ }
73
+ exports.Price = Price;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./fractions"), exports);
18
+ __exportStar(require("./tick"), exports);
19
+ __exportStar(require("./tickDataProvider"), exports);
20
+ __exportStar(require("./tickListDataProvider"), exports);
21
+ __exportStar(require("./currency"), exports);
22
+ __exportStar(require("./token"), exports);
23
+ __exportStar(require("./pool"), exports);
24
+ __exportStar(require("./position"), exports);
25
+ __exportStar(require("./route"), exports);
26
+ __exportStar(require("./trade"), exports);