@pancakeswap/sdk 3.0.0-2 → 3.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 (37) hide show
  1. package/dist/index.d.ts +515 -7
  2. package/dist/index.js +1012 -5
  3. package/dist/index.mjs +945 -0
  4. package/package.json +20 -14
  5. package/dist/constants.d.ts +0 -43
  6. package/dist/entities/currency.d.ts +0 -23
  7. package/dist/entities/fractions/currencyAmount.d.ts +0 -19
  8. package/dist/entities/fractions/fraction.d.ts +0 -23
  9. package/dist/entities/fractions/index.d.ts +0 -5
  10. package/dist/entities/fractions/percent.d.ts +0 -6
  11. package/dist/entities/fractions/price.d.ts +0 -19
  12. package/dist/entities/fractions/tokenAmount.d.ts +0 -9
  13. package/dist/entities/index.d.ts +0 -6
  14. package/dist/entities/pair.d.ts +0 -41
  15. package/dist/entities/route.d.ts +0 -14
  16. package/dist/entities/token.d.ts +0 -35
  17. package/dist/entities/trade.d.ts +0 -106
  18. package/dist/errors.d.ts +0 -16
  19. package/dist/fetcher.d.ts +0 -28
  20. package/dist/router.d.ts +0 -63
  21. package/dist/sdk.cjs.development.js +0 -2367
  22. package/dist/sdk.cjs.development.js.map +0 -1
  23. package/dist/sdk.cjs.production.min.js +0 -2
  24. package/dist/sdk.cjs.production.min.js.map +0 -1
  25. package/dist/sdk.esm.js +0 -2342
  26. package/dist/sdk.esm.js.map +0 -1
  27. package/dist/test/constants.test.d.ts +0 -1
  28. package/dist/test/data.test.d.ts +0 -1
  29. package/dist/test/entities.test.d.ts +0 -1
  30. package/dist/test/fraction.test.d.ts +0 -1
  31. package/dist/test/miscellaneous.test.d.ts +0 -1
  32. package/dist/test/pair.test.d.ts +0 -1
  33. package/dist/test/route.test.d.ts +0 -1
  34. package/dist/test/router.test.d.ts +0 -1
  35. package/dist/test/token.test.d.ts +0 -1
  36. package/dist/test/trade.test.d.ts +0 -1
  37. package/dist/utils.d.ts +0 -7
