@alcorexchange/alcor-swap-sdk 1.0.404 → 1.1.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 (88) hide show
  1. package/build/entities/fractions/currencyAmount.d.ts +2 -4
  2. package/build/entities/fractions/currencyAmount.js +12 -19
  3. package/build/entities/fractions/fraction.d.ts +3 -4
  4. package/build/entities/fractions/fraction.js +7 -12
  5. package/build/entities/fractions/percent.js +4 -13
  6. package/build/entities/fractions/price.js +4 -13
  7. package/build/entities/pool.d.ts +14 -16
  8. package/build/entities/pool.js +51 -28
  9. package/build/entities/position.d.ts +9 -10
  10. package/build/entities/position.js +57 -61
  11. package/build/entities/route.d.ts +2 -3
  12. package/build/entities/route.js +10 -11
  13. package/build/entities/tick.d.ts +7 -8
  14. package/build/entities/tick.js +12 -13
  15. package/build/entities/trade.js +44 -69
  16. package/build/esm/entities/baseCurrency.js +44 -0
  17. package/build/esm/entities/currency.js +1 -0
  18. package/build/esm/entities/fractions/currencyAmount.js +158 -0
  19. package/build/esm/entities/fractions/fraction.js +147 -0
  20. package/build/esm/entities/fractions/index.js +4 -0
  21. package/build/esm/entities/fractions/percent.js +70 -0
  22. package/build/esm/entities/fractions/price.js +103 -0
  23. package/build/esm/entities/index.js +10 -0
  24. package/build/esm/entities/pool.js +412 -0
  25. package/build/esm/entities/position.js +513 -0
  26. package/build/esm/entities/route.js +151 -0
  27. package/build/esm/entities/tick.js +68 -0
  28. package/build/esm/entities/tickDataProvider.js +31 -0
  29. package/build/esm/entities/tickListDataProvider.js +58 -0
  30. package/build/esm/entities/token.js +80 -0
  31. package/build/esm/entities/trade.js +632 -0
  32. package/build/esm/errors.js +54 -0
  33. package/build/esm/index.js +3 -0
  34. package/build/esm/internalConstants.js +54 -0
  35. package/build/esm/utils/common.js +52 -0
  36. package/build/esm/utils/computeAllRoutes.js +118 -0
  37. package/build/esm/utils/computeAllRoutesFast.js +139 -0
  38. package/build/esm/utils/computeAllRoutesWASM.js +160 -0
  39. package/build/esm/utils/encodeSqrtRatioX64.js +14 -0
  40. package/build/esm/utils/fullMath.js +32 -0
  41. package/build/esm/utils/getBestSwapRoute.js +173 -0
  42. package/build/esm/utils/index.js +18 -0
  43. package/build/esm/utils/isSorted.js +14 -0
  44. package/build/esm/utils/liquidityMath.js +24 -0
  45. package/build/esm/utils/maxLiquidityForAmounts.js +78 -0
  46. package/build/esm/utils/mostSignificantBit.js +16 -0
  47. package/build/esm/utils/nearestUsableTick.js +15 -0
  48. package/build/esm/utils/positionLibrary.js +25 -0
  49. package/build/esm/utils/priceTickConversions.js +39 -0
  50. package/build/esm/utils/sortedInsert.js +33 -0
  51. package/build/esm/utils/sqrt.js +27 -0
  52. package/build/esm/utils/sqrtPriceMath.js +99 -0
  53. package/build/esm/utils/swapMath.js +78 -0
  54. package/build/esm/utils/tickLibrary.js +54 -0
  55. package/build/esm/utils/tickList.js +189 -0
  56. package/build/esm/utils/tickMath.js +124 -0
  57. package/build/esm/utils/tradeCalculatorWASM.js +254 -0
  58. package/build/esm/utils/wasm_route_finder.js +639 -0
  59. package/build/esm/utils/wasm_route_finder_bg.wasm +0 -0
  60. package/build/internalConstants.d.ts +13 -14
  61. package/build/internalConstants.js +8 -8
  62. package/build/utils/common.js +6 -7
  63. package/build/utils/computeAllRoutesFast.js +2 -4
  64. package/build/utils/encodeSqrtRatioX64.d.ts +1 -2
  65. package/build/utils/encodeSqrtRatioX64.js +3 -3
  66. package/build/utils/fullMath.d.ts +1 -2
  67. package/build/utils/fullMath.js +2 -2
  68. package/build/utils/getBestSwapRoute.js +4 -5
  69. package/build/utils/liquidityMath.d.ts +1 -2
  70. package/build/utils/maxLiquidityForAmounts.d.ts +1 -2
  71. package/build/utils/mostSignificantBit.d.ts +1 -2
  72. package/build/utils/mostSignificantBit.js +1 -1
  73. package/build/utils/positionLibrary.d.ts +1 -2
  74. package/build/utils/positionLibrary.js +3 -3
  75. package/build/utils/sqrt.d.ts +2 -3
  76. package/build/utils/sqrt.js +3 -3
  77. package/build/utils/sqrtPriceMath.d.ts +4 -5
  78. package/build/utils/sqrtPriceMath.js +3 -3
  79. package/build/utils/swapMath.d.ts +1 -2
  80. package/build/utils/swapMath.js +1 -1
  81. package/build/utils/tickLibrary.d.ts +5 -6
  82. package/build/utils/tickList.js +10 -18
  83. package/build/utils/tickMath.d.ts +4 -5
  84. package/build/utils/tickMath.js +11 -11
  85. package/build/utils/tradeCalculatorWASM.js +5 -6
  86. package/build/utils/wasm_route_finder.js +77 -4
  87. package/build/utils/wasm_route_finder_bg.wasm +0 -0
  88. package/package.json +19 -7
