@pancakeswap/sdk 3.0.0 → 3.1.1
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/README.md +1 -1
- package/dist/index.d.ts +30 -257
- package/dist/index.js +134 -502
- package/dist/index.mjs +140 -467
- package/package.json +10 -5
package/dist/index.mjs
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
// src/constants.ts
|
|
5
|
-
import JSBI2 from "jsbi";
|
|
2
|
+
import JSBI3 from "jsbi";
|
|
6
3
|
|
|
7
4
|
// src/entities/token.ts
|
|
8
|
-
import
|
|
5
|
+
import { Token } from "@pancakeswap/swap-sdk-core";
|
|
9
6
|
|
|
10
7
|
// src/utils.ts
|
|
8
|
+
import { getAddress } from "@ethersproject/address";
|
|
9
|
+
import JSBI from "jsbi";
|
|
11
10
|
import invariant from "tiny-invariant";
|
|
12
11
|
import warning from "tiny-warning";
|
|
13
|
-
import
|
|
14
|
-
import { getAddress } from "@ethersproject/address";
|
|
15
|
-
function validateSolidityTypeInstance(value, solidityType) {
|
|
16
|
-
invariant(JSBI.greaterThanOrEqual(value, ZERO), `${value} is not a ${solidityType}.`);
|
|
17
|
-
invariant(JSBI.lessThanOrEqual(value, SOLIDITY_TYPE_MAXIMA[solidityType]), `${value} is not a ${solidityType}.`);
|
|
18
|
-
}
|
|
12
|
+
import { Percent } from "@pancakeswap/swap-sdk-core";
|
|
19
13
|
function validateAndParseAddress(address) {
|
|
20
14
|
try {
|
|
21
15
|
const checksummedAddress = getAddress(address);
|
|
@@ -25,22 +19,6 @@ function validateAndParseAddress(address) {
|
|
|
25
19
|
invariant(false, `${address} is not a valid address.`);
|
|
26
20
|
}
|
|
27
21
|
}
|
|
28
|
-
function sqrt(y) {
|
|
29
|
-
validateSolidityTypeInstance(y, "uint256" /* uint256 */);
|
|
30
|
-
let z = ZERO;
|
|
31
|
-
let x;
|
|
32
|
-
if (JSBI.greaterThan(y, THREE)) {
|
|
33
|
-
z = y;
|
|
34
|
-
x = JSBI.add(JSBI.divide(y, TWO), ONE);
|
|
35
|
-
while (JSBI.lessThan(x, z)) {
|
|
36
|
-
z = x;
|
|
37
|
-
x = JSBI.divide(JSBI.add(JSBI.divide(y, x), x), TWO);
|
|
38
|
-
}
|
|
39
|
-
} else if (JSBI.notEqual(y, ZERO)) {
|
|
40
|
-
z = ONE;
|
|
41
|
-
}
|
|
42
|
-
return z;
|
|
43
|
-
}
|
|
44
22
|
function sortedInsert(items, add, maxSize, comparator) {
|
|
45
23
|
invariant(maxSize > 0, "MAX_SIZE_ZERO");
|
|
46
24
|
invariant(items.length <= maxSize, "ITEMS_SIZE");
|
|
@@ -71,38 +49,10 @@ function computePriceImpact(midPrice, inputAmount, outputAmount) {
|
|
|
71
49
|
return new Percent(priceImpact.numerator, priceImpact.denominator);
|
|
72
50
|
}
|
|
73
51
|
|
|
74
|
-
// src/entities/baseCurrency.ts
|
|
75
|
-
import invariant2 from "tiny-invariant";
|
|
76
|
-
var BaseCurrency = class {
|
|
77
|
-
constructor(chainId, decimals, symbol, name) {
|
|
78
|
-
invariant2(Number.isSafeInteger(chainId), "CHAIN_ID");
|
|
79
|
-
invariant2(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
|
|
80
|
-
this.chainId = chainId;
|
|
81
|
-
this.decimals = decimals;
|
|
82
|
-
this.symbol = symbol;
|
|
83
|
-
this.name = name;
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
|
|
87
52
|
// src/entities/token.ts
|
|
88
|
-
var
|
|
53
|
+
var ERC20Token = class extends Token {
|
|
89
54
|
constructor(chainId, address, decimals, symbol, name, projectLink) {
|
|
90
|
-
super(chainId, decimals, symbol, name);
|
|
91
|
-
this.isNative = false;
|
|
92
|
-
this.isToken = true;
|
|
93
|
-
this.address = validateAndParseAddress(address);
|
|
94
|
-
this.projectLink = projectLink;
|
|
95
|
-
}
|
|
96
|
-
equals(other) {
|
|
97
|
-
return other.isToken && this.chainId === other.chainId && this.address === other.address;
|
|
98
|
-
}
|
|
99
|
-
sortsBefore(other) {
|
|
100
|
-
invariant3(this.chainId === other.chainId, "CHAIN_IDS");
|
|
101
|
-
invariant3(this.address !== other.address, "ADDRESSES");
|
|
102
|
-
return this.address.toLowerCase() < other.address.toLowerCase();
|
|
103
|
-
}
|
|
104
|
-
get wrapped() {
|
|
105
|
-
return this;
|
|
55
|
+
super(chainId, validateAndParseAddress(address), decimals, symbol, name, projectLink);
|
|
106
56
|
}
|
|
107
57
|
};
|
|
108
58
|
|
|
@@ -115,19 +65,8 @@ var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
|
115
65
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
116
66
|
return ChainId2;
|
|
117
67
|
})(ChainId || {});
|
|
118
|
-
var TradeType = /* @__PURE__ */ ((TradeType2) => {
|
|
119
|
-
TradeType2[TradeType2["EXACT_INPUT"] = 0] = "EXACT_INPUT";
|
|
120
|
-
TradeType2[TradeType2["EXACT_OUTPUT"] = 1] = "EXACT_OUTPUT";
|
|
121
|
-
return TradeType2;
|
|
122
|
-
})(TradeType || {});
|
|
123
|
-
var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
124
|
-
Rounding2[Rounding2["ROUND_DOWN"] = 0] = "ROUND_DOWN";
|
|
125
|
-
Rounding2[Rounding2["ROUND_HALF_UP"] = 1] = "ROUND_HALF_UP";
|
|
126
|
-
Rounding2[Rounding2["ROUND_UP"] = 2] = "ROUND_UP";
|
|
127
|
-
return Rounding2;
|
|
128
|
-
})(Rounding || {});
|
|
129
68
|
var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
130
|
-
var FACTORY_ADDRESS_ETH = "
|
|
69
|
+
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
131
70
|
var FACTORY_ADDRESS_MAP = {
|
|
132
71
|
[1 /* ETHEREUM */]: FACTORY_ADDRESS_ETH,
|
|
133
72
|
[4 /* RINKEBY */]: FACTORY_ADDRESS_ETH,
|
|
@@ -144,35 +83,15 @@ var INIT_CODE_HASH_MAP = {
|
|
|
144
83
|
[56 /* BSC */]: INIT_CODE_HASH,
|
|
145
84
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
146
85
|
};
|
|
147
|
-
var MINIMUM_LIQUIDITY = JSBI2.BigInt(1e3);
|
|
148
|
-
var ZERO = JSBI2.BigInt(0);
|
|
149
|
-
var ONE = JSBI2.BigInt(1);
|
|
150
|
-
var TWO = JSBI2.BigInt(2);
|
|
151
|
-
var THREE = JSBI2.BigInt(3);
|
|
152
|
-
var FIVE = JSBI2.BigInt(5);
|
|
153
|
-
var TEN = JSBI2.BigInt(10);
|
|
154
|
-
var _100 = JSBI2.BigInt(100);
|
|
155
|
-
var _9975 = JSBI2.BigInt(9975);
|
|
156
|
-
var _10000 = JSBI2.BigInt(1e4);
|
|
157
|
-
var MaxUint256 = JSBI2.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
158
|
-
var SolidityType = /* @__PURE__ */ ((SolidityType2) => {
|
|
159
|
-
SolidityType2["uint8"] = "uint8";
|
|
160
|
-
SolidityType2["uint256"] = "uint256";
|
|
161
|
-
return SolidityType2;
|
|
162
|
-
})(SolidityType || {});
|
|
163
|
-
var SOLIDITY_TYPE_MAXIMA = {
|
|
164
|
-
["uint8" /* uint8 */]: JSBI2.BigInt("0xff"),
|
|
165
|
-
["uint256" /* uint256 */]: JSBI2.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
|
166
|
-
};
|
|
167
86
|
var WETH9 = {
|
|
168
|
-
[1 /* ETHEREUM */]: new
|
|
169
|
-
[4 /* RINKEBY */]: new
|
|
170
|
-
[5 /* GOERLI */]: new
|
|
87
|
+
[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
|
+
[5 /* GOERLI */]: new ERC20Token(5 /* GOERLI */, "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", 18, "WETH", "Wrapped Ether", "https://weth.io")
|
|
171
90
|
};
|
|
172
91
|
var WBNB = {
|
|
173
|
-
[1 /* ETHEREUM */]: new
|
|
174
|
-
[56 /* BSC */]: new
|
|
175
|
-
[97 /* BSC_TESTNET */]: new
|
|
92
|
+
[1 /* ETHEREUM */]: new ERC20Token(1 /* ETHEREUM */, "0x418D75f65a02b3D53B2418FB8E1fe493759c7605", 18, "WBNB", "Wrapped BNB", "https://www.binance.org"),
|
|
93
|
+
[56 /* BSC */]: new ERC20Token(56 /* BSC */, "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", 18, "WBNB", "Wrapped BNB", "https://www.binance.org"),
|
|
94
|
+
[97 /* BSC_TESTNET */]: new ERC20Token(97 /* BSC_TESTNET */, "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd", 18, "WBNB", "Wrapped BNB", "https://www.binance.org")
|
|
176
95
|
};
|
|
177
96
|
var WNATIVE = {
|
|
178
97
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
@@ -197,268 +116,40 @@ var NATIVE = {
|
|
|
197
116
|
}
|
|
198
117
|
};
|
|
199
118
|
|
|
200
|
-
// src/errors.ts
|
|
201
|
-
var CAN_SET_PROTOTYPE = "setPrototypeOf" in Object;
|
|
202
|
-
var InsufficientReservesError = class extends Error {
|
|
203
|
-
constructor() {
|
|
204
|
-
super();
|
|
205
|
-
this.isInsufficientReservesError = true;
|
|
206
|
-
this.name = this.constructor.name;
|
|
207
|
-
if (CAN_SET_PROTOTYPE)
|
|
208
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
var InsufficientInputAmountError = class extends Error {
|
|
212
|
-
constructor() {
|
|
213
|
-
super();
|
|
214
|
-
this.isInsufficientInputAmountError = true;
|
|
215
|
-
this.name = this.constructor.name;
|
|
216
|
-
if (CAN_SET_PROTOTYPE)
|
|
217
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
218
|
-
}
|
|
219
|
-
};
|
|
220
|
-
|
|
221
119
|
// src/entities/pair.ts
|
|
120
|
+
import {
|
|
121
|
+
InsufficientInputAmountError,
|
|
122
|
+
InsufficientReservesError,
|
|
123
|
+
sqrt,
|
|
124
|
+
CurrencyAmount as CurrencyAmount2,
|
|
125
|
+
Price as Price2,
|
|
126
|
+
FIVE,
|
|
127
|
+
ONE,
|
|
128
|
+
ZERO,
|
|
129
|
+
_10000,
|
|
130
|
+
_9975,
|
|
131
|
+
MINIMUM_LIQUIDITY
|
|
132
|
+
} from "@pancakeswap/swap-sdk-core";
|
|
222
133
|
import { getCreate2Address } from "@ethersproject/address";
|
|
223
134
|
import { keccak256, pack } from "@ethersproject/solidity";
|
|
224
|
-
import
|
|
225
|
-
import
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
import JSBI5 from "jsbi";
|
|
229
|
-
import invariant6 from "tiny-invariant";
|
|
230
|
-
|
|
231
|
-
// src/entities/fractions/fraction.ts
|
|
232
|
-
import JSBI3 from "jsbi";
|
|
233
|
-
import invariant4 from "tiny-invariant";
|
|
234
|
-
import _Decimal from "decimal.js-light";
|
|
235
|
-
import _Big from "big.js";
|
|
236
|
-
import toFormat from "toformat";
|
|
237
|
-
var Decimal = toFormat(_Decimal);
|
|
238
|
-
var Big = toFormat(_Big);
|
|
239
|
-
var toSignificantRounding = {
|
|
240
|
-
[0 /* ROUND_DOWN */]: Decimal.ROUND_DOWN,
|
|
241
|
-
[1 /* ROUND_HALF_UP */]: Decimal.ROUND_HALF_UP,
|
|
242
|
-
[2 /* ROUND_UP */]: Decimal.ROUND_UP
|
|
243
|
-
};
|
|
244
|
-
var toFixedRounding = {
|
|
245
|
-
[0 /* ROUND_DOWN */]: 0 /* RoundDown */,
|
|
246
|
-
[1 /* ROUND_HALF_UP */]: 1 /* RoundHalfUp */,
|
|
247
|
-
[2 /* ROUND_UP */]: 3 /* RoundUp */
|
|
248
|
-
};
|
|
249
|
-
var Fraction = class {
|
|
250
|
-
constructor(numerator, denominator = JSBI3.BigInt(1)) {
|
|
251
|
-
this.numerator = JSBI3.BigInt(numerator);
|
|
252
|
-
this.denominator = JSBI3.BigInt(denominator);
|
|
253
|
-
}
|
|
254
|
-
static tryParseFraction(fractionish) {
|
|
255
|
-
if (fractionish instanceof JSBI3 || typeof fractionish === "number" || typeof fractionish === "string")
|
|
256
|
-
return new Fraction(fractionish);
|
|
257
|
-
if ("numerator" in fractionish && "denominator" in fractionish)
|
|
258
|
-
return fractionish;
|
|
259
|
-
throw new Error("Could not parse fraction");
|
|
260
|
-
}
|
|
261
|
-
get quotient() {
|
|
262
|
-
return JSBI3.divide(this.numerator, this.denominator);
|
|
263
|
-
}
|
|
264
|
-
get remainder() {
|
|
265
|
-
return new Fraction(JSBI3.remainder(this.numerator, this.denominator), this.denominator);
|
|
266
|
-
}
|
|
267
|
-
invert() {
|
|
268
|
-
return new Fraction(this.denominator, this.numerator);
|
|
269
|
-
}
|
|
270
|
-
add(other) {
|
|
271
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
272
|
-
if (JSBI3.equal(this.denominator, otherParsed.denominator)) {
|
|
273
|
-
return new Fraction(JSBI3.add(this.numerator, otherParsed.numerator), this.denominator);
|
|
274
|
-
}
|
|
275
|
-
return new Fraction(JSBI3.add(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator)), JSBI3.multiply(this.denominator, otherParsed.denominator));
|
|
276
|
-
}
|
|
277
|
-
subtract(other) {
|
|
278
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
279
|
-
if (JSBI3.equal(this.denominator, otherParsed.denominator)) {
|
|
280
|
-
return new Fraction(JSBI3.subtract(this.numerator, otherParsed.numerator), this.denominator);
|
|
281
|
-
}
|
|
282
|
-
return new Fraction(JSBI3.subtract(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator)), JSBI3.multiply(this.denominator, otherParsed.denominator));
|
|
283
|
-
}
|
|
284
|
-
lessThan(other) {
|
|
285
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
286
|
-
return JSBI3.lessThan(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator));
|
|
287
|
-
}
|
|
288
|
-
equalTo(other) {
|
|
289
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
290
|
-
return JSBI3.equal(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator));
|
|
291
|
-
}
|
|
292
|
-
greaterThan(other) {
|
|
293
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
294
|
-
return JSBI3.greaterThan(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator));
|
|
295
|
-
}
|
|
296
|
-
multiply(other) {
|
|
297
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
298
|
-
return new Fraction(JSBI3.multiply(this.numerator, otherParsed.numerator), JSBI3.multiply(this.denominator, otherParsed.denominator));
|
|
299
|
-
}
|
|
300
|
-
divide(other) {
|
|
301
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
302
|
-
return new Fraction(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(this.denominator, otherParsed.numerator));
|
|
303
|
-
}
|
|
304
|
-
toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
305
|
-
invariant4(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
|
|
306
|
-
invariant4(significantDigits > 0, `${significantDigits} is not positive.`);
|
|
307
|
-
Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });
|
|
308
|
-
const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
|
|
309
|
-
return quotient.toFormat(quotient.decimalPlaces(), format);
|
|
310
|
-
}
|
|
311
|
-
toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
312
|
-
invariant4(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
|
|
313
|
-
invariant4(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
|
|
314
|
-
Big.DP = decimalPlaces;
|
|
315
|
-
Big.RM = toFixedRounding[rounding];
|
|
316
|
-
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
|
|
317
|
-
}
|
|
318
|
-
get asFraction() {
|
|
319
|
-
return new Fraction(this.numerator, this.denominator);
|
|
320
|
-
}
|
|
321
|
-
};
|
|
322
|
-
|
|
323
|
-
// src/entities/fractions/currencyAmount.ts
|
|
324
|
-
import invariant5 from "tiny-invariant";
|
|
325
|
-
import JSBI4 from "jsbi";
|
|
326
|
-
import _Big2 from "big.js";
|
|
327
|
-
import toFormat2 from "toformat";
|
|
328
|
-
var Big2 = toFormat2(_Big2);
|
|
329
|
-
var CurrencyAmount2 = class extends Fraction {
|
|
330
|
-
constructor(currency, numerator, denominator) {
|
|
331
|
-
super(numerator, denominator);
|
|
332
|
-
invariant5(JSBI4.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT");
|
|
333
|
-
this.currency = currency;
|
|
334
|
-
this.decimalScale = JSBI4.exponentiate(JSBI4.BigInt(10), JSBI4.BigInt(currency.decimals));
|
|
335
|
-
}
|
|
336
|
-
static fromRawAmount(currency, rawAmount) {
|
|
337
|
-
return new CurrencyAmount2(currency, rawAmount);
|
|
338
|
-
}
|
|
339
|
-
static fromFractionalAmount(currency, numerator, denominator) {
|
|
340
|
-
return new CurrencyAmount2(currency, numerator, denominator);
|
|
341
|
-
}
|
|
342
|
-
add(other) {
|
|
343
|
-
invariant5(this.currency.equals(other.currency), "CURRENCY");
|
|
344
|
-
const added = super.add(other);
|
|
345
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, added.numerator, added.denominator);
|
|
346
|
-
}
|
|
347
|
-
subtract(other) {
|
|
348
|
-
invariant5(this.currency.equals(other.currency), "CURRENCY");
|
|
349
|
-
const subtracted = super.subtract(other);
|
|
350
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
|
|
351
|
-
}
|
|
352
|
-
multiply(other) {
|
|
353
|
-
const multiplied = super.multiply(other);
|
|
354
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
|
|
355
|
-
}
|
|
356
|
-
divide(other) {
|
|
357
|
-
const divided = super.divide(other);
|
|
358
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
|
|
359
|
-
}
|
|
360
|
-
toSignificant(significantDigits = 6, format, rounding = 0 /* ROUND_DOWN */) {
|
|
361
|
-
return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
|
|
362
|
-
}
|
|
363
|
-
toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
|
|
364
|
-
invariant5(decimalPlaces <= this.currency.decimals, "DECIMALS");
|
|
365
|
-
return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
|
|
366
|
-
}
|
|
367
|
-
toExact(format = { groupSeparator: "" }) {
|
|
368
|
-
Big2.DP = this.currency.decimals;
|
|
369
|
-
return new Big2(this.quotient.toString()).div(this.decimalScale.toString()).toFormat(format);
|
|
370
|
-
}
|
|
371
|
-
get wrapped() {
|
|
372
|
-
if (this.currency.isToken)
|
|
373
|
-
return this;
|
|
374
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator);
|
|
375
|
-
}
|
|
376
|
-
};
|
|
377
|
-
|
|
378
|
-
// src/entities/fractions/price.ts
|
|
379
|
-
var Price2 = class extends Fraction {
|
|
380
|
-
constructor(...args) {
|
|
381
|
-
let baseCurrency, quoteCurrency, denominator, numerator;
|
|
382
|
-
if (args.length === 4) {
|
|
383
|
-
;
|
|
384
|
-
[baseCurrency, quoteCurrency, denominator, numerator] = args;
|
|
385
|
-
} else {
|
|
386
|
-
const result = args[0].quoteAmount.divide(args[0].baseAmount);
|
|
387
|
-
[baseCurrency, quoteCurrency, denominator, numerator] = [
|
|
388
|
-
args[0].baseAmount.currency,
|
|
389
|
-
args[0].quoteAmount.currency,
|
|
390
|
-
result.denominator,
|
|
391
|
-
result.numerator
|
|
392
|
-
];
|
|
393
|
-
}
|
|
394
|
-
super(numerator, denominator);
|
|
395
|
-
this.baseCurrency = baseCurrency;
|
|
396
|
-
this.quoteCurrency = quoteCurrency;
|
|
397
|
-
this.scalar = new Fraction(JSBI5.exponentiate(JSBI5.BigInt(10), JSBI5.BigInt(baseCurrency.decimals)), JSBI5.exponentiate(JSBI5.BigInt(10), JSBI5.BigInt(quoteCurrency.decimals)));
|
|
398
|
-
}
|
|
399
|
-
invert() {
|
|
400
|
-
return new Price2(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
|
|
401
|
-
}
|
|
402
|
-
multiply(other) {
|
|
403
|
-
invariant6(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
|
|
404
|
-
const fraction = super.multiply(other);
|
|
405
|
-
return new Price2(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
|
|
406
|
-
}
|
|
407
|
-
quote(currencyAmount) {
|
|
408
|
-
invariant6(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
|
|
409
|
-
const result = super.multiply(currencyAmount);
|
|
410
|
-
return CurrencyAmount2.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
|
|
411
|
-
}
|
|
412
|
-
get adjustedForDecimals() {
|
|
413
|
-
return super.multiply(this.scalar);
|
|
414
|
-
}
|
|
415
|
-
toSignificant(significantDigits = 6, format, rounding) {
|
|
416
|
-
return this.adjustedForDecimals.toSignificant(significantDigits, format, rounding);
|
|
417
|
-
}
|
|
418
|
-
toFixed(decimalPlaces = 4, format, rounding) {
|
|
419
|
-
return this.adjustedForDecimals.toFixed(decimalPlaces, format, rounding);
|
|
420
|
-
}
|
|
421
|
-
};
|
|
422
|
-
|
|
423
|
-
// src/entities/fractions/percent.ts
|
|
424
|
-
import JSBI6 from "jsbi";
|
|
425
|
-
var ONE_HUNDRED = new Fraction(JSBI6.BigInt(100));
|
|
426
|
-
function toPercent(fraction) {
|
|
427
|
-
return new Percent(fraction.numerator, fraction.denominator);
|
|
428
|
-
}
|
|
429
|
-
var Percent = class extends Fraction {
|
|
430
|
-
constructor() {
|
|
431
|
-
super(...arguments);
|
|
432
|
-
this.isPercent = true;
|
|
433
|
-
}
|
|
434
|
-
add(other) {
|
|
435
|
-
return toPercent(super.add(other));
|
|
436
|
-
}
|
|
437
|
-
subtract(other) {
|
|
438
|
-
return toPercent(super.subtract(other));
|
|
439
|
-
}
|
|
440
|
-
multiply(other) {
|
|
441
|
-
return toPercent(super.multiply(other));
|
|
442
|
-
}
|
|
443
|
-
divide(other) {
|
|
444
|
-
return toPercent(super.divide(other));
|
|
445
|
-
}
|
|
446
|
-
toSignificant(significantDigits = 5, format, rounding) {
|
|
447
|
-
return super.multiply(ONE_HUNDRED).toSignificant(significantDigits, format, rounding);
|
|
448
|
-
}
|
|
449
|
-
toFixed(decimalPlaces = 2, format, rounding) {
|
|
450
|
-
return super.multiply(ONE_HUNDRED).toFixed(decimalPlaces, format, rounding);
|
|
451
|
-
}
|
|
452
|
-
};
|
|
453
|
-
|
|
454
|
-
// src/entities/pair.ts
|
|
135
|
+
import JSBI2 from "jsbi";
|
|
136
|
+
import invariant2 from "tiny-invariant";
|
|
137
|
+
var PAIR_ADDRESS_CACHE = {};
|
|
138
|
+
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
455
139
|
var computePairAddress = ({
|
|
456
140
|
factoryAddress,
|
|
457
141
|
tokenA,
|
|
458
142
|
tokenB
|
|
459
143
|
}) => {
|
|
460
144
|
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
|
|
461
|
-
|
|
145
|
+
const key = composeKey(token0, token1);
|
|
146
|
+
if ((PAIR_ADDRESS_CACHE == null ? void 0 : PAIR_ADDRESS_CACHE[key]) === void 0) {
|
|
147
|
+
PAIR_ADDRESS_CACHE = {
|
|
148
|
+
...PAIR_ADDRESS_CACHE,
|
|
149
|
+
[key]: getCreate2Address(factoryAddress, keccak256(["bytes"], [pack(["address", "address"], [token0.address, token1.address])]), INIT_CODE_HASH_MAP[token0.chainId])
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
return PAIR_ADDRESS_CACHE[key];
|
|
462
153
|
};
|
|
463
154
|
var Pair = class {
|
|
464
155
|
static getAddress(tokenA, tokenB) {
|
|
@@ -466,7 +157,7 @@ var Pair = class {
|
|
|
466
157
|
}
|
|
467
158
|
constructor(currencyAmountA, tokenAmountB) {
|
|
468
159
|
const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) ? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
|
|
469
|
-
this.liquidityToken = new
|
|
160
|
+
this.liquidityToken = new ERC20Token(tokenAmounts[0].currency.chainId, Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency), 18, "Cake-LP", "Pancake LPs");
|
|
470
161
|
this.tokenAmounts = tokenAmounts;
|
|
471
162
|
}
|
|
472
163
|
involvesToken(token) {
|
|
@@ -481,7 +172,7 @@ var Pair = class {
|
|
|
481
172
|
return new Price2(this.token1, this.token0, result.denominator, result.numerator);
|
|
482
173
|
}
|
|
483
174
|
priceOf(token) {
|
|
484
|
-
|
|
175
|
+
invariant2(this.involvesToken(token), "TOKEN");
|
|
485
176
|
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
486
177
|
}
|
|
487
178
|
get chainId() {
|
|
@@ -500,72 +191,72 @@ var Pair = class {
|
|
|
500
191
|
return this.tokenAmounts[1];
|
|
501
192
|
}
|
|
502
193
|
reserveOf(token) {
|
|
503
|
-
|
|
194
|
+
invariant2(this.involvesToken(token), "TOKEN");
|
|
504
195
|
return token.equals(this.token0) ? this.reserve0 : this.reserve1;
|
|
505
196
|
}
|
|
506
197
|
getOutputAmount(inputAmount) {
|
|
507
|
-
|
|
508
|
-
if (
|
|
198
|
+
invariant2(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
199
|
+
if (JSBI2.equal(this.reserve0.quotient, ZERO) || JSBI2.equal(this.reserve1.quotient, ZERO)) {
|
|
509
200
|
throw new InsufficientReservesError();
|
|
510
201
|
}
|
|
511
202
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
512
203
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
513
|
-
const inputAmountWithFee =
|
|
514
|
-
const numerator =
|
|
515
|
-
const denominator =
|
|
516
|
-
const outputAmount = CurrencyAmount2.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
517
|
-
if (
|
|
204
|
+
const inputAmountWithFee = JSBI2.multiply(inputAmount.quotient, _9975);
|
|
205
|
+
const numerator = JSBI2.multiply(inputAmountWithFee, outputReserve.quotient);
|
|
206
|
+
const denominator = JSBI2.add(JSBI2.multiply(inputReserve.quotient, _10000), inputAmountWithFee);
|
|
207
|
+
const outputAmount = CurrencyAmount2.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI2.divide(numerator, denominator));
|
|
208
|
+
if (JSBI2.equal(outputAmount.quotient, ZERO)) {
|
|
518
209
|
throw new InsufficientInputAmountError();
|
|
519
210
|
}
|
|
520
211
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
521
212
|
}
|
|
522
213
|
getInputAmount(outputAmount) {
|
|
523
|
-
|
|
524
|
-
if (
|
|
214
|
+
invariant2(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
215
|
+
if (JSBI2.equal(this.reserve0.quotient, ZERO) || JSBI2.equal(this.reserve1.quotient, ZERO) || JSBI2.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient)) {
|
|
525
216
|
throw new InsufficientReservesError();
|
|
526
217
|
}
|
|
527
218
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
528
219
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
529
|
-
const numerator =
|
|
530
|
-
const denominator =
|
|
531
|
-
const inputAmount = CurrencyAmount2.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
220
|
+
const numerator = JSBI2.multiply(JSBI2.multiply(inputReserve.quotient, outputAmount.quotient), _10000);
|
|
221
|
+
const denominator = JSBI2.multiply(JSBI2.subtract(outputReserve.quotient, outputAmount.quotient), _9975);
|
|
222
|
+
const inputAmount = CurrencyAmount2.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI2.add(JSBI2.divide(numerator, denominator), ONE));
|
|
532
223
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
533
224
|
}
|
|
534
225
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
535
|
-
|
|
226
|
+
invariant2(totalSupply.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
536
227
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
537
|
-
|
|
228
|
+
invariant2(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
538
229
|
let liquidity;
|
|
539
|
-
if (
|
|
540
|
-
liquidity =
|
|
230
|
+
if (JSBI2.equal(totalSupply.quotient, ZERO)) {
|
|
231
|
+
liquidity = JSBI2.subtract(sqrt(JSBI2.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), MINIMUM_LIQUIDITY);
|
|
541
232
|
} else {
|
|
542
|
-
const amount0 =
|
|
543
|
-
const amount1 =
|
|
544
|
-
liquidity =
|
|
233
|
+
const amount0 = JSBI2.divide(JSBI2.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
234
|
+
const amount1 = JSBI2.divide(JSBI2.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
235
|
+
liquidity = JSBI2.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
|
|
545
236
|
}
|
|
546
|
-
if (!
|
|
237
|
+
if (!JSBI2.greaterThan(liquidity, ZERO)) {
|
|
547
238
|
throw new InsufficientInputAmountError();
|
|
548
239
|
}
|
|
549
240
|
return CurrencyAmount2.fromRawAmount(this.liquidityToken, liquidity);
|
|
550
241
|
}
|
|
551
242
|
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
243
|
+
invariant2(this.involvesToken(token), "TOKEN");
|
|
244
|
+
invariant2(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
245
|
+
invariant2(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
246
|
+
invariant2(JSBI2.lessThanOrEqual(liquidity.quotient, totalSupply.quotient), "LIQUIDITY");
|
|
556
247
|
let totalSupplyAdjusted;
|
|
557
248
|
if (!feeOn) {
|
|
558
249
|
totalSupplyAdjusted = totalSupply;
|
|
559
250
|
} else {
|
|
560
|
-
|
|
561
|
-
const kLastParsed =
|
|
562
|
-
if (!
|
|
563
|
-
const rootK = sqrt(
|
|
251
|
+
invariant2(!!kLast, "K_LAST");
|
|
252
|
+
const kLastParsed = JSBI2.BigInt(kLast);
|
|
253
|
+
if (!JSBI2.equal(kLastParsed, ZERO)) {
|
|
254
|
+
const rootK = sqrt(JSBI2.multiply(this.reserve0.quotient, this.reserve1.quotient));
|
|
564
255
|
const rootKLast = sqrt(kLastParsed);
|
|
565
|
-
if (
|
|
566
|
-
const numerator =
|
|
567
|
-
const denominator =
|
|
568
|
-
const feeLiquidity =
|
|
256
|
+
if (JSBI2.greaterThan(rootK, rootKLast)) {
|
|
257
|
+
const numerator = JSBI2.multiply(totalSupply.quotient, JSBI2.subtract(rootK, rootKLast));
|
|
258
|
+
const denominator = JSBI2.add(JSBI2.multiply(rootK, FIVE), rootKLast);
|
|
259
|
+
const feeLiquidity = JSBI2.divide(numerator, denominator);
|
|
569
260
|
totalSupplyAdjusted = totalSupply.add(CurrencyAmount2.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
570
261
|
} else {
|
|
571
262
|
totalSupplyAdjusted = totalSupply;
|
|
@@ -574,25 +265,26 @@ var Pair = class {
|
|
|
574
265
|
totalSupplyAdjusted = totalSupply;
|
|
575
266
|
}
|
|
576
267
|
}
|
|
577
|
-
return CurrencyAmount2.fromRawAmount(token,
|
|
268
|
+
return CurrencyAmount2.fromRawAmount(token, JSBI2.divide(JSBI2.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
|
|
578
269
|
}
|
|
579
270
|
};
|
|
580
271
|
|
|
581
272
|
// src/entities/route.ts
|
|
582
|
-
import
|
|
273
|
+
import invariant3 from "tiny-invariant";
|
|
274
|
+
import { Price as Price3 } from "@pancakeswap/swap-sdk-core";
|
|
583
275
|
var Route = class {
|
|
584
276
|
constructor(pairs, input, output) {
|
|
585
277
|
this._midPrice = null;
|
|
586
|
-
|
|
278
|
+
invariant3(pairs.length > 0, "PAIRS");
|
|
587
279
|
const chainId = pairs[0].chainId;
|
|
588
|
-
|
|
280
|
+
invariant3(pairs.every((pair) => pair.chainId === chainId), "CHAIN_IDS");
|
|
589
281
|
const wrappedInput = input.wrapped;
|
|
590
|
-
|
|
591
|
-
|
|
282
|
+
invariant3(pairs[0].involvesToken(wrappedInput), "INPUT");
|
|
283
|
+
invariant3(typeof output === "undefined" || pairs[pairs.length - 1].involvesToken(output.wrapped), "OUTPUT");
|
|
592
284
|
const path = [wrappedInput];
|
|
593
285
|
for (const [i, pair] of pairs.entries()) {
|
|
594
286
|
const currentInput = path[i];
|
|
595
|
-
|
|
287
|
+
invariant3(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), "PATH");
|
|
596
288
|
const output2 = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
|
|
597
289
|
path.push(output2);
|
|
598
290
|
}
|
|
@@ -606,10 +298,10 @@ var Route = class {
|
|
|
606
298
|
return this._midPrice;
|
|
607
299
|
const prices = [];
|
|
608
300
|
for (const [i, pair] of this.pairs.entries()) {
|
|
609
|
-
prices.push(this.path[i].equals(pair.token0) ? new
|
|
301
|
+
prices.push(this.path[i].equals(pair.token0) ? new Price3(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient) : new Price3(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient));
|
|
610
302
|
}
|
|
611
303
|
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
612
|
-
return this._midPrice = new
|
|
304
|
+
return this._midPrice = new Price3(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
613
305
|
}
|
|
614
306
|
get chainId() {
|
|
615
307
|
return this.pairs[0].chainId;
|
|
@@ -617,10 +309,18 @@ var Route = class {
|
|
|
617
309
|
};
|
|
618
310
|
|
|
619
311
|
// src/entities/trade.ts
|
|
620
|
-
import
|
|
312
|
+
import invariant4 from "tiny-invariant";
|
|
313
|
+
import {
|
|
314
|
+
ONE as ONE2,
|
|
315
|
+
TradeType,
|
|
316
|
+
ZERO as ZERO2,
|
|
317
|
+
CurrencyAmount as CurrencyAmount3,
|
|
318
|
+
Fraction,
|
|
319
|
+
Price as Price4
|
|
320
|
+
} from "@pancakeswap/swap-sdk-core";
|
|
621
321
|
function inputOutputComparator(a, b) {
|
|
622
|
-
|
|
623
|
-
|
|
322
|
+
invariant4(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
|
|
323
|
+
invariant4(a.outputAmount.currency.equals(b.outputAmount.currency), "OUTPUT_CURRENCY");
|
|
624
324
|
if (a.outputAmount.equalTo(b.outputAmount)) {
|
|
625
325
|
if (a.inputAmount.equalTo(b.inputAmount)) {
|
|
626
326
|
return 0;
|
|
@@ -652,68 +352,68 @@ function tradeComparator(a, b) {
|
|
|
652
352
|
}
|
|
653
353
|
var Trade = class {
|
|
654
354
|
static exactIn(route, amountIn) {
|
|
655
|
-
return new Trade(route, amountIn,
|
|
355
|
+
return new Trade(route, amountIn, TradeType.EXACT_INPUT);
|
|
656
356
|
}
|
|
657
357
|
static exactOut(route, amountOut) {
|
|
658
|
-
return new Trade(route, amountOut,
|
|
358
|
+
return new Trade(route, amountOut, TradeType.EXACT_OUTPUT);
|
|
659
359
|
}
|
|
660
360
|
constructor(route, amount, tradeType) {
|
|
661
361
|
this.route = route;
|
|
662
362
|
this.tradeType = tradeType;
|
|
663
363
|
const tokenAmounts = new Array(route.path.length);
|
|
664
|
-
if (tradeType ===
|
|
665
|
-
|
|
364
|
+
if (tradeType === TradeType.EXACT_INPUT) {
|
|
365
|
+
invariant4(amount.currency.equals(route.input), "INPUT");
|
|
666
366
|
tokenAmounts[0] = amount.wrapped;
|
|
667
367
|
for (let i = 0; i < route.path.length - 1; i++) {
|
|
668
368
|
const pair = route.pairs[i];
|
|
669
369
|
const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
|
|
670
370
|
tokenAmounts[i + 1] = outputAmount;
|
|
671
371
|
}
|
|
672
|
-
this.inputAmount =
|
|
673
|
-
this.outputAmount =
|
|
372
|
+
this.inputAmount = CurrencyAmount3.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
373
|
+
this.outputAmount = CurrencyAmount3.fromFractionalAmount(route.output, tokenAmounts[tokenAmounts.length - 1].numerator, tokenAmounts[tokenAmounts.length - 1].denominator);
|
|
674
374
|
} else {
|
|
675
|
-
|
|
375
|
+
invariant4(amount.currency.equals(route.output), "OUTPUT");
|
|
676
376
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
677
377
|
for (let i = route.path.length - 1; i > 0; i--) {
|
|
678
378
|
const pair = route.pairs[i - 1];
|
|
679
379
|
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
680
380
|
tokenAmounts[i - 1] = inputAmount;
|
|
681
381
|
}
|
|
682
|
-
this.inputAmount =
|
|
683
|
-
this.outputAmount =
|
|
382
|
+
this.inputAmount = CurrencyAmount3.fromFractionalAmount(route.input, tokenAmounts[0].numerator, tokenAmounts[0].denominator);
|
|
383
|
+
this.outputAmount = CurrencyAmount3.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
684
384
|
}
|
|
685
|
-
this.executionPrice = new
|
|
385
|
+
this.executionPrice = new Price4(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
686
386
|
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
687
387
|
}
|
|
688
388
|
minimumAmountOut(slippageTolerance) {
|
|
689
|
-
|
|
690
|
-
if (this.tradeType ===
|
|
389
|
+
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
390
|
+
if (this.tradeType === TradeType.EXACT_OUTPUT) {
|
|
691
391
|
return this.outputAmount;
|
|
692
392
|
} else {
|
|
693
|
-
const slippageAdjustedAmountOut = new Fraction(
|
|
694
|
-
return
|
|
393
|
+
const slippageAdjustedAmountOut = new Fraction(ONE2).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
394
|
+
return CurrencyAmount3.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
695
395
|
}
|
|
696
396
|
}
|
|
697
397
|
maximumAmountIn(slippageTolerance) {
|
|
698
|
-
|
|
699
|
-
if (this.tradeType ===
|
|
398
|
+
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
399
|
+
if (this.tradeType === TradeType.EXACT_INPUT) {
|
|
700
400
|
return this.inputAmount;
|
|
701
401
|
} else {
|
|
702
|
-
const slippageAdjustedAmountIn = new Fraction(
|
|
703
|
-
return
|
|
402
|
+
const slippageAdjustedAmountIn = new Fraction(ONE2).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
403
|
+
return CurrencyAmount3.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
704
404
|
}
|
|
705
405
|
}
|
|
706
406
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
407
|
+
invariant4(pairs.length > 0, "PAIRS");
|
|
408
|
+
invariant4(maxHops > 0, "MAX_HOPS");
|
|
409
|
+
invariant4(currencyAmountIn === nextAmountIn || currentPairs.length > 0, "INVALID_RECURSION");
|
|
710
410
|
const amountIn = nextAmountIn.wrapped;
|
|
711
411
|
const tokenOut = currencyOut.wrapped;
|
|
712
412
|
for (let i = 0; i < pairs.length; i++) {
|
|
713
413
|
const pair = pairs[i];
|
|
714
414
|
if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
|
|
715
415
|
continue;
|
|
716
|
-
if (pair.reserve0.equalTo(
|
|
416
|
+
if (pair.reserve0.equalTo(ZERO2) || pair.reserve1.equalTo(ZERO2))
|
|
717
417
|
continue;
|
|
718
418
|
let amountOut;
|
|
719
419
|
try {
|
|
@@ -726,7 +426,7 @@ var Trade = class {
|
|
|
726
426
|
throw error;
|
|
727
427
|
}
|
|
728
428
|
if (amountOut.currency.equals(tokenOut)) {
|
|
729
|
-
sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn,
|
|
429
|
+
sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn, TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
|
|
730
430
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
731
431
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
732
432
|
Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
|
|
@@ -738,19 +438,19 @@ var Trade = class {
|
|
|
738
438
|
return bestTrades;
|
|
739
439
|
}
|
|
740
440
|
worstExecutionPrice(slippageTolerance) {
|
|
741
|
-
return new
|
|
441
|
+
return new Price4(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
742
442
|
}
|
|
743
443
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
444
|
+
invariant4(pairs.length > 0, "PAIRS");
|
|
445
|
+
invariant4(maxHops > 0, "MAX_HOPS");
|
|
446
|
+
invariant4(currencyAmountOut === nextAmountOut || currentPairs.length > 0, "INVALID_RECURSION");
|
|
747
447
|
const amountOut = nextAmountOut.wrapped;
|
|
748
448
|
const tokenIn = currencyIn.wrapped;
|
|
749
449
|
for (let i = 0; i < pairs.length; i++) {
|
|
750
450
|
const pair = pairs[i];
|
|
751
451
|
if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
|
|
752
452
|
continue;
|
|
753
|
-
if (pair.reserve0.equalTo(
|
|
453
|
+
if (pair.reserve0.equalTo(ZERO2) || pair.reserve1.equalTo(ZERO2))
|
|
754
454
|
continue;
|
|
755
455
|
let amountIn;
|
|
756
456
|
try {
|
|
@@ -763,7 +463,7 @@ var Trade = class {
|
|
|
763
463
|
throw error;
|
|
764
464
|
}
|
|
765
465
|
if (amountIn.currency.equals(tokenIn)) {
|
|
766
|
-
sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut,
|
|
466
|
+
sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut, TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
|
|
767
467
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
768
468
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
769
469
|
Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
|
|
@@ -776,17 +476,9 @@ var Trade = class {
|
|
|
776
476
|
}
|
|
777
477
|
};
|
|
778
478
|
|
|
779
|
-
// src/entities/nativeCurrency.ts
|
|
780
|
-
var NativeCurrency = class extends BaseCurrency {
|
|
781
|
-
constructor() {
|
|
782
|
-
super(...arguments);
|
|
783
|
-
this.isNative = true;
|
|
784
|
-
this.isToken = false;
|
|
785
|
-
}
|
|
786
|
-
};
|
|
787
|
-
|
|
788
479
|
// src/entities/native.ts
|
|
789
|
-
import
|
|
480
|
+
import invariant5 from "tiny-invariant";
|
|
481
|
+
import { NativeCurrency } from "@pancakeswap/swap-sdk-core";
|
|
790
482
|
var _Native = class extends NativeCurrency {
|
|
791
483
|
constructor({
|
|
792
484
|
chainId,
|
|
@@ -798,14 +490,14 @@ var _Native = class extends NativeCurrency {
|
|
|
798
490
|
}
|
|
799
491
|
get wrapped() {
|
|
800
492
|
const wnative = WNATIVE[this.chainId];
|
|
801
|
-
|
|
493
|
+
invariant5(!!wnative, "WRAPPED");
|
|
802
494
|
return wnative;
|
|
803
495
|
}
|
|
804
496
|
static onChain(chainId) {
|
|
805
497
|
if (chainId in this.cache) {
|
|
806
498
|
return this.cache[chainId];
|
|
807
499
|
}
|
|
808
|
-
|
|
500
|
+
invariant5(!!NATIVE[chainId], "NATIVE_CURRENCY");
|
|
809
501
|
const { decimals, name, symbol } = NATIVE[chainId];
|
|
810
502
|
return this.cache[chainId] = new _Native({ chainId, decimals, symbol, name });
|
|
811
503
|
}
|
|
@@ -817,7 +509,8 @@ var Native = _Native;
|
|
|
817
509
|
Native.cache = {};
|
|
818
510
|
|
|
819
511
|
// src/router.ts
|
|
820
|
-
import
|
|
512
|
+
import { TradeType as TradeType2 } from "@pancakeswap/swap-sdk-core";
|
|
513
|
+
import invariant6 from "tiny-invariant";
|
|
821
514
|
function toHex(currencyAmount) {
|
|
822
515
|
return `0x${currencyAmount.quotient.toString(16)}`;
|
|
823
516
|
}
|
|
@@ -828,8 +521,8 @@ var Router = class {
|
|
|
828
521
|
static swapCallParameters(trade, options) {
|
|
829
522
|
const etherIn = trade.inputAmount.currency.isNative;
|
|
830
523
|
const etherOut = trade.outputAmount.currency.isNative;
|
|
831
|
-
|
|
832
|
-
|
|
524
|
+
invariant6(!(etherIn && etherOut), "ETHER_IN_OUT");
|
|
525
|
+
invariant6(!("ttl" in options) || options.ttl > 0, "TTL");
|
|
833
526
|
const to = validateAndParseAddress(options.recipient);
|
|
834
527
|
const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
|
|
835
528
|
const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
|
|
@@ -840,7 +533,7 @@ var Router = class {
|
|
|
840
533
|
let args;
|
|
841
534
|
let value;
|
|
842
535
|
switch (trade.tradeType) {
|
|
843
|
-
case
|
|
536
|
+
case TradeType2.EXACT_INPUT:
|
|
844
537
|
if (etherIn) {
|
|
845
538
|
methodName = useFeeOnTransfer ? "swapExactETHForTokensSupportingFeeOnTransferTokens" : "swapExactETHForTokens";
|
|
846
539
|
args = [amountOut, path, to, deadline];
|
|
@@ -855,8 +548,8 @@ var Router = class {
|
|
|
855
548
|
value = ZERO_HEX;
|
|
856
549
|
}
|
|
857
550
|
break;
|
|
858
|
-
case
|
|
859
|
-
|
|
551
|
+
case TradeType2.EXACT_OUTPUT:
|
|
552
|
+
invariant6(!useFeeOnTransfer, "EXACT_OUT_FOT");
|
|
860
553
|
if (etherIn) {
|
|
861
554
|
methodName = "swapETHForExactTokens";
|
|
862
555
|
args = [amountOut, path, to, deadline];
|
|
@@ -879,46 +572,26 @@ var Router = class {
|
|
|
879
572
|
};
|
|
880
573
|
}
|
|
881
574
|
};
|
|
575
|
+
|
|
576
|
+
// src/index.ts
|
|
577
|
+
export * from "@pancakeswap/swap-sdk-core";
|
|
882
578
|
export {
|
|
883
|
-
BaseCurrency,
|
|
884
579
|
ChainId,
|
|
885
|
-
|
|
580
|
+
ERC20Token,
|
|
886
581
|
FACTORY_ADDRESS,
|
|
887
582
|
FACTORY_ADDRESS_MAP,
|
|
888
|
-
FIVE,
|
|
889
|
-
Fraction,
|
|
890
583
|
INIT_CODE_HASH,
|
|
891
584
|
INIT_CODE_HASH_MAP,
|
|
892
|
-
|
|
893
|
-
InsufficientReservesError,
|
|
894
|
-
JSBI8 as JSBI,
|
|
895
|
-
MINIMUM_LIQUIDITY,
|
|
896
|
-
MaxUint256,
|
|
585
|
+
JSBI3 as JSBI,
|
|
897
586
|
NATIVE,
|
|
898
587
|
Native,
|
|
899
|
-
NativeCurrency,
|
|
900
|
-
ONE,
|
|
901
588
|
Pair,
|
|
902
|
-
Percent,
|
|
903
|
-
Price2 as Price,
|
|
904
|
-
Rounding,
|
|
905
589
|
Route,
|
|
906
590
|
Router,
|
|
907
|
-
SOLIDITY_TYPE_MAXIMA,
|
|
908
|
-
SolidityType,
|
|
909
|
-
TEN,
|
|
910
|
-
THREE,
|
|
911
|
-
TWO,
|
|
912
|
-
Token,
|
|
913
591
|
Trade,
|
|
914
|
-
TradeType,
|
|
915
592
|
WBNB,
|
|
916
593
|
WETH9,
|
|
917
594
|
WNATIVE,
|
|
918
|
-
ZERO,
|
|
919
|
-
_100,
|
|
920
|
-
_10000,
|
|
921
|
-
_9975,
|
|
922
595
|
computePairAddress,
|
|
923
596
|
computePriceImpact,
|
|
924
597
|
inputOutputComparator,
|