@pancakeswap/sdk 5.8.2 → 5.8.3

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.mjs CHANGED
@@ -1,29 +1,11 @@
1
- import { ChainId } from '@pancakeswap/chains';
2
- export { ChainId } from '@pancakeswap/chains';
3
- import { Percent, Token, BaseCurrency, Price, ZERO, InsufficientReservesError, _9975, _10000, CurrencyAmount, InsufficientInputAmountError, ONE, sqrt, MINIMUM_LIQUIDITY, TradeType, computePriceImpact, Fraction, sortedInsert, NativeCurrency, FIVE } from '@pancakeswap/swap-sdk-core';
1
+ import { BaseCurrency } from '@pancakeswap/swap-sdk-core';
4
2
  export * from '@pancakeswap/swap-sdk-core';
5
- import invariant5 from 'tiny-invariant';
6
- import { createPublicClient, http, getAddress, keccak256, encodePacked, getContract, toBytes, pad, isBytes, slice, concat } from 'viem';
7
- import warning from 'tiny-warning';
8
- import { mainnet, bsc, bscTestnet, goerli } from 'viem/chains';
9
-
10
- // src/constants.ts
11
- function validateAndParseAddress(address) {
12
- try {
13
- const checksummedAddress = getAddress(address);
14
- warning(address === checksummedAddress, `${address} is not checksummed.`);
15
- return checksummedAddress;
16
- } catch (error) {
17
- invariant5(false, `${address} is not a valid address.`);
18
- }
19
- }
3
+ export * from '@pancakeswap/v2-sdk';
4
+ export * from '@pancakeswap/swap-sdk-evm';
5
+ export { ChainId } from '@pancakeswap/chains';
6
+ import invariant from 'tiny-invariant';
20
7
 