@@ -1,12 +1,10 @@
1
- /// <reference types="node" />
2
- import JSBI from "jsbi";
3
1
  import { Currency } from "../currency";
4
2
  import { Token } from "../token";
5
3
  import { Fraction } from "./fraction";
6
4
  import { BigintIsh, Rounding } from "../../internalConstants";
7
5
  export declare class CurrencyAmount<T extends Currency> extends Fraction {
8
6
  readonly currency: T;
9
- readonly decimalScale: JSBI;
7
+ readonly decimalScale: bigint;
10
8
  /**
11
9
  * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
12
10
  * @param currency the currency in the amount
@@ -34,5 +32,5 @@ export declare class CurrencyAmount<T extends Currency> extends Fraction {
34
32
  static toJSON<T extends Currency>(amount: CurrencyAmount<T>): object;
35
33
  static fromJSON(json: any): CurrencyAmount<Token>;
36
34
  static toBuffer<T extends Currency>(amount: CurrencyAmount<T>): object;
37
- static fromBuffer(buffer: Buffer): CurrencyAmount<Token>;
35
+ static fromBuffer(buffer: Uint8Array): CurrencyAmount<Token>;
38
36
  }
@@ -38,7 +38,7 @@ let CurrencyAmount = exports.CurrencyAmount = /*#__PURE__*/function (_Fraction)
38
38
  _defineProperty(_this, "decimalScale", void 0);
39
39
  (0, _tinyInvariant.default)(_this.quotient <= _internalConstants.MaxUint256, "AMOUNT");
40
40
  _this.currency = currency;
41
- _this.decimalScale = 10n ** BigInt(currency.decimals);
41
+ _this.decimalScale = BigInt(10) ** BigInt(currency.decimals);
42
42
  return _this;
43
43
  }
44
44
  _inherits(CurrencyAmount, _Fraction);
@@ -70,45 +70,38 @@ let CurrencyAmount = exports.CurrencyAmount = /*#__PURE__*/function (_Fraction)
70
70
  }
