@pancakeswap/swap-sdk-core 0.0.1 → 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.
package/dist/index.js CHANGED
@@ -1,63 +1,18 @@
1
- "use strict";
2
- var __create = Object.create;
3
- var __defProp = Object.defineProperty;
4
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
- var __getOwnPropNames = Object.getOwnPropertyNames;
6
- var __getProtoOf = Object.getPrototypeOf;
7
- var __hasOwnProp = Object.prototype.hasOwnProperty;
8
- var __export = (target, all) => {
9
- for (var name in all)
10
- __defProp(target, name, { get: all[name], enumerable: true });
11
- };
12
- var __copyProps = (to, from, except, desc) => {
13
- if (from && typeof from === "object" || typeof from === "function") {
14
- for (let key of __getOwnPropNames(from))
15
- if (!__hasOwnProp.call(to, key) && key !== except)
16
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
- }
18
- return to;
19
- };
20
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
21
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
1
+ 'use strict';
2
+
3
+ var invariant6 = require('tiny-invariant');
4
+ var _Decimal = require('decimal.js-light');
5
+ var _Big = require('big.js');
6
+ var toFormat = require('toformat');
7
+
8
+ function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
22
9
 
23
- // src/index.ts
24
- var src_exports = {};
25
- __export(src_exports, {
26
- BaseCurrency: () => BaseCurrency,
27
- CurrencyAmount: () => CurrencyAmount,
28
- FIVE: () => FIVE,
29
- Fraction: () => Fraction,
30
- InsufficientInputAmountError: () => InsufficientInputAmountError,
31
- InsufficientReservesError: () => InsufficientReservesError,
32
- JSBI: () => import_jsbi7.default,
33
- MINIMUM_LIQUIDITY: () => MINIMUM_LIQUIDITY,
34
- MaxUint256: () => MaxUint256,
35
- NativeCurrency: () => NativeCurrency,
36
- ONE: () => ONE,
37
- Percent: () => Percent,
38
- Price: () => Price,
39
- Rounding: () => Rounding,
40
- TEN: () => TEN,
41
- THREE: () => THREE,
42
- TWO: () => TWO,
43
- Token: () => Token,
44
- TradeType: () => TradeType,
45
- VMType: () => VMType,
46
- VM_TYPE_MAXIMA: () => VM_TYPE_MAXIMA,
47
- ZERO: () => ZERO,
48
- _100: () => _100,
49
- _10000: () => _10000,
50
- _9975: () => _9975,
51
- computePriceImpact: () => computePriceImpact,
52
- sortedInsert: () => sortedInsert,
53
- sqrt: () => sqrt,
54
- validateVMTypeInstance: () => validateVMTypeInstance
55
- });
56
- module.exports = __toCommonJS(src_exports);
57
- var import_jsbi7 = __toESM(require("jsbi"));
10
+ var invariant6__default = /*#__PURE__*/_interopDefault(invariant6);
11
+ var _Decimal__default = /*#__PURE__*/_interopDefault(_Decimal);
12
+ var _Big__default = /*#__PURE__*/_interopDefault(_Big);
13
+ var toFormat__default = /*#__PURE__*/_interopDefault(toFormat);
58
14
 
59
15
  // src/constants.ts
