@pancakeswap/sdk 3.1.1 → 3.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +6 -12
- package/dist/index.js +72 -87
- package/dist/index.mjs +83 -98
- 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 {
|
|
@@ -8,18 +8,18 @@ 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
|
|
15
14
|
}
|
|
15
|
+
declare const ZERO_PERCENT: Percent;
|
|
16
|
+
declare const ONE_HUNDRED_PERCENT: Percent;
|
|
16
17
|
declare const FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
17
18
|
declare const FACTORY_ADDRESS_MAP: Record<number, string>;
|
|
18
19
|
declare const INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5";
|
|
19
20
|
declare const INIT_CODE_HASH_MAP: Record<number, string>;
|
|
20
21
|
declare const WETH9: {
|
|
21
22
|
1: ERC20Token;
|
|
22
|
-
4: ERC20Token;
|
|
23
23
|
5: ERC20Token;
|
|
24
24
|
};
|
|
25
25
|
declare const WBNB: {
|
|
@@ -205,6 +205,8 @@ declare class Native extends NativeCurrency {
|
|
|
205
205
|
equals(other: Currency): boolean;
|
|
206
206
|
}
|
|
207
207
|
|
|
208
|
+
declare function isTradeBetter(tradeA: Trade<Currency, Currency, TradeType> | undefined | null, tradeB: Trade<Currency, Currency, TradeType> | undefined | null, minimumDelta?: Percent): boolean | undefined;
|
|
209
|
+
|
|
208
210
|
/**
|
|
209
211
|
* Options for producing the arguments to send call to the router.
|
|
210
212
|
*/
|
|
@@ -268,12 +270,4 @@ declare abstract class Router {
|
|
|
268
270
|
static swapCallParameters(trade: Trade<Currency, Currency, TradeType>, options: TradeOptions | TradeOptionsDeadline): SwapParameters;
|
|
269
271
|
}
|
|
270
272
|
|
|
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 };
|
|
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
|
@@ -30,9 +30,10 @@ __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
|
+
ONE_HUNDRED_PERCENT: () => ONE_HUNDRED_PERCENT,
|
|
36
37
|
Pair: () => Pair,
|
|
37
38
|
Route: () => Route,
|
|
38
39
|
Router: () => Router,
|
|
@@ -40,23 +41,25 @@ __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
|
-
computePriceImpact: () => computePriceImpact,
|
|
45
46
|
inputOutputComparator: () => inputOutputComparator,
|
|
47
|
+
isTradeBetter: () => isTradeBetter,
|
|
46
48
|
tradeComparator: () => tradeComparator
|
|
47
49
|
});
|
|
48
50
|
module.exports = __toCommonJS(src_exports);
|
|
49
|
-
var
|
|
51
|
+
var import_jsbi2 = __toESM(require("jsbi"));
|
|
50
52
|
|
|
51
|
-
// src/
|
|
53
|
+
// src/constants.ts
|
|
52
54
|
var import_swap_sdk_core2 = require("@pancakeswap/swap-sdk-core");
|
|
53
55
|
|
|
56
|
+
// src/entities/token.ts
|
|
57
|
+
var import_swap_sdk_core = require("@pancakeswap/swap-sdk-core");
|
|
58
|
+
|
|
54
59
|
// src/utils.ts
|
|
55
60
|
var import_address = require("@ethersproject/address");
|
|
56
|
-
var import_jsbi = __toESM(require("jsbi"));
|
|
57
61
|
var import_tiny_invariant = __toESM(require("tiny-invariant"));
|
|
58
62
|
var import_tiny_warning = __toESM(require("tiny-warning"));
|
|
59
|
-
var import_swap_sdk_core = require("@pancakeswap/swap-sdk-core");
|
|
60
63
|
function validateAndParseAddress(address) {
|
|
61
64
|
try {
|
|
62
65
|
const checksummedAddress = (0, import_address.getAddress)(address);
|
|
@@ -66,38 +69,9 @@ function validateAndParseAddress(address) {
|
|
|
66
69
|
(0, import_tiny_invariant.default)(false, `${address} is not a valid address.`);
|
|
67
70
|
}
|
|
68
71
|
}
|
|
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
72
|
|
|
99
73
|
// src/entities/token.ts
|
|
100
|
-
var ERC20Token = class extends
|
|
74
|
+
var ERC20Token = class extends import_swap_sdk_core.Token {
|
|
101
75
|
constructor(chainId, address, decimals, symbol, name, projectLink) {
|
|
102
76
|
super(chainId, validateAndParseAddress(address), decimals, symbol, name, projectLink);
|
|
103
77
|
}
|
|
@@ -106,17 +80,17 @@ var ERC20Token = class extends import_swap_sdk_core2.Token {
|
|
|
106
80
|
// src/constants.ts
|
|
107
81
|
var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
108
82
|
ChainId2[ChainId2["ETHEREUM"] = 1] = "ETHEREUM";
|
|
109
|
-
ChainId2[ChainId2["RINKEBY"] = 4] = "RINKEBY";
|
|
110
83
|
ChainId2[ChainId2["GOERLI"] = 5] = "GOERLI";
|
|
111
84
|
ChainId2[ChainId2["BSC"] = 56] = "BSC";
|
|
112
85
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
113
86
|
return ChainId2;
|
|
114
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");
|
|
115
90
|
var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
116
91
|
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
117
92
|
var FACTORY_ADDRESS_MAP = {
|
|
118
93
|
[1 /* ETHEREUM */]: FACTORY_ADDRESS_ETH,
|
|
119
|
-
[4 /* RINKEBY */]: FACTORY_ADDRESS_ETH,
|
|
120
94
|
[5 /* GOERLI */]: FACTORY_ADDRESS_ETH,
|
|
121
95
|
[56 /* BSC */]: FACTORY_ADDRESS,
|
|
122
96
|
[97 /* BSC_TESTNET */]: "0x6725f303b657a9451d8ba641348b6761a6cc7a17"
|
|
@@ -125,14 +99,12 @@ var INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a
|
|
|
125
99
|
var INIT_CODE_HASH_ETH = "0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d";
|
|
126
100
|
var INIT_CODE_HASH_MAP = {
|
|
127
101
|
[1 /* ETHEREUM */]: INIT_CODE_HASH_ETH,
|
|
128
|
-
[4 /* RINKEBY */]: INIT_CODE_HASH_ETH,
|
|
129
102
|
[5 /* GOERLI */]: INIT_CODE_HASH_ETH,
|
|
130
103
|
[56 /* BSC */]: INIT_CODE_HASH,
|
|
131
104
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
132
105
|
};
|
|
133
106
|
var WETH9 = {
|
|
134
107
|
[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
108
|
[5 /* GOERLI */]: new ERC20Token(5 /* GOERLI */, "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", 18, "WETH", "Wrapped Ether", "https://weth.io")
|
|
137
109
|
};
|
|
138
110
|
var WBNB = {
|
|
@@ -142,14 +114,12 @@ var WBNB = {
|
|
|
142
114
|
};
|
|
143
115
|
var WNATIVE = {
|
|
144
116
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
145
|
-
[4 /* RINKEBY */]: WETH9[4 /* RINKEBY */],
|
|
146
117
|
[5 /* GOERLI */]: WETH9[5 /* GOERLI */],
|
|
147
118
|
[56 /* BSC */]: WBNB[56 /* BSC */],
|
|
148
119
|
[97 /* BSC_TESTNET */]: WBNB[97 /* BSC_TESTNET */]
|
|
149
120
|
};
|
|
150
121
|
var NATIVE = {
|
|
151
122
|
[1 /* ETHEREUM */]: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
152
|
-
[4 /* RINKEBY */]: { name: "Rinkeby Ether", symbol: "RIN", decimals: 18 },
|
|
153
123
|
[5 /* GOERLI */]: { name: "Goerli Ether", symbol: "GOR", decimals: 18 },
|
|
154
124
|
[56 /* BSC */]: {
|
|
155
125
|
name: "Binance Chain Native Token",
|
|
@@ -163,11 +133,28 @@ var NATIVE = {
|
|
|
163
133
|
}
|
|
164
134
|
};
|
|
165
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
|
+
|
|
166
153
|
// src/entities/pair.ts
|
|
167
154
|
var import_swap_sdk_core3 = require("@pancakeswap/swap-sdk-core");
|
|
168
155
|
var import_address2 = require("@ethersproject/address");
|
|
169
156
|
var import_solidity = require("@ethersproject/solidity");
|
|
170
|
-
var
|
|
157
|
+
var import_jsbi = __toESM(require("jsbi"));
|
|
171
158
|
var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
|
|
172
159
|
var PAIR_ADDRESS_CACHE = {};
|
|
173
160
|
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
@@ -231,30 +218,30 @@ var Pair = class {
|
|
|
231
218
|
}
|
|
232
219
|
getOutputAmount(inputAmount) {
|
|
233
220
|
(0, import_tiny_invariant2.default)(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
234
|
-
if (
|
|
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)) {
|
|
235
222
|
throw new import_swap_sdk_core3.InsufficientReservesError();
|
|
236
223
|
}
|
|
237
224
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
238
225
|
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 = import_swap_sdk_core3.CurrencyAmount.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
243
|
-
if (
|
|
226
|
+
const inputAmountWithFee = import_jsbi.default.multiply(inputAmount.quotient, import_swap_sdk_core3._9975);
|
|
227
|
+
const numerator = import_jsbi.default.multiply(inputAmountWithFee, outputReserve.quotient);
|
|
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)) {
|
|
244
231
|
throw new import_swap_sdk_core3.InsufficientInputAmountError();
|
|
245
232
|
}
|
|
246
233
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
247
234
|
}
|
|
248
235
|
getInputAmount(outputAmount) {
|
|
249
236
|
(0, import_tiny_invariant2.default)(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
250
|
-
if (
|
|
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)) {
|
|
251
238
|
throw new import_swap_sdk_core3.InsufficientReservesError();
|
|
252
239
|
}
|
|
253
240
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
254
241
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
255
|
-
const numerator =
|
|
256
|
-
const denominator =
|
|
257
|
-
const inputAmount = import_swap_sdk_core3.CurrencyAmount.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
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));
|
|
258
245
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
259
246
|
}
|
|
260
247
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
@@ -262,14 +249,14 @@ var Pair = class {
|
|
|
262
249
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
263
250
|
(0, import_tiny_invariant2.default)(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
264
251
|
let liquidity;
|
|
265
|
-
if (
|
|
266
|
-
liquidity =
|
|
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);
|
|
267
254
|
} else {
|
|
268
|
-
const amount0 =
|
|
269
|
-
const amount1 =
|
|
270
|
-
liquidity =
|
|
255
|
+
const amount0 = import_jsbi.default.divide(import_jsbi.default.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
256
|
+
const amount1 = import_jsbi.default.divide(import_jsbi.default.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
257
|
+
liquidity = import_jsbi.default.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
|
|
271
258
|
}
|
|
272
|
-
if (!
|
|
259
|
+
if (!import_jsbi.default.greaterThan(liquidity, import_swap_sdk_core3.ZERO)) {
|
|
273
260
|
throw new import_swap_sdk_core3.InsufficientInputAmountError();
|
|
274
261
|
}
|
|
275
262
|
return import_swap_sdk_core3.CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
@@ -278,20 +265,20 @@ var Pair = class {
|
|
|
278
265
|
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
279
266
|
(0, import_tiny_invariant2.default)(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
280
267
|
(0, import_tiny_invariant2.default)(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
281
|
-
(0, import_tiny_invariant2.default)(
|
|
268
|
+
(0, import_tiny_invariant2.default)(import_jsbi.default.lessThanOrEqual(liquidity.quotient, totalSupply.quotient), "LIQUIDITY");
|
|
282
269
|
let totalSupplyAdjusted;
|
|
283
270
|
if (!feeOn) {
|
|
284
271
|
totalSupplyAdjusted = totalSupply;
|
|
285
272
|
} else {
|
|
286
273
|
(0, import_tiny_invariant2.default)(!!kLast, "K_LAST");
|
|
287
|
-
const kLastParsed =
|
|
288
|
-
if (!
|
|
289
|
-
const rootK = (0, import_swap_sdk_core3.sqrt)(
|
|
274
|
+
const kLastParsed = import_jsbi.default.BigInt(kLast);
|
|
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));
|
|
290
277
|
const rootKLast = (0, import_swap_sdk_core3.sqrt)(kLastParsed);
|
|
291
|
-
if (
|
|
292
|
-
const numerator =
|
|
293
|
-
const denominator =
|
|
294
|
-
const feeLiquidity =
|
|
278
|
+
if (import_jsbi.default.greaterThan(rootK, rootKLast)) {
|
|
279
|
+
const numerator = import_jsbi.default.multiply(totalSupply.quotient, import_jsbi.default.subtract(rootK, rootKLast));
|
|
280
|
+
const denominator = import_jsbi.default.add(import_jsbi.default.multiply(rootK, import_swap_sdk_core3.FIVE), rootKLast);
|
|
281
|
+
const feeLiquidity = import_jsbi.default.divide(numerator, denominator);
|
|
295
282
|
totalSupplyAdjusted = totalSupply.add(import_swap_sdk_core3.CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
296
283
|
} else {
|
|
297
284
|
totalSupplyAdjusted = totalSupply;
|
|
@@ -300,7 +287,7 @@ var Pair = class {
|
|
|
300
287
|
totalSupplyAdjusted = totalSupply;
|
|
301
288
|
}
|
|
302
289
|
}
|
|
303
|
-
return import_swap_sdk_core3.CurrencyAmount.fromRawAmount(token,
|
|
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));
|
|
304
291
|
}
|
|
305
292
|
};
|
|
306
293
|
|
|
@@ -355,16 +342,13 @@ function inputOutputComparator(a, b) {
|
|
|
355
342
|
}
|
|
356
343
|
if (a.inputAmount.lessThan(b.inputAmount)) {
|
|
357
344
|
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
345
|
}
|
|
346
|
+
return 1;
|
|
347
|
+
}
|
|
348
|
+
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
349
|
+
return 1;
|
|
367
350
|
}
|
|
351
|
+
return -1;
|
|
368
352
|
}
|
|
369
353
|
function tradeComparator(a, b) {
|
|
370
354
|
const ioComp = inputOutputComparator(a, b);
|
|
@@ -373,7 +357,8 @@ function tradeComparator(a, b) {
|
|
|
373
357
|
}
|
|
374
358
|
if (a.priceImpact.lessThan(b.priceImpact)) {
|
|
375
359
|
return -1;
|
|
376
|
-
}
|
|
360
|
+
}
|
|
361
|
+
if (a.priceImpact.greaterThan(b.priceImpact)) {
|
|
377
362
|
return 1;
|
|
378
363
|
}
|
|
379
364
|
return a.route.path.length - b.route.path.length;
|
|
@@ -411,25 +396,23 @@ var Trade = class {
|
|
|
411
396
|
this.outputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
412
397
|
}
|
|
413
398
|
this.executionPrice = new import_swap_sdk_core5.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
414
|
-
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
399
|
+
this.priceImpact = (0, import_swap_sdk_core5.computePriceImpact)(route.midPrice, this.inputAmount, this.outputAmount);
|
|
415
400
|
}
|
|
416
401
|
minimumAmountOut(slippageTolerance) {
|
|
417
402
|
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(import_swap_sdk_core5.ZERO), "SLIPPAGE_TOLERANCE");
|
|
418
403
|
if (this.tradeType === import_swap_sdk_core5.TradeType.EXACT_OUTPUT) {
|
|
419
404
|
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
405
|
}
|
|
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);
|
|
424
408
|
}
|
|
425
409
|
maximumAmountIn(slippageTolerance) {
|
|
426
410
|
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(import_swap_sdk_core5.ZERO), "SLIPPAGE_TOLERANCE");
|
|
427
411
|
if (this.tradeType === import_swap_sdk_core5.TradeType.EXACT_INPUT) {
|
|
428
412
|
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
413
|
}
|
|
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);
|
|
433
416
|
}
|
|
434
417
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
435
418
|
(0, import_tiny_invariant4.default)(pairs.length > 0, "PAIRS");
|
|
@@ -454,7 +437,7 @@ var Trade = class {
|
|
|
454
437
|
throw error;
|
|
455
438
|
}
|
|
456
439
|
if (amountOut.currency.equals(tokenOut)) {
|
|
457
|
-
sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn, import_swap_sdk_core5.TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
|
|
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);
|
|
458
441
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
459
442
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
460
443
|
Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
|
|
@@ -491,7 +474,7 @@ var Trade = class {
|
|
|
491
474
|
throw error;
|
|
492
475
|
}
|
|
493
476
|
if (amountIn.currency.equals(tokenIn)) {
|
|
494
|
-
sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut, import_swap_sdk_core5.TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
|
|
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);
|
|
495
478
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
496
479
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
497
480
|
Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
|
|
@@ -614,6 +597,7 @@ __reExport(src_exports, require("@pancakeswap/swap-sdk-core"), module.exports);
|
|
|
614
597
|
JSBI,
|
|
615
598
|
NATIVE,
|
|
616
599
|
Native,
|
|
600
|
+
ONE_HUNDRED_PERCENT,
|
|
617
601
|
Pair,
|
|
618
602
|
Route,
|
|
619
603
|
Router,
|
|
@@ -621,8 +605,9 @@ __reExport(src_exports, require("@pancakeswap/swap-sdk-core"), module.exports);
|
|
|
621
605
|
WBNB,
|
|
622
606
|
WETH9,
|
|
623
607
|
WNATIVE,
|
|
608
|
+
ZERO_PERCENT,
|
|
624
609
|
computePairAddress,
|
|
625
|
-
computePriceImpact,
|
|
626
610
|
inputOutputComparator,
|
|
611
|
+
isTradeBetter,
|
|
627
612
|
tradeComparator
|
|
628
613
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import
|
|
2
|
+
import JSBI2 from "jsbi";
|
|
3
|
+
|
|
4
|
+
// src/constants.ts
|
|
5
|
+
import { Percent } from "@pancakeswap/swap-sdk-core";
|
|
3
6
|
|
|
4
7
|
// src/entities/token.ts
|
|
5
8
|
import { Token } from "@pancakeswap/swap-sdk-core";
|
|
6
9
|
|
|
7
10
|
// src/utils.ts
|
|
8
11
|
import { getAddress } from "@ethersproject/address";
|
|
9
|
-
import JSBI from "jsbi";
|
|
10
12
|
import invariant from "tiny-invariant";
|
|
11
13
|
import warning from "tiny-warning";
|
|
12
|
-
import { Percent } from "@pancakeswap/swap-sdk-core";
|
|
13
14
|
function validateAndParseAddress(address) {
|
|
14
15
|
try {
|
|
15
16
|
const checksummedAddress = getAddress(address);
|
|
@@ -19,35 +20,6 @@ function validateAndParseAddress(address) {
|
|
|
19
20
|
invariant(false, `${address} is not a valid address.`);
|
|
20
21
|
}
|
|
21
22
|
}
|
|
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
23
|
|
|
52
24
|
// src/entities/token.ts
|
|
53
25
|
var ERC20Token = class extends Token {
|
|
@@ -59,17 +31,17 @@ var ERC20Token = class extends Token {
|
|
|
59
31
|
// src/constants.ts
|
|
60
32
|
var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
61
33
|
ChainId2[ChainId2["ETHEREUM"] = 1] = "ETHEREUM";
|
|
62
|
-
ChainId2[ChainId2["RINKEBY"] = 4] = "RINKEBY";
|
|
63
34
|
ChainId2[ChainId2["GOERLI"] = 5] = "GOERLI";
|
|
64
35
|
ChainId2[ChainId2["BSC"] = 56] = "BSC";
|
|
65
36
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
66
37
|
return ChainId2;
|
|
67
38
|
})(ChainId || {});
|
|
39
|
+
var ZERO_PERCENT = new Percent("0");
|
|
40
|
+
var ONE_HUNDRED_PERCENT = new Percent("1");
|
|
68
41
|
var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
69
42
|
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
70
43
|
var FACTORY_ADDRESS_MAP = {
|
|
71
44
|
[1 /* ETHEREUM */]: FACTORY_ADDRESS_ETH,
|
|
72
|
-
[4 /* RINKEBY */]: FACTORY_ADDRESS_ETH,
|
|
73
45
|
[5 /* GOERLI */]: FACTORY_ADDRESS_ETH,
|
|
74
46
|
[56 /* BSC */]: FACTORY_ADDRESS,
|
|
75
47
|
[97 /* BSC_TESTNET */]: "0x6725f303b657a9451d8ba641348b6761a6cc7a17"
|
|
@@ -78,14 +50,12 @@ var INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a
|
|
|
78
50
|
var INIT_CODE_HASH_ETH = "0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d";
|
|
79
51
|
var INIT_CODE_HASH_MAP = {
|
|
80
52
|
[1 /* ETHEREUM */]: INIT_CODE_HASH_ETH,
|
|
81
|
-
[4 /* RINKEBY */]: INIT_CODE_HASH_ETH,
|
|
82
53
|
[5 /* GOERLI */]: INIT_CODE_HASH_ETH,
|
|
83
54
|
[56 /* BSC */]: INIT_CODE_HASH,
|
|
84
55
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
85
56
|
};
|
|
86
57
|
var WETH9 = {
|
|
87
58
|
[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
59
|
[5 /* GOERLI */]: new ERC20Token(5 /* GOERLI */, "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", 18, "WETH", "Wrapped Ether", "https://weth.io")
|
|
90
60
|
};
|
|
91
61
|
var WBNB = {
|
|
@@ -95,14 +65,12 @@ var WBNB = {
|
|
|
95
65
|
};
|
|
96
66
|
var WNATIVE = {
|
|
97
67
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
98
|
-
[4 /* RINKEBY */]: WETH9[4 /* RINKEBY */],
|
|
99
68
|
[5 /* GOERLI */]: WETH9[5 /* GOERLI */],
|
|
100
69
|
[56 /* BSC */]: WBNB[56 /* BSC */],
|
|
101
70
|
[97 /* BSC_TESTNET */]: WBNB[97 /* BSC_TESTNET */]
|
|
102
71
|
};
|
|
103
72
|
var NATIVE = {
|
|
104
73
|
[1 /* ETHEREUM */]: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
105
|
-
[4 /* RINKEBY */]: { name: "Rinkeby Ether", symbol: "RIN", decimals: 18 },
|
|
106
74
|
[5 /* GOERLI */]: { name: "Goerli Ether", symbol: "GOR", decimals: 18 },
|
|
107
75
|
[56 /* BSC */]: {
|
|
108
76
|
name: "Binance Chain Native Token",
|
|
@@ -116,13 +84,30 @@ var NATIVE = {
|
|
|
116
84
|
}
|
|
117
85
|
};
|
|
118
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
|
+
|
|
119
104
|
// src/entities/pair.ts
|
|
120
105
|
import {
|
|
121
106
|
InsufficientInputAmountError,
|
|
122
107
|
InsufficientReservesError,
|
|
123
108
|
sqrt,
|
|
124
|
-
CurrencyAmount
|
|
125
|
-
Price
|
|
109
|
+
CurrencyAmount,
|
|
110
|
+
Price,
|
|
126
111
|
FIVE,
|
|
127
112
|
ONE,
|
|
128
113
|
ZERO,
|
|
@@ -132,7 +117,7 @@ import {
|
|
|
132
117
|
} from "@pancakeswap/swap-sdk-core";
|
|
133
118
|
import { getCreate2Address } from "@ethersproject/address";
|
|
134
119
|
import { keccak256, pack } from "@ethersproject/solidity";
|
|
135
|
-
import
|
|
120
|
+
import JSBI from "jsbi";
|
|
136
121
|
import invariant2 from "tiny-invariant";
|
|
137
122
|
var PAIR_ADDRESS_CACHE = {};
|
|
138
123
|
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
@@ -165,11 +150,11 @@ var Pair = class {
|
|
|
165
150
|
}
|
|
166
151
|
get token0Price() {
|
|
167
152
|
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
168
|
-
return new
|
|
153
|
+
return new Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
169
154
|
}
|
|
170
155
|
get token1Price() {
|
|
171
156
|
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
172
|
-
return new
|
|
157
|
+
return new Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
173
158
|
}
|
|
174
159
|
priceOf(token) {
|
|
175
160
|
invariant2(this.involvesToken(token), "TOKEN");
|
|
@@ -196,30 +181,30 @@ var Pair = class {
|
|
|
196
181
|
}
|
|
197
182
|
getOutputAmount(inputAmount) {
|
|
198
183
|
invariant2(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
199
|
-
if (
|
|
184
|
+
if (JSBI.equal(this.reserve0.quotient, ZERO) || JSBI.equal(this.reserve1.quotient, ZERO)) {
|
|
200
185
|
throw new InsufficientReservesError();
|
|
201
186
|
}
|
|
202
187
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
203
188
|
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 (
|
|
189
|
+
const inputAmountWithFee = JSBI.multiply(inputAmount.quotient, _9975);
|
|
190
|
+
const numerator = JSBI.multiply(inputAmountWithFee, outputReserve.quotient);
|
|
191
|
+
const denominator = JSBI.add(JSBI.multiply(inputReserve.quotient, _10000), inputAmountWithFee);
|
|
192
|
+
const outputAmount = CurrencyAmount.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI.divide(numerator, denominator));
|
|
193
|
+
if (JSBI.equal(outputAmount.quotient, ZERO)) {
|
|
209
194
|
throw new InsufficientInputAmountError();
|
|
210
195
|
}
|
|
211
196
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
212
197
|
}
|
|
213
198
|
getInputAmount(outputAmount) {
|
|
214
199
|
invariant2(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
215
|
-
if (
|
|
200
|
+
if (JSBI.equal(this.reserve0.quotient, ZERO) || JSBI.equal(this.reserve1.quotient, ZERO) || JSBI.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient)) {
|
|
216
201
|
throw new InsufficientReservesError();
|
|
217
202
|
}
|
|
218
203
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
219
204
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
220
|
-
const numerator =
|
|
221
|
-
const denominator =
|
|
222
|
-
const inputAmount =
|
|
205
|
+
const numerator = JSBI.multiply(JSBI.multiply(inputReserve.quotient, outputAmount.quotient), _10000);
|
|
206
|
+
const denominator = JSBI.multiply(JSBI.subtract(outputReserve.quotient, outputAmount.quotient), _9975);
|
|
207
|
+
const inputAmount = CurrencyAmount.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI.add(JSBI.divide(numerator, denominator), ONE));
|
|
223
208
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
224
209
|
}
|
|
225
210
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
@@ -227,37 +212,37 @@ var Pair = class {
|
|
|
227
212
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
228
213
|
invariant2(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
229
214
|
let liquidity;
|
|
230
|
-
if (
|
|
231
|
-
liquidity =
|
|
215
|
+
if (JSBI.equal(totalSupply.quotient, ZERO)) {
|
|
216
|
+
liquidity = JSBI.subtract(sqrt(JSBI.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), MINIMUM_LIQUIDITY);
|
|
232
217
|
} else {
|
|
233
|
-
const amount0 =
|
|
234
|
-
const amount1 =
|
|
235
|
-
liquidity =
|
|
218
|
+
const amount0 = JSBI.divide(JSBI.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
219
|
+
const amount1 = JSBI.divide(JSBI.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
220
|
+
liquidity = JSBI.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
|
|
236
221
|
}
|
|
237
|
-
if (!
|
|
222
|
+
if (!JSBI.greaterThan(liquidity, ZERO)) {
|
|
238
223
|
throw new InsufficientInputAmountError();
|
|
239
224
|
}
|
|
240
|
-
return
|
|
225
|
+
return CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
241
226
|
}
|
|
242
227
|
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
243
228
|
invariant2(this.involvesToken(token), "TOKEN");
|
|
244
229
|
invariant2(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
245
230
|
invariant2(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
246
|
-
invariant2(
|
|
231
|
+
invariant2(JSBI.lessThanOrEqual(liquidity.quotient, totalSupply.quotient), "LIQUIDITY");
|
|
247
232
|
let totalSupplyAdjusted;
|
|
248
233
|
if (!feeOn) {
|
|
249
234
|
totalSupplyAdjusted = totalSupply;
|
|
250
235
|
} else {
|
|
251
236
|
invariant2(!!kLast, "K_LAST");
|
|
252
|
-
const kLastParsed =
|
|
253
|
-
if (!
|
|
254
|
-
const rootK = sqrt(
|
|
237
|
+
const kLastParsed = JSBI.BigInt(kLast);
|
|
238
|
+
if (!JSBI.equal(kLastParsed, ZERO)) {
|
|
239
|
+
const rootK = sqrt(JSBI.multiply(this.reserve0.quotient, this.reserve1.quotient));
|
|
255
240
|
const rootKLast = sqrt(kLastParsed);
|
|
256
|
-
if (
|
|
257
|
-
const numerator =
|
|
258
|
-
const denominator =
|
|
259
|
-
const feeLiquidity =
|
|
260
|
-
totalSupplyAdjusted = totalSupply.add(
|
|
241
|
+
if (JSBI.greaterThan(rootK, rootKLast)) {
|
|
242
|
+
const numerator = JSBI.multiply(totalSupply.quotient, JSBI.subtract(rootK, rootKLast));
|
|
243
|
+
const denominator = JSBI.add(JSBI.multiply(rootK, FIVE), rootKLast);
|
|
244
|
+
const feeLiquidity = JSBI.divide(numerator, denominator);
|
|
245
|
+
totalSupplyAdjusted = totalSupply.add(CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
261
246
|
} else {
|
|
262
247
|
totalSupplyAdjusted = totalSupply;
|
|
263
248
|
}
|
|
@@ -265,13 +250,13 @@ var Pair = class {
|
|
|
265
250
|
totalSupplyAdjusted = totalSupply;
|
|
266
251
|
}
|
|
267
252
|
}
|
|
268
|
-
return
|
|
253
|
+
return CurrencyAmount.fromRawAmount(token, JSBI.divide(JSBI.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
|
|
269
254
|
}
|
|
270
255
|
};
|
|
271
256
|
|
|
272
257
|
// src/entities/route.ts
|
|
273
258
|
import invariant3 from "tiny-invariant";
|
|
274
|
-
import { Price as
|
|
259
|
+
import { Price as Price2 } from "@pancakeswap/swap-sdk-core";
|
|
275
260
|
var Route = class {
|
|
276
261
|
constructor(pairs, input, output) {
|
|
277
262
|
this._midPrice = null;
|
|
@@ -298,10 +283,10 @@ var Route = class {
|
|
|
298
283
|
return this._midPrice;
|
|
299
284
|
const prices = [];
|
|
300
285
|
for (const [i, pair] of this.pairs.entries()) {
|
|
301
|
-
prices.push(this.path[i].equals(pair.token0) ? new
|
|
286
|
+
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
287
|
}
|
|
303
288
|
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
304
|
-
return this._midPrice = new
|
|
289
|
+
return this._midPrice = new Price2(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
305
290
|
}
|
|
306
291
|
get chainId() {
|
|
307
292
|
return this.pairs[0].chainId;
|
|
@@ -314,9 +299,11 @@ import {
|
|
|
314
299
|
ONE as ONE2,
|
|
315
300
|
TradeType,
|
|
316
301
|
ZERO as ZERO2,
|
|
317
|
-
CurrencyAmount as
|
|
302
|
+
CurrencyAmount as CurrencyAmount2,
|
|
318
303
|
Fraction,
|
|
319
|
-
Price as
|
|
304
|
+
Price as Price3,
|
|
305
|
+
sortedInsert,
|
|
306
|
+
computePriceImpact
|
|
320
307
|
} from "@pancakeswap/swap-sdk-core";
|
|
321
308
|
function inputOutputComparator(a, b) {
|
|
322
309
|
invariant4(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
|
|
@@ -327,16 +314,13 @@ function inputOutputComparator(a, b) {
|
|
|
327
314
|
}
|
|
328
315
|
if (a.inputAmount.lessThan(b.inputAmount)) {
|
|
329
316
|
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
317
|
}
|
|
318
|
+
return 1;
|
|
319
|
+
}
|
|
320
|
+
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
321
|
+
return 1;
|
|
339
322
|
}
|
|
323
|
+
return -1;
|
|
340
324
|
}
|
|
341
325
|
function tradeComparator(a, b) {
|
|
342
326
|
const ioComp = inputOutputComparator(a, b);
|
|
@@ -345,7 +329,8 @@ function tradeComparator(a, b) {
|
|
|
345
329
|
}
|
|
346
330
|
if (a.priceImpact.lessThan(b.priceImpact)) {
|
|
347
331
|
return -1;
|
|
348
|
-
}
|
|
332
|
+
}
|
|
333
|
+
if (a.priceImpact.greaterThan(b.priceImpact)) {
|
|
349
334
|
return 1;
|
|
350
335
|
}
|
|
351
336
|
return a.route.path.length - b.route.path.length;
|
|
@@ -369,8 +354,8 @@ var Trade = class {
|
|
|
369
354
|
const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
|
|
370
355
|
tokenAmounts[i + 1] = outputAmount;
|
|
371
356
|
}
|
|
372
|
-
this.inputAmount =
|
|
373
|
-
this.outputAmount =
|
|
357
|
+
this.inputAmount = CurrencyAmount2.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
358
|
+
this.outputAmount = CurrencyAmount2.fromFractionalAmount(route.output, tokenAmounts[tokenAmounts.length - 1].numerator, tokenAmounts[tokenAmounts.length - 1].denominator);
|
|
374
359
|
} else {
|
|
375
360
|
invariant4(amount.currency.equals(route.output), "OUTPUT");
|
|
376
361
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
@@ -379,29 +364,27 @@ var Trade = class {
|
|
|
379
364
|
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
380
365
|
tokenAmounts[i - 1] = inputAmount;
|
|
381
366
|
}
|
|
382
|
-
this.inputAmount =
|
|
383
|
-
this.outputAmount =
|
|
367
|
+
this.inputAmount = CurrencyAmount2.fromFractionalAmount(route.input, tokenAmounts[0].numerator, tokenAmounts[0].denominator);
|
|
368
|
+
this.outputAmount = CurrencyAmount2.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
384
369
|
}
|
|
385
|
-
this.executionPrice = new
|
|
370
|
+
this.executionPrice = new Price3(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
386
371
|
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
387
372
|
}
|
|
388
373
|
minimumAmountOut(slippageTolerance) {
|
|
389
374
|
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
390
375
|
if (this.tradeType === TradeType.EXACT_OUTPUT) {
|
|
391
376
|
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
377
|
}
|
|
378
|
+
const slippageAdjustedAmountOut = new Fraction(ONE2).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
379
|
+
return CurrencyAmount2.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
396
380
|
}
|
|
397
381
|
maximumAmountIn(slippageTolerance) {
|
|
398
382
|
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
399
383
|
if (this.tradeType === TradeType.EXACT_INPUT) {
|
|
400
384
|
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
385
|
}
|
|
386
|
+
const slippageAdjustedAmountIn = new Fraction(ONE2).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
387
|
+
return CurrencyAmount2.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
405
388
|
}
|
|
406
389
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
407
390
|
invariant4(pairs.length > 0, "PAIRS");
|
|
@@ -438,7 +421,7 @@ var Trade = class {
|
|
|
438
421
|
return bestTrades;
|
|
439
422
|
}
|
|
440
423
|
worstExecutionPrice(slippageTolerance) {
|
|
441
|
-
return new
|
|
424
|
+
return new Price3(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
442
425
|
}
|
|
443
426
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
444
427
|
invariant4(pairs.length > 0, "PAIRS");
|
|
@@ -582,9 +565,10 @@ export {
|
|
|
582
565
|
FACTORY_ADDRESS_MAP,
|
|
583
566
|
INIT_CODE_HASH,
|
|
584
567
|
INIT_CODE_HASH_MAP,
|
|
585
|
-
|
|
568
|
+
JSBI2 as JSBI,
|
|
586
569
|
NATIVE,
|
|
587
570
|
Native,
|
|
571
|
+
ONE_HUNDRED_PERCENT,
|
|
588
572
|
Pair,
|
|
589
573
|
Route,
|
|
590
574
|
Router,
|
|
@@ -592,8 +576,9 @@ export {
|
|
|
592
576
|
WBNB,
|
|
593
577
|
WETH9,
|
|
594
578
|
WNATIVE,
|
|
579
|
+
ZERO_PERCENT,
|
|
595
580
|
computePairAddress,
|
|
596
|
-
computePriceImpact,
|
|
597
581
|
inputOutputComparator,
|
|
582
|
+
isTradeBetter,
|
|
598
583
|
tradeComparator
|
|
599
584
|
};
|