21
- // src/entities/token.ts
22
- var ERC20Token = class extends Token {
23
- constructor(chainId, address, decimals, symbol, name, projectLink) {
24
- super(chainId, validateAndParseAddress(address), decimals, symbol, name, projectLink);
25
- }
26
- };
8
+ // src/index.ts
27
9
  var OnRampCurrency = class extends BaseCurrency {
28
10
  constructor(chainId, address, decimals, symbol, name, projectLink) {
29
11
  super(chainId, decimals, symbol, name);
@@ -48,8 +30,8 @@ var OnRampCurrency = class extends BaseCurrency {
48
30
  sortsBefore(other) {
49
31
  if (!other.isToken)
50
32
  return false;
51
- invariant5(this.chainId === other.chainId, "CHAIN_IDS");
52
- invariant5(this.address !== other.address, "ADDRESSES");
33
+ invariant(this.chainId === other.chainId, "CHAIN_IDS");
34
+ invariant(this.address !== other.address, "ADDRESSES");
53
35
  return this.address.toLowerCase() < other.address.toLowerCase();
54
36
  }
55
37
  /**
@@ -70,1758 +52,4 @@ var OnRampCurrency = class extends BaseCurrency {
70
52
  }
71
53
  };
72
54
 
73
- // src/constants.ts
74
- var ZERO_PERCENT = new Percent("0");
75
- var ONE_HUNDRED_PERCENT = new Percent("1");
76
- var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
77
- var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
78
- var FACTORY_ADDRESS_MAP = {
79
- [ChainId.ETHEREUM]: FACTORY_ADDRESS_ETH,
80
- [ChainId.GOERLI]: FACTORY_ADDRESS_ETH,
81
- [ChainId.BSC]: FACTORY_ADDRESS,
82
- [ChainId.BSC_TESTNET]: "0x6725F303b657a9451d8BA641348b6761A6CC7a17",
83
- [ChainId.ARBITRUM_ONE]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
84
- [ChainId.ARBITRUM_GOERLI]: "0x333EAE459075b1d7dE8eb57997b5d4eee5F1070a",
85
- [ChainId.POLYGON_ZKEVM]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
86
- [ChainId.POLYGON_ZKEVM_TESTNET]: "0xBA40c83026213F9cbc79998752721a0312bdB74a",
87
- [ChainId.ZKSYNC]: "0xd03D8D566183F0086d8D09A84E1e30b58Dd5619d",
88
- [ChainId.ZKSYNC_TESTNET]: "0x48a33610Cd0E130af2024D55F67aE72a8C51aC27",
89
- [ChainId.LINEA]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
90
- [ChainId.LINEA_TESTNET]: "0xB6FAfd4ADbCd21cF665909767e0eD0D05709abfB",
91
- [ChainId.OPBNB]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
92
- [ChainId.OPBNB_TESTNET]: "0x776e4bD2f72de2176A59465e316335aaf8ed4E8F",
93
- [ChainId.BASE]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
94
- [ChainId.BASE_TESTNET]: "0x715303D2eF7dA7FFAbF637651D71FD11d41fAf7F",
95
- [ChainId.SCROLL_SEPOLIA]: "0x2B3C5df29F73dbF028BA82C33e0A5A6e5800F75e",
96
- [ChainId.SEPOLIA]: "0x1bdc540dEB9Ed1fA29964DeEcCc524A8f5e2198e",
97
- [ChainId.ARBITRUM_SEPOLIA]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E",
98
- [ChainId.BASE_SEPOLIA]: "0x02a84c1b3BBD7401a5f7fa98a384EBC70bB5749E"
99
- };
100
- var INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5";
101
- var INIT_CODE_HASH_ETH = "0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d";
102
- var INIT_CODE_HASH_MAP = {
103
- [ChainId.ETHEREUM]: INIT_CODE_HASH_ETH,
104
- [ChainId.GOERLI]: INIT_CODE_HASH_ETH,
105
- [ChainId.BSC]: INIT_CODE_HASH,
106
- [ChainId.BSC_TESTNET]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66",
107
- [ChainId.ARBITRUM_ONE]: INIT_CODE_HASH_ETH,
108
- [ChainId.ARBITRUM_GOERLI]: INIT_CODE_HASH_ETH,
109
- [ChainId.POLYGON_ZKEVM]: INIT_CODE_HASH_ETH,
110
- [ChainId.POLYGON_ZKEVM_TESTNET]: INIT_CODE_HASH_ETH,
111
- [ChainId.ZKSYNC]: "0x0100045707a42494392b3558029b9869f865ff9df8f375dc1bf20b0555093f43",
112
- [ChainId.ZKSYNC_TESTNET]: "0x0100045707a42494392b3558029b9869f865ff9df8f375dc1bf20b0555093f43",
113
- [ChainId.LINEA]: INIT_CODE_HASH_ETH,
114
- [ChainId.LINEA_TESTNET]: INIT_CODE_HASH_ETH,
115
- [ChainId.OPBNB]: INIT_CODE_HASH_ETH,
116
- [ChainId.OPBNB_TESTNET]: INIT_CODE_HASH_ETH,
117
- [ChainId.BASE]: INIT_CODE_HASH_ETH,
118
- [ChainId.BASE_TESTNET]: "0xa5934690703a592a07e841ca29d5e5c79b5e22ed4749057bb216dc31100be1c0",
119
- [ChainId.SCROLL_SEPOLIA]: INIT_CODE_HASH_ETH,
120
- [ChainId.SEPOLIA]: INIT_CODE_HASH_ETH,
121
- [ChainId.ARBITRUM_SEPOLIA]: INIT_CODE_HASH_ETH,
122
- [ChainId.BASE_SEPOLIA]: INIT_CODE_HASH_ETH
123
- };
124
- var WETH9 = {
125
- [ChainId.ETHEREUM]: new ERC20Token(
126
- ChainId.ETHEREUM,
127
- "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
128
- 18,
129
- "WETH",
130
- "Wrapped Ether",
131
- "https://weth.io"
132
- ),
133
- [ChainId.GOERLI]: new ERC20Token(
134
- ChainId.GOERLI,
135
- "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6",
136
- 18,
137
- "WETH",
138
- "Wrapped Ether",
139
- "https://weth.io"
140
- ),
141
- [ChainId.BSC]: new ERC20Token(
142
- ChainId.BSC,
143
- "0x2170Ed0880ac9A755fd29B2688956BD959F933F8",
144
- 18,
145
- "ETH",
146
- "Binance-Peg Ethereum Token",
147
- "https://ethereum.org"
148
- ),
149
- [ChainId.BSC_TESTNET]: new ERC20Token(
150
- ChainId.BSC,
151
- "0xE7bCB9e341D546b66a46298f4893f5650a56e99E",
152
- 18,
153
- "ETH",
154
- "ETH",
155
- "https://ethereum.org"
156
- ),
157
- [ChainId.ARBITRUM_ONE]: new ERC20Token(
158
- ChainId.ARBITRUM_ONE,
159
- "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
160
- 18,
161
- "WETH",
162
- "Wrapped Ether",
163
- "https://weth.io"
164
- ),
165
- [ChainId.ARBITRUM_GOERLI]: new ERC20Token(
166
- ChainId.ARBITRUM_GOERLI,
167
- "0xEe01c0CD76354C383B8c7B4e65EA88D00B06f36f",
168
- 18,
169
- "WETH",
170
- "Wrapped Ether",
171
- "https://weth.io"
172
- ),
173
- [ChainId.ZKSYNC]: new ERC20Token(
174
- ChainId.ZKSYNC,
175
- "0x5AEa5775959fBC2557Cc8789bC1bf90A239D9a91",
176
- 18,
177
- "WETH",
178
- "Wrapped Ether",
179
- "https://weth.io"
180
- ),
181
- [ChainId.ZKSYNC_TESTNET]: new ERC20Token(
182
- ChainId.ZKSYNC_TESTNET,
183
- "0x02968DB286f24cB18bB5b24903eC8eBFAcf591C0",
184
- 18,
185
- "WETH",
186
- "Wrapped Ether",
187
- "https://weth.io"
188
- ),
189
- [ChainId.POLYGON_ZKEVM]: new ERC20Token(
190
- ChainId.POLYGON_ZKEVM,
191
- "0x4F9A0e7FD2Bf6067db6994CF12E4495Df938E6e9",
192
- 18,
193
- "WETH",
194
- "Wrapped Ether",
195
- "https://weth.io"
196
- ),
197
- [ChainId.POLYGON_ZKEVM_TESTNET]: new ERC20Token(
198
- ChainId.POLYGON_ZKEVM_TESTNET,
199
- "0x30ec47F7DFae72eA79646e6cf64a8A7db538915b",
200
- 18,
201
- "WETH",
202
- "Wrapped Ether",
203
- "https://weth.io"
204
- ),
205
- [ChainId.LINEA]: new ERC20Token(
206
- ChainId.LINEA,
207
- "0xe5D7C2a44FfDDf6b295A15c148167daaAf5Cf34f",
208
- 18,
209
- "WETH",
210
- "Wrapped Ether",
211
- "https://weth.io"
212
- ),
213
- [ChainId.LINEA_TESTNET]: new ERC20Token(
214
- ChainId.LINEA_TESTNET,
215
- "0x2C1b868d6596a18e32E61B901E4060C872647b6C",
216
- 18,
217
- "WETH",
218
- "Wrapped Ether",
219
- "https://weth.io"
220
- ),
221
- [ChainId.OPBNB_TESTNET]: new ERC20Token(
222
- ChainId.OPBNB_TESTNET,
223
- "0x584f7b986d9942B0859a1E6921efA5342A673d04",
224
- 18,
225
- "WETH",
226
- "Wrapped Ether",
227
- "https://weth.io"
228
- ),
229
- [ChainId.OPBNB]: new ERC20Token(
230
- ChainId.OPBNB,
231
- "0xE7798f023fC62146e8Aa1b36Da45fb70855a77Ea",
232
- 18,
233
- "ETH",
234
- "Binance-Peg Ethereum Token",
235
- "https://ethereum.org"
236
- ),
237
- [ChainId.BASE]: new ERC20Token(
238
- ChainId.BASE,
239
- "0x4200000000000000000000000000000000000006",
240
- 18,
241
- "WETH",
242
- "Wrapped Ether",
243
- "https://weth.io"
244
- ),
245
- [ChainId.BASE_TESTNET]: new ERC20Token(
246
- ChainId.BASE_TESTNET,
247
- "0x4200000000000000000000000000000000000006",
248
- 18,
249
- "WETH",
250
- "Wrapped Ether",
251
- "https://weth.io"
252
- ),
253
- [ChainId.SCROLL_SEPOLIA]: new ERC20Token(
254
- ChainId.SCROLL_SEPOLIA,
255
- "0x5300000000000000000000000000000000000004",
256
- 18,
257
- "WETH",
258
- "Wrapped Ether",
259
- "https://weth.io"
260
- ),
261
- [ChainId.SEPOLIA]: new ERC20Token(
262
- ChainId.SEPOLIA,
263
- "0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14",
264
- 18,
265
- "WETH",
266
- "Wrapped Ether",
267
- "https://weth.io"
268
- ),
269
- [ChainId.ARBITRUM_SEPOLIA]: new ERC20Token(
270
- ChainId.ARBITRUM_SEPOLIA,
271
- "0x1bdc540dEB9Ed1fA29964DeEcCc524A8f5e2198e",
272
- 18,
273
- "WETH",
274
- "Wrapped Ether",
275
- "https://weth.io"
276
- ),
277
- [ChainId.BASE_SEPOLIA]: new ERC20Token(
278
- ChainId.BASE_SEPOLIA,
279
- "0x4200000000000000000000000000000000000006",
280
- 18,
281
- "WETH",
282
- "Wrapped Ether",
283
- "https://weth.io"
284
- )
285
- };
286
- var WBNB = {
287
- [ChainId.ETHEREUM]: new ERC20Token(
288
- ChainId.ETHEREUM,
289
- "0x418D75f65a02b3D53B2418FB8E1fe493759c7605",
290
- 18,
291
- "WBNB",
292
- "Wrapped BNB",
293
- "https://www.binance.org"
294
- ),
295
- [ChainId.BSC]: new ERC20Token(
296
- ChainId.BSC,
297
- "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
298
- 18,
299
- "WBNB",
300
- "Wrapped BNB",
301
- "https://www.binance.org"
302
- ),
303
- [ChainId.BSC_TESTNET]: new ERC20Token(
304
- ChainId.BSC_TESTNET,
305
- "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd",
306
- 18,
307
- "WBNB",
308
- "Wrapped BNB",
309
- "https://www.binance.org"
310
- ),
311
- [ChainId.OPBNB_TESTNET]: new ERC20Token(
312
- ChainId.OPBNB_TESTNET,
313
- "0x4200000000000000000000000000000000000006",
314
- 18,
315
- "WBNB",
316
- "Wrapped BNB",
317
- "https://www.binance.org"
318
- ),
319
- [ChainId.OPBNB]: new ERC20Token(
320
- ChainId.OPBNB,
321
- "0x4200000000000000000000000000000000000006",
322
- 18,
323
- "WBNB",
324
- "Wrapped BNB",
325
- "https://www.binance.org"
326
- )
327
- };
328
- var WNATIVE = {
329
- [ChainId.ETHEREUM]: WETH9[ChainId.ETHEREUM],
330
- [ChainId.GOERLI]: WETH9[ChainId.GOERLI],
331
- [ChainId.BSC]: WBNB[ChainId.BSC],
332
- [ChainId.BSC_TESTNET]: WBNB[ChainId.BSC_TESTNET],
333
- [ChainId.ARBITRUM_ONE]: WETH9[ChainId.ARBITRUM_ONE],
334
- [ChainId.ARBITRUM_GOERLI]: WETH9[ChainId.ARBITRUM_GOERLI],
335
- [ChainId.POLYGON_ZKEVM]: WETH9[ChainId.POLYGON_ZKEVM],
336
- [ChainId.POLYGON_ZKEVM_TESTNET]: WETH9[ChainId.POLYGON_ZKEVM_TESTNET],
337
- [ChainId.ZKSYNC]: WETH9[ChainId.ZKSYNC],
338
- [ChainId.ZKSYNC_TESTNET]: WETH9[ChainId.ZKSYNC_TESTNET],
339
- [ChainId.LINEA]: WETH9[ChainId.LINEA],
340
- [ChainId.LINEA_TESTNET]: WETH9[ChainId.LINEA_TESTNET],
341
- [ChainId.OPBNB_TESTNET]: WBNB[ChainId.OPBNB_TESTNET],
342
- [ChainId.OPBNB]: WBNB[ChainId.OPBNB],
343
- [ChainId.BASE]: WETH9[ChainId.BASE],
344
- [ChainId.BASE_TESTNET]: WETH9[ChainId.BASE_TESTNET],
345
- [ChainId.SCROLL_SEPOLIA]: WETH9[ChainId.SCROLL_SEPOLIA],
346
- [ChainId.SEPOLIA]: WETH9[ChainId.SEPOLIA],
347
- [ChainId.ARBITRUM_SEPOLIA]: WETH9[ChainId.ARBITRUM_SEPOLIA],
348
- [ChainId.BASE_SEPOLIA]: WETH9[ChainId.BASE_SEPOLIA]
349
- };
350
- var ETHER = { name: "Ether", symbol: "ETH", decimals: 18 };
351
- var BNB = {
352
- name: "Binance Chain Native Token",
353
- symbol: "BNB",
354
- decimals: 18
355
- };
356
- var NATIVE = {
357
- [ChainId.ETHEREUM]: ETHER,
358
- [ChainId.GOERLI]: { name: "Goerli Ether", symbol: "GOR", decimals: 18 },
359
- [ChainId.BSC]: BNB,
360
- [ChainId.BSC_TESTNET]: {
361
- name: "Binance Chain Native Token",
362
- symbol: "tBNB",
363
- decimals: 18
364
- },
365
- [ChainId.ARBITRUM_ONE]: ETHER,
366
- [ChainId.ARBITRUM_GOERLI]: {
367
- name: "Arbitrum Goerli Ether",
368
- symbol: "AGOR",
369
- decimals: 18
370
- },
371
- [ChainId.POLYGON_ZKEVM]: ETHER,
372
- [ChainId.POLYGON_ZKEVM_TESTNET]: ETHER,
373
- [ChainId.ZKSYNC]: ETHER,
374
- [ChainId.ZKSYNC_TESTNET]: ETHER,
375
- [ChainId.LINEA]: ETHER,
376
- [ChainId.LINEA_TESTNET]: ETHER,
377
- [ChainId.OPBNB]: BNB,
378
- [ChainId.OPBNB_TESTNET]: {
379
- name: "Binance Chain Native Token",
380
- symbol: "tBNB",
381
- decimals: 18
382
- },
383
- [ChainId.BASE]: ETHER,
384
- [ChainId.BASE_TESTNET]: ETHER,
385
- [ChainId.SCROLL_SEPOLIA]: ETHER,
386
- [ChainId.SEPOLIA]: {
387
- name: "Sepolia Ether",
388
- symbol: "SEP",
389
- decimals: 18
390
- },
391
- [ChainId.ARBITRUM_SEPOLIA]: {
392
- name: "Arbitrum Sepolia Ether",
393
- symbol: "ETH",
394
- decimals: 18
395
- },
396
- [ChainId.BASE_SEPOLIA]: {
397
- name: "Sepolia Ether",
398
- symbol: "ETH",
399
- decimals: 18
400
- }
401
- };
402
-
403
- // src/trade.ts
404
- function isTradeBetter(tradeA, tradeB, minimumDelta = ZERO_PERCENT) {
405
- if (tradeA && !tradeB)
406
- return false;
407
- if (tradeB && !tradeA)
408
- return true;
409
- if (!tradeA || !tradeB)
410
- return void 0;
411
- if (tradeA.tradeType !== tradeB.tradeType || !tradeA.inputAmount.currency.equals(tradeB.inputAmount.currency) || !tradeA.outputAmount.currency.equals(tradeB.outputAmount.currency)) {
412
- throw new Error("Trades are not comparable");
413
- }
414
- if (minimumDelta.equalTo(ZERO_PERCENT)) {
415
- return tradeA.executionPrice.lessThan(tradeB.executionPrice);
416
- }
417
- return tradeA.executionPrice.asFraction.multiply(minimumDelta.add(ONE_HUNDRED_PERCENT)).lessThan(tradeB.executionPrice);
418
- }
419
- var PAIR_ADDRESS_CACHE = {};
420
- var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
421
- function getCreate2Address(from_, salt_, initCodeHash) {
422
- const from = toBytes(getAddress(from_));
423
- const salt = pad(isBytes(salt_) ? salt_ : toBytes(salt_), {
424
- size: 32
425
- });
426
- return getAddress(slice(keccak256(concat([toBytes("0xff"), from, salt, toBytes(initCodeHash)])), 12));
427
- }
428
- var EMPTY_INPU_HASH = "0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470";
429
- var ZKSYNC_PREFIX = "0x2020dba91b30cc0006188af794c2fb30dd8520db7e2c088b7fc7c103c00ca494";
430
- function getCreate2AddressZkSync(from, salt, initCodeHash) {
431
- return getAddress(
432
- `0x${keccak256(concat([ZKSYNC_PREFIX, pad(from, { size: 32 }), salt, initCodeHash, EMPTY_INPU_HASH])).slice(26)}`
433
- );
434
- }
435
- var computePairAddress = ({
436
- factoryAddress,
437
- tokenA,
438
- tokenB
439
- }) => {
440
- const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
441
- const key = composeKey(token0, token1);
442
- if (PAIR_ADDRESS_CACHE?.[key] === void 0) {
443
- const getCreate2Address_ = token0.chainId === ChainId.ZKSYNC_TESTNET || token1.chainId === ChainId.ZKSYNC ? getCreate2AddressZkSync : getCreate2Address;
444
- PAIR_ADDRESS_CACHE = {
445
- ...PAIR_ADDRESS_CACHE,
446
- [key]: getCreate2Address_(
447
- factoryAddress,
448
- keccak256(encodePacked(["address", "address"], [token0.address, token1.address])),
449
- INIT_CODE_HASH_MAP[token0.chainId]
450
- )
451
- };
452
- }
453
- return PAIR_ADDRESS_CACHE[key];
454
- };
455
- var Pair = class {
456
- static getAddress(tokenA, tokenB) {
457
- return computePairAddress({
458
- factoryAddress: FACTORY_ADDRESS_MAP[tokenA.chainId],
459
- tokenA,
460
- tokenB
461
- });
462
- }
463
- constructor(currencyAmountA, tokenAmountB) {
464
- const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) ? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
465
- this.liquidityToken = new ERC20Token(
466
- tokenAmounts[0].currency.chainId,
467
- Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency),
468
- 18,
469
- "Cake-LP",
470
- "Pancake LPs"
471
- );
472
- this.tokenAmounts = tokenAmounts;
473
- }
474
- /**
475
- * Returns true if the token is either token0 or token1
476
- * @param token to check
477
- */
478
- involvesToken(token) {
479
- return token.equals(this.token0) || token.equals(this.token1);
480
- }
481
- /**
482
- * Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
483
- */
484
- get token0Price() {
485
- const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
486
- return new Price(this.token0, this.token1, result.denominator, result.numerator);
487
- }
488
- /**
489
- * Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
490
- */
491
- get token1Price() {
492
- const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
493
- return new Price(this.token1, this.token0, result.denominator, result.numerator);
494
- }
495
- /**
496
- * Return the price of the given token in terms of the other token in the pair.
497
- * @param token token to return price of
498
- */
499
- priceOf(token) {
500
- invariant5(this.involvesToken(token), "TOKEN");
501
- return token.equals(this.token0) ? this.token0Price : this.token1Price;
502
- }
503
- /**
504
- * Returns the chain ID of the tokens in the pair.
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
- invariant5(this.involvesToken(token), "TOKEN");
523
- return token.equals(this.token0) ? this.reserve0 : this.reserve1;
524
- }
525
- getOutputAmount(inputAmount) {
526
- invariant5(this.involvesToken(inputAmount.currency), "TOKEN");
527
- if (this.reserve0.quotient === ZERO || 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 = inputAmount.quotient * _9975;
533
- const numerator = inputAmountWithFee * outputReserve.quotient;
534
- const denominator = inputReserve.quotient * _10000 + inputAmountWithFee;
535
- const outputAmount = CurrencyAmount.fromRawAmount(
536
- inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
537
- numerator / denominator
538
- );
539
- if (outputAmount.quotient === ZERO) {
540
- throw new InsufficientInputAmountError();
541
- }
542
- return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
543
- }
544
- getInputAmount(outputAmount) {
545
- invariant5(this.involvesToken(outputAmount.currency), "TOKEN");
546
- if (this.reserve0.quotient === ZERO || this.reserve1.quotient === ZERO || outputAmount.quotient >= this.reserveOf(outputAmount.currency).quotient) {
547
- throw new InsufficientReservesError();
548
- }
549
- const outputReserve = this.reserveOf(outputAmount.currency);
550
- const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
551
- const numerator = inputReserve.quotient * outputAmount.quotient * _10000;
552
- const denominator = (outputReserve.quotient - outputAmount.quotient) * _9975;
553
- const inputAmount = CurrencyAmount.fromRawAmount(
554
- outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
555
- numerator / denominator + ONE
556
- );
557
- return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
558
- }
559
- getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
560
- invariant5(totalSupply.currency.equals(this.liquidityToken), "LIQUIDITY");
561
- const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
562
- invariant5(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
563
- let liquidity;
564
- if (totalSupply.quotient === ZERO) {
565
- liquidity = sqrt(tokenAmounts[0].quotient * tokenAmounts[1].quotient) - MINIMUM_LIQUIDITY;
566
- } else {
567
- const amount0 = tokenAmounts[0].quotient * totalSupply.quotient / this.reserve0.quotient;
568
- const amount1 = tokenAmounts[1].quotient * totalSupply.quotient / this.reserve1.quotient;
569
- liquidity = amount0 <= amount1 ? amount0 : amount1;
570
- }
571
- if (!(liquidity > ZERO)) {
572
- throw new InsufficientInputAmountError();
573
- }
574
- return CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
575
- }
576
- getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
577
- invariant5(this.involvesToken(token), "TOKEN");
578
- invariant5(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
579
- invariant5(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
580
- invariant5(liquidity.quotient <= totalSupply.quotient, "LIQUIDITY");
581
- let totalSupplyAdjusted;
582
- if (!feeOn) {
583
- totalSupplyAdjusted = totalSupply;
584
- } else {
585
- invariant5(!!kLast, "K_LAST");
586
- const kLastParsed = BigInt(kLast);
587
- if (!(kLastParsed === ZERO)) {
588
- const rootK = sqrt(this.reserve0.quotient * this.reserve1.quotient);
589
- const rootKLast = sqrt(kLastParsed);
590
- if (rootK > rootKLast) {
591
- const numerator = totalSupply.quotient * (rootK - rootKLast);
592
- const denominator = rootK * FIVE + rootKLast;
593
- const feeLiquidity = numerator / denominator;
594
- totalSupplyAdjusted = totalSupply.add(CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
595
- } else {
596
- totalSupplyAdjusted = totalSupply;
597
- }
598
- } else {
599
- totalSupplyAdjusted = totalSupply;
600
- }
601
- }
602
- return CurrencyAmount.fromRawAmount(
603
- token,
604
- liquidity.quotient * this.reserveOf(token).quotient / totalSupplyAdjusted.quotient
605
- );
606
- }
607
- };
608
- var Route = class {
609
- constructor(pairs, input, output) {
610
- this._midPrice = null;
611
- invariant5(pairs.length > 0, "PAIRS");
612
- const { chainId } = pairs[0];
613
- invariant5(
614
- pairs.every((pair) => pair.chainId === chainId),
615
- "CHAIN_IDS"
616
- );
617
- const wrappedInput = input.wrapped;
618
- invariant5(pairs[0].involvesToken(wrappedInput), "INPUT");
619
- invariant5(typeof output === "undefined" || pairs[pairs.length - 1].involvesToken(output.wrapped), "OUTPUT");
620
- const path = [wrappedInput];
621
- for (const [i, pair] of pairs.entries()) {
622
- const currentInput = path[i];
623
- invariant5(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), "PATH");
624
- const output2 = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
625
- path.push(output2);
626
- }
627
- this.pairs = pairs;
628
- this.path = path;
629
- this.input = input;
630
- this.output = output;
631
- }
632
- get midPrice() {
633
- if (this._midPrice !== null)
634
- return this._midPrice;
635
- const prices = [];
636
- for (const [i, pair] of this.pairs.entries()) {
637
- prices.push(
638
- this.path[i].equals(pair.token0) ? new Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient) : new Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient)
639
- );
640
- }
641
- const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
642
- return this._midPrice = new Price(this.input, this.output, reduced.denominator, reduced.numerator);
643
- }
644
- get chainId() {
645
- return this.pairs[0].chainId;
646
- }
647
- };
648
- function inputOutputComparator(a, b) {
649
- invariant5(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
650
- invariant5(a.outputAmount.currency.equals(b.outputAmount.currency), "OUTPUT_CURRENCY");
651
- if (a.outputAmount.equalTo(b.outputAmount)) {
652
- if (a.inputAmount.equalTo(b.inputAmount)) {
653
- return 0;
654
- }
655
- if (a.inputAmount.lessThan(b.inputAmount)) {
656
- return -1;
657
- }
658
- return 1;
659
- }
660
- if (a.outputAmount.lessThan(b.outputAmount)) {
661
- return 1;
662
- }
663
- return -1;
664
- }
665
- function tradeComparator(a, b) {
666
- const ioComp = inputOutputComparator(a, b);
667
- if (ioComp !== 0) {
668
- return ioComp;
669
- }
670
- if (a.priceImpact.lessThan(b.priceImpact)) {
671
- return -1;
672
- }
673
- if (a.priceImpact.greaterThan(b.priceImpact)) {
674
- return 1;
675
- }
676
- return a.route.path.length - b.route.path.length;
677
- }
678
- var Trade = class {
679
- /**
680
- * Constructs an exact in trade with the given amount in and route
681
- * @param route route of the exact in trade
682
- * @param amountIn the amount being passed in
683
- */
684
- static exactIn(route, amountIn) {
685
- return new Trade(route, amountIn, TradeType.EXACT_INPUT);
686
- }
687
- /**
688
- * Constructs an exact out trade with the given amount out and route
689
- * @param route route of the exact out trade
690
- * @param amountOut the amount returned by the trade
691
- */
692
- static exactOut(route, amountOut) {
693
- return new Trade(route, amountOut, TradeType.EXACT_OUTPUT);
694
- }
695
- constructor(route, amount, tradeType) {
696
- this.route = route;
697
- this.tradeType = tradeType;
698
- const tokenAmounts = new Array(route.path.length);
699
- if (tradeType === TradeType.EXACT_INPUT) {
700
- invariant5(amount.currency.equals(route.input), "INPUT");
701
- tokenAmounts[0] = amount.wrapped;
702
- for (let i = 0; i < route.path.length - 1; i++) {
703
- const pair = route.pairs[i];
704
- const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
705
- tokenAmounts[i + 1] = outputAmount;
706
- }
707
- this.inputAmount = CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
708
- this.outputAmount = CurrencyAmount.fromFractionalAmount(
709
- route.output,
710
- tokenAmounts[tokenAmounts.length - 1].numerator,
711
- tokenAmounts[tokenAmounts.length - 1].denominator
712
- );
713
- } else {
714
- invariant5(amount.currency.equals(route.output), "OUTPUT");
715
- tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
716
- for (let i = route.path.length - 1; i > 0; i--) {
717
- const pair = route.pairs[i - 1];
718
- const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
719
- tokenAmounts[i - 1] = inputAmount;
720
- }
721
- this.inputAmount = CurrencyAmount.fromFractionalAmount(
722
- route.input,
723
- tokenAmounts[0].numerator,
724
- tokenAmounts[0].denominator
725
- );
726
- this.outputAmount = CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
727
- }
728
- this.executionPrice = new Price(
729
- this.inputAmount.currency,
730
- this.outputAmount.currency,
731
- this.inputAmount.quotient,
732
- this.outputAmount.quotient
733
- );
734
- this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
735
- }
736
- /**
737
- * Get the minimum amount that must be received from this trade for the given slippage tolerance
738
- * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
739
- */
740
- minimumAmountOut(slippageTolerance) {
741
- invariant5(!slippageTolerance.lessThan(ZERO), "SLIPPAGE_TOLERANCE");
742
- if (this.tradeType === TradeType.EXACT_OUTPUT) {
743
- return this.outputAmount;
744
- }
745
- const slippageAdjustedAmountOut = new Fraction(ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
746
- return CurrencyAmount.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
747
- }
748
- /**
749
- * Get the maximum amount in that can be spent via this trade for the given slippage tolerance
750
- * @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
751
- */
752
- maximumAmountIn(slippageTolerance) {
753
- invariant5(!slippageTolerance.lessThan(ZERO), "SLIPPAGE_TOLERANCE");
754
- if (this.tradeType === TradeType.EXACT_INPUT) {
755
- return this.inputAmount;
756
- }
757
- const slippageAdjustedAmountIn = new Fraction(ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
758
- return CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
759
- }
760
- /**
761
- * Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
762
- * amount to an output token, making at most `maxHops` hops.
763
- * Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
764
- * the amount in among multiple routes.
765
- * @param pairs the pairs to consider in finding the best trade
766
- * @param nextAmountIn exact amount of input currency to spend
767
- * @param currencyOut the desired currency out
768
- * @param maxNumResults maximum number of results to return
769
- * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
770
- * @param currentPairs used in recursion; the current list of pairs
771
- * @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
772
- * @param bestTrades used in recursion; the current list of best trades
773
- */
774
- static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
775
- invariant5(pairs.length > 0, "PAIRS");
776
- invariant5(maxHops > 0, "MAX_HOPS");
777
- invariant5(currencyAmountIn === nextAmountIn || currentPairs.length > 0, "INVALID_RECURSION");
778
- const amountIn = nextAmountIn.wrapped;
779
- const tokenOut = currencyOut.wrapped;
780
- for (let i = 0; i < pairs.length; i++) {
781
- const pair = pairs[i];
782
- if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
783
- continue;
784
- if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO))
785
- continue;
786
- let amountOut;
787
- try {
788
- ;
789
- [amountOut] = pair.getOutputAmount(amountIn);
790
- } catch (error) {
791
- if (error.isInsufficientInputAmountError) {
792
- continue;
793
- }
794
- throw error;
795
- }
796
- if (amountOut.currency.equals(tokenOut)) {
797
- sortedInsert(
798
- bestTrades,
799
- new Trade(
800
- new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut),
801
- currencyAmountIn,
802
- TradeType.EXACT_INPUT
803
- ),
804
- maxNumResults,
805
- tradeComparator
806
- );
807
- } else if (maxHops > 1 && pairs.length > 1) {
808
- const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
809
- Trade.bestTradeExactIn(
810
- pairsExcludingThisPair,
811
- currencyAmountIn,
812
- currencyOut,
813
- {
814
- maxNumResults,
815
- maxHops: maxHops - 1
816
- },
817
- [...currentPairs, pair],
818
- amountOut,
819
- bestTrades
820
- );
821
- }
822
- }
823
- return bestTrades;
824
- }
825
- /**
826
- * Return the execution price after accounting for slippage tolerance
827
- * @param slippageTolerance the allowed tolerated slippage
828
- */
829
- worstExecutionPrice(slippageTolerance) {
830
- return new Price(
831
- this.inputAmount.currency,
832
- this.outputAmount.currency,
833
- this.maximumAmountIn(slippageTolerance).quotient,
834
- this.minimumAmountOut(slippageTolerance).quotient
835
- );
836
- }
837
- /**
838
- * similar to the above method but instead targets a fixed output amount
839
- * given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
840
- * to an output token amount, making at most `maxHops` hops
841
- * note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
842
- * the amount in among multiple routes.
843
- * @param pairs the pairs to consider in finding the best trade
844
- * @param currencyIn the currency to spend
845
- * @param nextAmountOut the exact amount of currency out
846
- * @param maxNumResults maximum number of results to return
847
- * @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
848
- * @param currentPairs used in recursion; the current list of pairs
849
- * @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter
850
- * @param bestTrades used in recursion; the current list of best trades
851
- */
852
- static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
853
- invariant5(pairs.length > 0, "PAIRS");
854
- invariant5(maxHops > 0, "MAX_HOPS");
855
- invariant5(currencyAmountOut === nextAmountOut || currentPairs.length > 0, "INVALID_RECURSION");
856
- const amountOut = nextAmountOut.wrapped;
857
- const tokenIn = currencyIn.wrapped;
858
- for (let i = 0; i < pairs.length; i++) {
859
- const pair = pairs[i];
860
- if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
861
- continue;
862
- if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO))
863
- continue;
864
- let amountIn;
865
- try {
866
- ;
867
- [amountIn] = pair.getInputAmount(amountOut);
868
- } catch (error) {
869
- if (error.isInsufficientReservesError) {
870
- continue;
871
- }
872
- throw error;
873
- }
874
- if (amountIn.currency.equals(tokenIn)) {
875
- sortedInsert(
876
- bestTrades,
877
- new Trade(
878
- new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency),
879
- currencyAmountOut,
880
- TradeType.EXACT_OUTPUT
881
- ),
882
- maxNumResults,
883
- tradeComparator
884
- );
885
- } else if (maxHops > 1 && pairs.length > 1) {
886
- const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
887
- Trade.bestTradeExactOut(
888
- pairsExcludingThisPair,
889
- currencyIn,
890
- currencyAmountOut,
891
- {
892
- maxNumResults,
893
- maxHops: maxHops - 1
894
- },
895
- [pair, ...currentPairs],
896
- amountIn,
897
- bestTrades
898
- );
899
- }
900
- }
901
- return bestTrades;
902
- }
903
- };
904
- var _Native = class extends NativeCurrency {
905
- constructor({
906
- chainId,
907
- decimals,
908
- name,
909
- symbol
910
- }) {
911
- super(chainId, decimals, symbol, name);
912
- }
913
- get wrapped() {
914
- const wnative = WNATIVE[this.chainId];
915
- invariant5(!!wnative, "WRAPPED");
916
- return wnative;
917
- }
918
- static onChain(chainId) {
919
- if (chainId in this.cache) {
920
- return this.cache[chainId];
921
- }
922
- invariant5(!!NATIVE[chainId], "NATIVE_CURRENCY");
923
- const { decimals, name, symbol } = NATIVE[chainId];
924
- return this.cache[chainId] = new _Native({ chainId, decimals, symbol, name });
925
- }
926
- equals(other) {
927
- return other.isNative && other.chainId === this.chainId;
928
- }
929
- };
930
- var Native = _Native;
931
- Native.cache = {};
932
- function toHex(currencyAmount) {
933
- return `0x${currencyAmount.quotient.toString(16)}`;
934
- }
935
- var ZERO_HEX = "0x0";
936
- var Router = class {
937
- /**
938
- * Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
939
- * @param trade to produce call parameters for
940
- * @param options options for the call parameters
941
- */
942
- static swapCallParameters(trade, options) {
943
- const etherIn = trade.inputAmount.currency.isNative;
944
- const etherOut = trade.outputAmount.currency.isNative;
945
- invariant5(!(etherIn && etherOut), "ETHER_IN_OUT");
946
- invariant5(!("ttl" in options) || options.ttl > 0, "TTL");
947
- const to = validateAndParseAddress(options.recipient);
948
- const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
949
- const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
950
- const path = trade.route.path.map((token) => token.address);
951
- const deadline = "ttl" in options ? `0x${(Math.floor(( new Date()).getTime() / 1e3) + options.ttl).toString(16)}` : `0x${options.deadline.toString(16)}`;
952
- const useFeeOnTransfer = Boolean(options.feeOnTransfer);
953
- let methodName;
954
- let args;
955
- let value;
956
- switch (trade.tradeType) {
957
- case TradeType.EXACT_INPUT:
958
- if (etherIn) {
959
- methodName = useFeeOnTransfer ? "swapExactETHForTokensSupportingFeeOnTransferTokens" : "swapExactETHForTokens";
960
- args = [amountOut, path, to, deadline];
961
- value = amountIn;
962
- } else if (etherOut) {
963
- methodName = useFeeOnTransfer ? "swapExactTokensForETHSupportingFeeOnTransferTokens" : "swapExactTokensForETH";
964
- args = [amountIn, amountOut, path, to, deadline];
965
- value = ZERO_HEX;
966
- } else {
967
- methodName = useFeeOnTransfer ? "swapExactTokensForTokensSupportingFeeOnTransferTokens" : "swapExactTokensForTokens";
968
- args = [amountIn, amountOut, path, to, deadline];
969
- value = ZERO_HEX;
970
- }
971
- break;
972
- case TradeType.EXACT_OUTPUT:
973
- invariant5(!useFeeOnTransfer, "EXACT_OUT_FOT");
974
- if (etherIn) {
975
- methodName = "swapETHForExactTokens";
976
- args = [amountOut, path, to, deadline];
977
- value = amountIn;
978
- } else if (etherOut) {
979
- methodName = "swapTokensForExactETH";
980
- args = [amountOut, amountIn, path, to, deadline];
981
- value = ZERO_HEX;
982
- } else {
983
- methodName = "swapTokensForExactTokens";
984
- args = [amountOut, amountIn, path, to, deadline];
985
- value = ZERO_HEX;
986
- }
987
- break;
988
- }
989
- return {
990
- methodName,
991
- args,
992
- value
993
- };
994
- }
995
- };
996
- var _Ether = class extends NativeCurrency {
997
- constructor(chainId) {
998
- super(chainId, 18, "ETH", "Ether");
999
- }
1000
- get wrapped() {
1001
- const weth9 = WETH9[this.chainId];
1002
- invariant5(!!weth9, "WRAPPED");
1003
- return weth9;
1004
- }
1005
- static onChain(chainId) {
1006
- if (!this._etherCache[chainId]) {
1007
- this._etherCache[chainId] = new _Ether(chainId);
1008
- }
1009
- return this._etherCache[chainId];
1010
- }
1011
- equals(other) {
1012
- return other.isNative && other.chainId === this.chainId;
1013
- }
1014
- };
1015
- var Ether = _Ether;
1016
- Ether._etherCache = {};
1017
-
1018
- // src/abis/ERC20.ts
1019
- var erc20Abi = [
1020
- {
1021
- constant: true,
1022
- inputs: [],
1023
- name: "decimals",
1024
- outputs: [{ name: "", type: "uint8" }],
1025
- payable: false,
1026
- stateMutability: "view",
1027
- type: "function"
1028
- },
1029
- {
1030
- constant: true,
1031
- inputs: [{ name: "", type: "address" }],
1032
- name: "balanceOf",
1033
- outputs: [{ name: "", type: "uint256" }],
1034
- payable: false,
1035
- stateMutability: "view",
1036
- type: "function"
1037
- }
1038
- ];
1039
-
1040
- // src/abis/IPancakePair.ts
1041
- var pancakePairV2ABI = [
1042
- {
1043
- anonymous: false,
1044
- inputs: [
1045
- {
1046
- indexed: true,
1047
- internalType: "address",
1048
- name: "owner",
1049
- type: "address"
1050
- },
1051
- {
1052
- indexed: true,
1053
- internalType: "address",
1054
- name: "spender",
1055
- type: "address"
1056
- },
1057
- {
1058
- indexed: false,
1059
- internalType: "uint256",
1060
- name: "value",
1061
- type: "uint256"
1062
- }
1063
- ],
1064
- name: "Approval",
1065
- type: "event"
1066
- },
1067
- {
1068
- anonymous: false,
1069
- inputs: [
1070
- {
1071
- indexed: true,
1072
- internalType: "address",
1073
- name: "sender",
1074
- type: "address"
1075
- },
1076
- {
1077
- indexed: false,
1078
- internalType: "uint256",
1079
- name: "amount0",
1080
- type: "uint256"
1081
- },
1082
- {
1083
- indexed: false,
1084
- internalType: "uint256",
1085
- name: "amount1",
1086
- type: "uint256"
1087
- },
1088
- {
1089
- indexed: true,
1090
- internalType: "address",
1091
- name: "to",
1092
- type: "address"
1093
- }
1094
- ],
1095
- name: "Burn",
1096
- type: "event"
1097
- },
1098
- {
1099
- anonymous: false,
1100
- inputs: [
1101
- {
1102
- indexed: true,
1103
- internalType: "address",
1104
- name: "sender",
1105
- type: "address"
1106
- },
1107
- {
1108
- indexed: false,
1109
- internalType: "uint256",
1110
- name: "amount0",
1111
- type: "uint256"
1112
- },
1113
- {
1114
- indexed: false,
1115
- internalType: "uint256",
1116
- name: "amount1",
1117
- type: "uint256"
1118
- }
1119
- ],
1120
- name: "Mint",
1121
- type: "event"
1122
- },
1123
- {
1124
- anonymous: false,
1125
- inputs: [
1126
- {
1127
- indexed: true,
1128
- internalType: "address",
1129
- name: "sender",
1130
- type: "address"
1131
- },
1132
- {
1133
- indexed: false,
1134
- internalType: "uint256",
1135
- name: "amount0In",
1136
- type: "uint256"
1137
- },
1138
- {
1139
- indexed: false,
1140
- internalType: "uint256",
1141
- name: "amount1In",
1142
- type: "uint256"
1143
- },
1144
- {
1145
- indexed: false,
1146
- internalType: "uint256",
1147
- name: "amount0Out",
1148
- type: "uint256"
1149
- },
1150
- {
1151
- indexed: false,
1152
- internalType: "uint256",
1153
- name: "amount1Out",
1154
- type: "uint256"
1155
- },
1156
- {
1157
- indexed: true,
1158
- internalType: "address",
1159
- name: "to",
1160
- type: "address"
1161
- }
1162
- ],
1163
- name: "Swap",
1164
- type: "event"
1165
- },
1166
- {
1167
- anonymous: false,
1168
- inputs: [
1169
- {
1170
- indexed: false,
1171
- internalType: "uint112",
1172
- name: "reserve0",
1173
- type: "uint112"
1174
- },
1175
- {
1176
- indexed: false,
1177
- internalType: "uint112",
1178
- name: "reserve1",
1179
- type: "uint112"
1180
- }
1181
- ],
1182
- name: "Sync",
1183
- type: "event"
1184
- },
1185
- {
1186
- anonymous: false,
1187
- inputs: [
1188
- {
1189
- indexed: true,
1190
- internalType: "address",
1191
- name: "from",
1192
- type: "address"
1193
- },
1194
- {
1195
- indexed: true,
1196
- internalType: "address",
1197
- name: "to",
1198
- type: "address"
1199
- },
1200
- {
1201
- indexed: false,
1202
- internalType: "uint256",
1203
- name: "value",
1204
- type: "uint256"
1205
- }
1206
- ],
1207
- name: "Transfer",
1208
- type: "event"
1209
- },
1210
- {
1211
- constant: true,
1212
- inputs: [],
1213
- name: "DOMAIN_SEPARATOR",
1214
- outputs: [
1215
- {
1216
- internalType: "bytes32",
1217
- name: "",
1218
- type: "bytes32"
1219
- }
1220
- ],
1221
- payable: false,
1222
- stateMutability: "view",
1223
- type: "function"
1224
- },
1225
- {
1226
- constant: true,
1227
- inputs: [],
1228
- name: "MINIMUM_LIQUIDITY",
1229
- outputs: [
1230
- {
1231
- internalType: "uint256",
1232
- name: "",
1233
- type: "uint256"
1234
- }
1235
- ],
1236
- payable: false,
1237
- stateMutability: "pure",
1238
- type: "function"
1239
- },
1240
- {
1241
- constant: true,
1242
- inputs: [],
1243
- name: "PERMIT_TYPEHASH",
1244
- outputs: [
1245
- {
1246
- internalType: "bytes32",
1247
- name: "",
1248
- type: "bytes32"
1249
- }
1250
- ],
1251
- payable: false,
1252
- stateMutability: "pure",
1253
- type: "function"
1254
- },
1255
- {
1256
- constant: true,
1257
- inputs: [
1258
- {
1259
- internalType: "address",
1260
- name: "owner",
1261
- type: "address"
1262
- },
1263
- {
1264
- internalType: "address",
1265
- name: "spender",
1266
- type: "address"
1267
- }
1268
- ],
1269
- name: "allowance",
1270
- outputs: [
1271
- {
1272
- internalType: "uint256",
1273
- name: "",
1274
- type: "uint256"
1275
- }
1276
- ],
1277
- payable: false,
1278
- stateMutability: "view",
1279
- type: "function"
1280
- },
1281
- {
1282
- constant: false,
1283
- inputs: [
1284
- {
1285
- internalType: "address",
1286
- name: "spender",
1287
- type: "address"
1288
- },
1289
- {
1290
- internalType: "uint256",
1291
- name: "value",
1292
- type: "uint256"
1293
- }
1294
- ],
1295
- name: "approve",
1296
- outputs: [
1297
- {
1298
- internalType: "bool",
1299
- name: "",
1300
- type: "bool"
1301
- }
1302
- ],
1303
- payable: false,
1304
- stateMutability: "nonpayable",
1305
- type: "function"
1306
- },
1307
- {
1308
- constant: true,
1309
- inputs: [
1310
- {
1311
- internalType: "address",
1312
- name: "owner",
1313
- type: "address"
1314
- }
1315
- ],
1316
- name: "balanceOf",
1317
- outputs: [
1318
- {
1319
- internalType: "uint256",
1320
- name: "",
1321
- type: "uint256"
1322
- }
1323
- ],
1324
- payable: false,
1325
- stateMutability: "view",
1326
- type: "function"
1327
- },
1328
- {
1329
- constant: false,
1330
- inputs: [
1331
- {
1332
- internalType: "address",
1333
- name: "to",
1334
- type: "address"
1335
- }
1336
- ],
1337
- name: "burn",
1338
- outputs: [
1339
- {
1340
- internalType: "uint256",
1341
- name: "amount0",
1342
- type: "uint256"
1343
- },
1344
- {
1345
- internalType: "uint256",
1346
- name: "amount1",
1347
- type: "uint256"
1348
- }
1349
- ],
1350
- payable: false,
1351
- stateMutability: "nonpayable",
1352
- type: "function"
1353
- },
1354
- {
1355
- constant: true,
1356
- inputs: [],
1357
- name: "decimals",
1358
- outputs: [
1359
- {
1360
- internalType: "uint8",
1361
- name: "",
1362
- type: "uint8"
1363
- }
1364
- ],
1365
- payable: false,
1366
- stateMutability: "pure",
1367
- type: "function"
1368
- },
1369
- {
1370
- constant: true,
1371
- inputs: [],
1372
- name: "factory",
1373
- outputs: [
1374
- {
1375
- internalType: "address",
1376
- name: "",
1377
- type: "address"
1378
- }
1379
- ],
1380
- payable: false,
1381
- stateMutability: "view",
1382
- type: "function"
1383
- },
1384
- {
1385
- constant: true,
1386
- inputs: [],
1387
- name: "getReserves",
1388
- outputs: [
1389
- {
1390
- internalType: "uint112",
1391
- name: "reserve0",
1392
- type: "uint112"
1393
- },
1394
- {
1395
- internalType: "uint112",
1396
- name: "reserve1",
1397
- type: "uint112"
1398
- },
1399
- {
1400
- internalType: "uint32",
1401
- name: "blockTimestampLast",
1402
- type: "uint32"
1403
- }
1404
- ],
1405
- payable: false,
1406
- stateMutability: "view",
1407
- type: "function"
1408
- },
1409
- {
1410
- constant: false,
1411
- inputs: [
1412
- {
1413
- internalType: "address",
1414
- name: "",
1415
- type: "address"
1416
- },
1417
- {
1418
- internalType: "address",
1419
- name: "",
1420
- type: "address"
1421
- }
1422
- ],
1423
- name: "initialize",
1424
- outputs: [],
1425
- payable: false,
1426
- stateMutability: "nonpayable",
1427
- type: "function"
1428
- },
1429
- {
1430
- constant: true,
1431
- inputs: [],
1432
- name: "kLast",
1433
- outputs: [
1434
- {
1435
- internalType: "uint256",
1436
- name: "",
1437
- type: "uint256"
1438
- }
1439
- ],
1440
- payable: false,
1441
- stateMutability: "view",
1442
- type: "function"
1443
- },
1444
- {
1445
- constant: false,
1446
- inputs: [
1447
- {
1448
- internalType: "address",
1449
- name: "to",
1450
- type: "address"
1451
- }
1452
- ],
1453
- name: "mint",
1454
- outputs: [
1455
- {
1456
- internalType: "uint256",
1457
- name: "liquidity",
1458
- type: "uint256"
1459
- }
1460
- ],
1461
- payable: false,
1462
- stateMutability: "nonpayable",
1463
- type: "function"
1464
- },
1465
- {
1466
- constant: true,
1467
- inputs: [],
1468
- name: "name",
1469
- outputs: [
1470
- {
1471
- internalType: "string",
1472
- name: "",
1473
- type: "string"
1474
- }
1475
- ],
1476
- payable: false,
1477
- stateMutability: "pure",
1478
- type: "function"
1479
- },
1480
- {
1481
- constant: true,
1482
- inputs: [
1483
- {
1484
- internalType: "address",
1485
- name: "owner",
1486
- type: "address"
1487
- }
1488
- ],
1489
- name: "nonces",
1490
- outputs: [
1491
- {
1492
- internalType: "uint256",
1493
- name: "",
1494
- type: "uint256"
1495
- }
1496
- ],
1497
- payable: false,
1498
- stateMutability: "view",
1499
- type: "function"
1500
- },
1501
- {
1502
- constant: false,
1503
- inputs: [
1504
- {
1505
- internalType: "address",
1506
- name: "owner",
1507
- type: "address"
1508
- },
1509
- {
1510
- internalType: "address",
1511
- name: "spender",
1512
- type: "address"
1513
- },
1514
- {
1515
- internalType: "uint256",
1516
- name: "value",
1517
- type: "uint256"
1518
- },
1519
- {
1520
- internalType: "uint256",
1521
- name: "deadline",
1522
- type: "uint256"
1523
- },
1524
- {
1525
- internalType: "uint8",
1526
- name: "v",
1527
- type: "uint8"
1528
- },
1529
- {
1530
- internalType: "bytes32",
1531
- name: "r",
1532
- type: "bytes32"
1533
- },
1534
- {
1535
- internalType: "bytes32",
1536
- name: "s",
1537
- type: "bytes32"
1538
- }
1539
- ],
1540
- name: "permit",
1541
- outputs: [],
1542
- payable: false,
1543
- stateMutability: "nonpayable",
1544
- type: "function"
1545
- },
1546
- {
1547
- constant: true,
1548
- inputs: [],
1549
- name: "price0CumulativeLast",
1550
- outputs: [
1551
- {
1552
- internalType: "uint256",
1553
- name: "",
1554
- type: "uint256"
1555
- }
1556
- ],
1557
- payable: false,
1558
- stateMutability: "view",
1559
- type: "function"
1560
- },
1561
- {
1562
- constant: true,
1563
- inputs: [],
1564
- name: "price1CumulativeLast",
1565
- outputs: [
1566
- {
1567
- internalType: "uint256",
1568
- name: "",
1569
- type: "uint256"
1570
- }
1571
- ],
1572
- payable: false,
1573
- stateMutability: "view",
1574
- type: "function"
1575
- },
1576
- {
1577
- constant: false,
1578
- inputs: [
1579
- {
1580
- internalType: "address",
1581
- name: "to",
1582
- type: "address"
1583
- }
1584
- ],
1585
- name: "skim",
1586
- outputs: [],
1587
- payable: false,
1588
- stateMutability: "nonpayable",
1589
- type: "function"
1590
- },
1591
- {
1592
- constant: false,
1593
- inputs: [
1594
- {
1595
- internalType: "uint256",
1596
- name: "amount0Out",
1597
- type: "uint256"
1598
- },
1599
- {
1600
- internalType: "uint256",
1601
- name: "amount1Out",
1602
- type: "uint256"
1603
- },
1604
- {
1605
- internalType: "address",
1606
- name: "to",
1607
- type: "address"
1608
- },
1609
- {
1610
- internalType: "bytes",
1611
- name: "data",
1612
- type: "bytes"
1613
- }
1614
- ],
1615
- name: "swap",
1616
- outputs: [],
1617
- payable: false,
1618
- stateMutability: "nonpayable",
1619
- type: "function"
1620
- },
1621
- {
1622
- constant: true,
1623
- inputs: [],
1624
- name: "symbol",
1625
- outputs: [
1626
- {
1627
- internalType: "string",
1628
- name: "",
1629
- type: "string"
1630
- }
1631
- ],
1632
- payable: false,
1633
- stateMutability: "pure",
1634
- type: "function"
1635
- },
1636
- {
1637
- constant: false,
1638
- inputs: [],
1639
- name: "sync",
1640
- outputs: [],
1641
- payable: false,
1642
- stateMutability: "nonpayable",
1643
- type: "function"
1644
- },
1645
- {
1646
- constant: true,
1647
- inputs: [],
1648
- name: "token0",
1649
- outputs: [
1650
- {
1651
- internalType: "address",
1652
- name: "",
1653
- type: "address"
1654
- }
1655
- ],
1656
- payable: false,
1657
- stateMutability: "view",
1658
- type: "function"
1659
- },
1660
- {
1661
- constant: true,
1662
- inputs: [],
1663
- name: "token1",
1664
- outputs: [
1665
- {
1666
- internalType: "address",
1667
- name: "",
1668
- type: "address"
1669
- }
1670
- ],
1671
- payable: false,
1672
- stateMutability: "view",
1673
- type: "function"
1674
- },
1675
- {
1676
- constant: true,
1677
- inputs: [],
1678
- name: "totalSupply",
1679
- outputs: [
1680
- {
1681
- internalType: "uint256",
1682
- name: "",
1683
- type: "uint256"
1684
- }
1685
- ],
1686
- payable: false,
1687
- stateMutability: "view",
1688
- type: "function"
1689
- },
1690
- {
1691
- constant: false,
1692
- inputs: [
1693
- {
1694
- internalType: "address",
1695
- name: "to",
1696
- type: "address"
1697
- },
1698
- {
1699
- internalType: "uint256",
1700
- name: "value",
1701
- type: "uint256"
1702
- }
1703
- ],
1704
- name: "transfer",
1705
- outputs: [
1706
- {
1707
- internalType: "bool",
1708
- name: "",
1709
- type: "bool"
1710
- }
1711
- ],
1712
- payable: false,
1713
- stateMutability: "nonpayable",
1714
- type: "function"
1715
- },
1716
- {
1717
- constant: false,
1718
- inputs: [
1719
- {
1720
- internalType: "address",
1721
- name: "from",
1722
- type: "address"
1723
- },
1724
- {
1725
- internalType: "address",
1726
- name: "to",
1727
- type: "address"
1728
- },
1729
- {
1730
- internalType: "uint256",
1731
- name: "value",
1732
- type: "uint256"
1733
- }
1734
- ],
1735
- name: "transferFrom",
1736
- outputs: [
1737
- {
1738
- internalType: "bool",
1739
- name: "",
1740
- type: "bool"
1741
- }
1742
- ],
1743
- payable: false,
1744
- stateMutability: "nonpayable",
1745
- type: "function"
1746
- }
1747
- ];
1748
-
1749
- // src/fetcher.ts
1750
- var TOKEN_DECIMALS_CACHE = {
1751
- [ChainId.BSC]: {}
1752
- };
1753
- var ethClient = createPublicClient({ chain: mainnet, transport: http() });
1754
- var bscClient = createPublicClient({ chain: bsc, transport: http() });
1755
- var bscTestnetClient = createPublicClient({ chain: bscTestnet, transport: http() });
1756
- var goerliClient = createPublicClient({ chain: goerli, transport: http() });
1757
- var getDefaultClient = (chainId) => {
1758
- switch (chainId) {
1759
- case ChainId.ETHEREUM:
1760
- return ethClient;
1761
- case ChainId.BSC:
1762
- return bscClient;
1763
- case ChainId.BSC_TESTNET:
1764
- return bscTestnetClient;
1765
- case ChainId.GOERLI:
1766
- return goerliClient;
1767
- default:
1768
- return bscClient;
1769
- }
1770
- };
1771
- var Fetcher = class {
1772
- /**
1773
- * Cannot be constructed.
1774
- */
1775
- // eslint-disable-next-line no-useless-constructor,@typescript-eslint/no-empty-function
1776
- constructor() {
1777
- }
1778
- /**
1779
- * Fetch information for a given token on the given chain, using the given viem provider.
1780
- * @param chainId chain of the token
1781
- * @param address address of the token on the chain
1782
- * @param provider provider used to fetch the token
1783
- * @param symbol symbol of the token
1784
- * @param name optional name of the token
1785
- */
1786
- static async fetchTokenData(chainId, address, publicClient = getDefaultClient(chainId), symbol, name) {
1787
- const erc20 = getContract({
1788
- abi: erc20Abi,
1789
- address,
1790
- client: publicClient
1791
- });
1792
- const parsedDecimals = typeof TOKEN_DECIMALS_CACHE?.[chainId]?.[address] === "number" ? TOKEN_DECIMALS_CACHE[chainId][address] : await erc20.read.decimals().then((decimals) => {
1793
- TOKEN_DECIMALS_CACHE = {
1794
- ...TOKEN_DECIMALS_CACHE,
1795
- [chainId]: {
1796
- ...TOKEN_DECIMALS_CACHE?.[chainId],
1797
- [address]: decimals
1798
- }
1799
- };
1800
- return decimals;
1801
- });
1802
- return new Token(chainId, address, parsedDecimals, symbol, name);
1803
- }
1804
- /**
1805
- * Fetches information about a pair and constructs a pair from the given two tokens.
1806
- * @param tokenA first token
1807
- * @param tokenB second token
1808
- * @param provider the provider to use to fetch the data
1809
- */
1810
- static async fetchPairData(tokenA, tokenB, publicClient = getDefaultClient(tokenA.chainId)) {
1811
- invariant5(tokenA.chainId === tokenB.chainId, "CHAIN_ID");
1812
- const address = Pair.getAddress(tokenA, tokenB);
1813
- const pairContract = getContract({
1814
- abi: pancakePairV2ABI,
1815
- address,
1816
- client: publicClient
1817
- });
1818
- const [reserves0, reserves1] = await pairContract.read.getReserves();
1819
- const balances = tokenA.sortsBefore(tokenB) ? [reserves0, reserves1] : [reserves1, reserves0];
1820
- return new Pair(
1821
- CurrencyAmount.fromRawAmount(tokenA, balances[0]),
1822
- CurrencyAmount.fromRawAmount(tokenB, balances[1])
1823
- );
1824
- }
1825
- };
1826
-
1827
- export { ERC20Token, Ether, FACTORY_ADDRESS, FACTORY_ADDRESS_MAP, Fetcher, INIT_CODE_HASH, INIT_CODE_HASH_MAP, NATIVE, Native, ONE_HUNDRED_PERCENT, OnRampCurrency, Pair, Route, Router, Trade, WBNB, WETH9, WNATIVE, ZERO_PERCENT, computePairAddress, erc20Abi, inputOutputComparator, isTradeBetter, pancakePairV2ABI, tradeComparator, validateAndParseAddress };
55
+ export { OnRampCurrency };