@pancakeswap/sdk 3.1.4 → 3.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +227 -30
- package/dist/index.mjs +219 -29
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -18,7 +18,14 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
20
|
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
21
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
22
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
23
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
24
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
25
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
26
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
27
|
+
mod
|
|
28
|
+
));
|
|
22
29
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
23
30
|
|
|
24
31
|
// src/index.ts
|
|
@@ -104,13 +111,48 @@ var INIT_CODE_HASH_MAP = {
|
|
|
104
111
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
105
112
|
};
|
|
106
113
|
var WETH9 = {
|
|
107
|
-
[1 /* ETHEREUM */]: new ERC20Token(
|
|
108
|
-
|
|
114
|
+
[1 /* ETHEREUM */]: new ERC20Token(
|
|
115
|
+
1 /* ETHEREUM */,
|
|
116
|
+
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
|
|
117
|
+
18,
|
|
118
|
+
"WETH",
|
|
119
|
+
"Wrapped Ether",
|
|
120
|
+
"https://weth.io"
|
|
121
|
+
),
|
|
122
|
+
[5 /* GOERLI */]: new ERC20Token(
|
|
123
|
+
5 /* GOERLI */,
|
|
124
|
+
"0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6",
|
|
125
|
+
18,
|
|
126
|
+
"WETH",
|
|
127
|
+
"Wrapped Ether",
|
|
128
|
+
"https://weth.io"
|
|
129
|
+
)
|
|
109
130
|
};
|
|
110
131
|
var WBNB = {
|
|
111
|
-
[1 /* ETHEREUM */]: new ERC20Token(
|
|
112
|
-
|
|
113
|
-
|
|
132
|
+
[1 /* ETHEREUM */]: new ERC20Token(
|
|
133
|
+
1 /* ETHEREUM */,
|
|
134
|
+
"0x418D75f65a02b3D53B2418FB8E1fe493759c7605",
|
|
135
|
+
18,
|
|
136
|
+
"WBNB",
|
|
137
|
+
"Wrapped BNB",
|
|
138
|
+
"https://www.binance.org"
|
|
139
|
+
),
|
|
140
|
+
[56 /* BSC */]: new ERC20Token(
|
|
141
|
+
56 /* BSC */,
|
|
142
|
+
"0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
|
|
143
|
+
18,
|
|
144
|
+
"WBNB",
|
|
145
|
+
"Wrapped BNB",
|
|
146
|
+
"https://www.binance.org"
|
|
147
|
+
),
|
|
148
|
+
[97 /* BSC_TESTNET */]: new ERC20Token(
|
|
149
|
+
97 /* BSC_TESTNET */,
|
|
150
|
+
"0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd",
|
|
151
|
+
18,
|
|
152
|
+
"WBNB",
|
|
153
|
+
"Wrapped BNB",
|
|
154
|
+
"https://www.binance.org"
|
|
155
|
+
)
|
|
114
156
|
};
|
|
115
157
|
var WNATIVE = {
|
|
116
158
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
@@ -168,7 +210,11 @@ var computePairAddress = ({
|
|
|
168
210
|
if ((PAIR_ADDRESS_CACHE == null ? void 0 : PAIR_ADDRESS_CACHE[key]) === void 0) {
|
|
169
211
|
PAIR_ADDRESS_CACHE = {
|
|
170
212
|
...PAIR_ADDRESS_CACHE,
|
|
171
|
-
[key]: (0, import_address2.getCreate2Address)(
|
|
213
|
+
[key]: (0, import_address2.getCreate2Address)(
|
|
214
|
+
factoryAddress,
|
|
215
|
+
(0, import_solidity.keccak256)(["bytes"], [(0, import_solidity.pack)(["address", "address"], [token0.address, token1.address])]),
|
|
216
|
+
INIT_CODE_HASH_MAP[token0.chainId]
|
|
217
|
+
)
|
|
172
218
|
};
|
|
173
219
|
}
|
|
174
220
|
return PAIR_ADDRESS_CACHE[key];
|
|
@@ -179,24 +225,47 @@ var Pair = class {
|
|
|
179
225
|
}
|
|
180
226
|
constructor(currencyAmountA, tokenAmountB) {
|
|
181
227
|
const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) ? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
|
|
182
|
-
this.liquidityToken = new ERC20Token(
|
|
228
|
+
this.liquidityToken = new ERC20Token(
|
|
229
|
+
tokenAmounts[0].currency.chainId,
|
|
230
|
+
Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency),
|
|
231
|
+
18,
|
|
232
|
+
"Cake-LP",
|
|
233
|
+
"Pancake LPs"
|
|
234
|
+
);
|
|
183
235
|
this.tokenAmounts = tokenAmounts;
|
|
184
236
|
}
|
|
237
|
+
/**
|
|
238
|
+
* Returns true if the token is either token0 or token1
|
|
239
|
+
* @param token to check
|
|
240
|
+
*/
|
|
185
241
|
involvesToken(token) {
|
|
186
242
|
return token.equals(this.token0) || token.equals(this.token1);
|
|
187
243
|
}
|
|
244
|
+
/**
|
|
245
|
+
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
|
|
246
|
+
*/
|
|
188
247
|
get token0Price() {
|
|
189
248
|
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
190
249
|
return new import_swap_sdk_core3.Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
191
250
|
}
|
|
251
|
+
/**
|
|
252
|
+
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
|
|
253
|
+
*/
|
|
192
254
|
get token1Price() {
|
|
193
255
|
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
194
256
|
return new import_swap_sdk_core3.Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
195
257
|
}
|
|
258
|
+
/**
|
|
259
|
+
* Return the price of the given token in terms of the other token in the pair.
|
|
260
|
+
* @param token token to return price of
|
|
261
|
+
*/
|
|
196
262
|
priceOf(token) {
|
|
197
263
|
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
198
264
|
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
199
265
|
}
|
|
266
|
+
/**
|
|
267
|
+
* Returns the chain ID of the tokens in the pair.
|
|
268
|
+
*/
|
|
200
269
|
get chainId() {
|
|
201
270
|
return this.token0.chainId;
|
|
202
271
|
}
|
|
@@ -226,7 +295,10 @@ var Pair = class {
|
|
|
226
295
|
const inputAmountWithFee = import_jsbi.default.multiply(inputAmount.quotient, import_swap_sdk_core3._9975);
|
|
227
296
|
const numerator = import_jsbi.default.multiply(inputAmountWithFee, outputReserve.quotient);
|
|
228
297
|
const denominator = import_jsbi.default.add(import_jsbi.default.multiply(inputReserve.quotient, import_swap_sdk_core3._10000), inputAmountWithFee);
|
|
229
|
-
const outputAmount = import_swap_sdk_core3.CurrencyAmount.fromRawAmount(
|
|
298
|
+
const outputAmount = import_swap_sdk_core3.CurrencyAmount.fromRawAmount(
|
|
299
|
+
inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
300
|
+
import_jsbi.default.divide(numerator, denominator)
|
|
301
|
+
);
|
|
230
302
|
if (import_jsbi.default.equal(outputAmount.quotient, import_swap_sdk_core3.ZERO)) {
|
|
231
303
|
throw new import_swap_sdk_core3.InsufficientInputAmountError();
|
|
232
304
|
}
|
|
@@ -241,7 +313,10 @@ var Pair = class {
|
|
|
241
313
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
242
314
|
const numerator = import_jsbi.default.multiply(import_jsbi.default.multiply(inputReserve.quotient, outputAmount.quotient), import_swap_sdk_core3._10000);
|
|
243
315
|
const denominator = import_jsbi.default.multiply(import_jsbi.default.subtract(outputReserve.quotient, outputAmount.quotient), import_swap_sdk_core3._9975);
|
|
244
|
-
const inputAmount = import_swap_sdk_core3.CurrencyAmount.fromRawAmount(
|
|
316
|
+
const inputAmount = import_swap_sdk_core3.CurrencyAmount.fromRawAmount(
|
|
317
|
+
outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
318
|
+
import_jsbi.default.add(import_jsbi.default.divide(numerator, denominator), import_swap_sdk_core3.ONE)
|
|
319
|
+
);
|
|
245
320
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
246
321
|
}
|
|
247
322
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
@@ -250,7 +325,10 @@ var Pair = class {
|
|
|
250
325
|
(0, import_tiny_invariant2.default)(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
251
326
|
let liquidity;
|
|
252
327
|
if (import_jsbi.default.equal(totalSupply.quotient, import_swap_sdk_core3.ZERO)) {
|
|
253
|
-
liquidity = import_jsbi.default.subtract(
|
|
328
|
+
liquidity = import_jsbi.default.subtract(
|
|
329
|
+
(0, import_swap_sdk_core3.sqrt)(import_jsbi.default.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)),
|
|
330
|
+
import_swap_sdk_core3.MINIMUM_LIQUIDITY
|
|
331
|
+
);
|
|
254
332
|
} else {
|
|
255
333
|
const amount0 = import_jsbi.default.divide(import_jsbi.default.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
256
334
|
const amount1 = import_jsbi.default.divide(import_jsbi.default.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
@@ -287,7 +365,10 @@ var Pair = class {
|
|
|
287
365
|
totalSupplyAdjusted = totalSupply;
|
|
288
366
|
}
|
|
289
367
|
}
|
|
290
|
-
return import_swap_sdk_core3.CurrencyAmount.fromRawAmount(
|
|
368
|
+
return import_swap_sdk_core3.CurrencyAmount.fromRawAmount(
|
|
369
|
+
token,
|
|
370
|
+
import_jsbi.default.divide(import_jsbi.default.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient)
|
|
371
|
+
);
|
|
291
372
|
}
|
|
292
373
|
};
|
|
293
374
|
|
|
@@ -299,7 +380,10 @@ var Route = class {
|
|
|
299
380
|
this._midPrice = null;
|
|
300
381
|
(0, import_tiny_invariant3.default)(pairs.length > 0, "PAIRS");
|
|
301
382
|
const chainId = pairs[0].chainId;
|
|
302
|
-
(0, import_tiny_invariant3.default)(
|
|
383
|
+
(0, import_tiny_invariant3.default)(
|
|
384
|
+
pairs.every((pair) => pair.chainId === chainId),
|
|
385
|
+
"CHAIN_IDS"
|
|
386
|
+
);
|
|
303
387
|
const wrappedInput = input.wrapped;
|
|
304
388
|
(0, import_tiny_invariant3.default)(pairs[0].involvesToken(wrappedInput), "INPUT");
|
|
305
389
|
(0, import_tiny_invariant3.default)(typeof output === "undefined" || pairs[pairs.length - 1].involvesToken(output.wrapped), "OUTPUT");
|
|
@@ -320,7 +404,9 @@ var Route = class {
|
|
|
320
404
|
return this._midPrice;
|
|
321
405
|
const prices = [];
|
|
322
406
|
for (const [i, pair] of this.pairs.entries()) {
|
|
323
|
-
prices.push(
|
|
407
|
+
prices.push(
|
|
408
|
+
this.path[i].equals(pair.token0) ? new import_swap_sdk_core4.Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient) : new import_swap_sdk_core4.Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient)
|
|
409
|
+
);
|
|
324
410
|
}
|
|
325
411
|
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
326
412
|
return this._midPrice = new import_swap_sdk_core4.Price(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
@@ -364,9 +450,19 @@ function tradeComparator(a, b) {
|
|
|
364
450
|
return a.route.path.length - b.route.path.length;
|
|
365
451
|
}
|
|
366
452
|
var Trade = class {
|
|
453
|
+
/**
|
|
454
|
+
* Constructs an exact in trade with the given amount in and route
|
|
455
|
+
* @param route route of the exact in trade
|
|
456
|
+
* @param amountIn the amount being passed in
|
|
457
|
+
*/
|
|
367
458
|
static exactIn(route, amountIn) {
|
|
368
459
|
return new Trade(route, amountIn, import_swap_sdk_core5.TradeType.EXACT_INPUT);
|
|
369
460
|
}
|
|
461
|
+
/**
|
|
462
|
+
* Constructs an exact out trade with the given amount out and route
|
|
463
|
+
* @param route route of the exact out trade
|
|
464
|
+
* @param amountOut the amount returned by the trade
|
|
465
|
+
*/
|
|
370
466
|
static exactOut(route, amountOut) {
|
|
371
467
|
return new Trade(route, amountOut, import_swap_sdk_core5.TradeType.EXACT_OUTPUT);
|
|
372
468
|
}
|
|
@@ -383,7 +479,11 @@ var Trade = class {
|
|
|
383
479
|
tokenAmounts[i + 1] = outputAmount;
|
|
384
480
|
}
|
|
385
481
|
this.inputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
386
|
-
this.outputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(
|
|
482
|
+
this.outputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(
|
|
483
|
+
route.output,
|
|
484
|
+
tokenAmounts[tokenAmounts.length - 1].numerator,
|
|
485
|
+
tokenAmounts[tokenAmounts.length - 1].denominator
|
|
486
|
+
);
|
|
387
487
|
} else {
|
|
388
488
|
(0, import_tiny_invariant4.default)(amount.currency.equals(route.output), "OUTPUT");
|
|
389
489
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
@@ -392,12 +492,25 @@ var Trade = class {
|
|
|
392
492
|
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
393
493
|
tokenAmounts[i - 1] = inputAmount;
|
|
394
494
|
}
|
|
395
|
-
this.inputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(
|
|
495
|
+
this.inputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(
|
|
496
|
+
route.input,
|
|
497
|
+
tokenAmounts[0].numerator,
|
|
498
|
+
tokenAmounts[0].denominator
|
|
499
|
+
);
|
|
396
500
|
this.outputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
397
501
|
}
|
|
398
|
-
this.executionPrice = new import_swap_sdk_core5.Price(
|
|
502
|
+
this.executionPrice = new import_swap_sdk_core5.Price(
|
|
503
|
+
this.inputAmount.currency,
|
|
504
|
+
this.outputAmount.currency,
|
|
505
|
+
this.inputAmount.quotient,
|
|
506
|
+
this.outputAmount.quotient
|
|
507
|
+
);
|
|
399
508
|
this.priceImpact = (0, import_swap_sdk_core5.computePriceImpact)(route.midPrice, this.inputAmount, this.outputAmount);
|
|
400
509
|
}
|
|
510
|
+
/**
|
|
511
|
+
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
512
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
513
|
+
*/
|
|
401
514
|
minimumAmountOut(slippageTolerance) {
|
|
402
515
|
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(import_swap_sdk_core5.ZERO), "SLIPPAGE_TOLERANCE");
|
|
403
516
|
if (this.tradeType === import_swap_sdk_core5.TradeType.EXACT_OUTPUT) {
|
|
@@ -406,6 +519,10 @@ var Trade = class {
|
|
|
406
519
|
const slippageAdjustedAmountOut = new import_swap_sdk_core5.Fraction(import_swap_sdk_core5.ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
407
520
|
return import_swap_sdk_core5.CurrencyAmount.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
408
521
|
}
|
|
522
|
+
/**
|
|
523
|
+
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
524
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
525
|
+
*/
|
|
409
526
|
maximumAmountIn(slippageTolerance) {
|
|
410
527
|
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(import_swap_sdk_core5.ZERO), "SLIPPAGE_TOLERANCE");
|
|
411
528
|
if (this.tradeType === import_swap_sdk_core5.TradeType.EXACT_INPUT) {
|
|
@@ -414,6 +531,20 @@ var Trade = class {
|
|
|
414
531
|
const slippageAdjustedAmountIn = new import_swap_sdk_core5.Fraction(import_swap_sdk_core5.ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
415
532
|
return import_swap_sdk_core5.CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
416
533
|
}
|
|
534
|
+
/**
|
|
535
|
+
* Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
|
|
536
|
+
* amount to an output token, making at most `maxHops` hops.
|
|
537
|
+
* Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
|
|
538
|
+
* the amount in among multiple routes.
|
|
539
|
+
* @param pairs the pairs to consider in finding the best trade
|
|
540
|
+
* @param nextAmountIn exact amount of input currency to spend
|
|
541
|
+
* @param currencyOut the desired currency out
|
|
542
|
+
* @param maxNumResults maximum number of results to return
|
|
543
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
544
|
+
* @param currentPairs used in recursion; the current list of pairs
|
|
545
|
+
* @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
|
|
546
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
547
|
+
*/
|
|
417
548
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
418
549
|
(0, import_tiny_invariant4.default)(pairs.length > 0, "PAIRS");
|
|
419
550
|
(0, import_tiny_invariant4.default)(maxHops > 0, "MAX_HOPS");
|
|
@@ -437,20 +568,61 @@ var Trade = class {
|
|
|
437
568
|
throw error;
|
|
438
569
|
}
|
|
439
570
|
if (amountOut.currency.equals(tokenOut)) {
|
|
440
|
-
(0, import_swap_sdk_core5.sortedInsert)(
|
|
571
|
+
(0, import_swap_sdk_core5.sortedInsert)(
|
|
572
|
+
bestTrades,
|
|
573
|
+
new Trade(
|
|
574
|
+
new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut),
|
|
575
|
+
currencyAmountIn,
|
|
576
|
+
import_swap_sdk_core5.TradeType.EXACT_INPUT
|
|
577
|
+
),
|
|
578
|
+
maxNumResults,
|
|
579
|
+
tradeComparator
|
|
580
|
+
);
|
|
441
581
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
442
582
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
443
|
-
Trade.bestTradeExactIn(
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
583
|
+
Trade.bestTradeExactIn(
|
|
584
|
+
pairsExcludingThisPair,
|
|
585
|
+
currencyAmountIn,
|
|
586
|
+
currencyOut,
|
|
587
|
+
{
|
|
588
|
+
maxNumResults,
|
|
589
|
+
maxHops: maxHops - 1
|
|
590
|
+
},
|
|
591
|
+
[...currentPairs, pair],
|
|
592
|
+
amountOut,
|
|
593
|
+
bestTrades
|
|
594
|
+
);
|
|
447
595
|
}
|
|
448
596
|
}
|
|
449
597
|
return bestTrades;
|
|
450
598
|
}
|
|
599
|
+
/**
|
|
600
|
+
* Return the execution price after accounting for slippage tolerance
|
|
601
|
+
* @param slippageTolerance the allowed tolerated slippage
|
|
602
|
+
*/
|
|
451
603
|
worstExecutionPrice(slippageTolerance) {
|
|
452
|
-
return new import_swap_sdk_core5.Price(
|
|
453
|
-
|
|
604
|
+
return new import_swap_sdk_core5.Price(
|
|
605
|
+
this.inputAmount.currency,
|
|
606
|
+
this.outputAmount.currency,
|
|
607
|
+
this.maximumAmountIn(slippageTolerance).quotient,
|
|
608
|
+
this.minimumAmountOut(slippageTolerance).quotient
|
|
609
|
+
);
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* similar to the above method but instead targets a fixed output amount
|
|
613
|
+
* given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
|
|
614
|
+
* to an output token amount, making at most `maxHops` hops
|
|
615
|
+
* note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
|
|
616
|
+
* the amount in among multiple routes.
|
|
617
|
+
* @param pairs the pairs to consider in finding the best trade
|
|
618
|
+
* @param currencyIn the currency to spend
|
|
619
|
+
* @param nextAmountOut the exact amount of currency out
|
|
620
|
+
* @param maxNumResults maximum number of results to return
|
|
621
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
622
|
+
* @param currentPairs used in recursion; the current list of pairs
|
|
623
|
+
* @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter
|
|
624
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
625
|
+
*/
|
|
454
626
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
455
627
|
(0, import_tiny_invariant4.default)(pairs.length > 0, "PAIRS");
|
|
456
628
|
(0, import_tiny_invariant4.default)(maxHops > 0, "MAX_HOPS");
|
|
@@ -474,13 +646,30 @@ var Trade = class {
|
|
|
474
646
|
throw error;
|
|
475
647
|
}
|
|
476
648
|
if (amountIn.currency.equals(tokenIn)) {
|
|
477
|
-
(0, import_swap_sdk_core5.sortedInsert)(
|
|
649
|
+
(0, import_swap_sdk_core5.sortedInsert)(
|
|
650
|
+
bestTrades,
|
|
651
|
+
new Trade(
|
|
652
|
+
new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency),
|
|
653
|
+
currencyAmountOut,
|
|
654
|
+
import_swap_sdk_core5.TradeType.EXACT_OUTPUT
|
|
655
|
+
),
|
|
656
|
+
maxNumResults,
|
|
657
|
+
tradeComparator
|
|
658
|
+
);
|
|
478
659
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
479
660
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
480
|
-
Trade.bestTradeExactOut(
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
661
|
+
Trade.bestTradeExactOut(
|
|
662
|
+
pairsExcludingThisPair,
|
|
663
|
+
currencyIn,
|
|
664
|
+
currencyAmountOut,
|
|
665
|
+
{
|
|
666
|
+
maxNumResults,
|
|
667
|
+
maxHops: maxHops - 1
|
|
668
|
+
},
|
|
669
|
+
[pair, ...currentPairs],
|
|
670
|
+
amountIn,
|
|
671
|
+
bestTrades
|
|
672
|
+
);
|
|
484
673
|
}
|
|
485
674
|
}
|
|
486
675
|
return bestTrades;
|
|
@@ -527,8 +716,16 @@ function toHex(currencyAmount) {
|
|
|
527
716
|
}
|
|
528
717
|
var ZERO_HEX = "0x0";
|
|
529
718
|
var Router = class {
|
|
719
|
+
/**
|
|
720
|
+
* Cannot be constructed.
|
|
721
|
+
*/
|
|
530
722
|
constructor() {
|
|
531
723
|
}
|
|
724
|
+
/**
|
|
725
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
726
|
+
* @param trade to produce call parameters for
|
|
727
|
+
* @param options options for the call parameters
|
|
728
|
+
*/
|
|
532
729
|
static swapCallParameters(trade, options) {
|
|
533
730
|
const etherIn = trade.inputAmount.currency.isNative;
|
|
534
731
|
const etherOut = trade.outputAmount.currency.isNative;
|
|
@@ -538,7 +735,7 @@ var Router = class {
|
|
|
538
735
|
const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
|
|
539
736
|
const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
|
|
540
737
|
const path = trade.route.path.map((token) => token.address);
|
|
541
|
-
const deadline = "ttl" in options ? `0x${(Math.floor(new Date().getTime() / 1e3) + options.ttl).toString(16)}` : `0x${options.deadline.toString(16)}`;
|
|
738
|
+
const deadline = "ttl" in options ? `0x${(Math.floor((/* @__PURE__ */ new Date()).getTime() / 1e3) + options.ttl).toString(16)}` : `0x${options.deadline.toString(16)}`;
|
|
542
739
|
const useFeeOnTransfer = Boolean(options.feeOnTransfer);
|
|
543
740
|
let methodName;
|
|
544
741
|
let args;
|
package/dist/index.mjs
CHANGED
|
@@ -55,13 +55,48 @@ var INIT_CODE_HASH_MAP = {
|
|
|
55
55
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
56
56
|
};
|
|
57
57
|
var WETH9 = {
|
|
58
|
-
[1 /* ETHEREUM */]: new ERC20Token(
|
|
59
|
-
|
|
58
|
+
[1 /* ETHEREUM */]: new ERC20Token(
|
|
59
|
+
1 /* ETHEREUM */,
|
|
60
|
+
"0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
|
|
61
|
+
18,
|
|
62
|
+
"WETH",
|
|
63
|
+
"Wrapped Ether",
|
|
64
|
+
"https://weth.io"
|
|
65
|
+
),
|
|
66
|
+
[5 /* GOERLI */]: new ERC20Token(
|
|
67
|
+
5 /* GOERLI */,
|
|
68
|
+
"0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6",
|
|
69
|
+
18,
|
|
70
|
+
"WETH",
|
|
71
|
+
"Wrapped Ether",
|
|
72
|
+
"https://weth.io"
|
|
73
|
+
)
|
|
60
74
|
};
|
|
61
75
|
var WBNB = {
|
|
62
|
-
[1 /* ETHEREUM */]: new ERC20Token(
|
|
63
|
-
|
|
64
|
-
|
|
76
|
+
[1 /* ETHEREUM */]: new ERC20Token(
|
|
77
|
+
1 /* ETHEREUM */,
|
|
78
|
+
"0x418D75f65a02b3D53B2418FB8E1fe493759c7605",
|
|
79
|
+
18,
|
|
80
|
+
"WBNB",
|
|
81
|
+
"Wrapped BNB",
|
|
82
|
+
"https://www.binance.org"
|
|
83
|
+
),
|
|
84
|
+
[56 /* BSC */]: new ERC20Token(
|
|
85
|
+
56 /* BSC */,
|
|
86
|
+
"0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c",
|
|
87
|
+
18,
|
|
88
|
+
"WBNB",
|
|
89
|
+
"Wrapped BNB",
|
|
90
|
+
"https://www.binance.org"
|
|
91
|
+
),
|
|
92
|
+
[97 /* BSC_TESTNET */]: new ERC20Token(
|
|
93
|
+
97 /* BSC_TESTNET */,
|
|
94
|
+
"0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd",
|
|
95
|
+
18,
|
|
96
|
+
"WBNB",
|
|
97
|
+
"Wrapped BNB",
|
|
98
|
+
"https://www.binance.org"
|
|
99
|
+
)
|
|
65
100
|
};
|
|
66
101
|
var WNATIVE = {
|
|
67
102
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
@@ -131,7 +166,11 @@ var computePairAddress = ({
|
|
|
131
166
|
if ((PAIR_ADDRESS_CACHE == null ? void 0 : PAIR_ADDRESS_CACHE[key]) === void 0) {
|
|
132
167
|
PAIR_ADDRESS_CACHE = {
|
|
133
168
|
...PAIR_ADDRESS_CACHE,
|
|
134
|
-
[key]: getCreate2Address(
|
|
169
|
+
[key]: getCreate2Address(
|
|
170
|
+
factoryAddress,
|
|
171
|
+
keccak256(["bytes"], [pack(["address", "address"], [token0.address, token1.address])]),
|
|
172
|
+
INIT_CODE_HASH_MAP[token0.chainId]
|
|
173
|
+
)
|
|
135
174
|
};
|
|
136
175
|
}
|
|
137
176
|
return PAIR_ADDRESS_CACHE[key];
|
|
@@ -142,24 +181,47 @@ var Pair = class {
|
|
|
142
181
|
}
|
|
143
182
|
constructor(currencyAmountA, tokenAmountB) {
|
|
144
183
|
const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) ? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
|
|
145
|
-
this.liquidityToken = new ERC20Token(
|
|
184
|
+
this.liquidityToken = new ERC20Token(
|
|
185
|
+
tokenAmounts[0].currency.chainId,
|
|
186
|
+
Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency),
|
|
187
|
+
18,
|
|
188
|
+
"Cake-LP",
|
|
189
|
+
"Pancake LPs"
|
|
190
|
+
);
|
|
146
191
|
this.tokenAmounts = tokenAmounts;
|
|
147
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Returns true if the token is either token0 or token1
|
|
195
|
+
* @param token to check
|
|
196
|
+
*/
|
|
148
197
|
involvesToken(token) {
|
|
149
198
|
return token.equals(this.token0) || token.equals(this.token1);
|
|
150
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Returns the current mid price of the pair in terms of token0, i.e. the ratio of reserve1 to reserve0
|
|
202
|
+
*/
|
|
151
203
|
get token0Price() {
|
|
152
204
|
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
153
205
|
return new Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
154
206
|
}
|
|
207
|
+
/**
|
|
208
|
+
* Returns the current mid price of the pair in terms of token1, i.e. the ratio of reserve0 to reserve1
|
|
209
|
+
*/
|
|
155
210
|
get token1Price() {
|
|
156
211
|
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
157
212
|
return new Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
158
213
|
}
|
|
214
|
+
/**
|
|
215
|
+
* Return the price of the given token in terms of the other token in the pair.
|
|
216
|
+
* @param token token to return price of
|
|
217
|
+
*/
|
|
159
218
|
priceOf(token) {
|
|
160
219
|
invariant2(this.involvesToken(token), "TOKEN");
|
|
161
220
|
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
162
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Returns the chain ID of the tokens in the pair.
|
|
224
|
+
*/
|
|
163
225
|
get chainId() {
|
|
164
226
|
return this.token0.chainId;
|
|
165
227
|
}
|
|
@@ -189,7 +251,10 @@ var Pair = class {
|
|
|
189
251
|
const inputAmountWithFee = JSBI.multiply(inputAmount.quotient, _9975);
|
|
190
252
|
const numerator = JSBI.multiply(inputAmountWithFee, outputReserve.quotient);
|
|
191
253
|
const denominator = JSBI.add(JSBI.multiply(inputReserve.quotient, _10000), inputAmountWithFee);
|
|
192
|
-
const outputAmount = CurrencyAmount.fromRawAmount(
|
|
254
|
+
const outputAmount = CurrencyAmount.fromRawAmount(
|
|
255
|
+
inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
256
|
+
JSBI.divide(numerator, denominator)
|
|
257
|
+
);
|
|
193
258
|
if (JSBI.equal(outputAmount.quotient, ZERO)) {
|
|
194
259
|
throw new InsufficientInputAmountError();
|
|
195
260
|
}
|
|
@@ -204,7 +269,10 @@ var Pair = class {
|
|
|
204
269
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
205
270
|
const numerator = JSBI.multiply(JSBI.multiply(inputReserve.quotient, outputAmount.quotient), _10000);
|
|
206
271
|
const denominator = JSBI.multiply(JSBI.subtract(outputReserve.quotient, outputAmount.quotient), _9975);
|
|
207
|
-
const inputAmount = CurrencyAmount.fromRawAmount(
|
|
272
|
+
const inputAmount = CurrencyAmount.fromRawAmount(
|
|
273
|
+
outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
274
|
+
JSBI.add(JSBI.divide(numerator, denominator), ONE)
|
|
275
|
+
);
|
|
208
276
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
209
277
|
}
|
|
210
278
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
@@ -213,7 +281,10 @@ var Pair = class {
|
|
|
213
281
|
invariant2(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
214
282
|
let liquidity;
|
|
215
283
|
if (JSBI.equal(totalSupply.quotient, ZERO)) {
|
|
216
|
-
liquidity = JSBI.subtract(
|
|
284
|
+
liquidity = JSBI.subtract(
|
|
285
|
+
sqrt(JSBI.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)),
|
|
286
|
+
MINIMUM_LIQUIDITY
|
|
287
|
+
);
|
|
217
288
|
} else {
|
|
218
289
|
const amount0 = JSBI.divide(JSBI.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
219
290
|
const amount1 = JSBI.divide(JSBI.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
@@ -250,7 +321,10 @@ var Pair = class {
|
|
|
250
321
|
totalSupplyAdjusted = totalSupply;
|
|
251
322
|
}
|
|
252
323
|
}
|
|
253
|
-
return CurrencyAmount.fromRawAmount(
|
|
324
|
+
return CurrencyAmount.fromRawAmount(
|
|
325
|
+
token,
|
|
326
|
+
JSBI.divide(JSBI.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient)
|
|
327
|
+
);
|
|
254
328
|
}
|
|
255
329
|
};
|
|
256
330
|
|
|
@@ -262,7 +336,10 @@ var Route = class {
|
|
|
262
336
|
this._midPrice = null;
|
|
263
337
|
invariant3(pairs.length > 0, "PAIRS");
|
|
264
338
|
const chainId = pairs[0].chainId;
|
|
265
|
-
invariant3(
|
|
339
|
+
invariant3(
|
|
340
|
+
pairs.every((pair) => pair.chainId === chainId),
|
|
341
|
+
"CHAIN_IDS"
|
|
342
|
+
);
|
|
266
343
|
const wrappedInput = input.wrapped;
|
|
267
344
|
invariant3(pairs[0].involvesToken(wrappedInput), "INPUT");
|
|
268
345
|
invariant3(typeof output === "undefined" || pairs[pairs.length - 1].involvesToken(output.wrapped), "OUTPUT");
|
|
@@ -283,7 +360,9 @@ var Route = class {
|
|
|
283
360
|
return this._midPrice;
|
|
284
361
|
const prices = [];
|
|
285
362
|
for (const [i, pair] of this.pairs.entries()) {
|
|
286
|
-
prices.push(
|
|
363
|
+
prices.push(
|
|
364
|
+
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)
|
|
365
|
+
);
|
|
287
366
|
}
|
|
288
367
|
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
289
368
|
return this._midPrice = new Price2(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
@@ -336,9 +415,19 @@ function tradeComparator(a, b) {
|
|
|
336
415
|
return a.route.path.length - b.route.path.length;
|
|
337
416
|
}
|
|
338
417
|
var Trade = class {
|
|
418
|
+
/**
|
|
419
|
+
* Constructs an exact in trade with the given amount in and route
|
|
420
|
+
* @param route route of the exact in trade
|
|
421
|
+
* @param amountIn the amount being passed in
|
|
422
|
+
*/
|
|
339
423
|
static exactIn(route, amountIn) {
|
|
340
424
|
return new Trade(route, amountIn, TradeType.EXACT_INPUT);
|
|
341
425
|
}
|
|
426
|
+
/**
|
|
427
|
+
* Constructs an exact out trade with the given amount out and route
|
|
428
|
+
* @param route route of the exact out trade
|
|
429
|
+
* @param amountOut the amount returned by the trade
|
|
430
|
+
*/
|
|
342
431
|
static exactOut(route, amountOut) {
|
|
343
432
|
return new Trade(route, amountOut, TradeType.EXACT_OUTPUT);
|
|
344
433
|
}
|
|
@@ -355,7 +444,11 @@ var Trade = class {
|
|
|
355
444
|
tokenAmounts[i + 1] = outputAmount;
|
|
356
445
|
}
|
|
357
446
|
this.inputAmount = CurrencyAmount2.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
358
|
-
this.outputAmount = CurrencyAmount2.fromFractionalAmount(
|
|
447
|
+
this.outputAmount = CurrencyAmount2.fromFractionalAmount(
|
|
448
|
+
route.output,
|
|
449
|
+
tokenAmounts[tokenAmounts.length - 1].numerator,
|
|
450
|
+
tokenAmounts[tokenAmounts.length - 1].denominator
|
|
451
|
+
);
|
|
359
452
|
} else {
|
|
360
453
|
invariant4(amount.currency.equals(route.output), "OUTPUT");
|
|
361
454
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
@@ -364,12 +457,25 @@ var Trade = class {
|
|
|
364
457
|
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
365
458
|
tokenAmounts[i - 1] = inputAmount;
|
|
366
459
|
}
|
|
367
|
-
this.inputAmount = CurrencyAmount2.fromFractionalAmount(
|
|
460
|
+
this.inputAmount = CurrencyAmount2.fromFractionalAmount(
|
|
461
|
+
route.input,
|
|
462
|
+
tokenAmounts[0].numerator,
|
|
463
|
+
tokenAmounts[0].denominator
|
|
464
|
+
);
|
|
368
465
|
this.outputAmount = CurrencyAmount2.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
369
466
|
}
|
|
370
|
-
this.executionPrice = new Price3(
|
|
467
|
+
this.executionPrice = new Price3(
|
|
468
|
+
this.inputAmount.currency,
|
|
469
|
+
this.outputAmount.currency,
|
|
470
|
+
this.inputAmount.quotient,
|
|
471
|
+
this.outputAmount.quotient
|
|
472
|
+
);
|
|
371
473
|
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
372
474
|
}
|
|
475
|
+
/**
|
|
476
|
+
* Get the minimum amount that must be received from this trade for the given slippage tolerance
|
|
477
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
478
|
+
*/
|
|
373
479
|
minimumAmountOut(slippageTolerance) {
|
|
374
480
|
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
375
481
|
if (this.tradeType === TradeType.EXACT_OUTPUT) {
|
|
@@ -378,6 +484,10 @@ var Trade = class {
|
|
|
378
484
|
const slippageAdjustedAmountOut = new Fraction(ONE2).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
379
485
|
return CurrencyAmount2.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
380
486
|
}
|
|
487
|
+
/**
|
|
488
|
+
* Get the maximum amount in that can be spent via this trade for the given slippage tolerance
|
|
489
|
+
* @param slippageTolerance tolerance of unfavorable slippage from the execution price of this trade
|
|
490
|
+
*/
|
|
381
491
|
maximumAmountIn(slippageTolerance) {
|
|
382
492
|
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
383
493
|
if (this.tradeType === TradeType.EXACT_INPUT) {
|
|
@@ -386,6 +496,20 @@ var Trade = class {
|
|
|
386
496
|
const slippageAdjustedAmountIn = new Fraction(ONE2).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
387
497
|
return CurrencyAmount2.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
388
498
|
}
|
|
499
|
+
/**
|
|
500
|
+
* Given a list of pairs, and a fixed amount in, returns the top `maxNumResults` trades that go from an input token
|
|
501
|
+
* amount to an output token, making at most `maxHops` hops.
|
|
502
|
+
* Note this does not consider aggregation, as routes are linear. It's possible a better route exists by splitting
|
|
503
|
+
* the amount in among multiple routes.
|
|
504
|
+
* @param pairs the pairs to consider in finding the best trade
|
|
505
|
+
* @param nextAmountIn exact amount of input currency to spend
|
|
506
|
+
* @param currencyOut the desired currency out
|
|
507
|
+
* @param maxNumResults maximum number of results to return
|
|
508
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
509
|
+
* @param currentPairs used in recursion; the current list of pairs
|
|
510
|
+
* @param currencyAmountIn used in recursion; the original value of the currencyAmountIn parameter
|
|
511
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
512
|
+
*/
|
|
389
513
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
390
514
|
invariant4(pairs.length > 0, "PAIRS");
|
|
391
515
|
invariant4(maxHops > 0, "MAX_HOPS");
|
|
@@ -409,20 +533,61 @@ var Trade = class {
|
|
|
409
533
|
throw error;
|
|
410
534
|
}
|
|
411
535
|
if (amountOut.currency.equals(tokenOut)) {
|
|
412
|
-
sortedInsert(
|
|
536
|
+
sortedInsert(
|
|
537
|
+
bestTrades,
|
|
538
|
+
new Trade(
|
|
539
|
+
new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut),
|
|
540
|
+
currencyAmountIn,
|
|
541
|
+
TradeType.EXACT_INPUT
|
|
542
|
+
),
|
|
543
|
+
maxNumResults,
|
|
544
|
+
tradeComparator
|
|
545
|
+
);
|
|
413
546
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
414
547
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
415
|
-
Trade.bestTradeExactIn(
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
548
|
+
Trade.bestTradeExactIn(
|
|
549
|
+
pairsExcludingThisPair,
|
|
550
|
+
currencyAmountIn,
|
|
551
|
+
currencyOut,
|
|
552
|
+
{
|
|
553
|
+
maxNumResults,
|
|
554
|
+
maxHops: maxHops - 1
|
|
555
|
+
},
|
|
556
|
+
[...currentPairs, pair],
|
|
557
|
+
amountOut,
|
|
558
|
+
bestTrades
|
|
559
|
+
);
|
|
419
560
|
}
|
|
420
561
|
}
|
|
421
562
|
return bestTrades;
|
|
422
563
|
}
|
|
564
|
+
/**
|
|
565
|
+
* Return the execution price after accounting for slippage tolerance
|
|
566
|
+
* @param slippageTolerance the allowed tolerated slippage
|
|
567
|
+
*/
|
|
423
568
|
worstExecutionPrice(slippageTolerance) {
|
|
424
|
-
return new Price3(
|
|
425
|
-
|
|
569
|
+
return new Price3(
|
|
570
|
+
this.inputAmount.currency,
|
|
571
|
+
this.outputAmount.currency,
|
|
572
|
+
this.maximumAmountIn(slippageTolerance).quotient,
|
|
573
|
+
this.minimumAmountOut(slippageTolerance).quotient
|
|
574
|
+
);
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* similar to the above method but instead targets a fixed output amount
|
|
578
|
+
* given a list of pairs, and a fixed amount out, returns the top `maxNumResults` trades that go from an input token
|
|
579
|
+
* to an output token amount, making at most `maxHops` hops
|
|
580
|
+
* note this does not consider aggregation, as routes are linear. it's possible a better route exists by splitting
|
|
581
|
+
* the amount in among multiple routes.
|
|
582
|
+
* @param pairs the pairs to consider in finding the best trade
|
|
583
|
+
* @param currencyIn the currency to spend
|
|
584
|
+
* @param nextAmountOut the exact amount of currency out
|
|
585
|
+
* @param maxNumResults maximum number of results to return
|
|
586
|
+
* @param maxHops maximum number of hops a returned trade can make, e.g. 1 hop goes through a single pair
|
|
587
|
+
* @param currentPairs used in recursion; the current list of pairs
|
|
588
|
+
* @param currencyAmountOut used in recursion; the original value of the currencyAmountOut parameter
|
|
589
|
+
* @param bestTrades used in recursion; the current list of best trades
|
|
590
|
+
*/
|
|
426
591
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
427
592
|
invariant4(pairs.length > 0, "PAIRS");
|
|
428
593
|
invariant4(maxHops > 0, "MAX_HOPS");
|
|
@@ -446,13 +611,30 @@ var Trade = class {
|
|
|
446
611
|
throw error;
|
|
447
612
|
}
|
|
448
613
|
if (amountIn.currency.equals(tokenIn)) {
|
|
449
|
-
sortedInsert(
|
|
614
|
+
sortedInsert(
|
|
615
|
+
bestTrades,
|
|
616
|
+
new Trade(
|
|
617
|
+
new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency),
|
|
618
|
+
currencyAmountOut,
|
|
619
|
+
TradeType.EXACT_OUTPUT
|
|
620
|
+
),
|
|
621
|
+
maxNumResults,
|
|
622
|
+
tradeComparator
|
|
623
|
+
);
|
|
450
624
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
451
625
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
452
|
-
Trade.bestTradeExactOut(
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
626
|
+
Trade.bestTradeExactOut(
|
|
627
|
+
pairsExcludingThisPair,
|
|
628
|
+
currencyIn,
|
|
629
|
+
currencyAmountOut,
|
|
630
|
+
{
|
|
631
|
+
maxNumResults,
|
|
632
|
+
maxHops: maxHops - 1
|
|
633
|
+
},
|
|
634
|
+
[pair, ...currentPairs],
|
|
635
|
+
amountIn,
|
|
636
|
+
bestTrades
|
|
637
|
+
);
|
|
456
638
|
}
|
|
457
639
|
}
|
|
458
640
|
return bestTrades;
|
|
@@ -499,8 +681,16 @@ function toHex(currencyAmount) {
|
|
|
499
681
|
}
|
|
500
682
|
var ZERO_HEX = "0x0";
|
|
501
683
|
var Router = class {
|
|
684
|
+
/**
|
|
685
|
+
* Cannot be constructed.
|
|
686
|
+
*/
|
|
502
687
|
constructor() {
|
|
503
688
|
}
|
|
689
|
+
/**
|
|
690
|
+
* Produces the on-chain method name to call and the hex encoded parameters to pass as arguments for a given trade.
|
|
691
|
+
* @param trade to produce call parameters for
|
|
692
|
+
* @param options options for the call parameters
|
|
693
|
+
*/
|
|
504
694
|
static swapCallParameters(trade, options) {
|
|
505
695
|
const etherIn = trade.inputAmount.currency.isNative;
|
|
506
696
|
const etherOut = trade.outputAmount.currency.isNative;
|
|
@@ -510,7 +700,7 @@ var Router = class {
|
|
|
510
700
|
const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
|
|
511
701
|
const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
|
|
512
702
|
const path = trade.route.path.map((token) => token.address);
|
|
513
|
-
const deadline = "ttl" in options ? `0x${(Math.floor(new Date().getTime() / 1e3) + options.ttl).toString(16)}` : `0x${options.deadline.toString(16)}`;
|
|
703
|
+
const deadline = "ttl" in options ? `0x${(Math.floor((/* @__PURE__ */ new Date()).getTime() / 1e3) + options.ttl).toString(16)}` : `0x${options.deadline.toString(16)}`;
|
|
514
704
|
const useFeeOnTransfer = Boolean(options.feeOnTransfer);
|
|
515
705
|
let methodName;
|
|
516
706
|
let args;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pancakeswap/sdk",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "3.1.
|
|
4
|
+
"version": "3.1.5",
|
|
5
5
|
"description": "🛠 An SDK for building applications on top of Pancakeswap.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"typings": "dist/index.d.ts",
|
|
@@ -52,7 +52,7 @@
|
|
|
52
52
|
"@types/big.js": "^4.0.5",
|
|
53
53
|
"@types/jest": "^24.0.25",
|
|
54
54
|
"babel-plugin-transform-jsbi-to-bigint": "^1.3.1",
|
|
55
|
-
"tsup": "^
|
|
55
|
+
"tsup": "^6.6.3"
|
|
56
56
|
},
|
|
57
57
|
"engines": {
|
|
58
58
|
"node": ">=10"
|