@pancakeswap/sdk 3.1.2 → 3.1.4
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.d.ts +6 -2
- package/dist/index.js +85 -57
- package/dist/index.mjs +25 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { default as JSBI } from 'jsbi';
|
|
2
|
-
import { Token, CurrencyAmount, Price, BigintIsh, Currency, TradeType,
|
|
2
|
+
import { Token, Percent, CurrencyAmount, Price, BigintIsh, Currency, TradeType, NativeCurrency } from '@pancakeswap/swap-sdk-core';
|
|
3
3
|
export * from '@pancakeswap/swap-sdk-core';
|
|
4
4
|
|
|
5
5
|
declare class ERC20Token extends Token {
|
|
@@ -12,6 +12,8 @@ declare enum ChainId {
|
|
|
12
12
|
BSC = 56,
|
|
13
13
|
BSC_TESTNET = 97
|
|
14
14
|
}
|
|
15
|
+
declare const ZERO_PERCENT: Percent;
|
|
16
|
+
declare const ONE_HUNDRED_PERCENT: Percent;
|
|
15
17
|
declare const FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
16
18
|
declare const FACTORY_ADDRESS_MAP: Record<number, string>;
|
|
17
19
|
declare const INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5";
|
|
@@ -203,6 +205,8 @@ declare class Native extends NativeCurrency {
|
|
|
203
205
|
equals(other: Currency): boolean;
|
|
204
206
|
}
|
|
205
207
|
|
|
208
|
+
declare function isTradeBetter(tradeA: Trade<Currency, Currency, TradeType> | undefined | null, tradeB: Trade<Currency, Currency, TradeType> | undefined | null, minimumDelta?: Percent): boolean | undefined;
|
|
209
|
+
|
|
206
210
|
/**
|
|
207
211
|
* Options for producing the arguments to send call to the router.
|
|
208
212
|
*/
|
|
@@ -266,4 +270,4 @@ declare abstract class Router {
|
|
|
266
270
|
static swapCallParameters(trade: Trade<Currency, Currency, TradeType>, options: TradeOptions | TradeOptionsDeadline): SwapParameters;
|
|
267
271
|
}
|
|
268
272
|
|
|
269
|
-
export { BestTradeOptions, ChainId, ERC20Token, FACTORY_ADDRESS, FACTORY_ADDRESS_MAP, INIT_CODE_HASH, INIT_CODE_HASH_MAP, NATIVE, Native, Pair, Route, Router, SwapParameters, Trade, TradeOptions, TradeOptionsDeadline, WBNB, WETH9, WNATIVE, computePairAddress, inputOutputComparator, tradeComparator };
|
|
273
|
+
export { BestTradeOptions, ChainId, ERC20Token, FACTORY_ADDRESS, FACTORY_ADDRESS_MAP, INIT_CODE_HASH, INIT_CODE_HASH_MAP, NATIVE, Native, ONE_HUNDRED_PERCENT, Pair, Route, Router, SwapParameters, Trade, TradeOptions, TradeOptionsDeadline, WBNB, WETH9, WNATIVE, ZERO_PERCENT, computePairAddress, inputOutputComparator, isTradeBetter, tradeComparator };
|
package/dist/index.js
CHANGED
|
@@ -33,6 +33,7 @@ __export(src_exports, {
|
|
|
33
33
|
JSBI: () => import_jsbi2.default,
|
|
34
34
|
NATIVE: () => NATIVE,
|
|
35
35
|
Native: () => Native,
|
|
36
|
+
ONE_HUNDRED_PERCENT: () => ONE_HUNDRED_PERCENT,
|
|
36
37
|
Pair: () => Pair,
|
|
37
38
|
Route: () => Route,
|
|
38
39
|
Router: () => Router,
|
|
@@ -40,13 +41,18 @@ __export(src_exports, {
|
|
|
40
41
|
WBNB: () => WBNB,
|
|
41
42
|
WETH9: () => WETH9,
|
|
42
43
|
WNATIVE: () => WNATIVE,
|
|
44
|
+
ZERO_PERCENT: () => ZERO_PERCENT,
|
|
43
45
|
computePairAddress: () => computePairAddress,
|
|
44
46
|
inputOutputComparator: () => inputOutputComparator,
|
|
47
|
+
isTradeBetter: () => isTradeBetter,
|
|
45
48
|
tradeComparator: () => tradeComparator
|
|
46
49
|
});
|
|
47
50
|
module.exports = __toCommonJS(src_exports);
|
|
48
51
|
var import_jsbi2 = __toESM(require("jsbi"));
|
|
49
52
|
|
|
53
|
+
// src/constants.ts
|
|
54
|
+
var import_swap_sdk_core2 = require("@pancakeswap/swap-sdk-core");
|
|
55
|
+
|
|
50
56
|
// src/entities/token.ts
|
|
51
57
|
var import_swap_sdk_core = require("@pancakeswap/swap-sdk-core");
|
|
52
58
|
|
|
@@ -79,6 +85,8 @@ var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
|
79
85
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
80
86
|
return ChainId2;
|
|
81
87
|
})(ChainId || {});
|
|
88
|
+
var ZERO_PERCENT = new import_swap_sdk_core2.Percent("0");
|
|
89
|
+
var ONE_HUNDRED_PERCENT = new import_swap_sdk_core2.Percent("1");
|
|
82
90
|
var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
83
91
|
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
84
92
|
var FACTORY_ADDRESS_MAP = {
|
|
@@ -125,8 +133,25 @@ var NATIVE = {
|
|
|
125
133
|
}
|
|
126
134
|
};
|
|
127
135
|
|
|
136
|
+
// src/trade.ts
|
|
137
|
+
function isTradeBetter(tradeA, tradeB, minimumDelta = ZERO_PERCENT) {
|
|
138
|
+
if (tradeA && !tradeB)
|
|
139
|
+
return false;
|
|
140
|
+
if (tradeB && !tradeA)
|
|
141
|
+
return true;
|
|
142
|
+
if (!tradeA || !tradeB)
|
|
143
|
+
return void 0;
|
|
144
|
+
if (tradeA.tradeType !== tradeB.tradeType || !tradeA.inputAmount.currency.equals(tradeB.inputAmount.currency) || !tradeA.outputAmount.currency.equals(tradeB.outputAmount.currency)) {
|
|
145
|
+
throw new Error("Trades are not comparable");
|
|
146
|
+
}
|
|
147
|
+
if (minimumDelta.equalTo(ZERO_PERCENT)) {
|
|
148
|
+
return tradeA.executionPrice.lessThan(tradeB.executionPrice);
|
|
149
|
+
}
|
|
150
|
+
return tradeA.executionPrice.asFraction.multiply(minimumDelta.add(ONE_HUNDRED_PERCENT)).lessThan(tradeB.executionPrice);
|
|
151
|
+
}
|
|
152
|
+
|
|
128
153
|
// src/entities/pair.ts
|
|
129
|
-
var
|
|
154
|
+
var import_swap_sdk_core3 = require("@pancakeswap/swap-sdk-core");
|
|
130
155
|
var import_address2 = require("@ethersproject/address");
|
|
131
156
|
var import_solidity = require("@ethersproject/solidity");
|
|
132
157
|
var import_jsbi = __toESM(require("jsbi"));
|
|
@@ -162,11 +187,11 @@ var Pair = class {
|
|
|
162
187
|
}
|
|
163
188
|
get token0Price() {
|
|
164
189
|
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
165
|
-
return new
|
|
190
|
+
return new import_swap_sdk_core3.Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
166
191
|
}
|
|
167
192
|
get token1Price() {
|
|
168
193
|
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
169
|
-
return new
|
|
194
|
+
return new import_swap_sdk_core3.Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
170
195
|
}
|
|
171
196
|
priceOf(token) {
|
|
172
197
|
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
@@ -193,30 +218,30 @@ var Pair = class {
|
|
|
193
218
|
}
|
|
194
219
|
getOutputAmount(inputAmount) {
|
|
195
220
|
(0, import_tiny_invariant2.default)(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
196
|
-
if (import_jsbi.default.equal(this.reserve0.quotient,
|
|
197
|
-
throw new
|
|
221
|
+
if (import_jsbi.default.equal(this.reserve0.quotient, import_swap_sdk_core3.ZERO) || import_jsbi.default.equal(this.reserve1.quotient, import_swap_sdk_core3.ZERO)) {
|
|
222
|
+
throw new import_swap_sdk_core3.InsufficientReservesError();
|
|
198
223
|
}
|
|
199
224
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
200
225
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
201
|
-
const inputAmountWithFee = import_jsbi.default.multiply(inputAmount.quotient,
|
|
226
|
+
const inputAmountWithFee = import_jsbi.default.multiply(inputAmount.quotient, import_swap_sdk_core3._9975);
|
|
202
227
|
const numerator = import_jsbi.default.multiply(inputAmountWithFee, outputReserve.quotient);
|
|
203
|
-
const denominator = import_jsbi.default.add(import_jsbi.default.multiply(inputReserve.quotient,
|
|
204
|
-
const outputAmount =
|
|
205
|
-
if (import_jsbi.default.equal(outputAmount.quotient,
|
|
206
|
-
throw new
|
|
228
|
+
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(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, import_jsbi.default.divide(numerator, denominator));
|
|
230
|
+
if (import_jsbi.default.equal(outputAmount.quotient, import_swap_sdk_core3.ZERO)) {
|
|
231
|
+
throw new import_swap_sdk_core3.InsufficientInputAmountError();
|
|
207
232
|
}
|
|
208
233
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
209
234
|
}
|
|
210
235
|
getInputAmount(outputAmount) {
|
|
211
236
|
(0, import_tiny_invariant2.default)(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
212
|
-
if (import_jsbi.default.equal(this.reserve0.quotient,
|
|
213
|
-
throw new
|
|
237
|
+
if (import_jsbi.default.equal(this.reserve0.quotient, import_swap_sdk_core3.ZERO) || import_jsbi.default.equal(this.reserve1.quotient, import_swap_sdk_core3.ZERO) || import_jsbi.default.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient)) {
|
|
238
|
+
throw new import_swap_sdk_core3.InsufficientReservesError();
|
|
214
239
|
}
|
|
215
240
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
216
241
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
217
|
-
const numerator = import_jsbi.default.multiply(import_jsbi.default.multiply(inputReserve.quotient, outputAmount.quotient),
|
|
218
|
-
const denominator = import_jsbi.default.multiply(import_jsbi.default.subtract(outputReserve.quotient, outputAmount.quotient),
|
|
219
|
-
const inputAmount =
|
|
242
|
+
const numerator = import_jsbi.default.multiply(import_jsbi.default.multiply(inputReserve.quotient, outputAmount.quotient), import_swap_sdk_core3._10000);
|
|
243
|
+
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(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, import_jsbi.default.add(import_jsbi.default.divide(numerator, denominator), import_swap_sdk_core3.ONE));
|
|
220
245
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
221
246
|
}
|
|
222
247
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
@@ -224,17 +249,17 @@ var Pair = class {
|
|
|
224
249
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
225
250
|
(0, import_tiny_invariant2.default)(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
226
251
|
let liquidity;
|
|
227
|
-
if (import_jsbi.default.equal(totalSupply.quotient,
|
|
228
|
-
liquidity = import_jsbi.default.subtract((0,
|
|
252
|
+
if (import_jsbi.default.equal(totalSupply.quotient, import_swap_sdk_core3.ZERO)) {
|
|
253
|
+
liquidity = import_jsbi.default.subtract((0, import_swap_sdk_core3.sqrt)(import_jsbi.default.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), import_swap_sdk_core3.MINIMUM_LIQUIDITY);
|
|
229
254
|
} else {
|
|
230
255
|
const amount0 = import_jsbi.default.divide(import_jsbi.default.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
231
256
|
const amount1 = import_jsbi.default.divide(import_jsbi.default.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
232
257
|
liquidity = import_jsbi.default.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
|
|
233
258
|
}
|
|
234
|
-
if (!import_jsbi.default.greaterThan(liquidity,
|
|
235
|
-
throw new
|
|
259
|
+
if (!import_jsbi.default.greaterThan(liquidity, import_swap_sdk_core3.ZERO)) {
|
|
260
|
+
throw new import_swap_sdk_core3.InsufficientInputAmountError();
|
|
236
261
|
}
|
|
237
|
-
return
|
|
262
|
+
return import_swap_sdk_core3.CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
238
263
|
}
|
|
239
264
|
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
240
265
|
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
@@ -247,14 +272,14 @@ var Pair = class {
|
|
|
247
272
|
} else {
|
|
248
273
|
(0, import_tiny_invariant2.default)(!!kLast, "K_LAST");
|
|
249
274
|
const kLastParsed = import_jsbi.default.BigInt(kLast);
|
|
250
|
-
if (!import_jsbi.default.equal(kLastParsed,
|
|
251
|
-
const rootK = (0,
|
|
252
|
-
const rootKLast = (0,
|
|
275
|
+
if (!import_jsbi.default.equal(kLastParsed, import_swap_sdk_core3.ZERO)) {
|
|
276
|
+
const rootK = (0, import_swap_sdk_core3.sqrt)(import_jsbi.default.multiply(this.reserve0.quotient, this.reserve1.quotient));
|
|
277
|
+
const rootKLast = (0, import_swap_sdk_core3.sqrt)(kLastParsed);
|
|
253
278
|
if (import_jsbi.default.greaterThan(rootK, rootKLast)) {
|
|
254
279
|
const numerator = import_jsbi.default.multiply(totalSupply.quotient, import_jsbi.default.subtract(rootK, rootKLast));
|
|
255
|
-
const denominator = import_jsbi.default.add(import_jsbi.default.multiply(rootK,
|
|
280
|
+
const denominator = import_jsbi.default.add(import_jsbi.default.multiply(rootK, import_swap_sdk_core3.FIVE), rootKLast);
|
|
256
281
|
const feeLiquidity = import_jsbi.default.divide(numerator, denominator);
|
|
257
|
-
totalSupplyAdjusted = totalSupply.add(
|
|
282
|
+
totalSupplyAdjusted = totalSupply.add(import_swap_sdk_core3.CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
258
283
|
} else {
|
|
259
284
|
totalSupplyAdjusted = totalSupply;
|
|
260
285
|
}
|
|
@@ -262,13 +287,13 @@ var Pair = class {
|
|
|
262
287
|
totalSupplyAdjusted = totalSupply;
|
|
263
288
|
}
|
|
264
289
|
}
|
|
265
|
-
return
|
|
290
|
+
return import_swap_sdk_core3.CurrencyAmount.fromRawAmount(token, import_jsbi.default.divide(import_jsbi.default.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
|
|
266
291
|
}
|
|
267
292
|
};
|
|
268
293
|
|
|
269
294
|
// src/entities/route.ts
|
|
270
295
|
var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
|
|
271
|
-
var
|
|
296
|
+
var import_swap_sdk_core4 = require("@pancakeswap/swap-sdk-core");
|
|
272
297
|
var Route = class {
|
|
273
298
|
constructor(pairs, input, output) {
|
|
274
299
|
this._midPrice = null;
|
|
@@ -295,10 +320,10 @@ var Route = class {
|
|
|
295
320
|
return this._midPrice;
|
|
296
321
|
const prices = [];
|
|
297
322
|
for (const [i, pair] of this.pairs.entries()) {
|
|
298
|
-
prices.push(this.path[i].equals(pair.token0) ? new
|
|
323
|
+
prices.push(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));
|
|
299
324
|
}
|
|
300
325
|
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
301
|
-
return this._midPrice = new
|
|
326
|
+
return this._midPrice = new import_swap_sdk_core4.Price(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
302
327
|
}
|
|
303
328
|
get chainId() {
|
|
304
329
|
return this.pairs[0].chainId;
|
|
@@ -307,7 +332,7 @@ var Route = class {
|
|
|
307
332
|
|
|
308
333
|
// src/entities/trade.ts
|
|
309
334
|
var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
|
|
310
|
-
var
|
|
335
|
+
var import_swap_sdk_core5 = require("@pancakeswap/swap-sdk-core");
|
|
311
336
|
function inputOutputComparator(a, b) {
|
|
312
337
|
(0, import_tiny_invariant4.default)(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
|
|
313
338
|
(0, import_tiny_invariant4.default)(a.outputAmount.currency.equals(b.outputAmount.currency), "OUTPUT_CURRENCY");
|
|
@@ -340,16 +365,16 @@ function tradeComparator(a, b) {
|
|
|
340
365
|
}
|
|
341
366
|
var Trade = class {
|
|
342
367
|
static exactIn(route, amountIn) {
|
|
343
|
-
return new Trade(route, amountIn,
|
|
368
|
+
return new Trade(route, amountIn, import_swap_sdk_core5.TradeType.EXACT_INPUT);
|
|
344
369
|
}
|
|
345
370
|
static exactOut(route, amountOut) {
|
|
346
|
-
return new Trade(route, amountOut,
|
|
371
|
+
return new Trade(route, amountOut, import_swap_sdk_core5.TradeType.EXACT_OUTPUT);
|
|
347
372
|
}
|
|
348
373
|
constructor(route, amount, tradeType) {
|
|
349
374
|
this.route = route;
|
|
350
375
|
this.tradeType = tradeType;
|
|
351
376
|
const tokenAmounts = new Array(route.path.length);
|
|
352
|
-
if (tradeType ===
|
|
377
|
+
if (tradeType === import_swap_sdk_core5.TradeType.EXACT_INPUT) {
|
|
353
378
|
(0, import_tiny_invariant4.default)(amount.currency.equals(route.input), "INPUT");
|
|
354
379
|
tokenAmounts[0] = amount.wrapped;
|
|
355
380
|
for (let i = 0; i < route.path.length - 1; i++) {
|
|
@@ -357,8 +382,8 @@ var Trade = class {
|
|
|
357
382
|
const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
|
|
358
383
|
tokenAmounts[i + 1] = outputAmount;
|
|
359
384
|
}
|
|
360
|
-
this.inputAmount =
|
|
361
|
-
this.outputAmount =
|
|
385
|
+
this.inputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
386
|
+
this.outputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.output, tokenAmounts[tokenAmounts.length - 1].numerator, tokenAmounts[tokenAmounts.length - 1].denominator);
|
|
362
387
|
} else {
|
|
363
388
|
(0, import_tiny_invariant4.default)(amount.currency.equals(route.output), "OUTPUT");
|
|
364
389
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
@@ -367,27 +392,27 @@ var Trade = class {
|
|
|
367
392
|
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
368
393
|
tokenAmounts[i - 1] = inputAmount;
|
|
369
394
|
}
|
|
370
|
-
this.inputAmount =
|
|
371
|
-
this.outputAmount =
|
|
395
|
+
this.inputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.input, tokenAmounts[0].numerator, tokenAmounts[0].denominator);
|
|
396
|
+
this.outputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
372
397
|
}
|
|
373
|
-
this.executionPrice = new
|
|
374
|
-
this.priceImpact = (0,
|
|
398
|
+
this.executionPrice = new import_swap_sdk_core5.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
399
|
+
this.priceImpact = (0, import_swap_sdk_core5.computePriceImpact)(route.midPrice, this.inputAmount, this.outputAmount);
|
|
375
400
|
}
|
|
376
401
|
minimumAmountOut(slippageTolerance) {
|
|
377
|
-
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(
|
|
378
|
-
if (this.tradeType ===
|
|
402
|
+
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(import_swap_sdk_core5.ZERO), "SLIPPAGE_TOLERANCE");
|
|
403
|
+
if (this.tradeType === import_swap_sdk_core5.TradeType.EXACT_OUTPUT) {
|
|
379
404
|
return this.outputAmount;
|
|
380
405
|
}
|
|
381
|
-
const slippageAdjustedAmountOut = new
|
|
382
|
-
return
|
|
406
|
+
const slippageAdjustedAmountOut = new import_swap_sdk_core5.Fraction(import_swap_sdk_core5.ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
407
|
+
return import_swap_sdk_core5.CurrencyAmount.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
383
408
|
}
|
|
384
409
|
maximumAmountIn(slippageTolerance) {
|
|
385
|
-
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(
|
|
386
|
-
if (this.tradeType ===
|
|
410
|
+
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(import_swap_sdk_core5.ZERO), "SLIPPAGE_TOLERANCE");
|
|
411
|
+
if (this.tradeType === import_swap_sdk_core5.TradeType.EXACT_INPUT) {
|
|
387
412
|
return this.inputAmount;
|
|
388
413
|
}
|
|
389
|
-
const slippageAdjustedAmountIn = new
|
|
390
|
-
return
|
|
414
|
+
const slippageAdjustedAmountIn = new import_swap_sdk_core5.Fraction(import_swap_sdk_core5.ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
415
|
+
return import_swap_sdk_core5.CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
391
416
|
}
|
|
392
417
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
393
418
|
(0, import_tiny_invariant4.default)(pairs.length > 0, "PAIRS");
|
|
@@ -399,7 +424,7 @@ var Trade = class {
|
|
|
399
424
|
const pair = pairs[i];
|
|
400
425
|
if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
|
|
401
426
|
continue;
|
|
402
|
-
if (pair.reserve0.equalTo(
|
|
427
|
+
if (pair.reserve0.equalTo(import_swap_sdk_core5.ZERO) || pair.reserve1.equalTo(import_swap_sdk_core5.ZERO))
|
|
403
428
|
continue;
|
|
404
429
|
let amountOut;
|
|
405
430
|
try {
|
|
@@ -412,7 +437,7 @@ var Trade = class {
|
|
|
412
437
|
throw error;
|
|
413
438
|
}
|
|
414
439
|
if (amountOut.currency.equals(tokenOut)) {
|
|
415
|
-
(0,
|
|
440
|
+
(0, import_swap_sdk_core5.sortedInsert)(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn, import_swap_sdk_core5.TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
|
|
416
441
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
417
442
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
418
443
|
Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
|
|
@@ -424,7 +449,7 @@ var Trade = class {
|
|
|
424
449
|
return bestTrades;
|
|
425
450
|
}
|
|
426
451
|
worstExecutionPrice(slippageTolerance) {
|
|
427
|
-
return new
|
|
452
|
+
return new import_swap_sdk_core5.Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
428
453
|
}
|
|
429
454
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
430
455
|
(0, import_tiny_invariant4.default)(pairs.length > 0, "PAIRS");
|
|
@@ -436,7 +461,7 @@ var Trade = class {
|
|
|
436
461
|
const pair = pairs[i];
|
|
437
462
|
if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
|
|
438
463
|
continue;
|
|
439
|
-
if (pair.reserve0.equalTo(
|
|
464
|
+
if (pair.reserve0.equalTo(import_swap_sdk_core5.ZERO) || pair.reserve1.equalTo(import_swap_sdk_core5.ZERO))
|
|
440
465
|
continue;
|
|
441
466
|
let amountIn;
|
|
442
467
|
try {
|
|
@@ -449,7 +474,7 @@ var Trade = class {
|
|
|
449
474
|
throw error;
|
|
450
475
|
}
|
|
451
476
|
if (amountIn.currency.equals(tokenIn)) {
|
|
452
|
-
(0,
|
|
477
|
+
(0, import_swap_sdk_core5.sortedInsert)(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut, import_swap_sdk_core5.TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
|
|
453
478
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
454
479
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
455
480
|
Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
|
|
@@ -464,8 +489,8 @@ var Trade = class {
|
|
|
464
489
|
|
|
465
490
|
// src/entities/native.ts
|
|
466
491
|
var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
|
|
467
|
-
var
|
|
468
|
-
var _Native = class extends
|
|
492
|
+
var import_swap_sdk_core6 = require("@pancakeswap/swap-sdk-core");
|
|
493
|
+
var _Native = class extends import_swap_sdk_core6.NativeCurrency {
|
|
469
494
|
constructor({
|
|
470
495
|
chainId,
|
|
471
496
|
decimals,
|
|
@@ -495,7 +520,7 @@ var Native = _Native;
|
|
|
495
520
|
Native.cache = {};
|
|
496
521
|
|
|
497
522
|
// src/router.ts
|
|
498
|
-
var
|
|
523
|
+
var import_swap_sdk_core7 = require("@pancakeswap/swap-sdk-core");
|
|
499
524
|
var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
|
|
500
525
|
function toHex(currencyAmount) {
|
|
501
526
|
return `0x${currencyAmount.quotient.toString(16)}`;
|
|
@@ -519,7 +544,7 @@ var Router = class {
|
|
|
519
544
|
let args;
|
|
520
545
|
let value;
|
|
521
546
|
switch (trade.tradeType) {
|
|
522
|
-
case
|
|
547
|
+
case import_swap_sdk_core7.TradeType.EXACT_INPUT:
|
|
523
548
|
if (etherIn) {
|
|
524
549
|
methodName = useFeeOnTransfer ? "swapExactETHForTokensSupportingFeeOnTransferTokens" : "swapExactETHForTokens";
|
|
525
550
|
args = [amountOut, path, to, deadline];
|
|
@@ -534,7 +559,7 @@ var Router = class {
|
|
|
534
559
|
value = ZERO_HEX;
|
|
535
560
|
}
|
|
536
561
|
break;
|
|
537
|
-
case
|
|
562
|
+
case import_swap_sdk_core7.TradeType.EXACT_OUTPUT:
|
|
538
563
|
(0, import_tiny_invariant6.default)(!useFeeOnTransfer, "EXACT_OUT_FOT");
|
|
539
564
|
if (etherIn) {
|
|
540
565
|
methodName = "swapETHForExactTokens";
|
|
@@ -572,6 +597,7 @@ __reExport(src_exports, require("@pancakeswap/swap-sdk-core"), module.exports);
|
|
|
572
597
|
JSBI,
|
|
573
598
|
NATIVE,
|
|
574
599
|
Native,
|
|
600
|
+
ONE_HUNDRED_PERCENT,
|
|
575
601
|
Pair,
|
|
576
602
|
Route,
|
|
577
603
|
Router,
|
|
@@ -579,7 +605,9 @@ __reExport(src_exports, require("@pancakeswap/swap-sdk-core"), module.exports);
|
|
|
579
605
|
WBNB,
|
|
580
606
|
WETH9,
|
|
581
607
|
WNATIVE,
|
|
608
|
+
ZERO_PERCENT,
|
|
582
609
|
computePairAddress,
|
|
583
610
|
inputOutputComparator,
|
|
611
|
+
isTradeBetter,
|
|
584
612
|
tradeComparator
|
|
585
613
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import JSBI2 from "jsbi";
|
|
3
3
|
|
|
4
|
+
// src/constants.ts
|
|
5
|
+
import { Percent } from "@pancakeswap/swap-sdk-core";
|
|
6
|
+
|
|
4
7
|
// src/entities/token.ts
|
|
5
8
|
import { Token } from "@pancakeswap/swap-sdk-core";
|
|
6
9
|
|
|
@@ -33,6 +36,8 @@ var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
|
33
36
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
34
37
|
return ChainId2;
|
|
35
38
|
})(ChainId || {});
|
|
39
|
+
var ZERO_PERCENT = new Percent("0");
|
|
40
|
+
var ONE_HUNDRED_PERCENT = new Percent("1");
|
|
36
41
|
var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
37
42
|
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
38
43
|
var FACTORY_ADDRESS_MAP = {
|
|
@@ -79,6 +84,23 @@ var NATIVE = {
|
|
|
79
84
|
}
|
|
80
85
|
};
|
|
81
86
|
|
|
87
|
+
// src/trade.ts
|
|
88
|
+
function isTradeBetter(tradeA, tradeB, minimumDelta = ZERO_PERCENT) {
|
|
89
|
+
if (tradeA && !tradeB)
|
|
90
|
+
return false;
|
|
91
|
+
if (tradeB && !tradeA)
|
|
92
|
+
return true;
|
|
93
|
+
if (!tradeA || !tradeB)
|
|
94
|
+
return void 0;
|
|
95
|
+
if (tradeA.tradeType !== tradeB.tradeType || !tradeA.inputAmount.currency.equals(tradeB.inputAmount.currency) || !tradeA.outputAmount.currency.equals(tradeB.outputAmount.currency)) {
|
|
96
|
+
throw new Error("Trades are not comparable");
|
|
97
|
+
}
|
|
98
|
+
if (minimumDelta.equalTo(ZERO_PERCENT)) {
|
|
99
|
+
return tradeA.executionPrice.lessThan(tradeB.executionPrice);
|
|
100
|
+
}
|
|
101
|
+
return tradeA.executionPrice.asFraction.multiply(minimumDelta.add(ONE_HUNDRED_PERCENT)).lessThan(tradeB.executionPrice);
|
|
102
|
+
}
|
|
103
|
+
|
|
82
104
|
// src/entities/pair.ts
|
|
83
105
|
import {
|
|
84
106
|
InsufficientInputAmountError,
|
|
@@ -546,6 +568,7 @@ export {
|
|
|
546
568
|
JSBI2 as JSBI,
|
|
547
569
|
NATIVE,
|
|
548
570
|
Native,
|
|
571
|
+
ONE_HUNDRED_PERCENT,
|
|
549
572
|
Pair,
|
|
550
573
|
Route,
|
|
551
574
|
Router,
|
|
@@ -553,7 +576,9 @@ export {
|
|
|
553
576
|
WBNB,
|
|
554
577
|
WETH9,
|
|
555
578
|
WNATIVE,
|
|
579
|
+
ZERO_PERCENT,
|
|
556
580
|
computePairAddress,
|
|
557
581
|
inputOutputComparator,
|
|
582
|
+
isTradeBetter,
|
|
558
583
|
tradeComparator
|
|
559
584
|
};
|