60
- var import_jsbi = __toESM(require("jsbi"));
61
16
  var TradeType = /* @__PURE__ */ ((TradeType2) => {
62
17
  TradeType2[TradeType2["EXACT_INPUT"] = 0] = "EXACT_INPUT";
63
18
  TradeType2[TradeType2["EXACT_OUTPUT"] = 1] = "EXACT_OUTPUT";
@@ -69,48 +24,45 @@ var Rounding = /* @__PURE__ */ ((Rounding2) => {
69
24
  Rounding2[Rounding2["ROUND_UP"] = 2] = "ROUND_UP";
70
25
  return Rounding2;
71
26
  })(Rounding || {});
72
- var MINIMUM_LIQUIDITY = import_jsbi.default.BigInt(1e3);
73
- var ZERO = import_jsbi.default.BigInt(0);
74
- var ONE = import_jsbi.default.BigInt(1);
75
- var TWO = import_jsbi.default.BigInt(2);
76
- var THREE = import_jsbi.default.BigInt(3);
77
- var FIVE = import_jsbi.default.BigInt(5);
78
- var TEN = import_jsbi.default.BigInt(10);
79
- var _100 = import_jsbi.default.BigInt(100);
80
- var _9975 = import_jsbi.default.BigInt(9975);
81
- var _10000 = import_jsbi.default.BigInt(1e4);
82
- var MaxUint256 = import_jsbi.default.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
83
- var VMType = /* @__PURE__ */ ((VMType2) => {
84
- VMType2["uint8"] = "uint8";
85
- VMType2["uint256"] = "uint256";
86
- return VMType2;
27
+ var MINIMUM_LIQUIDITY = 1000n;
28
+ var ZERO = 0n;
29
+ var ONE = 1n;
30
+ var TWO = 2n;
31
+ var THREE = 3n;
32
+ var FIVE = 5n;
33
+ var TEN = 10n;
34
+ var _100 = 100n;
35
+ var _9975 = 9975n;
36
+ var _10000 = 10000n;
37
+ var MaxUint256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
38
+ var VMType = /* @__PURE__ */ ((VMType3) => {
39
+ VMType3["uint8"] = "uint8";
40
+ VMType3["uint256"] = "uint256";
41
+ return VMType3;
87
42
  })(VMType || {});
88
43
  var VM_TYPE_MAXIMA = {
89
- ["uint8" /* uint8 */]: import_jsbi.default.BigInt("0xff"),
90
- ["uint256" /* uint256 */]: import_jsbi.default.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
44
+ ["uint8" /* uint8 */]: BigInt("0xff"),
45
+ ["uint256" /* uint256 */]: BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
91
46
  };
92
-
93
- // src/baseCurrency.ts
94
- var import_tiny_invariant = __toESM(require("tiny-invariant"));
95
47
  var BaseCurrency = class {
48
+ /**
49
+ * Constructs an instance of the base class `BaseCurrency`.
50
+ * @param chainId the chain ID on which this currency resides
51
+ * @param decimals decimals of the currency
52
+ * @param symbol symbol of the currency
53
+ * @param name of the currency
54
+ */
96
55
  constructor(chainId, decimals, symbol, name) {
97
- (0, import_tiny_invariant.default)(Number.isSafeInteger(chainId), "CHAIN_ID");
98
- (0, import_tiny_invariant.default)(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
56
+ invariant6__default.default(Number.isSafeInteger(chainId), "CHAIN_ID");
57
+ invariant6__default.default(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
99
58
  this.chainId = chainId;
100
59
  this.decimals = decimals;
101
60
  this.symbol = symbol;
102
61
  this.name = name;
103
62
  }
104
63
  };
105
-
106
- // src/fractions/fraction.ts
107
- var import_jsbi2 = __toESM(require("jsbi"));
108
- var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
109
- var import_decimal = __toESM(require("decimal.js-light"));
110
- var import_big = __toESM(require("big.js"));
111
- var import_toformat = __toESM(require("toformat"));
112
- var Decimal = (0, import_toformat.default)(import_decimal.default);
113
- var Big = (0, import_toformat.default)(import_big.default);
64
+ var Decimal = toFormat__default.default(_Decimal__default.default);
65
+ var Big = toFormat__default.default(_Big__default.default);
114
66
  var toSignificantRounding = {
115
67
  [0 /* ROUND_DOWN */]: Decimal.ROUND_DOWN,
116
68
  [1 /* ROUND_HALF_UP */]: Decimal.ROUND_HALF_UP,
@@ -122,88 +74,101 @@ var toFixedRounding = {
122
74
  [2 /* ROUND_UP */]: 3 /* RoundUp */
123
75
  };
124
76
  var Fraction = class {
125
- constructor(numerator, denominator = import_jsbi2.default.BigInt(1)) {
126
- this.numerator = import_jsbi2.default.BigInt(numerator);
127
- this.denominator = import_jsbi2.default.BigInt(denominator);
77
+ constructor(numerator, denominator = 1n) {
78
+ this.numerator = BigInt(numerator);
79
+ this.denominator = BigInt(denominator);
128
80
  }
129
81
  static tryParseFraction(fractionish) {
130
- if (fractionish instanceof import_jsbi2.default || typeof fractionish === "number" || typeof fractionish === "string")
82
+ if (typeof fractionish === "bigint" || typeof fractionish === "number" || typeof fractionish === "string")
131
83
  return new Fraction(fractionish);
132
84
  if ("numerator" in fractionish && "denominator" in fractionish)
133
85
  return fractionish;
134
86
  throw new Error("Could not parse fraction");
135
87
  }
88
+ // performs floor division
136
89
  get quotient() {
137
- return import_jsbi2.default.divide(this.numerator, this.denominator);
90
+ return this.numerator / this.denominator;
138
91
  }
92
+ // remainder after floor division
139
93
  get remainder() {
140
- return new Fraction(import_jsbi2.default.remainder(this.numerator, this.denominator), this.denominator);
94
+ return new Fraction(this.numerator % this.denominator, this.denominator);
141
95
  }
142
96
  invert() {
143
97
  return new Fraction(this.denominator, this.numerator);
144
98
  }
145
99
  add(other) {
146
100
  const otherParsed = Fraction.tryParseFraction(other);
147
- if (import_jsbi2.default.equal(this.denominator, otherParsed.denominator)) {
148
- return new Fraction(import_jsbi2.default.add(this.numerator, otherParsed.numerator), this.denominator);
101
+ if (this.denominator === otherParsed.denominator) {
102
+ return new Fraction(this.numerator + otherParsed.numerator, this.denominator);
149
103
  }
150
- return new Fraction(import_jsbi2.default.add(import_jsbi2.default.multiply(this.numerator, otherParsed.denominator), import_jsbi2.default.multiply(otherParsed.numerator, this.denominator)), import_jsbi2.default.multiply(this.denominator, otherParsed.denominator));
104
+ return new Fraction(
105
+ this.numerator * otherParsed.denominator + otherParsed.numerator * this.denominator,
106
+ this.denominator * otherParsed.denominator
107
+ );
151
108
  }
152
109
  subtract(other) {
153
110
  const otherParsed = Fraction.tryParseFraction(other);
154
- if (import_jsbi2.default.equal(this.denominator, otherParsed.denominator)) {
155
- return new Fraction(import_jsbi2.default.subtract(this.numerator, otherParsed.numerator), this.denominator);
111
+ if (this.denominator === otherParsed.denominator) {
112
+ return new Fraction(this.numerator - otherParsed.numerator, this.denominator);
156
113
  }
157
- return new Fraction(import_jsbi2.default.subtract(import_jsbi2.default.multiply(this.numerator, otherParsed.denominator), import_jsbi2.default.multiply(otherParsed.numerator, this.denominator)), import_jsbi2.default.multiply(this.denominator, otherParsed.denominator));
114
+ return new Fraction(
115
+ this.numerator * otherParsed.denominator - otherParsed.numerator * this.denominator,
116
+ this.denominator * otherParsed.denominator
117
+ );
158
118
  }
159
119
  lessThan(other) {
160
120
  const otherParsed = Fraction.tryParseFraction(other);
161
- return import_jsbi2.default.lessThan(import_jsbi2.default.multiply(this.numerator, otherParsed.denominator), import_jsbi2.default.multiply(otherParsed.numerator, this.denominator));
121
+ return this.numerator * otherParsed.denominator < otherParsed.numerator * this.denominator;
162
122
  }
163
123
  equalTo(other) {
164
124
  const otherParsed = Fraction.tryParseFraction(other);
165
- return import_jsbi2.default.equal(import_jsbi2.default.multiply(this.numerator, otherParsed.denominator), import_jsbi2.default.multiply(otherParsed.numerator, this.denominator));
125
+ return this.numerator * otherParsed.denominator === otherParsed.numerator * this.denominator;
166
126
  }
167
127
  greaterThan(other) {
168
128
  const otherParsed = Fraction.tryParseFraction(other);
169
- return import_jsbi2.default.greaterThan(import_jsbi2.default.multiply(this.numerator, otherParsed.denominator), import_jsbi2.default.multiply(otherParsed.numerator, this.denominator));
129
+ return this.numerator * otherParsed.denominator > otherParsed.numerator * this.denominator;
170
130
  }
171
131
  multiply(other) {
172
132
  const otherParsed = Fraction.tryParseFraction(other);
173
- return new Fraction(import_jsbi2.default.multiply(this.numerator, otherParsed.numerator), import_jsbi2.default.multiply(this.denominator, otherParsed.denominator));
133
+ return new Fraction(this.numerator * otherParsed.numerator, this.denominator * otherParsed.denominator);
174
134
  }
175
135
  divide(other) {
176
136
  const otherParsed = Fraction.tryParseFraction(other);
177
- return new Fraction(import_jsbi2.default.multiply(this.numerator, otherParsed.denominator), import_jsbi2.default.multiply(this.denominator, otherParsed.numerator));
137
+ return new Fraction(this.numerator * otherParsed.denominator, this.denominator * otherParsed.numerator);
178
138
  }
179
139
  toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
180
- (0, import_tiny_invariant2.default)(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
181
- (0, import_tiny_invariant2.default)(significantDigits > 0, `${significantDigits} is not positive.`);
140
+ invariant6__default.default(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
141
+ invariant6__default.default(significantDigits > 0, `${significantDigits} is not positive.`);
182
142
  Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });
183
143
  const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
184
144
  return quotient.toFormat(quotient.decimalPlaces(), format);
185
145
  }
186
146
  toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
187
- (0, import_tiny_invariant2.default)(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
188
- (0, import_tiny_invariant2.default)(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
147
+ invariant6__default.default(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
148
+ invariant6__default.default(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
189
149
  Big.DP = decimalPlaces;
190
150
  Big.RM = toFixedRounding[rounding];
191
151
  return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
192
152
  }
153
+ /**
154
+ * Helper method for converting any super class back to a fraction
155
+ */
193
156
  get asFraction() {
194
157
  return new Fraction(this.numerator, this.denominator);
195
158
  }
196
159
  };
197
160
 
198
161
  // src/fractions/percent.ts
199
- var import_jsbi3 = __toESM(require("jsbi"));
200
- var ONE_HUNDRED = new Fraction(import_jsbi3.default.BigInt(100));
162
+ var ONE_HUNDRED = new Fraction(100n);
201
163
  function toPercent(fraction) {
202
164
  return new Percent(fraction.numerator, fraction.denominator);
203
165
  }
204
166
  var Percent = class extends Fraction {
205
167
  constructor() {
206
168
  super(...arguments);
169
+ /**
170
+ * This boolean prevents a fraction from being interpreted as a Percent
171
+ */
207
172
  this.isPercent = true;
208
173
  }
209
174
  add(other) {
@@ -225,33 +190,38 @@ var Percent = class extends Fraction {
225
190
  return super.multiply(ONE_HUNDRED).toFixed(decimalPlaces, format, rounding);
226
191
  }
227
192
  };
228
-
229
- // src/fractions/currencyAmount.ts
230
- var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
231
- var import_jsbi4 = __toESM(require("jsbi"));
232
- var import_big2 = __toESM(require("big.js"));
233
- var import_toformat2 = __toESM(require("toformat"));
234
- var Big2 = (0, import_toformat2.default)(import_big2.default);
193
+ var Big2 = toFormat__default.default(_Big__default.default);
235
194
  var CurrencyAmount = class extends Fraction {
236
195
  constructor(currency, numerator, denominator) {
237
196
  super(numerator, denominator);
238
- (0, import_tiny_invariant3.default)(import_jsbi4.default.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT");
197
+ invariant6__default.default(this.quotient <= MaxUint256, "AMOUNT");
239
198
  this.currency = currency;
240
- this.decimalScale = import_jsbi4.default.exponentiate(import_jsbi4.default.BigInt(10), import_jsbi4.default.BigInt(currency.decimals));
199
+ this.decimalScale = 10n ** BigInt(currency.decimals);
241
200
  }
201
+ /**
202
+ * Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
203
+ * @param currency the currency in the amount
204
+ * @param rawAmount the raw token or ether amount
205
+ */
242
206
  static fromRawAmount(currency, rawAmount) {
243
207
  return new CurrencyAmount(currency, rawAmount);
244
208
  }
209
+ /**
210
+ * Construct a currency amount with a denominator that is not equal to 1
211
+ * @param currency the currency
212
+ * @param numerator the numerator of the fractional token amount
213
+ * @param denominator the denominator of the fractional token amount
214
+ */
245
215
  static fromFractionalAmount(currency, numerator, denominator) {
246
216
  return new CurrencyAmount(currency, numerator, denominator);
247
217
  }
248
218
  add(other) {
249
- (0, import_tiny_invariant3.default)(this.currency.equals(other.currency), "CURRENCY");
219
+ invariant6__default.default(this.currency.equals(other.currency), "CURRENCY");
250
220
  const added = super.add(other);
251
221
  return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
252
222
  }
253
223
  subtract(other) {
254
- (0, import_tiny_invariant3.default)(this.currency.equals(other.currency), "CURRENCY");
224
+ invariant6__default.default(this.currency.equals(other.currency), "CURRENCY");
255
225
  const subtracted = super.subtract(other);
256
226
  return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
257
227
  }
@@ -267,7 +237,7 @@ var CurrencyAmount = class extends Fraction {
267
237
  return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
268
238
  }
269
239
  toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
270
- (0, import_tiny_invariant3.default)(decimalPlaces <= this.currency.decimals, "DECIMALS");
240
+ invariant6__default.default(decimalPlaces <= this.currency.decimals, "DECIMALS");
271
241
  return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
272
242
  }
273
243
  toExact(format = { groupSeparator: "" }) {
@@ -280,18 +250,18 @@ var CurrencyAmount = class extends Fraction {
280
250
  return CurrencyAmount.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator);
281
251
  }
282
252
  };
283
-
284
- // src/fractions/price.ts
285
- var import_jsbi5 = __toESM(require("jsbi"));
286
- var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
287
253
  var Price = class extends Fraction {
254
+ // used to adjust the raw fraction w/r/t the decimals of the {base,quote}Token
255
+ /**
256
+ * Construct a price, either with the base and quote currency amount, or the
257
+ * @param args
258
+ */
288
259
  constructor(...args) {
289
260
  let baseCurrency;
290
261
  let quoteCurrency;
291
262
  let denominator;
292
263
  let numerator;
293
264
  if (args.length === 4) {
294
- ;
295
265
  [baseCurrency, quoteCurrency, denominator, numerator] = args;
296
266
  } else {
297
267
  const result = args[0].quoteAmount.divide(args[0].baseAmount);
@@ -305,21 +275,36 @@ var Price = class extends Fraction {
305
275
  super(numerator, denominator);
306
276
  this.baseCurrency = baseCurrency;
307
277
  this.quoteCurrency = quoteCurrency;
308
- this.scalar = new Fraction(import_jsbi5.default.exponentiate(import_jsbi5.default.BigInt(10), import_jsbi5.default.BigInt(baseCurrency.decimals)), import_jsbi5.default.exponentiate(import_jsbi5.default.BigInt(10), import_jsbi5.default.BigInt(quoteCurrency.decimals)));
278
+ this.scalar = new Fraction(10n ** BigInt(baseCurrency.decimals), 10n ** BigInt(quoteCurrency.decimals));
309
279
  }
280
+ /**
281
+ * Flip the price, switching the base and quote currency
282
+ */
310
283
  invert() {
311
284
  return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
312
285
  }
286
+ /**
287
+ * 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
288
+ * @param other the other price
289
+ */
313
290
  multiply(other) {
314
- (0, import_tiny_invariant4.default)(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
291
+ invariant6__default.default(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
315
292
  const fraction = super.multiply(other);
316
293
  return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
317
294
  }
295
+ /**
296
+ * Return the amount of quote currency corresponding to a given amount of the base currency
297
+ * @param currencyAmount the amount of base currency to quote against the price
298
+ */
318
299
  quote(currencyAmount) {
319
- (0, import_tiny_invariant4.default)(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
300
+ invariant6__default.default(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
320
301
  const result = super.multiply(currencyAmount);
321
302
  return CurrencyAmount.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
322
303
  }
304
+ /**
305
+ * Get the value scaled by decimals for formatting
306
+ * @private
307
+ */
323
308
  get adjustedForDecimals() {
324
309
  return super.multiply(this.scalar);
325
310
  }
@@ -339,9 +324,6 @@ var NativeCurrency = class extends BaseCurrency {
339
324
  this.isToken = false;
340
325
  }
341
326
  };
342
-
343
- // src/token.ts
344
- var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
345
327
  var Token = class extends BaseCurrency {
346
328
  constructor(chainId, address, decimals, symbol, name, projectLink) {
347
329
  super(chainId, decimals, symbol, name);
@@ -350,14 +332,27 @@ var Token = class extends BaseCurrency {
350
332
  this.address = address;
351
333
  this.projectLink = projectLink;
352
334
  }
335
+ /**
336
+ * Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
337
+ * @param other other token to compare
338
+ */
353
339
  equals(other) {
354
340
  return other.isToken && this.chainId === other.chainId && this.address === other.address;
355
341
  }
342
+ /**
343
+ * Returns true if the address of this token sorts before the address of the other token
344
+ * @param other other token to compare
345
+ * @throws if the tokens have the same address
346
+ * @throws if the tokens are on different chains
347
+ */
356
348
  sortsBefore(other) {
357
- (0, import_tiny_invariant5.default)(this.chainId === other.chainId, "CHAIN_IDS");
358
- (0, import_tiny_invariant5.default)(this.address !== other.address, "ADDRESSES");
349
+ invariant6__default.default(this.chainId === other.chainId, "CHAIN_IDS");
350
+ invariant6__default.default(this.address !== other.address, "ADDRESSES");
359
351
  return this.address.toLowerCase() < other.address.toLowerCase();
360
352
  }
353
+ /**
354
+ * Return this token, which does not need to be wrapped
355
+ */
361
356
  get wrapped() {
362
357
  return this;
363
358
  }
@@ -393,33 +388,29 @@ var InsufficientInputAmountError = class extends Error {
393
388
  Object.setPrototypeOf(this, new.target.prototype);
394
389
  }
395
390
  };
396
-
397
- // src/utils.ts
398
- var import_jsbi6 = __toESM(require("jsbi"));
399
- var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
400
391
  function validateVMTypeInstance(value, vmType) {
401
- (0, import_tiny_invariant6.default)(import_jsbi6.default.greaterThanOrEqual(value, ZERO), `${value} is not a ${vmType}.`);
402
- (0, import_tiny_invariant6.default)(import_jsbi6.default.lessThanOrEqual(value, VM_TYPE_MAXIMA[vmType]), `${value} is not a ${vmType}.`);
392
+ invariant6__default.default(value >= ZERO, `${value} is not a ${vmType}.`);
393
+ invariant6__default.default(value <= VM_TYPE_MAXIMA[vmType], `${value} is not a ${vmType}.`);
403
394
  }
404
395
  function sqrt(y) {
405
- validateVMTypeInstance(y, "uint256" /* uint256 */);
396
+ invariant6__default.default(y >= ZERO, "NEGATIVE");
406
397
  let z = ZERO;
407
398
  let x;
408
- if (import_jsbi6.default.greaterThan(y, THREE)) {
399
+ if (y > THREE) {
409
400
  z = y;
410
- x = import_jsbi6.default.add(import_jsbi6.default.divide(y, TWO), ONE);
411
- while (import_jsbi6.default.lessThan(x, z)) {
401
+ x = y / TWO + ONE;
402
+ while (x < z) {
412
403
  z = x;
413
- x = import_jsbi6.default.divide(import_jsbi6.default.add(import_jsbi6.default.divide(y, x), x), TWO);
404
+ x = (y / x + x) / TWO;
414
405
  }
415
- } else if (import_jsbi6.default.notEqual(y, ZERO)) {
406
+ } else if (y !== ZERO) {
416
407
  z = ONE;
417
408
  }
418
409
  return z;
419
410
  }
420
411
  function sortedInsert(items, add, maxSize, comparator) {
421
- (0, import_tiny_invariant6.default)(maxSize > 0, "MAX_SIZE_ZERO");
422
- (0, import_tiny_invariant6.default)(items.length <= maxSize, "ITEMS_SIZE");
412
+ invariant6__default.default(maxSize > 0, "MAX_SIZE_ZERO");
413
+ invariant6__default.default(items.length <= maxSize, "ITEMS_SIZE");
423
414
  if (items.length === 0) {
424
415
  items.push(add);
425
416
  return null;
@@ -446,35 +437,58 @@ function computePriceImpact(midPrice, inputAmount, outputAmount) {
446
437
  const priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount);
447
438
  return new Percent(priceImpact.numerator, priceImpact.denominator);
448
439
  }
449
- // Annotate the CommonJS export names for ESM import in node:
450
- 0 && (module.exports = {
451
- BaseCurrency,
452
- CurrencyAmount,
453
- FIVE,
454
- Fraction,
455
- InsufficientInputAmountError,
456
- InsufficientReservesError,
457
- JSBI,
458
- MINIMUM_LIQUIDITY,
459
- MaxUint256,
460
- NativeCurrency,
461
- ONE,
462
- Percent,
463
- Price,
464
- Rounding,
465
- TEN,
466
- THREE,
467
- TWO,
468
- Token,
469
- TradeType,
470
- VMType,
471
- VM_TYPE_MAXIMA,
472
- ZERO,
473
- _100,
474
- _10000,
475
- _9975,
476
- computePriceImpact,
477
- sortedInsert,
478
- sqrt,
479
- validateVMTypeInstance
480
- });
440
+ function balanceComparator(balanceA, balanceB) {
441
+ if (balanceA && balanceB) {
442
+ return balanceA.greaterThan(balanceB) ? -1 : balanceA.equalTo(balanceB) ? 0 : 1;
443
+ }
444
+ if (balanceA && balanceA.greaterThan("0")) {
445
+ return -1;
446
+ }
447
+ if (balanceB && balanceB.greaterThan("0")) {
448
+ return 1;
449
+ }
450
+ return 0;
451
+ }
452
+ function getTokenComparator(balances) {
453
+ return function sortTokens(tokenA, tokenB) {
454
+ const balanceA = balances[tokenA.address];
455
+ const balanceB = balances[tokenB.address];
456
+ const balanceComp = balanceComparator(balanceA, balanceB);
457
+ if (balanceComp !== 0)
458
+ return balanceComp;
459
+ if (tokenA.symbol && tokenB.symbol) {
460
+ return tokenA.symbol.toLowerCase() < tokenB.symbol.toLowerCase() ? -1 : 1;
461
+ }
462
+ return tokenA.symbol ? -1 : tokenB.symbol ? -1 : 0;
463
+ };
464
+ }
465
+
466
+ exports.BaseCurrency = BaseCurrency;
467
+ exports.CurrencyAmount = CurrencyAmount;
468
+ exports.FIVE = FIVE;
469
+ exports.Fraction = Fraction;
470
+ exports.InsufficientInputAmountError = InsufficientInputAmountError;
471
+ exports.InsufficientReservesError = InsufficientReservesError;
472
+ exports.MINIMUM_LIQUIDITY = MINIMUM_LIQUIDITY;
473
+ exports.MaxUint256 = MaxUint256;
474
+ exports.NativeCurrency = NativeCurrency;
475
+ exports.ONE = ONE;
476
+ exports.Percent = Percent;
477
+ exports.Price = Price;
478
+ exports.Rounding = Rounding;
479
+ exports.TEN = TEN;
480
+ exports.THREE = THREE;
481
+ exports.TWO = TWO;
482
+ exports.Token = Token;
483
+ exports.TradeType = TradeType;
484
+ exports.VMType = VMType;
485
+ exports.VM_TYPE_MAXIMA = VM_TYPE_MAXIMA;
486
+ exports.ZERO = ZERO;
487
+ exports._100 = _100;
488
+ exports._10000 = _10000;
489
+ exports._9975 = _9975;
490
+ exports.computePriceImpact = computePriceImpact;
491
+ exports.getTokenComparator = getTokenComparator;
492
+ exports.sortedInsert = sortedInsert;
493
+ exports.sqrt = sqrt;
494
+ exports.validateVMTypeInstance = validateVMTypeInstance;