@pancakeswap/sdk 3.1.1 → 3.1.2
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 +1 -11
- package/dist/index.js +78 -121
- package/dist/index.mjs +58 -98
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -8,7 +8,6 @@ declare class ERC20Token extends Token {
|
|
|
8
8
|
|
|
9
9
|
declare enum ChainId {
|
|
10
10
|
ETHEREUM = 1,
|
|
11
|
-
RINKEBY = 4,
|
|
12
11
|
GOERLI = 5,
|
|
13
12
|
BSC = 56,
|
|
14
13
|
BSC_TESTNET = 97
|
|
@@ -19,7 +18,6 @@ declare const INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d0781
|
|
|
19
18
|
declare const INIT_CODE_HASH_MAP: Record<number, string>;
|
|
20
19
|
declare const WETH9: {
|
|
21
20
|
1: ERC20Token;
|
|
22
|
-
4: ERC20Token;
|
|
23
21
|
5: ERC20Token;
|
|
24
22
|
};
|
|
25
23
|
declare const WBNB: {
|
|
@@ -268,12 +266,4 @@ declare abstract class Router {
|
|
|
268
266
|
static swapCallParameters(trade: Trade<Currency, Currency, TradeType>, options: TradeOptions | TradeOptionsDeadline): SwapParameters;
|
|
269
267
|
}
|
|
270
268
|
|
|
271
|
-
|
|
272
|
-
* Returns the percent difference between the mid price and the execution price, i.e. price impact.
|
|
273
|
-
* @param midPrice mid price before the trade
|
|
274
|
-
* @param inputAmount the input amount of the trade
|
|
275
|
-
* @param outputAmount the output amount of the trade
|
|
276
|
-
*/
|
|
277
|
-
declare function computePriceImpact<TBase extends Currency, TQuote extends Currency>(midPrice: Price<TBase, TQuote>, inputAmount: CurrencyAmount<TBase>, outputAmount: CurrencyAmount<TQuote>): Percent;
|
|
278
|
-
|
|
279
|
-
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, computePriceImpact, inputOutputComparator, tradeComparator };
|
|
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 };
|
package/dist/index.js
CHANGED
|
@@ -30,7 +30,7 @@ __export(src_exports, {
|
|
|
30
30
|
FACTORY_ADDRESS_MAP: () => FACTORY_ADDRESS_MAP,
|
|
31
31
|
INIT_CODE_HASH: () => INIT_CODE_HASH,
|
|
32
32
|
INIT_CODE_HASH_MAP: () => INIT_CODE_HASH_MAP,
|
|
33
|
-
JSBI: () =>
|
|
33
|
+
JSBI: () => import_jsbi2.default,
|
|
34
34
|
NATIVE: () => NATIVE,
|
|
35
35
|
Native: () => Native,
|
|
36
36
|
Pair: () => Pair,
|
|
@@ -41,22 +41,19 @@ __export(src_exports, {
|
|
|
41
41
|
WETH9: () => WETH9,
|
|
42
42
|
WNATIVE: () => WNATIVE,
|
|
43
43
|
computePairAddress: () => computePairAddress,
|
|
44
|
-
computePriceImpact: () => computePriceImpact,
|
|
45
44
|
inputOutputComparator: () => inputOutputComparator,
|
|
46
45
|
tradeComparator: () => tradeComparator
|
|
47
46
|
});
|
|
48
47
|
module.exports = __toCommonJS(src_exports);
|
|
49
|
-
var
|
|
48
|
+
var import_jsbi2 = __toESM(require("jsbi"));
|
|
50
49
|
|
|
51
50
|
// src/entities/token.ts
|
|
52
|
-
var
|
|
51
|
+
var import_swap_sdk_core = require("@pancakeswap/swap-sdk-core");
|
|
53
52
|
|
|
54
53
|
// src/utils.ts
|
|
55
54
|
var import_address = require("@ethersproject/address");
|
|
56
|
-
var import_jsbi = __toESM(require("jsbi"));
|
|
57
55
|
var import_tiny_invariant = __toESM(require("tiny-invariant"));
|
|
58
56
|
var import_tiny_warning = __toESM(require("tiny-warning"));
|
|
59
|
-
var import_swap_sdk_core = require("@pancakeswap/swap-sdk-core");
|
|
60
57
|
function validateAndParseAddress(address) {
|
|
61
58
|
try {
|
|
62
59
|
const checksummedAddress = (0, import_address.getAddress)(address);
|
|
@@ -66,38 +63,9 @@ function validateAndParseAddress(address) {
|
|
|
66
63
|
(0, import_tiny_invariant.default)(false, `${address} is not a valid address.`);
|
|
67
64
|
}
|
|
68
65
|
}
|
|
69
|
-
function sortedInsert(items, add, maxSize, comparator) {
|
|
70
|
-
(0, import_tiny_invariant.default)(maxSize > 0, "MAX_SIZE_ZERO");
|
|
71
|
-
(0, import_tiny_invariant.default)(items.length <= maxSize, "ITEMS_SIZE");
|
|
72
|
-
if (items.length === 0) {
|
|
73
|
-
items.push(add);
|
|
74
|
-
return null;
|
|
75
|
-
} else {
|
|
76
|
-
const isFull = items.length === maxSize;
|
|
77
|
-
if (isFull && comparator(items[items.length - 1], add) <= 0) {
|
|
78
|
-
return add;
|
|
79
|
-
}
|
|
80
|
-
let lo = 0, hi = items.length;
|
|
81
|
-
while (lo < hi) {
|
|
82
|
-
const mid = lo + hi >>> 1;
|
|
83
|
-
if (comparator(items[mid], add) <= 0) {
|
|
84
|
-
lo = mid + 1;
|
|
85
|
-
} else {
|
|
86
|
-
hi = mid;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
items.splice(lo, 0, add);
|
|
90
|
-
return isFull ? items.pop() : null;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
function computePriceImpact(midPrice, inputAmount, outputAmount) {
|
|
94
|
-
const quotedOutputAmount = midPrice.quote(inputAmount);
|
|
95
|
-
const priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount);
|
|
96
|
-
return new import_swap_sdk_core.Percent(priceImpact.numerator, priceImpact.denominator);
|
|
97
|
-
}
|
|
98
66
|
|
|
99
67
|
// src/entities/token.ts
|
|
100
|
-
var ERC20Token = class extends
|
|
68
|
+
var ERC20Token = class extends import_swap_sdk_core.Token {
|
|
101
69
|
constructor(chainId, address, decimals, symbol, name, projectLink) {
|
|
102
70
|
super(chainId, validateAndParseAddress(address), decimals, symbol, name, projectLink);
|
|
103
71
|
}
|
|
@@ -106,7 +74,6 @@ var ERC20Token = class extends import_swap_sdk_core2.Token {
|
|
|
106
74
|
// src/constants.ts
|
|
107
75
|
var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
108
76
|
ChainId2[ChainId2["ETHEREUM"] = 1] = "ETHEREUM";
|
|
109
|
-
ChainId2[ChainId2["RINKEBY"] = 4] = "RINKEBY";
|
|
110
77
|
ChainId2[ChainId2["GOERLI"] = 5] = "GOERLI";
|
|
111
78
|
ChainId2[ChainId2["BSC"] = 56] = "BSC";
|
|
112
79
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
@@ -116,7 +83,6 @@ var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
|
116
83
|
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
117
84
|
var FACTORY_ADDRESS_MAP = {
|
|
118
85
|
[1 /* ETHEREUM */]: FACTORY_ADDRESS_ETH,
|
|
119
|
-
[4 /* RINKEBY */]: FACTORY_ADDRESS_ETH,
|
|
120
86
|
[5 /* GOERLI */]: FACTORY_ADDRESS_ETH,
|
|
121
87
|
[56 /* BSC */]: FACTORY_ADDRESS,
|
|
122
88
|
[97 /* BSC_TESTNET */]: "0x6725f303b657a9451d8ba641348b6761a6cc7a17"
|
|
@@ -125,14 +91,12 @@ var INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a
|
|
|
125
91
|
var INIT_CODE_HASH_ETH = "0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d";
|
|
126
92
|
var INIT_CODE_HASH_MAP = {
|
|
127
93
|
[1 /* ETHEREUM */]: INIT_CODE_HASH_ETH,
|
|
128
|
-
[4 /* RINKEBY */]: INIT_CODE_HASH_ETH,
|
|
129
94
|
[5 /* GOERLI */]: INIT_CODE_HASH_ETH,
|
|
130
95
|
[56 /* BSC */]: INIT_CODE_HASH,
|
|
131
96
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
132
97
|
};
|
|
133
98
|
var WETH9 = {
|
|
134
99
|
[1 /* ETHEREUM */]: new ERC20Token(1 /* ETHEREUM */, "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 18, "WETH", "Wrapped Ether", "https://weth.io"),
|
|
135
|
-
[4 /* RINKEBY */]: new ERC20Token(4 /* RINKEBY */, "0xc778417E063141139Fce010982780140Aa0cD5Ab", 18, "WETH", "Wrapped Ether", "https://weth.io"),
|
|
136
100
|
[5 /* GOERLI */]: new ERC20Token(5 /* GOERLI */, "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", 18, "WETH", "Wrapped Ether", "https://weth.io")
|
|
137
101
|
};
|
|
138
102
|
var WBNB = {
|
|
@@ -142,14 +106,12 @@ var WBNB = {
|
|
|
142
106
|
};
|
|
143
107
|
var WNATIVE = {
|
|
144
108
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
145
|
-
[4 /* RINKEBY */]: WETH9[4 /* RINKEBY */],
|
|
146
109
|
[5 /* GOERLI */]: WETH9[5 /* GOERLI */],
|
|
147
110
|
[56 /* BSC */]: WBNB[56 /* BSC */],
|
|
148
111
|
[97 /* BSC_TESTNET */]: WBNB[97 /* BSC_TESTNET */]
|
|
149
112
|
};
|
|
150
113
|
var NATIVE = {
|
|
151
114
|
[1 /* ETHEREUM */]: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
152
|
-
[4 /* RINKEBY */]: { name: "Rinkeby Ether", symbol: "RIN", decimals: 18 },
|
|
153
115
|
[5 /* GOERLI */]: { name: "Goerli Ether", symbol: "GOR", decimals: 18 },
|
|
154
116
|
[56 /* BSC */]: {
|
|
155
117
|
name: "Binance Chain Native Token",
|
|
@@ -164,10 +126,10 @@ var NATIVE = {
|
|
|
164
126
|
};
|
|
165
127
|
|
|
166
128
|
// src/entities/pair.ts
|
|
167
|
-
var
|
|
129
|
+
var import_swap_sdk_core2 = require("@pancakeswap/swap-sdk-core");
|
|
168
130
|
var import_address2 = require("@ethersproject/address");
|
|
169
131
|
var import_solidity = require("@ethersproject/solidity");
|
|
170
|
-
var
|
|
132
|
+
var import_jsbi = __toESM(require("jsbi"));
|
|
171
133
|
var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
|
|
172
134
|
var PAIR_ADDRESS_CACHE = {};
|
|
173
135
|
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
@@ -200,11 +162,11 @@ var Pair = class {
|
|
|
200
162
|
}
|
|
201
163
|
get token0Price() {
|
|
202
164
|
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
203
|
-
return new
|
|
165
|
+
return new import_swap_sdk_core2.Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
204
166
|
}
|
|
205
167
|
get token1Price() {
|
|
206
168
|
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
207
|
-
return new
|
|
169
|
+
return new import_swap_sdk_core2.Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
208
170
|
}
|
|
209
171
|
priceOf(token) {
|
|
210
172
|
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
@@ -231,30 +193,30 @@ var Pair = class {
|
|
|
231
193
|
}
|
|
232
194
|
getOutputAmount(inputAmount) {
|
|
233
195
|
(0, import_tiny_invariant2.default)(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
234
|
-
if (
|
|
235
|
-
throw new
|
|
196
|
+
if (import_jsbi.default.equal(this.reserve0.quotient, import_swap_sdk_core2.ZERO) || import_jsbi.default.equal(this.reserve1.quotient, import_swap_sdk_core2.ZERO)) {
|
|
197
|
+
throw new import_swap_sdk_core2.InsufficientReservesError();
|
|
236
198
|
}
|
|
237
199
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
238
200
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
239
|
-
const inputAmountWithFee =
|
|
240
|
-
const numerator =
|
|
241
|
-
const denominator =
|
|
242
|
-
const outputAmount =
|
|
243
|
-
if (
|
|
244
|
-
throw new
|
|
201
|
+
const inputAmountWithFee = import_jsbi.default.multiply(inputAmount.quotient, import_swap_sdk_core2._9975);
|
|
202
|
+
const numerator = import_jsbi.default.multiply(inputAmountWithFee, outputReserve.quotient);
|
|
203
|
+
const denominator = import_jsbi.default.add(import_jsbi.default.multiply(inputReserve.quotient, import_swap_sdk_core2._10000), inputAmountWithFee);
|
|
204
|
+
const outputAmount = import_swap_sdk_core2.CurrencyAmount.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, import_jsbi.default.divide(numerator, denominator));
|
|
205
|
+
if (import_jsbi.default.equal(outputAmount.quotient, import_swap_sdk_core2.ZERO)) {
|
|
206
|
+
throw new import_swap_sdk_core2.InsufficientInputAmountError();
|
|
245
207
|
}
|
|
246
208
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
247
209
|
}
|
|
248
210
|
getInputAmount(outputAmount) {
|
|
249
211
|
(0, import_tiny_invariant2.default)(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
250
|
-
if (
|
|
251
|
-
throw new
|
|
212
|
+
if (import_jsbi.default.equal(this.reserve0.quotient, import_swap_sdk_core2.ZERO) || import_jsbi.default.equal(this.reserve1.quotient, import_swap_sdk_core2.ZERO) || import_jsbi.default.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient)) {
|
|
213
|
+
throw new import_swap_sdk_core2.InsufficientReservesError();
|
|
252
214
|
}
|
|
253
215
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
254
216
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
255
|
-
const numerator =
|
|
256
|
-
const denominator =
|
|
257
|
-
const inputAmount =
|
|
217
|
+
const numerator = import_jsbi.default.multiply(import_jsbi.default.multiply(inputReserve.quotient, outputAmount.quotient), import_swap_sdk_core2._10000);
|
|
218
|
+
const denominator = import_jsbi.default.multiply(import_jsbi.default.subtract(outputReserve.quotient, outputAmount.quotient), import_swap_sdk_core2._9975);
|
|
219
|
+
const inputAmount = import_swap_sdk_core2.CurrencyAmount.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, import_jsbi.default.add(import_jsbi.default.divide(numerator, denominator), import_swap_sdk_core2.ONE));
|
|
258
220
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
259
221
|
}
|
|
260
222
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
@@ -262,37 +224,37 @@ var Pair = class {
|
|
|
262
224
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
263
225
|
(0, import_tiny_invariant2.default)(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
264
226
|
let liquidity;
|
|
265
|
-
if (
|
|
266
|
-
liquidity =
|
|
227
|
+
if (import_jsbi.default.equal(totalSupply.quotient, import_swap_sdk_core2.ZERO)) {
|
|
228
|
+
liquidity = import_jsbi.default.subtract((0, import_swap_sdk_core2.sqrt)(import_jsbi.default.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), import_swap_sdk_core2.MINIMUM_LIQUIDITY);
|
|
267
229
|
} else {
|
|
268
|
-
const amount0 =
|
|
269
|
-
const amount1 =
|
|
270
|
-
liquidity =
|
|
230
|
+
const amount0 = import_jsbi.default.divide(import_jsbi.default.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
231
|
+
const amount1 = import_jsbi.default.divide(import_jsbi.default.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
232
|
+
liquidity = import_jsbi.default.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
|
|
271
233
|
}
|
|
272
|
-
if (!
|
|
273
|
-
throw new
|
|
234
|
+
if (!import_jsbi.default.greaterThan(liquidity, import_swap_sdk_core2.ZERO)) {
|
|
235
|
+
throw new import_swap_sdk_core2.InsufficientInputAmountError();
|
|
274
236
|
}
|
|
275
|
-
return
|
|
237
|
+
return import_swap_sdk_core2.CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
276
238
|
}
|
|
277
239
|
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
278
240
|
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
279
241
|
(0, import_tiny_invariant2.default)(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
280
242
|
(0, import_tiny_invariant2.default)(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
281
|
-
(0, import_tiny_invariant2.default)(
|
|
243
|
+
(0, import_tiny_invariant2.default)(import_jsbi.default.lessThanOrEqual(liquidity.quotient, totalSupply.quotient), "LIQUIDITY");
|
|
282
244
|
let totalSupplyAdjusted;
|
|
283
245
|
if (!feeOn) {
|
|
284
246
|
totalSupplyAdjusted = totalSupply;
|
|
285
247
|
} else {
|
|
286
248
|
(0, import_tiny_invariant2.default)(!!kLast, "K_LAST");
|
|
287
|
-
const kLastParsed =
|
|
288
|
-
if (!
|
|
289
|
-
const rootK = (0,
|
|
290
|
-
const rootKLast = (0,
|
|
291
|
-
if (
|
|
292
|
-
const numerator =
|
|
293
|
-
const denominator =
|
|
294
|
-
const feeLiquidity =
|
|
295
|
-
totalSupplyAdjusted = totalSupply.add(
|
|
249
|
+
const kLastParsed = import_jsbi.default.BigInt(kLast);
|
|
250
|
+
if (!import_jsbi.default.equal(kLastParsed, import_swap_sdk_core2.ZERO)) {
|
|
251
|
+
const rootK = (0, import_swap_sdk_core2.sqrt)(import_jsbi.default.multiply(this.reserve0.quotient, this.reserve1.quotient));
|
|
252
|
+
const rootKLast = (0, import_swap_sdk_core2.sqrt)(kLastParsed);
|
|
253
|
+
if (import_jsbi.default.greaterThan(rootK, rootKLast)) {
|
|
254
|
+
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, import_swap_sdk_core2.FIVE), rootKLast);
|
|
256
|
+
const feeLiquidity = import_jsbi.default.divide(numerator, denominator);
|
|
257
|
+
totalSupplyAdjusted = totalSupply.add(import_swap_sdk_core2.CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
296
258
|
} else {
|
|
297
259
|
totalSupplyAdjusted = totalSupply;
|
|
298
260
|
}
|
|
@@ -300,13 +262,13 @@ var Pair = class {
|
|
|
300
262
|
totalSupplyAdjusted = totalSupply;
|
|
301
263
|
}
|
|
302
264
|
}
|
|
303
|
-
return
|
|
265
|
+
return import_swap_sdk_core2.CurrencyAmount.fromRawAmount(token, import_jsbi.default.divide(import_jsbi.default.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
|
|
304
266
|
}
|
|
305
267
|
};
|
|
306
268
|
|
|
307
269
|
// src/entities/route.ts
|
|
308
270
|
var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
|
|
309
|
-
var
|
|
271
|
+
var import_swap_sdk_core3 = require("@pancakeswap/swap-sdk-core");
|
|
310
272
|
var Route = class {
|
|
311
273
|
constructor(pairs, input, output) {
|
|
312
274
|
this._midPrice = null;
|
|
@@ -333,10 +295,10 @@ var Route = class {
|
|
|
333
295
|
return this._midPrice;
|
|
334
296
|
const prices = [];
|
|
335
297
|
for (const [i, pair] of this.pairs.entries()) {
|
|
336
|
-
prices.push(this.path[i].equals(pair.token0) ? new
|
|
298
|
+
prices.push(this.path[i].equals(pair.token0) ? new import_swap_sdk_core3.Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient) : new import_swap_sdk_core3.Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient));
|
|
337
299
|
}
|
|
338
300
|
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
339
|
-
return this._midPrice = new
|
|
301
|
+
return this._midPrice = new import_swap_sdk_core3.Price(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
340
302
|
}
|
|
341
303
|
get chainId() {
|
|
342
304
|
return this.pairs[0].chainId;
|
|
@@ -345,7 +307,7 @@ var Route = class {
|
|
|
345
307
|
|
|
346
308
|
// src/entities/trade.ts
|
|
347
309
|
var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
|
|
348
|
-
var
|
|
310
|
+
var import_swap_sdk_core4 = require("@pancakeswap/swap-sdk-core");
|
|
349
311
|
function inputOutputComparator(a, b) {
|
|
350
312
|
(0, import_tiny_invariant4.default)(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
|
|
351
313
|
(0, import_tiny_invariant4.default)(a.outputAmount.currency.equals(b.outputAmount.currency), "OUTPUT_CURRENCY");
|
|
@@ -355,16 +317,13 @@ function inputOutputComparator(a, b) {
|
|
|
355
317
|
}
|
|
356
318
|
if (a.inputAmount.lessThan(b.inputAmount)) {
|
|
357
319
|
return -1;
|
|
358
|
-
} else {
|
|
359
|
-
return 1;
|
|
360
|
-
}
|
|
361
|
-
} else {
|
|
362
|
-
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
363
|
-
return 1;
|
|
364
|
-
} else {
|
|
365
|
-
return -1;
|
|
366
320
|
}
|
|
321
|
+
return 1;
|
|
367
322
|
}
|
|
323
|
+
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
324
|
+
return 1;
|
|
325
|
+
}
|
|
326
|
+
return -1;
|
|
368
327
|
}
|
|
369
328
|
function tradeComparator(a, b) {
|
|
370
329
|
const ioComp = inputOutputComparator(a, b);
|
|
@@ -373,23 +332,24 @@ function tradeComparator(a, b) {
|
|
|
373
332
|
}
|
|
374
333
|
if (a.priceImpact.lessThan(b.priceImpact)) {
|
|
375
334
|
return -1;
|
|
376
|
-
}
|
|
335
|
+
}
|
|
336
|
+
if (a.priceImpact.greaterThan(b.priceImpact)) {
|
|
377
337
|
return 1;
|
|
378
338
|
}
|
|
379
339
|
return a.route.path.length - b.route.path.length;
|
|
380
340
|
}
|
|
381
341
|
var Trade = class {
|
|
382
342
|
static exactIn(route, amountIn) {
|
|
383
|
-
return new Trade(route, amountIn,
|
|
343
|
+
return new Trade(route, amountIn, import_swap_sdk_core4.TradeType.EXACT_INPUT);
|
|
384
344
|
}
|
|
385
345
|
static exactOut(route, amountOut) {
|
|
386
|
-
return new Trade(route, amountOut,
|
|
346
|
+
return new Trade(route, amountOut, import_swap_sdk_core4.TradeType.EXACT_OUTPUT);
|
|
387
347
|
}
|
|
388
348
|
constructor(route, amount, tradeType) {
|
|
389
349
|
this.route = route;
|
|
390
350
|
this.tradeType = tradeType;
|
|
391
351
|
const tokenAmounts = new Array(route.path.length);
|
|
392
|
-
if (tradeType ===
|
|
352
|
+
if (tradeType === import_swap_sdk_core4.TradeType.EXACT_INPUT) {
|
|
393
353
|
(0, import_tiny_invariant4.default)(amount.currency.equals(route.input), "INPUT");
|
|
394
354
|
tokenAmounts[0] = amount.wrapped;
|
|
395
355
|
for (let i = 0; i < route.path.length - 1; i++) {
|
|
@@ -397,8 +357,8 @@ var Trade = class {
|
|
|
397
357
|
const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
|
|
398
358
|
tokenAmounts[i + 1] = outputAmount;
|
|
399
359
|
}
|
|
400
|
-
this.inputAmount =
|
|
401
|
-
this.outputAmount =
|
|
360
|
+
this.inputAmount = import_swap_sdk_core4.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
361
|
+
this.outputAmount = import_swap_sdk_core4.CurrencyAmount.fromFractionalAmount(route.output, tokenAmounts[tokenAmounts.length - 1].numerator, tokenAmounts[tokenAmounts.length - 1].denominator);
|
|
402
362
|
} else {
|
|
403
363
|
(0, import_tiny_invariant4.default)(amount.currency.equals(route.output), "OUTPUT");
|
|
404
364
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
@@ -407,29 +367,27 @@ var Trade = class {
|
|
|
407
367
|
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
408
368
|
tokenAmounts[i - 1] = inputAmount;
|
|
409
369
|
}
|
|
410
|
-
this.inputAmount =
|
|
411
|
-
this.outputAmount =
|
|
370
|
+
this.inputAmount = import_swap_sdk_core4.CurrencyAmount.fromFractionalAmount(route.input, tokenAmounts[0].numerator, tokenAmounts[0].denominator);
|
|
371
|
+
this.outputAmount = import_swap_sdk_core4.CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
412
372
|
}
|
|
413
|
-
this.executionPrice = new
|
|
414
|
-
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
373
|
+
this.executionPrice = new import_swap_sdk_core4.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
374
|
+
this.priceImpact = (0, import_swap_sdk_core4.computePriceImpact)(route.midPrice, this.inputAmount, this.outputAmount);
|
|
415
375
|
}
|
|
416
376
|
minimumAmountOut(slippageTolerance) {
|
|
417
|
-
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(
|
|
418
|
-
if (this.tradeType ===
|
|
377
|
+
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(import_swap_sdk_core4.ZERO), "SLIPPAGE_TOLERANCE");
|
|
378
|
+
if (this.tradeType === import_swap_sdk_core4.TradeType.EXACT_OUTPUT) {
|
|
419
379
|
return this.outputAmount;
|
|
420
|
-
} else {
|
|
421
|
-
const slippageAdjustedAmountOut = new import_swap_sdk_core5.Fraction(import_swap_sdk_core5.ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
422
|
-
return import_swap_sdk_core5.CurrencyAmount.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
423
380
|
}
|
|
381
|
+
const slippageAdjustedAmountOut = new import_swap_sdk_core4.Fraction(import_swap_sdk_core4.ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
382
|
+
return import_swap_sdk_core4.CurrencyAmount.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
424
383
|
}
|
|
425
384
|
maximumAmountIn(slippageTolerance) {
|
|
426
|
-
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(
|
|
427
|
-
if (this.tradeType ===
|
|
385
|
+
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(import_swap_sdk_core4.ZERO), "SLIPPAGE_TOLERANCE");
|
|
386
|
+
if (this.tradeType === import_swap_sdk_core4.TradeType.EXACT_INPUT) {
|
|
428
387
|
return this.inputAmount;
|
|
429
|
-
} else {
|
|
430
|
-
const slippageAdjustedAmountIn = new import_swap_sdk_core5.Fraction(import_swap_sdk_core5.ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
431
|
-
return import_swap_sdk_core5.CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
432
388
|
}
|
|
389
|
+
const slippageAdjustedAmountIn = new import_swap_sdk_core4.Fraction(import_swap_sdk_core4.ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
390
|
+
return import_swap_sdk_core4.CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
433
391
|
}
|
|
434
392
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
435
393
|
(0, import_tiny_invariant4.default)(pairs.length > 0, "PAIRS");
|
|
@@ -441,7 +399,7 @@ var Trade = class {
|
|
|
441
399
|
const pair = pairs[i];
|
|
442
400
|
if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
|
|
443
401
|
continue;
|
|
444
|
-
if (pair.reserve0.equalTo(
|
|
402
|
+
if (pair.reserve0.equalTo(import_swap_sdk_core4.ZERO) || pair.reserve1.equalTo(import_swap_sdk_core4.ZERO))
|
|
445
403
|
continue;
|
|
446
404
|
let amountOut;
|
|
447
405
|
try {
|
|
@@ -454,7 +412,7 @@ var Trade = class {
|
|
|
454
412
|
throw error;
|
|
455
413
|
}
|
|
456
414
|
if (amountOut.currency.equals(tokenOut)) {
|
|
457
|
-
sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn,
|
|
415
|
+
(0, import_swap_sdk_core4.sortedInsert)(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn, import_swap_sdk_core4.TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
|
|
458
416
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
459
417
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
460
418
|
Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
|
|
@@ -466,7 +424,7 @@ var Trade = class {
|
|
|
466
424
|
return bestTrades;
|
|
467
425
|
}
|
|
468
426
|
worstExecutionPrice(slippageTolerance) {
|
|
469
|
-
return new
|
|
427
|
+
return new import_swap_sdk_core4.Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
470
428
|
}
|
|
471
429
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
472
430
|
(0, import_tiny_invariant4.default)(pairs.length > 0, "PAIRS");
|
|
@@ -478,7 +436,7 @@ var Trade = class {
|
|
|
478
436
|
const pair = pairs[i];
|
|
479
437
|
if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
|
|
480
438
|
continue;
|
|
481
|
-
if (pair.reserve0.equalTo(
|
|
439
|
+
if (pair.reserve0.equalTo(import_swap_sdk_core4.ZERO) || pair.reserve1.equalTo(import_swap_sdk_core4.ZERO))
|
|
482
440
|
continue;
|
|
483
441
|
let amountIn;
|
|
484
442
|
try {
|
|
@@ -491,7 +449,7 @@ var Trade = class {
|
|
|
491
449
|
throw error;
|
|
492
450
|
}
|
|
493
451
|
if (amountIn.currency.equals(tokenIn)) {
|
|
494
|
-
sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut,
|
|
452
|
+
(0, import_swap_sdk_core4.sortedInsert)(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut, import_swap_sdk_core4.TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
|
|
495
453
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
496
454
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
497
455
|
Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
|
|
@@ -506,8 +464,8 @@ var Trade = class {
|
|
|
506
464
|
|
|
507
465
|
// src/entities/native.ts
|
|
508
466
|
var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
|
|
509
|
-
var
|
|
510
|
-
var _Native = class extends
|
|
467
|
+
var import_swap_sdk_core5 = require("@pancakeswap/swap-sdk-core");
|
|
468
|
+
var _Native = class extends import_swap_sdk_core5.NativeCurrency {
|
|
511
469
|
constructor({
|
|
512
470
|
chainId,
|
|
513
471
|
decimals,
|
|
@@ -537,7 +495,7 @@ var Native = _Native;
|
|
|
537
495
|
Native.cache = {};
|
|
538
496
|
|
|
539
497
|
// src/router.ts
|
|
540
|
-
var
|
|
498
|
+
var import_swap_sdk_core6 = require("@pancakeswap/swap-sdk-core");
|
|
541
499
|
var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
|
|
542
500
|
function toHex(currencyAmount) {
|
|
543
501
|
return `0x${currencyAmount.quotient.toString(16)}`;
|
|
@@ -561,7 +519,7 @@ var Router = class {
|
|
|
561
519
|
let args;
|
|
562
520
|
let value;
|
|
563
521
|
switch (trade.tradeType) {
|
|
564
|
-
case
|
|
522
|
+
case import_swap_sdk_core6.TradeType.EXACT_INPUT:
|
|
565
523
|
if (etherIn) {
|
|
566
524
|
methodName = useFeeOnTransfer ? "swapExactETHForTokensSupportingFeeOnTransferTokens" : "swapExactETHForTokens";
|
|
567
525
|
args = [amountOut, path, to, deadline];
|
|
@@ -576,7 +534,7 @@ var Router = class {
|
|
|
576
534
|
value = ZERO_HEX;
|
|
577
535
|
}
|
|
578
536
|
break;
|
|
579
|
-
case
|
|
537
|
+
case import_swap_sdk_core6.TradeType.EXACT_OUTPUT:
|
|
580
538
|
(0, import_tiny_invariant6.default)(!useFeeOnTransfer, "EXACT_OUT_FOT");
|
|
581
539
|
if (etherIn) {
|
|
582
540
|
methodName = "swapETHForExactTokens";
|
|
@@ -622,7 +580,6 @@ __reExport(src_exports, require("@pancakeswap/swap-sdk-core"), module.exports);
|
|
|
622
580
|
WETH9,
|
|
623
581
|
WNATIVE,
|
|
624
582
|
computePairAddress,
|
|
625
|
-
computePriceImpact,
|
|
626
583
|
inputOutputComparator,
|
|
627
584
|
tradeComparator
|
|
628
585
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import
|
|
2
|
+
import JSBI2 from "jsbi";
|
|
3
3
|
|
|
4
4
|
// src/entities/token.ts
|
|
5
5
|
import { Token } from "@pancakeswap/swap-sdk-core";
|
|
6
6
|
|
|
7
7
|
// src/utils.ts
|
|
8
8
|
import { getAddress } from "@ethersproject/address";
|
|
9
|
-
import JSBI from "jsbi";
|
|
10
9
|
import invariant from "tiny-invariant";
|
|
11
10
|
import warning from "tiny-warning";
|
|
12
|
-
import { Percent } from "@pancakeswap/swap-sdk-core";
|
|
13
11
|
function validateAndParseAddress(address) {
|
|
14
12
|
try {
|
|
15
13
|
const checksummedAddress = getAddress(address);
|
|
@@ -19,35 +17,6 @@ function validateAndParseAddress(address) {
|
|
|
19
17
|
invariant(false, `${address} is not a valid address.`);
|
|
20
18
|
}
|
|
21
19
|
}
|
|
22
|
-
function sortedInsert(items, add, maxSize, comparator) {
|
|
23
|
-
invariant(maxSize > 0, "MAX_SIZE_ZERO");
|
|
24
|
-
invariant(items.length <= maxSize, "ITEMS_SIZE");
|
|
25
|
-
if (items.length === 0) {
|
|
26
|
-
items.push(add);
|
|
27
|
-
return null;
|
|
28
|
-
} else {
|
|
29
|
-
const isFull = items.length === maxSize;
|
|
30
|
-
if (isFull && comparator(items[items.length - 1], add) <= 0) {
|
|
31
|
-
return add;
|
|
32
|
-
}
|
|
33
|
-
let lo = 0, hi = items.length;
|
|
34
|
-
while (lo < hi) {
|
|
35
|
-
const mid = lo + hi >>> 1;
|
|
36
|
-
if (comparator(items[mid], add) <= 0) {
|
|
37
|
-
lo = mid + 1;
|
|
38
|
-
} else {
|
|
39
|
-
hi = mid;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
items.splice(lo, 0, add);
|
|
43
|
-
return isFull ? items.pop() : null;
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
function computePriceImpact(midPrice, inputAmount, outputAmount) {
|
|
47
|
-
const quotedOutputAmount = midPrice.quote(inputAmount);
|
|
48
|
-
const priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount);
|
|
49
|
-
return new Percent(priceImpact.numerator, priceImpact.denominator);
|
|
50
|
-
}
|
|
51
20
|
|
|
52
21
|
// src/entities/token.ts
|
|
53
22
|
var ERC20Token = class extends Token {
|
|
@@ -59,7 +28,6 @@ var ERC20Token = class extends Token {
|
|
|
59
28
|
// src/constants.ts
|
|
60
29
|
var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
61
30
|
ChainId2[ChainId2["ETHEREUM"] = 1] = "ETHEREUM";
|
|
62
|
-
ChainId2[ChainId2["RINKEBY"] = 4] = "RINKEBY";
|
|
63
31
|
ChainId2[ChainId2["GOERLI"] = 5] = "GOERLI";
|
|
64
32
|
ChainId2[ChainId2["BSC"] = 56] = "BSC";
|
|
65
33
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
@@ -69,7 +37,6 @@ var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
|
69
37
|
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
70
38
|
var FACTORY_ADDRESS_MAP = {
|
|
71
39
|
[1 /* ETHEREUM */]: FACTORY_ADDRESS_ETH,
|
|
72
|
-
[4 /* RINKEBY */]: FACTORY_ADDRESS_ETH,
|
|
73
40
|
[5 /* GOERLI */]: FACTORY_ADDRESS_ETH,
|
|
74
41
|
[56 /* BSC */]: FACTORY_ADDRESS,
|
|
75
42
|
[97 /* BSC_TESTNET */]: "0x6725f303b657a9451d8ba641348b6761a6cc7a17"
|
|
@@ -78,14 +45,12 @@ var INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a
|
|
|
78
45
|
var INIT_CODE_HASH_ETH = "0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d";
|
|
79
46
|
var INIT_CODE_HASH_MAP = {
|
|
80
47
|
[1 /* ETHEREUM */]: INIT_CODE_HASH_ETH,
|
|
81
|
-
[4 /* RINKEBY */]: INIT_CODE_HASH_ETH,
|
|
82
48
|
[5 /* GOERLI */]: INIT_CODE_HASH_ETH,
|
|
83
49
|
[56 /* BSC */]: INIT_CODE_HASH,
|
|
84
50
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
85
51
|
};
|
|
86
52
|
var WETH9 = {
|
|
87
53
|
[1 /* ETHEREUM */]: new ERC20Token(1 /* ETHEREUM */, "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 18, "WETH", "Wrapped Ether", "https://weth.io"),
|
|
88
|
-
[4 /* RINKEBY */]: new ERC20Token(4 /* RINKEBY */, "0xc778417E063141139Fce010982780140Aa0cD5Ab", 18, "WETH", "Wrapped Ether", "https://weth.io"),
|
|
89
54
|
[5 /* GOERLI */]: new ERC20Token(5 /* GOERLI */, "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", 18, "WETH", "Wrapped Ether", "https://weth.io")
|
|
90
55
|
};
|
|
91
56
|
var WBNB = {
|
|
@@ -95,14 +60,12 @@ var WBNB = {
|
|
|
95
60
|
};
|
|
96
61
|
var WNATIVE = {
|
|
97
62
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
98
|
-
[4 /* RINKEBY */]: WETH9[4 /* RINKEBY */],
|
|
99
63
|
[5 /* GOERLI */]: WETH9[5 /* GOERLI */],
|
|
100
64
|
[56 /* BSC */]: WBNB[56 /* BSC */],
|
|
101
65
|
[97 /* BSC_TESTNET */]: WBNB[97 /* BSC_TESTNET */]
|
|
102
66
|
};
|
|
103
67
|
var NATIVE = {
|
|
104
68
|
[1 /* ETHEREUM */]: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
105
|
-
[4 /* RINKEBY */]: { name: "Rinkeby Ether", symbol: "RIN", decimals: 18 },
|
|
106
69
|
[5 /* GOERLI */]: { name: "Goerli Ether", symbol: "GOR", decimals: 18 },
|
|
107
70
|
[56 /* BSC */]: {
|
|
108
71
|
name: "Binance Chain Native Token",
|
|
@@ -121,8 +84,8 @@ import {
|
|
|
121
84
|
InsufficientInputAmountError,
|
|
122
85
|
InsufficientReservesError,
|
|
123
86
|
sqrt,
|
|
124
|
-
CurrencyAmount
|
|
125
|
-
Price
|
|
87
|
+
CurrencyAmount,
|
|
88
|
+
Price,
|
|
126
89
|
FIVE,
|
|
127
90
|
ONE,
|
|
128
91
|
ZERO,
|
|
@@ -132,7 +95,7 @@ import {
|
|
|
132
95
|
} from "@pancakeswap/swap-sdk-core";
|
|
133
96
|
import { getCreate2Address } from "@ethersproject/address";
|
|
134
97
|
import { keccak256, pack } from "@ethersproject/solidity";
|
|
135
|
-
import
|
|
98
|
+
import JSBI from "jsbi";
|
|
136
99
|
import invariant2 from "tiny-invariant";
|
|
137
100
|
var PAIR_ADDRESS_CACHE = {};
|
|
138
101
|
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
@@ -165,11 +128,11 @@ var Pair = class {
|
|
|
165
128
|
}
|
|
166
129
|
get token0Price() {
|
|
167
130
|
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
168
|
-
return new
|
|
131
|
+
return new Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
169
132
|
}
|
|
170
133
|
get token1Price() {
|
|
171
134
|
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
172
|
-
return new
|
|
135
|
+
return new Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
173
136
|
}
|
|
174
137
|
priceOf(token) {
|
|
175
138
|
invariant2(this.involvesToken(token), "TOKEN");
|
|
@@ -196,30 +159,30 @@ var Pair = class {
|
|
|
196
159
|
}
|
|
197
160
|
getOutputAmount(inputAmount) {
|
|
198
161
|
invariant2(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
199
|
-
if (
|
|
162
|
+
if (JSBI.equal(this.reserve0.quotient, ZERO) || JSBI.equal(this.reserve1.quotient, ZERO)) {
|
|
200
163
|
throw new InsufficientReservesError();
|
|
201
164
|
}
|
|
202
165
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
203
166
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
204
|
-
const inputAmountWithFee =
|
|
205
|
-
const numerator =
|
|
206
|
-
const denominator =
|
|
207
|
-
const outputAmount =
|
|
208
|
-
if (
|
|
167
|
+
const inputAmountWithFee = JSBI.multiply(inputAmount.quotient, _9975);
|
|
168
|
+
const numerator = JSBI.multiply(inputAmountWithFee, outputReserve.quotient);
|
|
169
|
+
const denominator = JSBI.add(JSBI.multiply(inputReserve.quotient, _10000), inputAmountWithFee);
|
|
170
|
+
const outputAmount = CurrencyAmount.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI.divide(numerator, denominator));
|
|
171
|
+
if (JSBI.equal(outputAmount.quotient, ZERO)) {
|
|
209
172
|
throw new InsufficientInputAmountError();
|
|
210
173
|
}
|
|
211
174
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
212
175
|
}
|
|
213
176
|
getInputAmount(outputAmount) {
|
|
214
177
|
invariant2(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
215
|
-
if (
|
|
178
|
+
if (JSBI.equal(this.reserve0.quotient, ZERO) || JSBI.equal(this.reserve1.quotient, ZERO) || JSBI.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient)) {
|
|
216
179
|
throw new InsufficientReservesError();
|
|
217
180
|
}
|
|
218
181
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
219
182
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
220
|
-
const numerator =
|
|
221
|
-
const denominator =
|
|
222
|
-
const inputAmount =
|
|
183
|
+
const numerator = JSBI.multiply(JSBI.multiply(inputReserve.quotient, outputAmount.quotient), _10000);
|
|
184
|
+
const denominator = JSBI.multiply(JSBI.subtract(outputReserve.quotient, outputAmount.quotient), _9975);
|
|
185
|
+
const inputAmount = CurrencyAmount.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI.add(JSBI.divide(numerator, denominator), ONE));
|
|
223
186
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
224
187
|
}
|
|
225
188
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
@@ -227,37 +190,37 @@ var Pair = class {
|
|
|
227
190
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
228
191
|
invariant2(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
229
192
|
let liquidity;
|
|
230
|
-
if (
|
|
231
|
-
liquidity =
|
|
193
|
+
if (JSBI.equal(totalSupply.quotient, ZERO)) {
|
|
194
|
+
liquidity = JSBI.subtract(sqrt(JSBI.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), MINIMUM_LIQUIDITY);
|
|
232
195
|
} else {
|
|
233
|
-
const amount0 =
|
|
234
|
-
const amount1 =
|
|
235
|
-
liquidity =
|
|
196
|
+
const amount0 = JSBI.divide(JSBI.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
197
|
+
const amount1 = JSBI.divide(JSBI.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
198
|
+
liquidity = JSBI.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
|
|
236
199
|
}
|
|
237
|
-
if (!
|
|
200
|
+
if (!JSBI.greaterThan(liquidity, ZERO)) {
|
|
238
201
|
throw new InsufficientInputAmountError();
|
|
239
202
|
}
|
|
240
|
-
return
|
|
203
|
+
return CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
241
204
|
}
|
|
242
205
|
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
243
206
|
invariant2(this.involvesToken(token), "TOKEN");
|
|
244
207
|
invariant2(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
245
208
|
invariant2(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
246
|
-
invariant2(
|
|
209
|
+
invariant2(JSBI.lessThanOrEqual(liquidity.quotient, totalSupply.quotient), "LIQUIDITY");
|
|
247
210
|
let totalSupplyAdjusted;
|
|
248
211
|
if (!feeOn) {
|
|
249
212
|
totalSupplyAdjusted = totalSupply;
|
|
250
213
|
} else {
|
|
251
214
|
invariant2(!!kLast, "K_LAST");
|
|
252
|
-
const kLastParsed =
|
|
253
|
-
if (!
|
|
254
|
-
const rootK = sqrt(
|
|
215
|
+
const kLastParsed = JSBI.BigInt(kLast);
|
|
216
|
+
if (!JSBI.equal(kLastParsed, ZERO)) {
|
|
217
|
+
const rootK = sqrt(JSBI.multiply(this.reserve0.quotient, this.reserve1.quotient));
|
|
255
218
|
const rootKLast = sqrt(kLastParsed);
|
|
256
|
-
if (
|
|
257
|
-
const numerator =
|
|
258
|
-
const denominator =
|
|
259
|
-
const feeLiquidity =
|
|
260
|
-
totalSupplyAdjusted = totalSupply.add(
|
|
219
|
+
if (JSBI.greaterThan(rootK, rootKLast)) {
|
|
220
|
+
const numerator = JSBI.multiply(totalSupply.quotient, JSBI.subtract(rootK, rootKLast));
|
|
221
|
+
const denominator = JSBI.add(JSBI.multiply(rootK, FIVE), rootKLast);
|
|
222
|
+
const feeLiquidity = JSBI.divide(numerator, denominator);
|
|
223
|
+
totalSupplyAdjusted = totalSupply.add(CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
261
224
|
} else {
|
|
262
225
|
totalSupplyAdjusted = totalSupply;
|
|
263
226
|
}
|
|
@@ -265,13 +228,13 @@ var Pair = class {
|
|
|
265
228
|
totalSupplyAdjusted = totalSupply;
|
|
266
229
|
}
|
|
267
230
|
}
|
|
268
|
-
return
|
|
231
|
+
return CurrencyAmount.fromRawAmount(token, JSBI.divide(JSBI.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
|
|
269
232
|
}
|
|
270
233
|
};
|
|
271
234
|
|
|
272
235
|
// src/entities/route.ts
|
|
273
236
|
import invariant3 from "tiny-invariant";
|
|
274
|
-
import { Price as
|
|
237
|
+
import { Price as Price2 } from "@pancakeswap/swap-sdk-core";
|
|
275
238
|
var Route = class {
|
|
276
239
|
constructor(pairs, input, output) {
|
|
277
240
|
this._midPrice = null;
|
|
@@ -298,10 +261,10 @@ var Route = class {
|
|
|
298
261
|
return this._midPrice;
|
|
299
262
|
const prices = [];
|
|
300
263
|
for (const [i, pair] of this.pairs.entries()) {
|
|
301
|
-
prices.push(this.path[i].equals(pair.token0) ? new
|
|
264
|
+
prices.push(this.path[i].equals(pair.token0) ? new Price2(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient) : new Price2(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient));
|
|
302
265
|
}
|
|
303
266
|
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
304
|
-
return this._midPrice = new
|
|
267
|
+
return this._midPrice = new Price2(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
305
268
|
}
|
|
306
269
|
get chainId() {
|
|
307
270
|
return this.pairs[0].chainId;
|
|
@@ -314,9 +277,11 @@ import {
|
|
|
314
277
|
ONE as ONE2,
|
|
315
278
|
TradeType,
|
|
316
279
|
ZERO as ZERO2,
|
|
317
|
-
CurrencyAmount as
|
|
280
|
+
CurrencyAmount as CurrencyAmount2,
|
|
318
281
|
Fraction,
|
|
319
|
-
Price as
|
|
282
|
+
Price as Price3,
|
|
283
|
+
sortedInsert,
|
|
284
|
+
computePriceImpact
|
|
320
285
|
} from "@pancakeswap/swap-sdk-core";
|
|
321
286
|
function inputOutputComparator(a, b) {
|
|
322
287
|
invariant4(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
|
|
@@ -327,16 +292,13 @@ function inputOutputComparator(a, b) {
|
|
|
327
292
|
}
|
|
328
293
|
if (a.inputAmount.lessThan(b.inputAmount)) {
|
|
329
294
|
return -1;
|
|
330
|
-
} else {
|
|
331
|
-
return 1;
|
|
332
|
-
}
|
|
333
|
-
} else {
|
|
334
|
-
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
335
|
-
return 1;
|
|
336
|
-
} else {
|
|
337
|
-
return -1;
|
|
338
295
|
}
|
|
296
|
+
return 1;
|
|
339
297
|
}
|
|
298
|
+
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
299
|
+
return 1;
|
|
300
|
+
}
|
|
301
|
+
return -1;
|
|
340
302
|
}
|
|
341
303
|
function tradeComparator(a, b) {
|
|
342
304
|
const ioComp = inputOutputComparator(a, b);
|
|
@@ -345,7 +307,8 @@ function tradeComparator(a, b) {
|
|
|
345
307
|
}
|
|
346
308
|
if (a.priceImpact.lessThan(b.priceImpact)) {
|
|
347
309
|
return -1;
|
|
348
|
-
}
|
|
310
|
+
}
|
|
311
|
+
if (a.priceImpact.greaterThan(b.priceImpact)) {
|
|
349
312
|
return 1;
|
|
350
313
|
}
|
|
351
314
|
return a.route.path.length - b.route.path.length;
|
|
@@ -369,8 +332,8 @@ var Trade = class {
|
|
|
369
332
|
const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
|
|
370
333
|
tokenAmounts[i + 1] = outputAmount;
|
|
371
334
|
}
|
|
372
|
-
this.inputAmount =
|
|
373
|
-
this.outputAmount =
|
|
335
|
+
this.inputAmount = CurrencyAmount2.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
336
|
+
this.outputAmount = CurrencyAmount2.fromFractionalAmount(route.output, tokenAmounts[tokenAmounts.length - 1].numerator, tokenAmounts[tokenAmounts.length - 1].denominator);
|
|
374
337
|
} else {
|
|
375
338
|
invariant4(amount.currency.equals(route.output), "OUTPUT");
|
|
376
339
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
@@ -379,29 +342,27 @@ var Trade = class {
|
|
|
379
342
|
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
380
343
|
tokenAmounts[i - 1] = inputAmount;
|
|
381
344
|
}
|
|
382
|
-
this.inputAmount =
|
|
383
|
-
this.outputAmount =
|
|
345
|
+
this.inputAmount = CurrencyAmount2.fromFractionalAmount(route.input, tokenAmounts[0].numerator, tokenAmounts[0].denominator);
|
|
346
|
+
this.outputAmount = CurrencyAmount2.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
384
347
|
}
|
|
385
|
-
this.executionPrice = new
|
|
348
|
+
this.executionPrice = new Price3(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
386
349
|
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
387
350
|
}
|
|
388
351
|
minimumAmountOut(slippageTolerance) {
|
|
389
352
|
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
390
353
|
if (this.tradeType === TradeType.EXACT_OUTPUT) {
|
|
391
354
|
return this.outputAmount;
|
|
392
|
-
} else {
|
|
393
|
-
const slippageAdjustedAmountOut = new Fraction(ONE2).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
394
|
-
return CurrencyAmount3.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
395
355
|
}
|
|
356
|
+
const slippageAdjustedAmountOut = new Fraction(ONE2).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
357
|
+
return CurrencyAmount2.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
396
358
|
}
|
|
397
359
|
maximumAmountIn(slippageTolerance) {
|
|
398
360
|
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
399
361
|
if (this.tradeType === TradeType.EXACT_INPUT) {
|
|
400
362
|
return this.inputAmount;
|
|
401
|
-
} else {
|
|
402
|
-
const slippageAdjustedAmountIn = new Fraction(ONE2).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
403
|
-
return CurrencyAmount3.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
404
363
|
}
|
|
364
|
+
const slippageAdjustedAmountIn = new Fraction(ONE2).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
365
|
+
return CurrencyAmount2.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
405
366
|
}
|
|
406
367
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
407
368
|
invariant4(pairs.length > 0, "PAIRS");
|
|
@@ -438,7 +399,7 @@ var Trade = class {
|
|
|
438
399
|
return bestTrades;
|
|
439
400
|
}
|
|
440
401
|
worstExecutionPrice(slippageTolerance) {
|
|
441
|
-
return new
|
|
402
|
+
return new Price3(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
442
403
|
}
|
|
443
404
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
444
405
|
invariant4(pairs.length > 0, "PAIRS");
|
|
@@ -582,7 +543,7 @@ export {
|
|
|
582
543
|
FACTORY_ADDRESS_MAP,
|
|
583
544
|
INIT_CODE_HASH,
|
|
584
545
|
INIT_CODE_HASH_MAP,
|
|
585
|
-
|
|
546
|
+
JSBI2 as JSBI,
|
|
586
547
|
NATIVE,
|
|
587
548
|
Native,
|
|
588
549
|
Pair,
|
|
@@ -593,7 +554,6 @@ export {
|
|
|
593
554
|
WETH9,
|
|
594
555
|
WNATIVE,
|
|
595
556
|
computePairAddress,
|
|
596
|
-
computePriceImpact,
|
|
597
557
|
inputOutputComparator,
|
|
598
558
|
tradeComparator
|
|
599
559
|
};
|