71
71
  }, {
72
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;
73
+ value: function toSignificant(significantDigits = 6, format, rounding = _internalConstants.Rounding.ROUND_DOWN) {
77
74
  return _superPropGet(CurrencyAmount, "divide", this, 3)([this.decimalScale]).toSignificant(significantDigits, format, rounding);
78
75
  }
79
76
  }, {
80
77
  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;
78
+ value: function toFixed(decimalPlaces = this.currency.decimals, format, rounding = _internalConstants.Rounding.ROUND_DOWN) {
85
79
  (0, _tinyInvariant.default)(decimalPlaces <= this.currency.decimals, "DECIMALS");
86
80
  return _superPropGet(CurrencyAmount, "divide", this, 3)([this.decimalScale]).toFixed(decimalPlaces, format, rounding);
87
81
  }
88
82
  }, {
89
83
  key: "toExact",
90
- value: function toExact() {
91
- let format = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {
92
- groupSeparator: ""
93
- };
84
+ value: function toExact(format = {
85
+ groupSeparator: ""
86
+ }) {
94
87
  Big.DP = this.currency.decimals;
95
88
  return new Big(this.quotient.toString()).div(this.decimalScale.toString()).toFormat(format);
96
89
  }
97
90
  }, {
98
91
  key: "toAsset",
99
- value: function toAsset() {
100
- return `${this.toFixed(...arguments)} ${this.currency.symbol}`;
92
+ value: function toAsset(...args) {
93
+ return `${this.toFixed(...args)} ${this.currency.symbol}`;
101
94
  }
102
95
  }, {
103
96
  key: "toExtendedAsset",
104
- value: function toExtendedAsset() {
105
- return `${this.toFixed(...arguments)} ${this.currency.symbol}@${this.currency.contract}`;
97
+ value: function toExtendedAsset(...args) {
98
+ return `${this.toFixed(...args)} ${this.currency.symbol}@${this.currency.contract}`;
106
99
  }
107
100
  }, {
108
101
  key: "toExtendedAssetObject",
109
- value: function toExtendedAssetObject() {
102
+ value: function toExtendedAssetObject(...args) {
110
103
  return {
111
- quantity: `${this.toFixed(...arguments)} ${this.currency.symbol}`,
104
+ quantity: `${this.toFixed(...args)} ${this.currency.symbol}`,
112
105
  contract: this.currency.contract
113
106
  };
114
107
  }
@@ -1,11 +1,10 @@
1
- import JSBI from "jsbi";
2
1
  import { BigintIsh, Rounding } from "../../internalConstants";
3
2
  export declare class Fraction {
4
- readonly numerator: JSBI;
5
- readonly denominator: JSBI;
3
+ readonly numerator: bigint;
4
+ readonly denominator: bigint;
6
5
  constructor(numerator: BigintIsh, denominator?: BigintIsh);
7
6
  private static tryParseFraction;
8
- get quotient(): JSBI;
7
+ get quotient(): bigint;
9
8
  get remainder(): Fraction;
10
9
  invert(): Fraction;
11
10
  add(other: Fraction | BigintIsh): Fraction;
@@ -35,8 +35,7 @@ const toFixedRounding = {
35
35
  [_internalConstants.Rounding.ROUND_UP]: 3
36
36
  };
37
37
  let Fraction = exports.Fraction = /*#__PURE__*/function () {
38
- function Fraction(numerator) {
39
- let denominator = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1n;
38
+ function Fraction(numerator, denominator = BigInt(1)) {
40
39
  _classCallCheck(this, Fraction);
41
40
  _defineProperty(this, "numerator", void 0);
42
41
  _defineProperty(this, "denominator", void 0);
@@ -112,11 +111,9 @@ let Fraction = exports.Fraction = /*#__PURE__*/function () {
112
111
  }
113
112
  }, {
114
113
  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;
114
+ value: function toSignificant(significantDigits, format = {
115
+ groupSeparator: ""
116
+ }, rounding = _internalConstants.Rounding.ROUND_HALF_UP) {
120
117
  (0, _tinyInvariant.default)(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
121
118
  (0, _tinyInvariant.default)(significantDigits > 0, `${significantDigits} is not positive.`);
122
119
  Decimal.set({
@@ -128,11 +125,9 @@ let Fraction = exports.Fraction = /*#__PURE__*/function () {
128
125
  }
129
126
  }, {
130
127
  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;
128
+ value: function toFixed(decimalPlaces, format = {
129
+ groupSeparator: ""
130
+ }, rounding = _internalConstants.Rounding.ROUND_HALF_UP) {
136
131
  (0, _tinyInvariant.default)(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
137
132
  (0, _tinyInvariant.default)(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
138
133
  Big.DP = decimalPlaces;
@@ -21,7 +21,7 @@ function _setPrototypeOf(t, e) { return _setPrototypeOf = Object.setPrototypeOf
21
21
  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; }
22
22
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
23
23
  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); }
24
- const ONE_HUNDRED = new _fraction.Fraction(100n);
24
+ const ONE_HUNDRED = new _fraction.Fraction(BigInt(100));
25
25
 
26
26
  /**
27
27
  * Converts a fraction to a percent
@@ -31,12 +31,9 @@ function toPercent(fraction) {
31
31
  return new Percent(fraction.numerator, fraction.denominator);
32
32
  }
33
33
  let Percent = exports.Percent = /*#__PURE__*/function (_Fraction) {
34
- function Percent() {
34
+ function Percent(...args) {
35
35
  var _this;
36
36
  _classCallCheck(this, Percent);
37
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
38
- args[_key] = arguments[_key];
39
- }
40
37
  _this = _callSuper(this, Percent, [...args]);
41
38
  /**
42
39
  * This boolean prevents a fraction from being interpreted as a Percent
@@ -67,18 +64,12 @@ let Percent = exports.Percent = /*#__PURE__*/function (_Fraction) {
67
64
  }
68
65
  }, {
69
66
  key: "toSignificant",
70
- value: function toSignificant() {
71
- let significantDigits = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 5;
72
- let format = arguments.length > 1 ? arguments[1] : undefined;
73
- let rounding = arguments.length > 2 ? arguments[2] : undefined;
67
+ value: function toSignificant(significantDigits = 5, format, rounding) {
74
68
  return _superPropGet(Percent, "multiply", this, 3)([ONE_HUNDRED]).toSignificant(significantDigits, format, rounding);
75
69
  }
76
70
  }, {
77
71
  key: "toFixed",
78
- value: function toFixed() {
79
- let decimalPlaces = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 2;
80
- let format = arguments.length > 1 ? arguments[1] : undefined;
81
- let rounding = arguments.length > 2 ? arguments[2] : undefined;
72
+ value: function toFixed(decimalPlaces = 2, format, rounding) {
82
73
  return _superPropGet(Percent, "multiply", this, 3)([ONE_HUNDRED]).toFixed(decimalPlaces, format, rounding);
83
74
  }
84
75
  }]);
@@ -31,13 +31,10 @@ let Price = exports.Price = /*#__PURE__*/function (_Fraction) {
31
31
  * Construct a price, either with the base and quote currency amount, or the
32
32
  * @param args
33
33
  */
34
- function Price() {
34
+ function Price(...args) {
35
35
  var _this;
36
36
  _classCallCheck(this, Price);
37
37
  let baseCurrency, quoteCurrency, denominator, numerator;
38
- for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
39
- args[_key] = arguments[_key];
40
- }
41
38
  if (args.length === 4) {
42
39
  [baseCurrency, quoteCurrency, denominator, numerator] = args;
43
40
  } else {
@@ -52,7 +49,7 @@ let Price = exports.Price = /*#__PURE__*/function (_Fraction) {
52
49
  _defineProperty(_this, "scalar", void 0);
53
50
  _this.baseCurrency = baseCurrency;
54
51
  _this.quoteCurrency = quoteCurrency;
55
- _this.scalar = new _fraction.Fraction(10n ** BigInt(baseCurrency.decimals), 10n ** BigInt(quoteCurrency.decimals));
52
+ _this.scalar = new _fraction.Fraction(BigInt(10) ** BigInt(baseCurrency.decimals), BigInt(10) ** BigInt(quoteCurrency.decimals));
56
53
  return _this;
57
54
  }
58
55
 
@@ -101,18 +98,12 @@ let Price = exports.Price = /*#__PURE__*/function (_Fraction) {
101
98
  }
102
99
  }, {
103
100
  key: "toSignificant",
104
- value: function toSignificant() {
105
- let significantDigits = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 6;
106
- let format = arguments.length > 1 ? arguments[1] : undefined;
107
- let rounding = arguments.length > 2 ? arguments[2] : undefined;
101
+ value: function toSignificant(significantDigits = 6, format, rounding) {
108
102
  return this.adjustedForDecimals.toSignificant(significantDigits, format, rounding);
109
103
  }
110
104
  }, {
111
105
  key: "toFixed",
112
- value: function toFixed() {
113
- let decimalPlaces = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 4;
114
- let format = arguments.length > 1 ? arguments[1] : undefined;
115
- let rounding = arguments.length > 2 ? arguments[2] : undefined;
106
+ value: function toFixed(decimalPlaces = 4, format, rounding) {
116
107
  return this.adjustedForDecimals.toFixed(decimalPlaces, format, rounding);
117
108
  }
118
109
  }]);
@@ -1,8 +1,6 @@
1
- /// <reference types="node" />
2
1
  import { CurrencyAmount, Price } from "./fractions";
3
2
  import { Token } from "./token";
4
3
  import { BigintIsh, FeeAmount } from "../internalConstants";
5
- import JSBI from "jsbi";
6
4
  import { Tick, TickConstructorArgs } from "./tick";
7
5
  import { TickDataProvider } from "./tickDataProvider";
8
6
  export interface PoolConstructorArgs {
@@ -27,14 +25,14 @@ export declare class Pool {
27
25
  readonly tokenA: Token;
28
26
  readonly tokenB: Token;
29
27
  readonly fee: FeeAmount;
30
- readonly sqrtPriceX64: JSBI;
31
- readonly liquidity: JSBI;
28
+ readonly sqrtPriceX64: bigint;
29
+ readonly liquidity: bigint;
32
30
  readonly tickCurrent: number;
33
- readonly feeGrowthGlobalAX64: JSBI;
34
- readonly feeGrowthGlobalBX64: JSBI;
31
+ readonly feeGrowthGlobalAX64: bigint;
32
+ readonly feeGrowthGlobalBX64: bigint;
35
33
  readonly tickDataProvider: TickDataProvider;
36
34
  json?: any;
37
- buffer?: Buffer;
35
+ buffer?: Uint8Array;
38
36
  bufferHash?: string;
39
37
  static hashToPoolMap: Map<string, Pool>;
40
38
  static idToPoolMap: Map<number, Pool>;
@@ -77,14 +75,14 @@ export declare class Pool {
77
75
  * @param sqrtPriceLimitX64 The Q64.96 sqrt price limit
78
76
  * @returns The output amount and the pool with updated state
79
77
  */
80
- getOutputAmount(inputAmount: CurrencyAmount<Token>, sqrtPriceLimitX64?: JSBI): CurrencyAmount<Token>;
78
+ getOutputAmount(inputAmount: CurrencyAmount<Token>, sqrtPriceLimitX64?: bigint): CurrencyAmount<Token>;
81
79
  /**
82
80
  * Given a desired output amount of a token, return the computed input amount and a pool with state updated after the trade
83
81
  * @param outputAmount the output amount for which to quote the input amount
84
82
  * @param sqrtPriceLimitX64 The Q64.96 sqrt price limit. If zero for one, the price cannot be less than this value after the swap. If one for zero, the price cannot be greater than this value after the swap
85
83
  * @returns The input amount and the pool with updated state
86
84
  */
87
- getInputAmount(outputAmount: CurrencyAmount<Token>, sqrtPriceLimitX64?: JSBI): CurrencyAmount<Token>;
85
+ getInputAmount(outputAmount: CurrencyAmount<Token>, sqrtPriceLimitX64?: bigint): CurrencyAmount<Token>;
88
86
  /**
89
87
  * Executes a swap
90
88
  * @param zeroForOne Whether the amount in is tokenA or tokenB
@@ -100,19 +98,19 @@ export declare class Pool {
100
98
  static toJSON(pool: Pool): object;
101
99
  static fromJSON(json: any): Pool;
102
100
  /**
103
- * Converts the pool to a Buffer using msgpack encoding.
101
+ * Converts the pool to a byte array using msgpack encoding.
104
102
  * @param {Pool} pool - The pool instance to convert.
105
- * @returns {Buffer} The encoded buffer.
103
+ * @returns {Uint8Array} The encoded bytes.
106
104
  */
107
- static toBuffer(pool: Pool): Buffer;
105
+ static toBuffer(pool: Pool): Uint8Array;
108
106
  /**
109
- * Creates a Pool instance from a Buffer or serialized data.
110
- * @param {Buffer | any} data - The buffer or serialized data.
107
+ * Creates a Pool instance from bytes or serialized data.
108
+ * @param {Uint8Array | any} data - The bytes or serialized data.
111
109
  * @returns {Pool} The pool instance.
112
110
  */
113
- static fromBuffer(data: Buffer | any): Pool;
111
+ static fromBuffer(data: Uint8Array | any): Pool;
114
112
  static fromId(id: number): Pool;
115
- static createHash(buffer: Buffer, pool?: Pool): string;
113
+ static createHash(buffer: Uint8Array, pool?: Pool): string | null;
116
114
  static hashEquals(pool: Pool, hash: string): boolean;
117
115
  equals(other: Pool): boolean;
118
116
  }
@@ -5,7 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.Pool = void 0;
7
7
  var _msgpackLite = _interopRequireDefault(require("msgpack-lite"));
8
- var _crypto = _interopRequireDefault(require("crypto"));
9
8
  var _fractions = require("./fractions");
10
9
  var _token = require("./token");
11
10
  var _internalConstants = require("../internalConstants");
@@ -24,6 +23,25 @@ function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r),
24
23
  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; }
25
24
  function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
26
25
  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); }
26
+ // Avoid importing node crypto at module load so browser bundles can tree-shake/ignore it.
27
+ const getNodeCrypto = () => {
28
+ try {
29
+ // Use eval to avoid bundlers eagerly pulling in node:crypto for browser builds.
30
+ const nodeRequire = typeof require !== 'undefined' ? require : (0, eval)('require');
31
+ return nodeRequire('crypto');
32
+ } catch (_) {
33
+ // Ignore; browser or crypto not available.
34
+ }
35
+ return null;
36
+ };
37
+ const isBuffer = value => typeof Buffer !== 'undefined' && typeof Buffer.isBuffer === 'function' && Buffer.isBuffer(value);
38
+ const toUint8Array = data => {
39
+ if (data instanceof Uint8Array) return data;
40
+ if (isBuffer(data)) return data;
41
+ if (data && data.buffer instanceof ArrayBuffer) return new Uint8Array(data.buffer);
42
+ if (data instanceof ArrayBuffer) return new Uint8Array(data);
43
+ throw new Error('Expected Buffer/Uint8Array/ArrayBuffer-like data');
44
+ };
27
45
  /**
28
46
  * By default, pools will not allow operations that require ticks.
29
47
  */
@@ -43,20 +61,19 @@ let Pool = exports.Pool = /*#__PURE__*/function () {
43
61
  * @param tickCurrent The current tick of the pool
44
62
  * @param ticks The current state of the pool ticks or a data provider that can return tick data
45
63
  */
46
- function Pool(_ref) {
47
- let {
48
- id,
49
- active,
50
- tokenA,
51
- tokenB,
52
- fee,
53
- sqrtPriceX64,
54
- liquidity,
55
- tickCurrent,
56
- ticks = NO_TICK_DATA_PROVIDER_DEFAULT,
57
- feeGrowthGlobalAX64 = 0,
58
- feeGrowthGlobalBX64 = 0
59
- } = _ref;
64
+ function Pool({
65
+ id,
66
+ active,
67
+ tokenA,
68
+ tokenB,
69
+ fee,
70
+ sqrtPriceX64,
71
+ liquidity,
72
+ tickCurrent,
73
+ ticks = NO_TICK_DATA_PROVIDER_DEFAULT,
74
+ feeGrowthGlobalAX64 = 0,
75
+ feeGrowthGlobalBX64 = 0
76
+ }) {
60
77
  _classCallCheck(this, Pool);
61
78
  // public readonly id: number;
62
79
  _defineProperty(this, "id", void 0);
@@ -154,9 +171,9 @@ let Pool = exports.Pool = /*#__PURE__*/function () {
154
171
  const outputAmount = zeroForOne ? amountB : amountA;
155
172
  const amountIn = zeroForOne ? amountA : amountB;
156
173
 
157
- //console.log(JSBI.equal(amountIn, inputAmount.quotient))
174
+ //console.log((amountIn === inputAmount.quotient))
158
175
  if (!(inputAmount.quotient >= amountIn)) {
159
- //if (!JSBI.equal(amountIn, inputAmount.quotient)) {
176
+ //if (!(amountIn === inputAmount.quotient)) {
160
177
  throw new _errors.InsufficientInputAmountError();
161
178
  }
162
179
  return _fractions.CurrencyAmount.fromRawAmount(outputToken, outputAmount * _internalConstants.NEGATIVE_ONE);
@@ -178,7 +195,7 @@ let Pool = exports.Pool = /*#__PURE__*/function () {
178
195
  } = this.swap(zeroForOne, outputAmount.quotient * _internalConstants.NEGATIVE_ONE, sqrtPriceLimitX64);
179
196
  const inputToken = zeroForOne ? this.tokenA : this.tokenB;
180
197
  const inputAmount = zeroForOne ? amountA : amountB;
181
- const amountOutReceived = (zeroForOne ? amountB : amountA) * _internalConstants.NEGATIVE_ONE;
198
+ const amountOutReceived = zeroForOne ? amountB : amountA * _internalConstants.NEGATIVE_ONE;
182
199
  if (!(amountOutReceived === outputAmount.quotient)) {
183
200
  throw new _errors.InsufficientReservesError();
184
201
  }
@@ -329,9 +346,9 @@ let Pool = exports.Pool = /*#__PURE__*/function () {
329
346
  });
330
347
  }
331
348
  /**
332
- * Converts the pool to a Buffer using msgpack encoding.
349
+ * Converts the pool to a byte array using msgpack encoding.
333
350
  * @param {Pool} pool - The pool instance to convert.
334
- * @returns {Buffer} The encoded buffer.
351
+ * @returns {Uint8Array} The encoded bytes.
335
352
  */
336
353
  }, {
337
354
  key: "toBuffer",
@@ -339,25 +356,27 @@ let Pool = exports.Pool = /*#__PURE__*/function () {
339
356
  if (pool.buffer) return pool.buffer;
340
357
  const json = Pool.toJSON(pool);
341
358
  pool.buffer = _msgpackLite.default.encode(json);
342
- pool.bufferHash = Pool.createHash(pool.buffer);
359
+ const hash = Pool.createHash(pool.buffer, pool);
360
+ if (hash) pool.bufferHash = hash;
343
361
  return pool.buffer;
344
362
  }
345
363
 
346
364
  /**
347
- * Creates a Pool instance from a Buffer or serialized data.
348
- * @param {Buffer | any} data - The buffer or serialized data.
365
+ * Creates a Pool instance from bytes or serialized data.
366
+ * @param {Uint8Array | any} data - The bytes or serialized data.
349
367
  * @returns {Pool} The pool instance.
350
368
  */
351
369
  }, {
352
370
  key: "fromBuffer",
353
371
  value: function fromBuffer(data) {
354
- const bufferHash = Pool.createHash(data instanceof Buffer ? data : data.buffer);
355
- if (this.hashToPoolMap.has(bufferHash)) {
372
+ const bytes = toUint8Array(data);
373
+ const bufferHash = Pool.createHash(bytes);
374
+ if (bufferHash && this.hashToPoolMap.has(bufferHash)) {
356
375
  return this.hashToPoolMap.get(bufferHash);
357
376
  }
358
- const json = _msgpackLite.default.decode(data instanceof Buffer ? data : data.buffer);
377
+ const json = _msgpackLite.default.decode(bytes);
359
378
  const pool = Pool.fromJSON(json);
360
- this.hashToPoolMap.set(bufferHash, pool);
379
+ if (bufferHash) this.hashToPoolMap.set(bufferHash, pool);
361
380
  this.idToPoolMap.set(pool.id, pool);
362
381
  return pool;
363
382
  }
@@ -375,7 +394,11 @@ let Pool = exports.Pool = /*#__PURE__*/function () {
375
394
  if (pool && pool.bufferHash) {
376
395
  return pool.bufferHash;
377
396
  }
378
- const hash = _crypto.default.createHash('sha256');
397
+ const crypto = getNodeCrypto();
398
+ if (!crypto || typeof crypto.createHash !== 'function') {
399
+ return null;
400
+ }
401
+ const hash = crypto.createHash('sha256');
379
402
  hash.update(buffer);
380
403
  const hexHash = hash.digest('hex');
381
404
  if (pool) {
@@ -1,7 +1,6 @@
1
1
  import { CurrencyAmount, Price, Percent } from "./fractions";
2
2
  import { Token } from "./token";
3
3
  import { BigintIsh } from "../internalConstants";
4
- import JSBI from "jsbi";
5
4
  import { Pool } from "./pool";
6
5
  interface PositionConstructorArgs {
7
6
  id: number;
@@ -25,11 +24,11 @@ export declare class Position {
25
24
  readonly pool: Pool;
26
25
  readonly tickLower: number;
27
26
  readonly tickUpper: number;
28
- readonly liquidity: JSBI;
29
- readonly feesA: JSBI;
30
- readonly feesB: JSBI;
31
- readonly feeGrowthInsideALastX64: JSBI;
32
- readonly feeGrowthInsideBLastX64: JSBI;
27
+ readonly liquidity: bigint;
28
+ readonly feesA: bigint;
29
+ readonly feesB: bigint;
30
+ readonly feeGrowthInsideALastX64: bigint;
31
+ readonly feeGrowthInsideBLastX64: bigint;
33
32
  private _tokenAAmount;
34
33
  private _tokenBAmount;
35
34
  private _mintAmounts;
@@ -71,8 +70,8 @@ export declare class Position {
71
70
  * @returns The amounts, with slippage
72
71
  */
73
72
  mintAmountsWithSlippage(slippageTolerance: Percent): Readonly<{
74
- amountA: JSBI;
75
- amountB: JSBI;
73
+ amountA: bigint;
74
+ amountB: bigint;
76
75
  }>;
77
76
  /**
78
77
  * Returns the minimum amounts that should be requested in order to safely burn the amount of liquidity held by the
@@ -89,8 +88,8 @@ export declare class Position {
89
88
  * the current price for the pool
90
89
  */
91
90
  get mintAmounts(): Readonly<{
92
- amountA: JSBI;
93
- amountB: JSBI;
91
+ amountA: bigint;
92
+ amountB: bigint;
94
93
  }>;
95
94
  /**
96
95
  * Computes the maximum amount of liquidity received for a given amount of tokenA, tokenB,