@pancakeswap/sdk 3.1.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 +29 -265
- package/dist/index.js +123 -510
- package/dist/index.mjs +129 -475
- package/package.json +5 -4
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,48 +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;
|
|
106
|
-
}
|
|
107
|
-
get serialize() {
|
|
108
|
-
return {
|
|
109
|
-
address: this.address,
|
|
110
|
-
chainId: this.chainId,
|
|
111
|
-
decimals: this.decimals,
|
|
112
|
-
symbol: this.symbol,
|
|
113
|
-
name: this.name,
|
|
114
|
-
projectLink: this.projectLink
|
|
115
|
-
};
|
|
55
|
+
super(chainId, validateAndParseAddress(address), decimals, symbol, name, projectLink);
|
|
116
56
|
}
|
|
117
57
|
};
|
|
118
58
|
|
|
@@ -125,17 +65,6 @@ var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
|
125
65
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
126
66
|
return ChainId2;
|
|
127
67
|
})(ChainId || {});
|
|
128
|
-
var TradeType = /* @__PURE__ */ ((TradeType2) => {
|
|
129
|
-
TradeType2[TradeType2["EXACT_INPUT"] = 0] = "EXACT_INPUT";
|
|
130
|
-
TradeType2[TradeType2["EXACT_OUTPUT"] = 1] = "EXACT_OUTPUT";
|
|
131
|
-
return TradeType2;
|
|
132
|
-
})(TradeType || {});
|
|
133
|
-
var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
134
|
-
Rounding2[Rounding2["ROUND_DOWN"] = 0] = "ROUND_DOWN";
|
|
135
|
-
Rounding2[Rounding2["ROUND_HALF_UP"] = 1] = "ROUND_HALF_UP";
|
|
136
|
-
Rounding2[Rounding2["ROUND_UP"] = 2] = "ROUND_UP";
|
|
137
|
-
return Rounding2;
|
|
138
|
-
})(Rounding || {});
|
|
139
68
|
var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
140
69
|
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
141
70
|
var FACTORY_ADDRESS_MAP = {
|
|
@@ -154,35 +83,15 @@ var INIT_CODE_HASH_MAP = {
|
|
|
154
83
|
[56 /* BSC */]: INIT_CODE_HASH,
|
|
155
84
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
156
85
|
};
|
|
157
|
-
var MINIMUM_LIQUIDITY = JSBI2.BigInt(1e3);
|
|
158
|
-
var ZERO = JSBI2.BigInt(0);
|
|
159
|
-
var ONE = JSBI2.BigInt(1);
|
|
160
|
-
var TWO = JSBI2.BigInt(2);
|
|
161
|
-
var THREE = JSBI2.BigInt(3);
|
|
162
|
-
var FIVE = JSBI2.BigInt(5);
|
|
163
|
-
var TEN = JSBI2.BigInt(10);
|
|
164
|
-
var _100 = JSBI2.BigInt(100);
|
|
165
|
-
var _9975 = JSBI2.BigInt(9975);
|
|
166
|
-
var _10000 = JSBI2.BigInt(1e4);
|
|
167
|
-
var MaxUint256 = JSBI2.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
168
|
-
var SolidityType = /* @__PURE__ */ ((SolidityType2) => {
|
|
169
|
-
SolidityType2["uint8"] = "uint8";
|
|
170
|
-
SolidityType2["uint256"] = "uint256";
|
|
171
|
-
return SolidityType2;
|
|
172
|
-
})(SolidityType || {});
|
|
173
|
-
var SOLIDITY_TYPE_MAXIMA = {
|
|
174
|
-
["uint8" /* uint8 */]: JSBI2.BigInt("0xff"),
|
|
175
|
-
["uint256" /* uint256 */]: JSBI2.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
|
176
|
-
};
|
|
177
86
|
var WETH9 = {
|
|
178
|
-
[1 /* ETHEREUM */]: new
|
|
179
|
-
[4 /* RINKEBY */]: new
|
|
180
|
-
[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")
|
|
181
90
|
};
|
|
182
91
|
var WBNB = {
|
|
183
|
-
[1 /* ETHEREUM */]: new
|
|
184
|
-
[56 /* BSC */]: new
|
|
185
|
-
[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")
|
|
186
95
|
};
|
|
187
96
|
var WNATIVE = {
|
|
188
97
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
@@ -207,261 +116,24 @@ var NATIVE = {
|
|
|
207
116
|
}
|
|
208
117
|
};
|
|
209
118
|
|
|
210
|
-
// src/errors.ts
|
|
211
|
-
var CAN_SET_PROTOTYPE = "setPrototypeOf" in Object;
|
|
212
|
-
var InsufficientReservesError = class extends Error {
|
|
213
|
-
constructor() {
|
|
214
|
-
super();
|
|
215
|
-
this.isInsufficientReservesError = true;
|
|
216
|
-
this.name = this.constructor.name;
|
|
217
|
-
if (CAN_SET_PROTOTYPE)
|
|
218
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
219
|
-
}
|
|
220
|
-
};
|
|
221
|
-
var InsufficientInputAmountError = class extends Error {
|
|
222
|
-
constructor() {
|
|
223
|
-
super();
|
|
224
|
-
this.isInsufficientInputAmountError = true;
|
|
225
|
-
this.name = this.constructor.name;
|
|
226
|
-
if (CAN_SET_PROTOTYPE)
|
|
227
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
228
|
-
}
|
|
229
|
-
};
|
|
230
|
-
|
|
231
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";
|
|
232
133
|
import { getCreate2Address } from "@ethersproject/address";
|
|
233
134
|
import { keccak256, pack } from "@ethersproject/solidity";
|
|
234
|
-
import
|
|
235
|
-
import
|
|
236
|
-
|
|
237
|
-
// src/entities/fractions/price.ts
|
|
238
|
-
import JSBI5 from "jsbi";
|
|
239
|
-
import invariant6 from "tiny-invariant";
|
|
240
|
-
|
|
241
|
-
// src/entities/fractions/fraction.ts
|
|
242
|
-
import JSBI3 from "jsbi";
|
|
243
|
-
import invariant4 from "tiny-invariant";
|
|
244
|
-
import _Decimal from "decimal.js-light";
|
|
245
|
-
import _Big from "big.js";
|
|
246
|
-
import toFormat from "toformat";
|
|
247
|
-
var Decimal = toFormat(_Decimal);
|
|
248
|
-
var Big = toFormat(_Big);
|
|
249
|
-
var toSignificantRounding = {
|
|
250
|
-
[0 /* ROUND_DOWN */]: Decimal.ROUND_DOWN,
|
|
251
|
-
[1 /* ROUND_HALF_UP */]: Decimal.ROUND_HALF_UP,
|
|
252
|
-
[2 /* ROUND_UP */]: Decimal.ROUND_UP
|
|
253
|
-
};
|
|
254
|
-
var toFixedRounding = {
|
|
255
|
-
[0 /* ROUND_DOWN */]: 0 /* RoundDown */,
|
|
256
|
-
[1 /* ROUND_HALF_UP */]: 1 /* RoundHalfUp */,
|
|
257
|
-
[2 /* ROUND_UP */]: 3 /* RoundUp */
|
|
258
|
-
};
|
|
259
|
-
var Fraction = class {
|
|
260
|
-
constructor(numerator, denominator = JSBI3.BigInt(1)) {
|
|
261
|
-
this.numerator = JSBI3.BigInt(numerator);
|
|
262
|
-
this.denominator = JSBI3.BigInt(denominator);
|
|
263
|
-
}
|
|
264
|
-
static tryParseFraction(fractionish) {
|
|
265
|
-
if (fractionish instanceof JSBI3 || typeof fractionish === "number" || typeof fractionish === "string")
|
|
266
|
-
return new Fraction(fractionish);
|
|
267
|
-
if ("numerator" in fractionish && "denominator" in fractionish)
|
|
268
|
-
return fractionish;
|
|
269
|
-
throw new Error("Could not parse fraction");
|
|
270
|
-
}
|
|
271
|
-
get quotient() {
|
|
272
|
-
return JSBI3.divide(this.numerator, this.denominator);
|
|
273
|
-
}
|
|
274
|
-
get remainder() {
|
|
275
|
-
return new Fraction(JSBI3.remainder(this.numerator, this.denominator), this.denominator);
|
|
276
|
-
}
|
|
277
|
-
invert() {
|
|
278
|
-
return new Fraction(this.denominator, this.numerator);
|
|
279
|
-
}
|
|
280
|
-
add(other) {
|
|
281
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
282
|
-
if (JSBI3.equal(this.denominator, otherParsed.denominator)) {
|
|
283
|
-
return new Fraction(JSBI3.add(this.numerator, otherParsed.numerator), this.denominator);
|
|
284
|
-
}
|
|
285
|
-
return new Fraction(JSBI3.add(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator)), JSBI3.multiply(this.denominator, otherParsed.denominator));
|
|
286
|
-
}
|
|
287
|
-
subtract(other) {
|
|
288
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
289
|
-
if (JSBI3.equal(this.denominator, otherParsed.denominator)) {
|
|
290
|
-
return new Fraction(JSBI3.subtract(this.numerator, otherParsed.numerator), this.denominator);
|
|
291
|
-
}
|
|
292
|
-
return new Fraction(JSBI3.subtract(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator)), JSBI3.multiply(this.denominator, otherParsed.denominator));
|
|
293
|
-
}
|
|
294
|
-
lessThan(other) {
|
|
295
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
296
|
-
return JSBI3.lessThan(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator));
|
|
297
|
-
}
|
|
298
|
-
equalTo(other) {
|
|
299
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
300
|
-
return JSBI3.equal(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator));
|
|
301
|
-
}
|
|
302
|
-
greaterThan(other) {
|
|
303
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
304
|
-
return JSBI3.greaterThan(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(otherParsed.numerator, this.denominator));
|
|
305
|
-
}
|
|
306
|
-
multiply(other) {
|
|
307
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
308
|
-
return new Fraction(JSBI3.multiply(this.numerator, otherParsed.numerator), JSBI3.multiply(this.denominator, otherParsed.denominator));
|
|
309
|
-
}
|
|
310
|
-
divide(other) {
|
|
311
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
312
|
-
return new Fraction(JSBI3.multiply(this.numerator, otherParsed.denominator), JSBI3.multiply(this.denominator, otherParsed.numerator));
|
|
313
|
-
}
|
|
314
|
-
toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
315
|
-
invariant4(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
|
|
316
|
-
invariant4(significantDigits > 0, `${significantDigits} is not positive.`);
|
|
317
|
-
Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });
|
|
318
|
-
const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
|
|
319
|
-
return quotient.toFormat(quotient.decimalPlaces(), format);
|
|
320
|
-
}
|
|
321
|
-
toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
322
|
-
invariant4(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
|
|
323
|
-
invariant4(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
|
|
324
|
-
Big.DP = decimalPlaces;
|
|
325
|
-
Big.RM = toFixedRounding[rounding];
|
|
326
|
-
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
|
|
327
|
-
}
|
|
328
|
-
get asFraction() {
|
|
329
|
-
return new Fraction(this.numerator, this.denominator);
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
|
|
333
|
-
// src/entities/fractions/currencyAmount.ts
|
|
334
|
-
import invariant5 from "tiny-invariant";
|
|
335
|
-
import JSBI4 from "jsbi";
|
|
336
|
-
import _Big2 from "big.js";
|
|
337
|
-
import toFormat2 from "toformat";
|
|
338
|
-
var Big2 = toFormat2(_Big2);
|
|
339
|
-
var CurrencyAmount2 = class extends Fraction {
|
|
340
|
-
constructor(currency, numerator, denominator) {
|
|
341
|
-
super(numerator, denominator);
|
|
342
|
-
invariant5(JSBI4.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT");
|
|
343
|
-
this.currency = currency;
|
|
344
|
-
this.decimalScale = JSBI4.exponentiate(JSBI4.BigInt(10), JSBI4.BigInt(currency.decimals));
|
|
345
|
-
}
|
|
346
|
-
static fromRawAmount(currency, rawAmount) {
|
|
347
|
-
return new CurrencyAmount2(currency, rawAmount);
|
|
348
|
-
}
|
|
349
|
-
static fromFractionalAmount(currency, numerator, denominator) {
|
|
350
|
-
return new CurrencyAmount2(currency, numerator, denominator);
|
|
351
|
-
}
|
|
352
|
-
add(other) {
|
|
353
|
-
invariant5(this.currency.equals(other.currency), "CURRENCY");
|
|
354
|
-
const added = super.add(other);
|
|
355
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, added.numerator, added.denominator);
|
|
356
|
-
}
|
|
357
|
-
subtract(other) {
|
|
358
|
-
invariant5(this.currency.equals(other.currency), "CURRENCY");
|
|
359
|
-
const subtracted = super.subtract(other);
|
|
360
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
|
|
361
|
-
}
|
|
362
|
-
multiply(other) {
|
|
363
|
-
const multiplied = super.multiply(other);
|
|
364
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
|
|
365
|
-
}
|
|
366
|
-
divide(other) {
|
|
367
|
-
const divided = super.divide(other);
|
|
368
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
|
|
369
|
-
}
|
|
370
|
-
toSignificant(significantDigits = 6, format, rounding = 0 /* ROUND_DOWN */) {
|
|
371
|
-
return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
|
|
372
|
-
}
|
|
373
|
-
toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
|
|
374
|
-
invariant5(decimalPlaces <= this.currency.decimals, "DECIMALS");
|
|
375
|
-
return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
|
|
376
|
-
}
|
|
377
|
-
toExact(format = { groupSeparator: "" }) {
|
|
378
|
-
Big2.DP = this.currency.decimals;
|
|
379
|
-
return new Big2(this.quotient.toString()).div(this.decimalScale.toString()).toFormat(format);
|
|
380
|
-
}
|
|
381
|
-
get wrapped() {
|
|
382
|
-
if (this.currency.isToken)
|
|
383
|
-
return this;
|
|
384
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator);
|
|
385
|
-
}
|
|
386
|
-
};
|
|
387
|
-
|
|
388
|
-
// src/entities/fractions/price.ts
|
|
389
|
-
var Price2 = class extends Fraction {
|
|
390
|
-
constructor(...args) {
|
|
391
|
-
let baseCurrency, quoteCurrency, denominator, numerator;
|
|
392
|
-
if (args.length === 4) {
|
|
393
|
-
;
|
|
394
|
-
[baseCurrency, quoteCurrency, denominator, numerator] = args;
|
|
395
|
-
} else {
|
|
396
|
-
const result = args[0].quoteAmount.divide(args[0].baseAmount);
|
|
397
|
-
[baseCurrency, quoteCurrency, denominator, numerator] = [
|
|
398
|
-
args[0].baseAmount.currency,
|
|
399
|
-
args[0].quoteAmount.currency,
|
|
400
|
-
result.denominator,
|
|
401
|
-
result.numerator
|
|
402
|
-
];
|
|
403
|
-
}
|
|
404
|
-
super(numerator, denominator);
|
|
405
|
-
this.baseCurrency = baseCurrency;
|
|
406
|
-
this.quoteCurrency = quoteCurrency;
|
|
407
|
-
this.scalar = new Fraction(JSBI5.exponentiate(JSBI5.BigInt(10), JSBI5.BigInt(baseCurrency.decimals)), JSBI5.exponentiate(JSBI5.BigInt(10), JSBI5.BigInt(quoteCurrency.decimals)));
|
|
408
|
-
}
|
|
409
|
-
invert() {
|
|
410
|
-
return new Price2(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
|
|
411
|
-
}
|
|
412
|
-
multiply(other) {
|
|
413
|
-
invariant6(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
|
|
414
|
-
const fraction = super.multiply(other);
|
|
415
|
-
return new Price2(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
|
|
416
|
-
}
|
|
417
|
-
quote(currencyAmount) {
|
|
418
|
-
invariant6(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
|
|
419
|
-
const result = super.multiply(currencyAmount);
|
|
420
|
-
return CurrencyAmount2.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
|
|
421
|
-
}
|
|
422
|
-
get adjustedForDecimals() {
|
|
423
|
-
return super.multiply(this.scalar);
|
|
424
|
-
}
|
|
425
|
-
toSignificant(significantDigits = 6, format, rounding) {
|
|
426
|
-
return this.adjustedForDecimals.toSignificant(significantDigits, format, rounding);
|
|
427
|
-
}
|
|
428
|
-
toFixed(decimalPlaces = 4, format, rounding) {
|
|
429
|
-
return this.adjustedForDecimals.toFixed(decimalPlaces, format, rounding);
|
|
430
|
-
}
|
|
431
|
-
};
|
|
432
|
-
|
|
433
|
-
// src/entities/fractions/percent.ts
|
|
434
|
-
import JSBI6 from "jsbi";
|
|
435
|
-
var ONE_HUNDRED = new Fraction(JSBI6.BigInt(100));
|
|
436
|
-
function toPercent(fraction) {
|
|
437
|
-
return new Percent(fraction.numerator, fraction.denominator);
|
|
438
|
-
}
|
|
439
|
-
var Percent = class extends Fraction {
|
|
440
|
-
constructor() {
|
|
441
|
-
super(...arguments);
|
|
442
|
-
this.isPercent = true;
|
|
443
|
-
}
|
|
444
|
-
add(other) {
|
|
445
|
-
return toPercent(super.add(other));
|
|
446
|
-
}
|
|
447
|
-
subtract(other) {
|
|
448
|
-
return toPercent(super.subtract(other));
|
|
449
|
-
}
|
|
450
|
-
multiply(other) {
|
|
451
|
-
return toPercent(super.multiply(other));
|
|
452
|
-
}
|
|
453
|
-
divide(other) {
|
|
454
|
-
return toPercent(super.divide(other));
|
|
455
|
-
}
|
|
456
|
-
toSignificant(significantDigits = 5, format, rounding) {
|
|
457
|
-
return super.multiply(ONE_HUNDRED).toSignificant(significantDigits, format, rounding);
|
|
458
|
-
}
|
|
459
|
-
toFixed(decimalPlaces = 2, format, rounding) {
|
|
460
|
-
return super.multiply(ONE_HUNDRED).toFixed(decimalPlaces, format, rounding);
|
|
461
|
-
}
|
|
462
|
-
};
|
|
463
|
-
|
|
464
|
-
// src/entities/pair.ts
|
|
135
|
+
import JSBI2 from "jsbi";
|
|
136
|
+
import invariant2 from "tiny-invariant";
|
|
465
137
|
var PAIR_ADDRESS_CACHE = {};
|
|
466
138
|
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
467
139
|
var computePairAddress = ({
|
|
@@ -485,7 +157,7 @@ var Pair = class {
|
|
|
485
157
|
}
|
|
486
158
|
constructor(currencyAmountA, tokenAmountB) {
|
|
487
159
|
const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) ? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
|
|
488
|
-
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");
|
|
489
161
|
this.tokenAmounts = tokenAmounts;
|
|
490
162
|
}
|
|
491
163
|
involvesToken(token) {
|
|
@@ -500,7 +172,7 @@ var Pair = class {
|
|
|
500
172
|
return new Price2(this.token1, this.token0, result.denominator, result.numerator);
|
|
501
173
|
}
|
|
502
174
|
priceOf(token) {
|
|
503
|
-
|
|
175
|
+
invariant2(this.involvesToken(token), "TOKEN");
|
|
504
176
|
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
505
177
|
}
|
|
506
178
|
get chainId() {
|
|
@@ -519,72 +191,72 @@ var Pair = class {
|
|
|
519
191
|
return this.tokenAmounts[1];
|
|
520
192
|
}
|
|
521
193
|
reserveOf(token) {
|
|
522
|
-
|
|
194
|
+
invariant2(this.involvesToken(token), "TOKEN");
|
|
523
195
|
return token.equals(this.token0) ? this.reserve0 : this.reserve1;
|
|
524
196
|
}
|
|
525
197
|
getOutputAmount(inputAmount) {
|
|
526
|
-
|
|
527
|
-
if (
|
|
198
|
+
invariant2(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
199
|
+
if (JSBI2.equal(this.reserve0.quotient, ZERO) || JSBI2.equal(this.reserve1.quotient, ZERO)) {
|
|
528
200
|
throw new InsufficientReservesError();
|
|
529
201
|
}
|
|
530
202
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
531
203
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
532
|
-
const inputAmountWithFee =
|
|
533
|
-
const numerator =
|
|
534
|
-
const denominator =
|
|
535
|
-
const outputAmount = CurrencyAmount2.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0,
|
|
536
|
-
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)) {
|
|
537
209
|
throw new InsufficientInputAmountError();
|
|
538
210
|
}
|
|
539
211
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
540
212
|
}
|
|
541
213
|
getInputAmount(outputAmount) {
|
|
542
|
-
|
|
543
|
-
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)) {
|
|
544
216
|
throw new InsufficientReservesError();
|
|
545
217
|
}
|
|
546
218
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
547
219
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
548
|
-
const numerator =
|
|
549
|
-
const denominator =
|
|
550
|
-
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));
|
|
551
223
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
552
224
|
}
|
|
553
225
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
554
|
-
|
|
226
|
+
invariant2(totalSupply.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
555
227
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
556
|
-
|
|
228
|
+
invariant2(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
557
229
|
let liquidity;
|
|
558
|
-
if (
|
|
559
|
-
liquidity =
|
|
230
|
+
if (JSBI2.equal(totalSupply.quotient, ZERO)) {
|
|
231
|
+
liquidity = JSBI2.subtract(sqrt(JSBI2.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), MINIMUM_LIQUIDITY);
|
|
560
232
|
} else {
|
|
561
|
-
const amount0 =
|
|
562
|
-
const amount1 =
|
|
563
|
-
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;
|
|
564
236
|
}
|
|
565
|
-
if (!
|
|
237
|
+
if (!JSBI2.greaterThan(liquidity, ZERO)) {
|
|
566
238
|
throw new InsufficientInputAmountError();
|
|
567
239
|
}
|
|
568
240
|
return CurrencyAmount2.fromRawAmount(this.liquidityToken, liquidity);
|
|
569
241
|
}
|
|
570
242
|
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
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");
|
|
575
247
|
let totalSupplyAdjusted;
|
|
576
248
|
if (!feeOn) {
|
|
577
249
|
totalSupplyAdjusted = totalSupply;
|
|
578
250
|
} else {
|
|
579
|
-
|
|
580
|
-
const kLastParsed =
|
|
581
|
-
if (!
|
|
582
|
-
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));
|
|
583
255
|
const rootKLast = sqrt(kLastParsed);
|
|
584
|
-
if (
|
|
585
|
-
const numerator =
|
|
586
|
-
const denominator =
|
|
587
|
-
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);
|
|
588
260
|
totalSupplyAdjusted = totalSupply.add(CurrencyAmount2.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
589
261
|
} else {
|
|
590
262
|
totalSupplyAdjusted = totalSupply;
|
|
@@ -593,25 +265,26 @@ var Pair = class {
|
|
|
593
265
|
totalSupplyAdjusted = totalSupply;
|
|
594
266
|
}
|
|
595
267
|
}
|
|
596
|
-
return CurrencyAmount2.fromRawAmount(token,
|
|
268
|
+
return CurrencyAmount2.fromRawAmount(token, JSBI2.divide(JSBI2.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
|
|
597
269
|
}
|
|
598
270
|
};
|
|
599
271
|
|
|
600
272
|
// src/entities/route.ts
|
|
601
|
-
import
|
|
273
|
+
import invariant3 from "tiny-invariant";
|
|
274
|
+
import { Price as Price3 } from "@pancakeswap/swap-sdk-core";
|
|
602
275
|
var Route = class {
|
|
603
276
|
constructor(pairs, input, output) {
|
|
604
277
|
this._midPrice = null;
|
|
605
|
-
|
|
278
|
+
invariant3(pairs.length > 0, "PAIRS");
|
|
606
279
|
const chainId = pairs[0].chainId;
|
|
607
|
-
|
|
280
|
+
invariant3(pairs.every((pair) => pair.chainId === chainId), "CHAIN_IDS");
|
|
608
281
|
const wrappedInput = input.wrapped;
|
|
609
|
-
|
|
610
|
-
|
|
282
|
+
invariant3(pairs[0].involvesToken(wrappedInput), "INPUT");
|
|
283
|
+
invariant3(typeof output === "undefined" || pairs[pairs.length - 1].involvesToken(output.wrapped), "OUTPUT");
|
|
611
284
|
const path = [wrappedInput];
|
|
612
285
|
for (const [i, pair] of pairs.entries()) {
|
|
613
286
|
const currentInput = path[i];
|
|
614
|
-
|
|
287
|
+
invariant3(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), "PATH");
|
|
615
288
|
const output2 = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
|
|
616
289
|
path.push(output2);
|
|
617
290
|
}
|
|
@@ -625,10 +298,10 @@ var Route = class {
|
|
|
625
298
|
return this._midPrice;
|
|
626
299
|
const prices = [];
|
|
627
300
|
for (const [i, pair] of this.pairs.entries()) {
|
|
628
|
-
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));
|
|
629
302
|
}
|
|
630
303
|
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
631
|
-
return this._midPrice = new
|
|
304
|
+
return this._midPrice = new Price3(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
632
305
|
}
|
|
633
306
|
get chainId() {
|
|
634
307
|
return this.pairs[0].chainId;
|
|
@@ -636,10 +309,18 @@ var Route = class {
|
|
|
636
309
|
};
|
|
637
310
|
|
|
638
311
|
// src/entities/trade.ts
|
|
639
|
-
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";
|
|
640
321
|
function inputOutputComparator(a, b) {
|
|
641
|
-
|
|
642
|
-
|
|
322
|
+
invariant4(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
|
|
323
|
+
invariant4(a.outputAmount.currency.equals(b.outputAmount.currency), "OUTPUT_CURRENCY");
|
|
643
324
|
if (a.outputAmount.equalTo(b.outputAmount)) {
|
|
644
325
|
if (a.inputAmount.equalTo(b.inputAmount)) {
|
|
645
326
|
return 0;
|
|
@@ -671,68 +352,68 @@ function tradeComparator(a, b) {
|
|
|
671
352
|
}
|
|
672
353
|
var Trade = class {
|
|
673
354
|
static exactIn(route, amountIn) {
|
|
674
|
-
return new Trade(route, amountIn,
|
|
355
|
+
return new Trade(route, amountIn, TradeType.EXACT_INPUT);
|
|
675
356
|
}
|
|
676
357
|
static exactOut(route, amountOut) {
|
|
677
|
-
return new Trade(route, amountOut,
|
|
358
|
+
return new Trade(route, amountOut, TradeType.EXACT_OUTPUT);
|
|
678
359
|
}
|
|
679
360
|
constructor(route, amount, tradeType) {
|
|
680
361
|
this.route = route;
|
|
681
362
|
this.tradeType = tradeType;
|
|
682
363
|
const tokenAmounts = new Array(route.path.length);
|
|
683
|
-
if (tradeType ===
|
|
684
|
-
|
|
364
|
+
if (tradeType === TradeType.EXACT_INPUT) {
|
|
365
|
+
invariant4(amount.currency.equals(route.input), "INPUT");
|
|
685
366
|
tokenAmounts[0] = amount.wrapped;
|
|
686
367
|
for (let i = 0; i < route.path.length - 1; i++) {
|
|
687
368
|
const pair = route.pairs[i];
|
|
688
369
|
const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
|
|
689
370
|
tokenAmounts[i + 1] = outputAmount;
|
|
690
371
|
}
|
|
691
|
-
this.inputAmount =
|
|
692
|
-
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);
|
|
693
374
|
} else {
|
|
694
|
-
|
|
375
|
+
invariant4(amount.currency.equals(route.output), "OUTPUT");
|
|
695
376
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
696
377
|
for (let i = route.path.length - 1; i > 0; i--) {
|
|
697
378
|
const pair = route.pairs[i - 1];
|
|
698
379
|
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
699
380
|
tokenAmounts[i - 1] = inputAmount;
|
|
700
381
|
}
|
|
701
|
-
this.inputAmount =
|
|
702
|
-
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);
|
|
703
384
|
}
|
|
704
|
-
this.executionPrice = new
|
|
385
|
+
this.executionPrice = new Price4(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
705
386
|
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
706
387
|
}
|
|
707
388
|
minimumAmountOut(slippageTolerance) {
|
|
708
|
-
|
|
709
|
-
if (this.tradeType ===
|
|
389
|
+
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
390
|
+
if (this.tradeType === TradeType.EXACT_OUTPUT) {
|
|
710
391
|
return this.outputAmount;
|
|
711
392
|
} else {
|
|
712
|
-
const slippageAdjustedAmountOut = new Fraction(
|
|
713
|
-
return
|
|
393
|
+
const slippageAdjustedAmountOut = new Fraction(ONE2).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
394
|
+
return CurrencyAmount3.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
714
395
|
}
|
|
715
396
|
}
|
|
716
397
|
maximumAmountIn(slippageTolerance) {
|
|
717
|
-
|
|
718
|
-
if (this.tradeType ===
|
|
398
|
+
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
399
|
+
if (this.tradeType === TradeType.EXACT_INPUT) {
|
|
719
400
|
return this.inputAmount;
|
|
720
401
|
} else {
|
|
721
|
-
const slippageAdjustedAmountIn = new Fraction(
|
|
722
|
-
return
|
|
402
|
+
const slippageAdjustedAmountIn = new Fraction(ONE2).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
403
|
+
return CurrencyAmount3.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
723
404
|
}
|
|
724
405
|
}
|
|
725
406
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
407
|
+
invariant4(pairs.length > 0, "PAIRS");
|
|
408
|
+
invariant4(maxHops > 0, "MAX_HOPS");
|
|
409
|
+
invariant4(currencyAmountIn === nextAmountIn || currentPairs.length > 0, "INVALID_RECURSION");
|
|
729
410
|
const amountIn = nextAmountIn.wrapped;
|
|
730
411
|
const tokenOut = currencyOut.wrapped;
|
|
731
412
|
for (let i = 0; i < pairs.length; i++) {
|
|
732
413
|
const pair = pairs[i];
|
|
733
414
|
if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
|
|
734
415
|
continue;
|
|
735
|
-
if (pair.reserve0.equalTo(
|
|
416
|
+
if (pair.reserve0.equalTo(ZERO2) || pair.reserve1.equalTo(ZERO2))
|
|
736
417
|
continue;
|
|
737
418
|
let amountOut;
|
|
738
419
|
try {
|
|
@@ -745,7 +426,7 @@ var Trade = class {
|
|
|
745
426
|
throw error;
|
|
746
427
|
}
|
|
747
428
|
if (amountOut.currency.equals(tokenOut)) {
|
|
748
|
-
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);
|
|
749
430
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
750
431
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
751
432
|
Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
|
|
@@ -757,19 +438,19 @@ var Trade = class {
|
|
|
757
438
|
return bestTrades;
|
|
758
439
|
}
|
|
759
440
|
worstExecutionPrice(slippageTolerance) {
|
|
760
|
-
return new
|
|
441
|
+
return new Price4(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
761
442
|
}
|
|
762
443
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
444
|
+
invariant4(pairs.length > 0, "PAIRS");
|
|
445
|
+
invariant4(maxHops > 0, "MAX_HOPS");
|
|
446
|
+
invariant4(currencyAmountOut === nextAmountOut || currentPairs.length > 0, "INVALID_RECURSION");
|
|
766
447
|
const amountOut = nextAmountOut.wrapped;
|
|
767
448
|
const tokenIn = currencyIn.wrapped;
|
|
768
449
|
for (let i = 0; i < pairs.length; i++) {
|
|
769
450
|
const pair = pairs[i];
|
|
770
451
|
if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
|
|
771
452
|
continue;
|
|
772
|
-
if (pair.reserve0.equalTo(
|
|
453
|
+
if (pair.reserve0.equalTo(ZERO2) || pair.reserve1.equalTo(ZERO2))
|
|
773
454
|
continue;
|
|
774
455
|
let amountIn;
|
|
775
456
|
try {
|
|
@@ -782,7 +463,7 @@ var Trade = class {
|
|
|
782
463
|
throw error;
|
|
783
464
|
}
|
|
784
465
|
if (amountIn.currency.equals(tokenIn)) {
|
|
785
|
-
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);
|
|
786
467
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
787
468
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
788
469
|
Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
|
|
@@ -795,17 +476,9 @@ var Trade = class {
|
|
|
795
476
|
}
|
|
796
477
|
};
|
|
797
478
|
|
|
798
|
-
// src/entities/nativeCurrency.ts
|
|
799
|
-
var NativeCurrency = class extends BaseCurrency {
|
|
800
|
-
constructor() {
|
|
801
|
-
super(...arguments);
|
|
802
|
-
this.isNative = true;
|
|
803
|
-
this.isToken = false;
|
|
804
|
-
}
|
|
805
|
-
};
|
|
806
|
-
|
|
807
479
|
// src/entities/native.ts
|
|
808
|
-
import
|
|
480
|
+
import invariant5 from "tiny-invariant";
|
|
481
|
+
import { NativeCurrency } from "@pancakeswap/swap-sdk-core";
|
|
809
482
|
var _Native = class extends NativeCurrency {
|
|
810
483
|
constructor({
|
|
811
484
|
chainId,
|
|
@@ -817,14 +490,14 @@ var _Native = class extends NativeCurrency {
|
|
|
817
490
|
}
|
|
818
491
|
get wrapped() {
|
|
819
492
|
const wnative = WNATIVE[this.chainId];
|
|
820
|
-
|
|
493
|
+
invariant5(!!wnative, "WRAPPED");
|
|
821
494
|
return wnative;
|
|
822
495
|
}
|
|
823
496
|
static onChain(chainId) {
|
|
824
497
|
if (chainId in this.cache) {
|
|
825
498
|
return this.cache[chainId];
|
|
826
499
|
}
|
|
827
|
-
|
|
500
|
+
invariant5(!!NATIVE[chainId], "NATIVE_CURRENCY");
|
|
828
501
|
const { decimals, name, symbol } = NATIVE[chainId];
|
|
829
502
|
return this.cache[chainId] = new _Native({ chainId, decimals, symbol, name });
|
|
830
503
|
}
|
|
@@ -836,7 +509,8 @@ var Native = _Native;
|
|
|
836
509
|
Native.cache = {};
|
|
837
510
|
|
|
838
511
|
// src/router.ts
|
|
839
|
-
import
|
|
512
|
+
import { TradeType as TradeType2 } from "@pancakeswap/swap-sdk-core";
|
|
513
|
+
import invariant6 from "tiny-invariant";
|
|
840
514
|
function toHex(currencyAmount) {
|
|
841
515
|
return `0x${currencyAmount.quotient.toString(16)}`;
|
|
842
516
|
}
|
|
@@ -847,8 +521,8 @@ var Router = class {
|
|
|
847
521
|
static swapCallParameters(trade, options) {
|
|
848
522
|
const etherIn = trade.inputAmount.currency.isNative;
|
|
849
523
|
const etherOut = trade.outputAmount.currency.isNative;
|
|
850
|
-
|
|
851
|
-
|
|
524
|
+
invariant6(!(etherIn && etherOut), "ETHER_IN_OUT");
|
|
525
|
+
invariant6(!("ttl" in options) || options.ttl > 0, "TTL");
|
|
852
526
|
const to = validateAndParseAddress(options.recipient);
|
|
853
527
|
const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
|
|
854
528
|
const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
|
|
@@ -859,7 +533,7 @@ var Router = class {
|
|
|
859
533
|
let args;
|
|
860
534
|
let value;
|
|
861
535
|
switch (trade.tradeType) {
|
|
862
|
-
case
|
|
536
|
+
case TradeType2.EXACT_INPUT:
|
|
863
537
|
if (etherIn) {
|
|
864
538
|
methodName = useFeeOnTransfer ? "swapExactETHForTokensSupportingFeeOnTransferTokens" : "swapExactETHForTokens";
|
|
865
539
|
args = [amountOut, path, to, deadline];
|
|
@@ -874,8 +548,8 @@ var Router = class {
|
|
|
874
548
|
value = ZERO_HEX;
|
|
875
549
|
}
|
|
876
550
|
break;
|
|
877
|
-
case
|
|
878
|
-
|
|
551
|
+
case TradeType2.EXACT_OUTPUT:
|
|
552
|
+
invariant6(!useFeeOnTransfer, "EXACT_OUT_FOT");
|
|
879
553
|
if (etherIn) {
|
|
880
554
|
methodName = "swapETHForExactTokens";
|
|
881
555
|
args = [amountOut, path, to, deadline];
|
|
@@ -898,46 +572,26 @@ var Router = class {
|
|
|
898
572
|
};
|
|
899
573
|
}
|
|
900
574
|
};
|
|
575
|
+
|
|
576
|
+
// src/index.ts
|
|
577
|
+
export * from "@pancakeswap/swap-sdk-core";
|
|
901
578
|
export {
|
|
902
|
-
BaseCurrency,
|
|
903
579
|
ChainId,
|
|
904
|
-
|
|
580
|
+
ERC20Token,
|
|
905
581
|
FACTORY_ADDRESS,
|
|
906
582
|
FACTORY_ADDRESS_MAP,
|
|
907
|
-
FIVE,
|
|
908
|
-
Fraction,
|
|
909
583
|
INIT_CODE_HASH,
|
|
910
584
|
INIT_CODE_HASH_MAP,
|
|
911
|
-
|
|
912
|
-
InsufficientReservesError,
|
|
913
|
-
JSBI8 as JSBI,
|
|
914
|
-
MINIMUM_LIQUIDITY,
|
|
915
|
-
MaxUint256,
|
|
585
|
+
JSBI3 as JSBI,
|
|
916
586
|
NATIVE,
|
|
917
587
|
Native,
|
|
918
|
-
NativeCurrency,
|
|
919
|
-
ONE,
|
|
920
588
|
Pair,
|
|
921
|
-
Percent,
|
|
922
|
-
Price2 as Price,
|
|
923
|
-
Rounding,
|
|
924
589
|
Route,
|
|
925
590
|
Router,
|
|
926
|
-
SOLIDITY_TYPE_MAXIMA,
|
|
927
|
-
SolidityType,
|
|
928
|
-
TEN,
|
|
929
|
-
THREE,
|
|
930
|
-
TWO,
|
|
931
|
-
Token,
|
|
932
591
|
Trade,
|
|
933
|
-
TradeType,
|
|
934
592
|
WBNB,
|
|
935
593
|
WETH9,
|
|
936
594
|
WNATIVE,
|
|
937
|
-
ZERO,
|
|
938
|
-
_100,
|
|
939
|
-
_10000,
|
|
940
|
-
_9975,
|
|
941
595
|
computePairAddress,
|
|
942
596
|
computePriceImpact,
|
|
943
597
|
inputOutputComparator,
|