package/dist/index.mjs ADDED
@@ -0,0 +1,945 @@
1
+ // src/index.ts
2
+ import JSBI8 from "jsbi";
3
+
4
+ // src/constants.ts
5
+ import JSBI2 from "jsbi";
6
+
7
+ // src/entities/token.ts
8
+ import invariant3 from "tiny-invariant";
9
+
10
+ // src/utils.ts
11
+ import invariant from "tiny-invariant";
12
+ import warning from "tiny-warning";
13
+ import JSBI from "jsbi";
14
+ import { getAddress } from "@ethersproject/address";
15
+ function validateSolidityTypeInstance(value, solidityType) {
16
+ invariant(JSBI.greaterThanOrEqual(value, ZERO), `${value} is not a ${solidityType}.`);
17
+ invariant(JSBI.lessThanOrEqual(value, SOLIDITY_TYPE_MAXIMA[solidityType]), `${value} is not a ${solidityType}.`);
18
+ }
19
+ function validateAndParseAddress(address) {
20
+ try {
21
+ const checksummedAddress = getAddress(address);
22
+ warning(address === checksummedAddress, `${address} is not checksummed.`);
23
+ return checksummedAddress;
24
+ } catch (error) {
25
+ invariant(false, `${address} is not a valid address.`);
26
+ }
27
+ }
28
+ function sqrt(y) {
29
+ validateSolidityTypeInstance(y, "uint256" /* uint256 */);
30
+ let z = ZERO;
31
+ let x;
32
+ if (JSBI.greaterThan(y, THREE)) {
33
+ z = y;
34
+ x = JSBI.add(JSBI.divide(y, TWO), ONE);
35
+ while (JSBI.lessThan(x, z)) {
36
+ z = x;
37
+ x = JSBI.divide(JSBI.add(JSBI.divide(y, x), x), TWO);
38
+ }
39
+ } else if (JSBI.notEqual(y, ZERO)) {
40
+ z = ONE;
41
+ }
42
+ return z;
43
+ }
44
+ function sortedInsert(items, add, maxSize, comparator) {
45
+ invariant(maxSize > 0, "MAX_SIZE_ZERO");
46
+ invariant(items.length <= maxSize, "ITEMS_SIZE");
47
+ if (items.length === 0) {
48
+ items.push(add);
49
+ return null;
50
+ } else {
51
+ const isFull = items.length === maxSize;
52
+ if (isFull && comparator(items[items.length - 1], add) <= 0) {
53
+ return add;
54
+ }
55
+ let lo = 0, hi = items.length;
56
+ while (lo < hi) {
57
+ const mid = lo + hi >>> 1;
58
+ if (comparator(items[mid], add) <= 0) {
59
+ lo = mid + 1;
60
+ } else {
61
+ hi = mid;
62
+ }
63
+ }
64
+ items.splice(lo, 0, add);
65
+ return isFull ? items.pop() : null;
66
+ }
67
+ }
68
+ function computePriceImpact(midPrice, inputAmount, outputAmount) {
69
+ const quotedOutputAmount = midPrice.quote(inputAmount);
70
+ const priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount);
71
+ return new Percent(priceImpact.numerator, priceImpact.denominator);
72
+ }
73
+
74
+ // src/entities/baseCurrency.ts
75
+ import invariant2 from "tiny-invariant";
76
+ var BaseCurrency = class {
77
+ constructor(chainId, decimals, symbol, name) {
78
+ invariant2(Number.isSafeInteger(chainId), "CHAIN_ID");
79
+ invariant2(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
80
+ this.chainId = chainId;
81
+ this.decimals = decimals;
82
+ this.symbol = symbol;
83
+ this.name = name;
84
+ }
85
+ };
86
+
87
+ // src/entities/token.ts
88
+ var Token = class extends BaseCurrency {
89
+ constructor(chainId, address, decimals, symbol, name, projectLink) {
90
+ super(chainId, decimals, symbol, name);
91
+ this.isNative = false;
92
+ this.isToken = true;
93
+ this.address = validateAndParseAddress(address);
94
+ this.projectLink = projectLink;
95
+ }
96
+ equals(other) {
97
+ return other.isToken && this.chainId === other.chainId && this.address === other.address;
98
+ }
99
+ sortsBefore(other) {
100
+ invariant3(this.chainId === other.chainId, "CHAIN_IDS");
101
+ invariant3(this.address !== other.address, "ADDRESSES");
102
+ return this.address.toLowerCase() < other.address.toLowerCase();
103
+ }
104
+ get wrapped() {
105
+ return this;
106
+ }
107
+ get serialize() {
108
+ return {
109
+ address: this.address,
110
+ chainId: this.chainId,
111
+ decimals: this.decimals,
112
+ symbol: this.symbol,
113
+ name: this.name,
114
+ projectLink: this.projectLink
115
+ };
116
+ }
117
+ };
118
+
119
+ // src/constants.ts
120
+ var ChainId = /* @__PURE__ */ ((ChainId2) => {
121
+ ChainId2[ChainId2["ETHEREUM"] = 1] = "ETHEREUM";
122
+ ChainId2[ChainId2["RINKEBY"] = 4] = "RINKEBY";
123
+ ChainId2[ChainId2["GOERLI"] = 5] = "GOERLI";
124
+ ChainId2[ChainId2["BSC"] = 56] = "BSC";
125
+ ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
126
+ return ChainId2;
127
+ })(ChainId || {});
128
+ var TradeType = /* @__PURE__ */ ((TradeType2) => {
129
+ TradeType2[TradeType2["EXACT_INPUT"] = 0] = "EXACT_INPUT";
130
+ TradeType2[TradeType2["EXACT_OUTPUT"] = 1] = "EXACT_OUTPUT";
131
+ return TradeType2;
132
+ })(TradeType || {});
133
+ var Rounding = /* @__PURE__ */ ((Rounding2) => {
134
+ Rounding2[Rounding2["ROUND_DOWN"] = 0] = "ROUND_DOWN";
135
+ Rounding2[Rounding2["ROUND_HALF_UP"] = 1] = "ROUND_HALF_UP";
136
+ Rounding2[Rounding2["ROUND_UP"] = 2] = "ROUND_UP";
137
+ return Rounding2;
138
+ })(Rounding || {});
139
+ var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
140
+ var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
141
+ var FACTORY_ADDRESS_MAP = {
142
+ [1 /* ETHEREUM */]: FACTORY_ADDRESS_ETH,
143
+ [4 /* RINKEBY */]: FACTORY_ADDRESS_ETH,
144
+ [5 /* GOERLI */]: FACTORY_ADDRESS_ETH,
145
+ [56 /* BSC */]: FACTORY_ADDRESS,
146
+ [97 /* BSC_TESTNET */]: "0x6725f303b657a9451d8ba641348b6761a6cc7a17"
147
+ };
148
+ var INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5";
149
+ var INIT_CODE_HASH_ETH = "0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d";
150
+ var INIT_CODE_HASH_MAP = {
151
+ [1 /* ETHEREUM */]: INIT_CODE_HASH_ETH,
152
+ [4 /* RINKEBY */]: INIT_CODE_HASH_ETH,
153
+ [5 /* GOERLI */]: INIT_CODE_HASH_ETH,
154
+ [56 /* BSC */]: INIT_CODE_HASH,
155
+ [97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
156
+ };
157
+ var MINIMUM_LIQUIDITY = JSBI2.BigInt(1e3);
158
+ var ZERO = JSBI2.BigInt(0);
159
+ var ONE = JSBI2.BigInt(1);
160
+ var TWO = JSBI2.BigInt(2);
161
+ var THREE = JSBI2.BigInt(3);
162
+ var FIVE = JSBI2.BigInt(5);
163
+ var TEN = JSBI2.BigInt(10);
164
+ var _100 = JSBI2.BigInt(100);
165
+ var _9975 = JSBI2.BigInt(9975);
166
+ var _10000 = JSBI2.BigInt(1e4);
167
+ var MaxUint256 = JSBI2.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
168
+ var SolidityType = /* @__PURE__ */ ((SolidityType2) => {
169
+ SolidityType2["uint8"] = "uint8";
170
+ SolidityType2["uint256"] = "uint256";
171
+ return SolidityType2;
172
+ })(SolidityType || {});
173
+ var SOLIDITY_TYPE_MAXIMA = {
174
+ ["uint8" /* uint8 */]: JSBI2.BigInt("0xff"),
175
+ ["uint256" /* uint256 */]: JSBI2.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
176
+ };
177
+ var WETH9 = {
178
+ [1 /* ETHEREUM */]: new Token(1 /* ETHEREUM */, "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 18, "WETH", "Wrapped Ether", "https://weth.io"),
179
+ [4 /* RINKEBY */]: new Token(4 /* RINKEBY */, "0xc778417E063141139Fce010982780140Aa0cD5Ab", 18, "WETH", "Wrapped Ether", "https://weth.io"),
180
+ [5 /* GOERLI */]: new Token(5 /* GOERLI */, "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", 18, "WETH", "Wrapped Ether", "https://weth.io")
181
+ };
182
+ var WBNB = {
183
+ [1 /* ETHEREUM */]: new Token(1 /* ETHEREUM */, "0x418D75f65a02b3D53B2418FB8E1fe493759c7605", 18, "WBNB", "Wrapped BNB", "https://www.binance.org"),
184
+ [56 /* BSC */]: new Token(56 /* BSC */, "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", 18, "WBNB", "Wrapped BNB", "https://www.binance.org"),
185
+ [97 /* BSC_TESTNET */]: new Token(97 /* BSC_TESTNET */, "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd", 18, "WBNB", "Wrapped BNB", "https://www.binance.org")
186
+ };
187
+ var WNATIVE = {
188
+ [1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
189
+ [4 /* RINKEBY */]: WETH9[4 /* RINKEBY */],
190
+ [5 /* GOERLI */]: WETH9[5 /* GOERLI */],
191
+ [56 /* BSC */]: WBNB[56 /* BSC */],
192
+ [97 /* BSC_TESTNET */]: WBNB[97 /* BSC_TESTNET */]
193
+ };
194
+ var NATIVE = {
195
+ [1 /* ETHEREUM */]: { name: "Ether", symbol: "ETH", decimals: 18 },
196
+ [4 /* RINKEBY */]: { name: "Rinkeby Ether", symbol: "RIN", decimals: 18 },
197
+ [5 /* GOERLI */]: { name: "Goerli Ether", symbol: "GOR", decimals: 18 },
198
+ [56 /* BSC */]: {
199
+ name: "Binance Chain Native Token",
200
+ symbol: "BNB",
201
+ decimals: 18
202
+ },
203
+ [97 /* BSC_TESTNET */]: {
204
+ name: "Binance Chain Native Token",
205
+ symbol: "tBNB",
206
+ decimals: 18
207
+ }
208
+ };
209
+
210
+ // src/errors.ts
211
+ var CAN_SET_PROTOTYPE = "setPrototypeOf" in Object;
212
+ var InsufficientReservesError = class extends Error {
213
+ constructor() {
214
+ super();
215
+ this.isInsufficientReservesError = true;
216
+ this.name = this.constructor.name;
217
+ if (CAN_SET_PROTOTYPE)
218
+ Object.setPrototypeOf(this, new.target.prototype);
219
+ }
220
+ };
221
+ var InsufficientInputAmountError = class extends Error {
222
+ constructor() {
223
+ super();
224
+ this.isInsufficientInputAmountError = true;
225
+ this.name = this.constructor.name;
226
+ if (CAN_SET_PROTOTYPE)
227
+ Object.setPrototypeOf(this, new.target.prototype);
228
+ }
229
+ };
230
+
231
+ // src/entities/pair.ts
232
+ import { getCreate2Address } from "@ethersproject/address";
233
+ import { keccak256, pack } from "@ethersproject/solidity";
234
+ import JSBI7 from "jsbi";
235
+ import invariant7 from "tiny-invariant";
236
+
237
+ // src/entities/fractions/price.ts
238
+ import JSBI5 from "jsbi";
239
+ import invariant6 from "tiny-invariant";
240
+
241
+ // src/entities/fractions/fraction.ts
242
+ import JSBI3 from "jsbi";
243
+ import invariant4 from "tiny-invariant";
244
+ import _Decimal from "decimal.js-light";
245
+ import _Big from "big.js";
246
+ import toFormat from "toformat";
247
+ var Decimal = toFormat(_Decimal);
248
+ var Big = toFormat(_Big);
249
+ var toSignificantRounding = {
250
+ [0 /* ROUND_DOWN */]: Decimal.ROUND_DOWN,
251
+ [1 /* ROUND_HALF_UP */]: Decimal.ROUND_HALF_UP,
252
+ [2 /* ROUND_UP */]: Decimal.ROUND_UP
253
+ };
254
+ var toFixedRounding = {
255
+ [0 /* ROUND_DOWN */]: 0 /* RoundDown */,
256
+ [1 /* ROUND_HALF_UP */]: 1 /* RoundHalfUp */,
257
+ [2 /* ROUND_UP */]: 3 /* RoundUp */
258
+ };
259
+ var Fraction = class {
260
+ constructor(numerator, denominator = JSBI3.BigInt(1)) {
261
+ this.numerator = JSBI3.BigInt(numerator);
262
+ this.denominator = JSBI3.BigInt(denominator);
263
+ }
264
+ static tryParseFraction(fractionish) {
265
+ if (fractionish instanceof JSBI3 || typeof fractionish === "number" || typeof fractionish === "string")
266
+ return new Fraction(fractionish);
267
+ if ("numerator" in fractionish && "denominator" in fractionish)
268
+ return fractionish;
269
+ throw new Error("Could not parse fraction");
270
+ }
271
+ get quotient() {
272
+ return JSBI3.divide(this.numerator, this.denominator);
273
+ }
274
+ get remainder() {
275
+ return new Fraction(JSBI3.remainder(this.numerator, this.denominator), this.denominator);
276
+ }
277
+ invert() {
278
+ return new Fraction(this.denominator, this.numerator);
279
+ }
280
+ add(other) {
281
+ const otherParsed = Fraction.tryParseFraction(other);
282
+ if (JSBI3.equal(this.denominator, otherParsed.denominator)) {
283
+ return new Fraction(JSBI3.add(this.numerator, otherParsed.numerator), this.denominator);
284
+ }
285
+ return new Fraction(JSBI3.add(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator)), JSBI3.multiply(this.denominator, otherParsed.denominator));
286
+ }
287
+ subtract(other) {
288
+ const otherParsed = Fraction.tryParseFraction(other);
289
+ if (JSBI3.equal(this.denominator, otherParsed.denominator)) {
290
+ return new Fraction(JSBI3.subtract(this.numerator, otherParsed.numerator), this.denominator);
291
+ }
292
+ return new Fraction(JSBI3.subtract(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator)), JSBI3.multiply(this.denominator, otherParsed.denominator));
293
+ }
294
+ lessThan(other) {
295
+ const otherParsed = Fraction.tryParseFraction(other);
296
+ return JSBI3.lessThan(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator));
297
+ }
298
+ equalTo(other) {
299
+ const otherParsed = Fraction.tryParseFraction(other);
300
+ return JSBI3.equal(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator));
301
+ }
302
+ greaterThan(other) {
303
+ const otherParsed = Fraction.tryParseFraction(other);
304
+ return JSBI3.greaterThan(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator));
305
+ }
306
+ multiply(other) {
307
+ const otherParsed = Fraction.tryParseFraction(other);
308
+ return new Fraction(JSBI3.multiply(this.numerator, otherParsed.numerator), JSBI3.multiply(this.denominator, otherParsed.denominator));
309
+ }
310
+ divide(other) {
311
+ const otherParsed = Fraction.tryParseFraction(other);
312
+ return new Fraction(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(this.denominator, otherParsed.numerator));
313
+ }
314
+ toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
315
+ invariant4(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
316
+ invariant4(significantDigits > 0, `${significantDigits} is not positive.`);
317
+ Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });
318
+ const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
319
+ return quotient.toFormat(quotient.decimalPlaces(), format);
320
+ }
321
+ toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
322
+ invariant4(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
323
+ invariant4(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
324
+ Big.DP = decimalPlaces;
325
+ Big.RM = toFixedRounding[rounding];
326
+ return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
327
+ }
328
+ get asFraction() {
329
+ return new Fraction(this.numerator, this.denominator);
330
+ }
331
+ };
332
+
333
+ // src/entities/fractions/currencyAmount.ts
334
+ import invariant5 from "tiny-invariant";
335
+ import JSBI4 from "jsbi";
336
+ import _Big2 from "big.js";
337
+ import toFormat2 from "toformat";
338
+ var Big2 = toFormat2(_Big2);
339
+ var CurrencyAmount2 = class extends Fraction {
340
+ constructor(currency, numerator, denominator) {
341
+ super(numerator, denominator);
342
+ invariant5(JSBI4.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT");
343
+ this.currency = currency;
344
+ this.decimalScale = JSBI4.exponentiate(JSBI4.BigInt(10), JSBI4.BigInt(currency.decimals));
345
+ }
346
+ static fromRawAmount(currency, rawAmount) {
347
+ return new CurrencyAmount2(currency, rawAmount);
348
+ }
349
+ static fromFractionalAmount(currency, numerator, denominator) {
350
+ return new CurrencyAmount2(currency, numerator, denominator);
351
+ }
352
+ add(other) {
353
+ invariant5(this.currency.equals(other.currency), "CURRENCY");
354
+ const added = super.add(other);
355
+ return CurrencyAmount2.fromFractionalAmount(this.currency, added.numerator, added.denominator);
356
+ }
357
+ subtract(other) {
358
+ invariant5(this.currency.equals(other.currency), "CURRENCY");
359
+ const subtracted = super.subtract(other);
360
+ return CurrencyAmount2.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
361
+ }
362
+ multiply(other) {
363
+ const multiplied = super.multiply(other);
364
+ return CurrencyAmount2.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
365
+ }
366
+ divide(other) {
367
+ const divided = super.divide(other);
368
+ return CurrencyAmount2.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
369
+ }
370
+ toSignificant(significantDigits = 6, format, rounding = 0 /* ROUND_DOWN */) {
371
+ return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
372
+ }
373
+ toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
374
+ invariant5(decimalPlaces <= this.currency.decimals, "DECIMALS");
375
+ return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
376
+ }
377
+ toExact(format = { groupSeparator: "" }) {
378
+ Big2.DP = this.currency.decimals;
379
+ return new Big2(this.quotient.toString()).div(this.decimalScale.toString()).toFormat(format);
380
+ }
381
+ get wrapped() {
382
+ if (this.currency.isToken)
383
+ return this;
384
+ return CurrencyAmount2.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator);
385
+ }
386
+ };
387
+
388
+ // src/entities/fractions/price.ts
389
+ var Price2 = class extends Fraction {
390
+ constructor(...args) {
391
+ let baseCurrency, quoteCurrency, denominator, numerator;
392
+ if (args.length === 4) {
393
+ ;
394
+ [baseCurrency, quoteCurrency, denominator, numerator] = args;
395
+ } else {
396
+ const result = args[0].quoteAmount.divide(args[0].baseAmount);
397
+ [baseCurrency, quoteCurrency, denominator, numerator] = [
398
+ args[0].baseAmount.currency,
399
+ args[0].quoteAmount.currency,
400
+ result.denominator,
401
+ result.numerator
402
+ ];
403
+ }
404
+ super(numerator, denominator);
405
+ this.baseCurrency = baseCurrency;
406
+ this.quoteCurrency = quoteCurrency;
407
+ this.scalar = new Fraction(JSBI5.exponentiate(JSBI5.BigInt(10), JSBI5.BigInt(baseCurrency.decimals)), JSBI5.exponentiate(JSBI5.BigInt(10), JSBI5.BigInt(quoteCurrency.decimals)));
408
+ }
409
+ invert() {
410
+ return new Price2(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
411
+ }
412
+ multiply(other) {
413
+ invariant6(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
414
+ const fraction = super.multiply(other);
415
+ return new Price2(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
416
+ }
417
+ quote(currencyAmount) {
418
+ invariant6(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
419
+ const result = super.multiply(currencyAmount);
420
+ return CurrencyAmount2.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
421
+ }
422
+ get adjustedForDecimals() {
423
+ return super.multiply(this.scalar);
424
+ }
425
+ toSignificant(significantDigits = 6, format, rounding) {
426
+ return this.adjustedForDecimals.toSignificant(significantDigits, format, rounding);
427
+ }
428
+ toFixed(decimalPlaces = 4, format, rounding) {
429
+ return this.adjustedForDecimals.toFixed(decimalPlaces, format, rounding);
430
+ }
431
+ };
432
+
433
+ // src/entities/fractions/percent.ts
434
+ import JSBI6 from "jsbi";
435
+ var ONE_HUNDRED = new Fraction(JSBI6.BigInt(100));
436
+ function toPercent(fraction) {
437
+ return new Percent(fraction.numerator, fraction.denominator);
438
+ }
439
+ var Percent = class extends Fraction {
440
+ constructor() {
441
+ super(...arguments);
442
+ this.isPercent = true;
443
+ }
444
+ add(other) {
445
+ return toPercent(super.add(other));
446
+ }
447
+ subtract(other) {
448
+ return toPercent(super.subtract(other));
449
+ }
450
+ multiply(other) {
451
+ return toPercent(super.multiply(other));
452
+ }
453
+ divide(other) {
454
+ return toPercent(super.divide(other));
455
+ }
456
+ toSignificant(significantDigits = 5, format, rounding) {
457
+ return super.multiply(ONE_HUNDRED).toSignificant(significantDigits, format, rounding);
458
+ }
459
+ toFixed(decimalPlaces = 2, format, rounding) {
460
+ return super.multiply(ONE_HUNDRED).toFixed(decimalPlaces, format, rounding);
461
+ }
462
+ };
463
+
464
+ // src/entities/pair.ts
465
+ var PAIR_ADDRESS_CACHE = {};
466
+ var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
467
+ var computePairAddress = ({
468
+ factoryAddress,
469
+ tokenA,
470
+ tokenB
471
+ }) => {
472
+ const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
473
+ const key = composeKey(token0, token1);
474
+ if ((PAIR_ADDRESS_CACHE == null ? void 0 : PAIR_ADDRESS_CACHE[key]) === void 0) {
475
+ PAIR_ADDRESS_CACHE = {
476
+ ...PAIR_ADDRESS_CACHE,
477
+ [key]: getCreate2Address(factoryAddress, keccak256(["bytes"], [pack(["address", "address"], [token0.address, token1.address])]), INIT_CODE_HASH_MAP[token0.chainId])
478
+ };
479
+ }
480
+ return PAIR_ADDRESS_CACHE[key];
481
+ };
482
+ var Pair = class {
483
+ static getAddress(tokenA, tokenB) {
484
+ return computePairAddress({ factoryAddress: FACTORY_ADDRESS_MAP[tokenA.chainId], tokenA, tokenB });
485
+ }
486
+ constructor(currencyAmountA, tokenAmountB) {
487
+ const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) ? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
488
+ this.liquidityToken = new Token(tokenAmounts[0].currency.chainId, Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency), 18, "Cake-LP", "Pancake LPs");
489
+ this.tokenAmounts = tokenAmounts;
490
+ }
491
+ involvesToken(token) {
492
+ return token.equals(this.token0) || token.equals(this.token1);
493
+ }
494
+ get token0Price() {
495
+ const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
496
+ return new Price2(this.token0, this.token1, result.denominator, result.numerator);
497
+ }
498
+ get token1Price() {
499
+ const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
500
+ return new Price2(this.token1, this.token0, result.denominator, result.numerator);
501
+ }
502
+ priceOf(token) {
503
+ invariant7(this.involvesToken(token), "TOKEN");
504
+ return token.equals(this.token0) ? this.token0Price : this.token1Price;
505
+ }
506
+ get chainId() {
507
+ return this.token0.chainId;
508
+ }
509
+ get token0() {
510
+ return this.tokenAmounts[0].currency;
511
+ }
512
+ get token1() {
513
+ return this.tokenAmounts[1].currency;
514
+ }
515
+ get reserve0() {
516
+ return this.tokenAmounts[0];
517
+ }
518
+ get reserve1() {
519
+ return this.tokenAmounts[1];
520
+ }
521
+ reserveOf(token) {
522
+ invariant7(this.involvesToken(token), "TOKEN");
523
+ return token.equals(this.token0) ? this.reserve0 : this.reserve1;
524
+ }
525
+ getOutputAmount(inputAmount) {
526
+ invariant7(this.involvesToken(inputAmount.currency), "TOKEN");
527
+ if (JSBI7.equal(this.reserve0.quotient, ZERO) || JSBI7.equal(this.reserve1.quotient, ZERO)) {
528
+ throw new InsufficientReservesError();
529
+ }
530
+ const inputReserve = this.reserveOf(inputAmount.currency);
531
+ const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
532
+ const inputAmountWithFee = JSBI7.multiply(inputAmount.quotient, _9975);
533
+ const numerator = JSBI7.multiply(inputAmountWithFee, outputReserve.quotient);
534
+ const denominator = JSBI7.add(JSBI7.multiply(inputReserve.quotient, _10000), inputAmountWithFee);
535
+ const outputAmount = CurrencyAmount2.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI7.divide(numerator, denominator));
536
+ if (JSBI7.equal(outputAmount.quotient, ZERO)) {
537
+ throw new InsufficientInputAmountError();
538
+ }
539
+ return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
540
+ }
541
+ getInputAmount(outputAmount) {
542
+ invariant7(this.involvesToken(outputAmount.currency), "TOKEN");
543
+ if (JSBI7.equal(this.reserve0.quotient, ZERO) || JSBI7.equal(this.reserve1.quotient, ZERO) || JSBI7.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient)) {
544
+ throw new InsufficientReservesError();
545
+ }
546
+ const outputReserve = this.reserveOf(outputAmount.currency);
547
+ const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
548
+ const numerator = JSBI7.multiply(JSBI7.multiply(inputReserve.quotient, outputAmount.quotient), _10000);
549
+ const denominator = JSBI7.multiply(JSBI7.subtract(outputReserve.quotient, outputAmount.quotient), _9975);
550
+ const inputAmount = CurrencyAmount2.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI7.add(JSBI7.divide(numerator, denominator), ONE));
551
+ return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
552
+ }
553
+ getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
554
+ invariant7(totalSupply.currency.equals(this.liquidityToken), "LIQUIDITY");
555
+ const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
556
+ invariant7(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
557
+ let liquidity;
558
+ if (JSBI7.equal(totalSupply.quotient, ZERO)) {
559
+ liquidity = JSBI7.subtract(sqrt(JSBI7.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), MINIMUM_LIQUIDITY);
560
+ } else {
561
+ const amount0 = JSBI7.divide(JSBI7.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
562
+ const amount1 = JSBI7.divide(JSBI7.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
563
+ liquidity = JSBI7.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
564
+ }
565
+ if (!JSBI7.greaterThan(liquidity, ZERO)) {
566
+ throw new InsufficientInputAmountError();
567
+ }
568
+ return CurrencyAmount2.fromRawAmount(this.liquidityToken, liquidity);
569
+ }
570
+ getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
571
+ invariant7(this.involvesToken(token), "TOKEN");
572
+ invariant7(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
573
+ invariant7(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
574
+ invariant7(JSBI7.lessThanOrEqual(liquidity.quotient, totalSupply.quotient), "LIQUIDITY");
575
+ let totalSupplyAdjusted;
576
+ if (!feeOn) {
577
+ totalSupplyAdjusted = totalSupply;
578
+ } else {
579
+ invariant7(!!kLast, "K_LAST");
580
+ const kLastParsed = JSBI7.BigInt(kLast);
581
+ if (!JSBI7.equal(kLastParsed, ZERO)) {
582
+ const rootK = sqrt(JSBI7.multiply(this.reserve0.quotient, this.reserve1.quotient));
583
+ const rootKLast = sqrt(kLastParsed);
584
+ if (JSBI7.greaterThan(rootK, rootKLast)) {
585
+ const numerator = JSBI7.multiply(totalSupply.quotient, JSBI7.subtract(rootK, rootKLast));
586
+ const denominator = JSBI7.add(JSBI7.multiply(rootK, FIVE), rootKLast);
587
+ const feeLiquidity = JSBI7.divide(numerator, denominator);
588
+ totalSupplyAdjusted = totalSupply.add(CurrencyAmount2.fromRawAmount(this.liquidityToken, feeLiquidity));
589
+ } else {
590
+ totalSupplyAdjusted = totalSupply;
591
+ }
592
+ } else {
593
+ totalSupplyAdjusted = totalSupply;
594
+ }
595
+ }
596
+ return CurrencyAmount2.fromRawAmount(token, JSBI7.divide(JSBI7.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
597
+ }
598
+ };
599
+
600
+ // src/entities/route.ts
601
+ import invariant8 from "tiny-invariant";
602
+ var Route = class {
603
+ constructor(pairs, input, output) {
604
+ this._midPrice = null;
605
+ invariant8(pairs.length > 0, "PAIRS");
606
+ const chainId = pairs[0].chainId;
607
+ invariant8(pairs.every((pair) => pair.chainId === chainId), "CHAIN_IDS");
608
+ const wrappedInput = input.wrapped;
609
+ invariant8(pairs[0].involvesToken(wrappedInput), "INPUT");
610
+ invariant8(typeof output === "undefined" || pairs[pairs.length - 1].involvesToken(output.wrapped), "OUTPUT");
611
+ const path = [wrappedInput];
612
+ for (const [i, pair] of pairs.entries()) {
613
+ const currentInput = path[i];
614
+ invariant8(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), "PATH");
615
+ const output2 = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
616
+ path.push(output2);
617
+ }
618
+ this.pairs = pairs;
619
+ this.path = path;
620
+ this.input = input;
621
+ this.output = output;
622
+ }
623
+ get midPrice() {
624
+ if (this._midPrice !== null)
625
+ return this._midPrice;
626
+ const prices = [];
627
+ for (const [i, pair] of this.pairs.entries()) {
628
+ prices.push(this.path[i].equals(pair.token0) ? new Price2(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient) : new Price2(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient));
629
+ }
630
+ const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
631
+ return this._midPrice = new Price2(this.input, this.output, reduced.denominator, reduced.numerator);
632
+ }
633
+ get chainId() {
634
+ return this.pairs[0].chainId;
635
+ }
636
+ };
637
+
638
+ // src/entities/trade.ts
639
+ import invariant9 from "tiny-invariant";
640
+ function inputOutputComparator(a, b) {
641
+ invariant9(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
642
+ invariant9(a.outputAmount.currency.equals(b.outputAmount.currency), "OUTPUT_CURRENCY");
643
+ if (a.outputAmount.equalTo(b.outputAmount)) {
644
+ if (a.inputAmount.equalTo(b.inputAmount)) {
645
+ return 0;
646
+ }
647
+ if (a.inputAmount.lessThan(b.inputAmount)) {
648
+ return -1;
649
+ } else {
650
+ return 1;
651
+ }
652
+ } else {
653
+ if (a.outputAmount.lessThan(b.outputAmount)) {
654
+ return 1;
655
+ } else {
656
+ return -1;
657
+ }
658
+ }
659
+ }
660
+ function tradeComparator(a, b) {
661
+ const ioComp = inputOutputComparator(a, b);
662
+ if (ioComp !== 0) {
663
+ return ioComp;
664
+ }
665
+ if (a.priceImpact.lessThan(b.priceImpact)) {
666
+ return -1;
667
+ } else if (a.priceImpact.greaterThan(b.priceImpact)) {
668
+ return 1;
669
+ }
670
+ return a.route.path.length - b.route.path.length;
671
+ }
672
+ var Trade = class {
673
+ static exactIn(route, amountIn) {
674
+ return new Trade(route, amountIn, 0 /* EXACT_INPUT */);
675
+ }
676
+ static exactOut(route, amountOut) {
677
+ return new Trade(route, amountOut, 1 /* EXACT_OUTPUT */);
678
+ }
679
+ constructor(route, amount, tradeType) {
680
+ this.route = route;
681
+ this.tradeType = tradeType;
682
+ const tokenAmounts = new Array(route.path.length);
683
+ if (tradeType === 0 /* EXACT_INPUT */) {
684
+ invariant9(amount.currency.equals(route.input), "INPUT");
685
+ tokenAmounts[0] = amount.wrapped;
686
+ for (let i = 0; i < route.path.length - 1; i++) {
687
+ const pair = route.pairs[i];
688
+ const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
689
+ tokenAmounts[i + 1] = outputAmount;
690
+ }
691
+ this.inputAmount = CurrencyAmount2.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
692
+ this.outputAmount = CurrencyAmount2.fromFractionalAmount(route.output, tokenAmounts[tokenAmounts.length - 1].numerator, tokenAmounts[tokenAmounts.length - 1].denominator);
693
+ } else {
694
+ invariant9(amount.currency.equals(route.output), "OUTPUT");
695
+ tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
696
+ for (let i = route.path.length - 1; i > 0; i--) {
697
+ const pair = route.pairs[i - 1];
698
+ const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
699
+ tokenAmounts[i - 1] = inputAmount;
700
+ }
701
+ this.inputAmount = CurrencyAmount2.fromFractionalAmount(route.input, tokenAmounts[0].numerator, tokenAmounts[0].denominator);
702
+ this.outputAmount = CurrencyAmount2.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
703
+ }
704
+ this.executionPrice = new Price2(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
705
+ this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
706
+ }
707
+ minimumAmountOut(slippageTolerance) {
708
+ invariant9(!slippageTolerance.lessThan(ZERO), "SLIPPAGE_TOLERANCE");
709
+ if (this.tradeType === 1 /* EXACT_OUTPUT */) {
710
+ return this.outputAmount;
711
+ } else {
712
+ const slippageAdjustedAmountOut = new Fraction(ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
713
+ return CurrencyAmount2.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
714
+ }
715
+ }
716
+ maximumAmountIn(slippageTolerance) {
717
+ invariant9(!slippageTolerance.lessThan(ZERO), "SLIPPAGE_TOLERANCE");
718
+ if (this.tradeType === 0 /* EXACT_INPUT */) {
719
+ return this.inputAmount;
720
+ } else {
721
+ const slippageAdjustedAmountIn = new Fraction(ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
722
+ return CurrencyAmount2.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
723
+ }
724
+ }
725
+ static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
726
+ invariant9(pairs.length > 0, "PAIRS");
727
+ invariant9(maxHops > 0, "MAX_HOPS");
728
+ invariant9(currencyAmountIn === nextAmountIn || currentPairs.length > 0, "INVALID_RECURSION");
729
+ const amountIn = nextAmountIn.wrapped;
730
+ const tokenOut = currencyOut.wrapped;
731
+ for (let i = 0; i < pairs.length; i++) {
732
+ const pair = pairs[i];
733
+ if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
734
+ continue;
735
+ if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO))
736
+ continue;
737
+ let amountOut;
738
+ try {
739
+ ;
740
+ [amountOut] = pair.getOutputAmount(amountIn);
741
+ } catch (error) {
742
+ if (error.isInsufficientInputAmountError) {
743
+ continue;
744
+ }
745
+ throw error;
746
+ }
747
+ if (amountOut.currency.equals(tokenOut)) {
748
+ sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn, 0 /* EXACT_INPUT */), maxNumResults, tradeComparator);
749
+ } else if (maxHops > 1 && pairs.length > 1) {
750
+ const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
751
+ Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
752
+ maxNumResults,
753
+ maxHops: maxHops - 1
754
+ }, [...currentPairs, pair], amountOut, bestTrades);
755
+ }
756
+ }
757
+ return bestTrades;
758
+ }
759
+ worstExecutionPrice(slippageTolerance) {
760
+ return new Price2(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
761
+ }
762
+ static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
763
+ invariant9(pairs.length > 0, "PAIRS");
764
+ invariant9(maxHops > 0, "MAX_HOPS");
765
+ invariant9(currencyAmountOut === nextAmountOut || currentPairs.length > 0, "INVALID_RECURSION");
766
+ const amountOut = nextAmountOut.wrapped;
767
+ const tokenIn = currencyIn.wrapped;
768
+ for (let i = 0; i < pairs.length; i++) {
769
+ const pair = pairs[i];
770
+ if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
771
+ continue;
772
+ if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO))
773
+ continue;
774
+ let amountIn;
775
+ try {
776
+ ;
777
+ [amountIn] = pair.getInputAmount(amountOut);
778
+ } catch (error) {
779
+ if (error.isInsufficientReservesError) {
780
+ continue;
781
+ }
782
+ throw error;
783
+ }
784
+ if (amountIn.currency.equals(tokenIn)) {
785
+ sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut, 1 /* EXACT_OUTPUT */), maxNumResults, tradeComparator);
786
+ } else if (maxHops > 1 && pairs.length > 1) {
787
+ const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
788
+ Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
789
+ maxNumResults,
790
+ maxHops: maxHops - 1
791
+ }, [pair, ...currentPairs], amountIn, bestTrades);
792
+ }
793
+ }
794
+ return bestTrades;
795
+ }
796
+ };
797
+
798
+ // src/entities/nativeCurrency.ts
799
+ var NativeCurrency = class extends BaseCurrency {
800
+ constructor() {
801
+ super(...arguments);
802
+ this.isNative = true;
803
+ this.isToken = false;
804
+ }
805
+ };
806
+
807
+ // src/entities/native.ts
808
+ import invariant10 from "tiny-invariant";
809
+ var _Native = class extends NativeCurrency {
810
+ constructor({
811
+ chainId,
812
+ decimals,
813
+ name,
814
+ symbol
815
+ }) {
816
+ super(chainId, decimals, symbol, name);
817
+ }
818
+ get wrapped() {
819
+ const wnative = WNATIVE[this.chainId];
820
+ invariant10(!!wnative, "WRAPPED");
821
+ return wnative;
822
+ }
823
+ static onChain(chainId) {
824
+ if (chainId in this.cache) {
825
+ return this.cache[chainId];
826
+ }
827
+ invariant10(!!NATIVE[chainId], "NATIVE_CURRENCY");
828
+ const { decimals, name, symbol } = NATIVE[chainId];
829
+ return this.cache[chainId] = new _Native({ chainId, decimals, symbol, name });
830
+ }
831
+ equals(other) {
832
+ return other.isNative && other.chainId === this.chainId;
833
+ }
834
+ };
835
+ var Native = _Native;
836
+ Native.cache = {};
837
+
838
+ // src/router.ts
839
+ import invariant11 from "tiny-invariant";
840
+ function toHex(currencyAmount) {
841
+ return `0x${currencyAmount.quotient.toString(16)}`;
842
+ }
843
+ var ZERO_HEX = "0x0";
844
+ var Router = class {
845
+ constructor() {
846
+ }
847
+ static swapCallParameters(trade, options) {
848
+ const etherIn = trade.inputAmount.currency.isNative;
849
+ const etherOut = trade.outputAmount.currency.isNative;
850
+ invariant11(!(etherIn && etherOut), "ETHER_IN_OUT");
851
+ invariant11(!("ttl" in options) || options.ttl > 0, "TTL");
852
+ const to = validateAndParseAddress(options.recipient);
853
+ const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
854
+ const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
855
+ const path = trade.route.path.map((token) => token.address);
856
+ const deadline = "ttl" in options ? `0x${(Math.floor(new Date().getTime() / 1e3) + options.ttl).toString(16)}` : `0x${options.deadline.toString(16)}`;
857
+ const useFeeOnTransfer = Boolean(options.feeOnTransfer);
858
+ let methodName;
859
+ let args;
860
+ let value;
861
+ switch (trade.tradeType) {
862
+ case 0 /* EXACT_INPUT */:
863
+ if (etherIn) {
864
+ methodName = useFeeOnTransfer ? "swapExactETHForTokensSupportingFeeOnTransferTokens" : "swapExactETHForTokens";
865
+ args = [amountOut, path, to, deadline];
866
+ value = amountIn;
867
+ } else if (etherOut) {
868
+ methodName = useFeeOnTransfer ? "swapExactTokensForETHSupportingFeeOnTransferTokens" : "swapExactTokensForETH";
869
+ args = [amountIn, amountOut, path, to, deadline];
870
+ value = ZERO_HEX;
871
+ } else {
872
+ methodName = useFeeOnTransfer ? "swapExactTokensForTokensSupportingFeeOnTransferTokens" : "swapExactTokensForTokens";
873
+ args = [amountIn, amountOut, path, to, deadline];
874
+ value = ZERO_HEX;
875
+ }
876
+ break;
877
+ case 1 /* EXACT_OUTPUT */:
878
+ invariant11(!useFeeOnTransfer, "EXACT_OUT_FOT");
879
+ if (etherIn) {
880
+ methodName = "swapETHForExactTokens";
881
+ args = [amountOut, path, to, deadline];
882
+ value = amountIn;
883
+ } else if (etherOut) {
884
+ methodName = "swapTokensForExactETH";
885
+ args = [amountOut, amountIn, path, to, deadline];
886
+ value = ZERO_HEX;
887
+ } else {
888
+ methodName = "swapTokensForExactTokens";
889
+ args = [amountOut, amountIn, path, to, deadline];
890
+ value = ZERO_HEX;
891
+ }
892
+ break;
893
+ }
894
+ return {
895
+ methodName,
896
+ args,
897
+ value
898
+ };
899
+ }
900
+ };
901
+ export {
902
+ BaseCurrency,
903
+ ChainId,
904
+ CurrencyAmount2 as CurrencyAmount,
905
+ FACTORY_ADDRESS,
906
+ FACTORY_ADDRESS_MAP,
907
+ FIVE,
908
+ Fraction,
909
+ INIT_CODE_HASH,
910
+ INIT_CODE_HASH_MAP,
911
+ InsufficientInputAmountError,
912
+ InsufficientReservesError,
913
+ JSBI8 as JSBI,
914
+ MINIMUM_LIQUIDITY,
915
+ MaxUint256,
916
+ NATIVE,
917
+ Native,
918
+ NativeCurrency,
919
+ ONE,
920
+ Pair,
921
+ Percent,
922
+ Price2 as Price,
923
+ Rounding,
924
+ Route,
925
+ Router,
926
+ SOLIDITY_TYPE_MAXIMA,
927
+ SolidityType,
928
+ TEN,
929
+ THREE,
930
+ TWO,
931
+ Token,
932
+ Trade,
933
+ TradeType,
934
+ WBNB,
935
+ WETH9,
936
+ WNATIVE,
937
+ ZERO,
938
+ _100,
939
+ _10000,
940
+ _9975,
941
+ computePairAddress,
942
+ computePriceImpact,
943
+ inputOutputComparator,
944
+ tradeComparator
945
+ };