@pancakeswap/sdk 3.1.0 → 3.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +28 -274
- package/dist/index.js +126 -556
- package/dist/index.mjs +132 -518
- package/package.json +5 -4
package/dist/index.mjs
CHANGED
|
@@ -1,21 +1,13 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import JSBI8 from "jsbi";
|
|
3
|
-
|
|
4
|
-
// src/constants.ts
|
|
5
2
|
import JSBI2 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";
|
|
11
9
|
import invariant from "tiny-invariant";
|
|
12
10
|
import warning from "tiny-warning";
|
|
13
|
-
import JSBI from "jsbi";
|
|
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
|
-
}
|
|
19
11
|
function validateAndParseAddress(address) {
|
|
20
12
|
try {
|
|
21
13
|
const checksummedAddress = getAddress(address);
|
|
@@ -25,122 +17,26 @@ function validateAndParseAddress(address) {
|
|
|
25
17
|
invariant(false, `${address} is not a valid address.`);
|
|
26
18
|
}
|
|
27
19
|
}
|
|
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
|
-
function sortedInsert(items, add, maxSize, comparator) {
|
|
45
|
-
invariant(maxSize > 0, "MAX_SIZE_ZERO");
|
|
46
|
-
invariant(items.length <= maxSize, "ITEMS_SIZE");
|
|
47
|
-
if (items.length === 0) {
|
|
48
|
-
items.push(add);
|
|
49
|
-
return null;
|
|
50
|
-
} else {
|
|
51
|
-
const isFull = items.length === maxSize;
|
|
52
|
-
if (isFull && comparator(items[items.length - 1], add) <= 0) {
|
|
53
|
-
return add;
|
|
54
|
-
}
|
|
55
|
-
let lo = 0, hi = items.length;
|
|
56
|
-
while (lo < hi) {
|
|
57
|
-
const mid = lo + hi >>> 1;
|
|
58
|
-
if (comparator(items[mid], add) <= 0) {
|
|
59
|
-
lo = mid + 1;
|
|
60
|
-
} else {
|
|
61
|
-
hi = mid;
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
items.splice(lo, 0, add);
|
|
65
|
-
return isFull ? items.pop() : null;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
function computePriceImpact(midPrice, inputAmount, outputAmount) {
|
|
69
|
-
const quotedOutputAmount = midPrice.quote(inputAmount);
|
|
70
|
-
const priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount);
|
|
71
|
-
return new Percent(priceImpact.numerator, priceImpact.denominator);
|
|
72
|
-
}
|
|
73
|
-
|
|
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
20
|
|
|
87
21
|
// src/entities/token.ts
|
|
88
|
-
var
|
|
22
|
+
var ERC20Token = class extends Token {
|
|
89
23
|
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
|
-
};
|
|
24
|
+
super(chainId, validateAndParseAddress(address), decimals, symbol, name, projectLink);
|
|
116
25
|
}
|
|
117
26
|
};
|
|
118
27
|
|
|
119
28
|
// src/constants.ts
|
|
120
29
|
var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
121
30
|
ChainId2[ChainId2["ETHEREUM"] = 1] = "ETHEREUM";
|
|
122
|
-
ChainId2[ChainId2["RINKEBY"] = 4] = "RINKEBY";
|
|
123
31
|
ChainId2[ChainId2["GOERLI"] = 5] = "GOERLI";
|
|
124
32
|
ChainId2[ChainId2["BSC"] = 56] = "BSC";
|
|
125
33
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
126
34
|
return ChainId2;
|
|
127
35
|
})(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
36
|
var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
140
37
|
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
141
38
|
var FACTORY_ADDRESS_MAP = {
|
|
142
39
|
[1 /* ETHEREUM */]: FACTORY_ADDRESS_ETH,
|
|
143
|
-
[4 /* RINKEBY */]: FACTORY_ADDRESS_ETH,
|
|
144
40
|
[5 /* GOERLI */]: FACTORY_ADDRESS_ETH,
|
|
145
41
|
[56 /* BSC */]: FACTORY_ADDRESS,
|
|
146
42
|
[97 /* BSC_TESTNET */]: "0x6725f303b657a9451d8ba641348b6761a6cc7a17"
|
|
@@ -149,51 +45,27 @@ var INIT_CODE_HASH = "0x00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a
|
|
|
149
45
|
var INIT_CODE_HASH_ETH = "0x57224589c67f3f30a6b0d7a1b54cf3153ab84563bc609ef41dfb34f8b2974d2d";
|
|
150
46
|
var INIT_CODE_HASH_MAP = {
|
|
151
47
|
[1 /* ETHEREUM */]: INIT_CODE_HASH_ETH,
|
|
152
|
-
[4 /* RINKEBY */]: INIT_CODE_HASH_ETH,
|
|
153
48
|
[5 /* GOERLI */]: INIT_CODE_HASH_ETH,
|
|
154
49
|
[56 /* BSC */]: INIT_CODE_HASH,
|
|
155
50
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
156
51
|
};
|
|
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
52
|
var WETH9 = {
|
|
178
|
-
[1 /* ETHEREUM */]: new
|
|
179
|
-
[
|
|
180
|
-
[5 /* GOERLI */]: new Token(5 /* GOERLI */, "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", 18, "WETH", "Wrapped Ether", "https://weth.io")
|
|
53
|
+
[1 /* ETHEREUM */]: new ERC20Token(1 /* ETHEREUM */, "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 18, "WETH", "Wrapped Ether", "https://weth.io"),
|
|
54
|
+
[5 /* GOERLI */]: new ERC20Token(5 /* GOERLI */, "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", 18, "WETH", "Wrapped Ether", "https://weth.io")
|
|
181
55
|
};
|
|
182
56
|
var WBNB = {
|
|
183
|
-
[1 /* ETHEREUM */]: new
|
|
184
|
-
[56 /* BSC */]: new
|
|
185
|
-
[97 /* BSC_TESTNET */]: new
|
|
57
|
+
[1 /* ETHEREUM */]: new ERC20Token(1 /* ETHEREUM */, "0x418D75f65a02b3D53B2418FB8E1fe493759c7605", 18, "WBNB", "Wrapped BNB", "https://www.binance.org"),
|
|
58
|
+
[56 /* BSC */]: new ERC20Token(56 /* BSC */, "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", 18, "WBNB", "Wrapped BNB", "https://www.binance.org"),
|
|
59
|
+
[97 /* BSC_TESTNET */]: new ERC20Token(97 /* BSC_TESTNET */, "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd", 18, "WBNB", "Wrapped BNB", "https://www.binance.org")
|
|
186
60
|
};
|
|
187
61
|
var WNATIVE = {
|
|
188
62
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
189
|
-
[4 /* RINKEBY */]: WETH9[4 /* RINKEBY */],
|
|
190
63
|
[5 /* GOERLI */]: WETH9[5 /* GOERLI */],
|
|
191
64
|
[56 /* BSC */]: WBNB[56 /* BSC */],
|
|
192
65
|
[97 /* BSC_TESTNET */]: WBNB[97 /* BSC_TESTNET */]
|
|
193
66
|
};
|
|
194
67
|
var NATIVE = {
|
|
195
68
|
[1 /* ETHEREUM */]: { name: "Ether", symbol: "ETH", decimals: 18 },
|
|
196
|
-
[4 /* RINKEBY */]: { name: "Rinkeby Ether", symbol: "RIN", decimals: 18 },
|
|
197
69
|
[5 /* GOERLI */]: { name: "Goerli Ether", symbol: "GOR", decimals: 18 },
|
|
198
70
|
[56 /* BSC */]: {
|
|
199
71
|
name: "Binance Chain Native Token",
|
|
@@ -207,261 +79,24 @@ var NATIVE = {
|
|
|
207
79
|
}
|
|
208
80
|
};
|
|
209
81
|
|
|
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
82
|
// src/entities/pair.ts
|
|
83
|
+
import {
|
|
84
|
+
InsufficientInputAmountError,
|
|
85
|
+
InsufficientReservesError,
|
|
86
|
+
sqrt,
|
|
87
|
+
CurrencyAmount,
|
|
88
|
+
Price,
|
|
89
|
+
FIVE,
|
|
90
|
+
ONE,
|
|
91
|
+
ZERO,
|
|
92
|
+
_10000,
|
|
93
|
+
_9975,
|
|
94
|
+
MINIMUM_LIQUIDITY
|
|
95
|
+
} from "@pancakeswap/swap-sdk-core";
|
|
232
96
|
import { getCreate2Address } from "@ethersproject/address";
|
|
233
97
|
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
|
|
98
|
+
import JSBI from "jsbi";
|
|
99
|
+
import invariant2 from "tiny-invariant";
|
|
465
100
|
var PAIR_ADDRESS_CACHE = {};
|
|
466
101
|
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
467
102
|
var computePairAddress = ({
|
|
@@ -485,7 +120,7 @@ var Pair = class {
|
|
|
485
120
|
}
|
|
486
121
|
constructor(currencyAmountA, tokenAmountB) {
|
|
487
122
|
const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) ? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
|
|
488
|
-
this.liquidityToken = new
|
|
123
|
+
this.liquidityToken = new ERC20Token(tokenAmounts[0].currency.chainId, Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency), 18, "Cake-LP", "Pancake LPs");
|
|
489
124
|
this.tokenAmounts = tokenAmounts;
|
|
490
125
|
}
|
|
491
126
|
involvesToken(token) {
|
|
@@ -493,14 +128,14 @@ var Pair = class {
|
|
|
493
128
|
}
|
|
494
129
|
get token0Price() {
|
|
495
130
|
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
496
|
-
return new
|
|
131
|
+
return new Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
497
132
|
}
|
|
498
133
|
get token1Price() {
|
|
499
134
|
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
500
|
-
return new
|
|
135
|
+
return new Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
501
136
|
}
|
|
502
137
|
priceOf(token) {
|
|
503
|
-
|
|
138
|
+
invariant2(this.involvesToken(token), "TOKEN");
|
|
504
139
|
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
505
140
|
}
|
|
506
141
|
get chainId() {
|
|
@@ -519,73 +154,73 @@ var Pair = class {
|
|
|
519
154
|
return this.tokenAmounts[1];
|
|
520
155
|
}
|
|
521
156
|
reserveOf(token) {
|
|
522
|
-
|
|
157
|
+
invariant2(this.involvesToken(token), "TOKEN");
|
|
523
158
|
return token.equals(this.token0) ? this.reserve0 : this.reserve1;
|
|
524
159
|
}
|
|
525
160
|
getOutputAmount(inputAmount) {
|
|
526
|
-
|
|
527
|
-
if (
|
|
161
|
+
invariant2(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
162
|
+
if (JSBI.equal(this.reserve0.quotient, ZERO) || JSBI.equal(this.reserve1.quotient, ZERO)) {
|
|
528
163
|
throw new InsufficientReservesError();
|
|
529
164
|
}
|
|
530
165
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
531
166
|
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 =
|
|
536
|
-
if (
|
|
167
|
+
const inputAmountWithFee = JSBI.multiply(inputAmount.quotient, _9975);
|
|
168
|
+
const numerator = JSBI.multiply(inputAmountWithFee, outputReserve.quotient);
|
|
169
|
+
const denominator = JSBI.add(JSBI.multiply(inputReserve.quotient, _10000), inputAmountWithFee);
|
|
170
|
+
const outputAmount = CurrencyAmount.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI.divide(numerator, denominator));
|
|
171
|
+
if (JSBI.equal(outputAmount.quotient, ZERO)) {
|
|
537
172
|
throw new InsufficientInputAmountError();
|
|
538
173
|
}
|
|
539
174
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
540
175
|
}
|
|
541
176
|
getInputAmount(outputAmount) {
|
|
542
|
-
|
|
543
|
-
if (
|
|
177
|
+
invariant2(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
178
|
+
if (JSBI.equal(this.reserve0.quotient, ZERO) || JSBI.equal(this.reserve1.quotient, ZERO) || JSBI.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient)) {
|
|
544
179
|
throw new InsufficientReservesError();
|
|
545
180
|
}
|
|
546
181
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
547
182
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
548
|
-
const numerator =
|
|
549
|
-
const denominator =
|
|
550
|
-
const inputAmount =
|
|
183
|
+
const numerator = JSBI.multiply(JSBI.multiply(inputReserve.quotient, outputAmount.quotient), _10000);
|
|
184
|
+
const denominator = JSBI.multiply(JSBI.subtract(outputReserve.quotient, outputAmount.quotient), _9975);
|
|
185
|
+
const inputAmount = CurrencyAmount.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, JSBI.add(JSBI.divide(numerator, denominator), ONE));
|
|
551
186
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
552
187
|
}
|
|
553
188
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
554
|
-
|
|
189
|
+
invariant2(totalSupply.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
555
190
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
556
|
-
|
|
191
|
+
invariant2(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
557
192
|
let liquidity;
|
|
558
|
-
if (
|
|
559
|
-
liquidity =
|
|
193
|
+
if (JSBI.equal(totalSupply.quotient, ZERO)) {
|
|
194
|
+
liquidity = JSBI.subtract(sqrt(JSBI.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), MINIMUM_LIQUIDITY);
|
|
560
195
|
} else {
|
|
561
|
-
const amount0 =
|
|
562
|
-
const amount1 =
|
|
563
|
-
liquidity =
|
|
196
|
+
const amount0 = JSBI.divide(JSBI.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
197
|
+
const amount1 = JSBI.divide(JSBI.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
198
|
+
liquidity = JSBI.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
|
|
564
199
|
}
|
|
565
|
-
if (!
|
|
200
|
+
if (!JSBI.greaterThan(liquidity, ZERO)) {
|
|
566
201
|
throw new InsufficientInputAmountError();
|
|
567
202
|
}
|
|
568
|
-
return
|
|
203
|
+
return CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
569
204
|
}
|
|
570
205
|
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
206
|
+
invariant2(this.involvesToken(token), "TOKEN");
|
|
207
|
+
invariant2(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
208
|
+
invariant2(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
209
|
+
invariant2(JSBI.lessThanOrEqual(liquidity.quotient, totalSupply.quotient), "LIQUIDITY");
|
|
575
210
|
let totalSupplyAdjusted;
|
|
576
211
|
if (!feeOn) {
|
|
577
212
|
totalSupplyAdjusted = totalSupply;
|
|
578
213
|
} else {
|
|
579
|
-
|
|
580
|
-
const kLastParsed =
|
|
581
|
-
if (!
|
|
582
|
-
const rootK = sqrt(
|
|
214
|
+
invariant2(!!kLast, "K_LAST");
|
|
215
|
+
const kLastParsed = JSBI.BigInt(kLast);
|
|
216
|
+
if (!JSBI.equal(kLastParsed, ZERO)) {
|
|
217
|
+
const rootK = sqrt(JSBI.multiply(this.reserve0.quotient, this.reserve1.quotient));
|
|
583
218
|
const rootKLast = sqrt(kLastParsed);
|
|
584
|
-
if (
|
|
585
|
-
const numerator =
|
|
586
|
-
const denominator =
|
|
587
|
-
const feeLiquidity =
|
|
588
|
-
totalSupplyAdjusted = totalSupply.add(
|
|
219
|
+
if (JSBI.greaterThan(rootK, rootKLast)) {
|
|
220
|
+
const numerator = JSBI.multiply(totalSupply.quotient, JSBI.subtract(rootK, rootKLast));
|
|
221
|
+
const denominator = JSBI.add(JSBI.multiply(rootK, FIVE), rootKLast);
|
|
222
|
+
const feeLiquidity = JSBI.divide(numerator, denominator);
|
|
223
|
+
totalSupplyAdjusted = totalSupply.add(CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
589
224
|
} else {
|
|
590
225
|
totalSupplyAdjusted = totalSupply;
|
|
591
226
|
}
|
|
@@ -593,25 +228,26 @@ var Pair = class {
|
|
|
593
228
|
totalSupplyAdjusted = totalSupply;
|
|
594
229
|
}
|
|
595
230
|
}
|
|
596
|
-
return
|
|
231
|
+
return CurrencyAmount.fromRawAmount(token, JSBI.divide(JSBI.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
|
|
597
232
|
}
|
|
598
233
|
};
|
|
599
234
|
|
|
600
235
|
// src/entities/route.ts
|
|
601
|
-
import
|
|
236
|
+
import invariant3 from "tiny-invariant";
|
|
237
|
+
import { Price as Price2 } from "@pancakeswap/swap-sdk-core";
|
|
602
238
|
var Route = class {
|
|
603
239
|
constructor(pairs, input, output) {
|
|
604
240
|
this._midPrice = null;
|
|
605
|
-
|
|
241
|
+
invariant3(pairs.length > 0, "PAIRS");
|
|
606
242
|
const chainId = pairs[0].chainId;
|
|
607
|
-
|
|
243
|
+
invariant3(pairs.every((pair) => pair.chainId === chainId), "CHAIN_IDS");
|
|
608
244
|
const wrappedInput = input.wrapped;
|
|
609
|
-
|
|
610
|
-
|
|
245
|
+
invariant3(pairs[0].involvesToken(wrappedInput), "INPUT");
|
|
246
|
+
invariant3(typeof output === "undefined" || pairs[pairs.length - 1].involvesToken(output.wrapped), "OUTPUT");
|
|
611
247
|
const path = [wrappedInput];
|
|
612
248
|
for (const [i, pair] of pairs.entries()) {
|
|
613
249
|
const currentInput = path[i];
|
|
614
|
-
|
|
250
|
+
invariant3(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), "PATH");
|
|
615
251
|
const output2 = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
|
|
616
252
|
path.push(output2);
|
|
617
253
|
}
|
|
@@ -636,26 +272,33 @@ var Route = class {
|
|
|
636
272
|
};
|
|
637
273
|
|
|
638
274
|
// src/entities/trade.ts
|
|
639
|
-
import
|
|
275
|
+
import invariant4 from "tiny-invariant";
|
|
276
|
+
import {
|
|
277
|
+
ONE as ONE2,
|
|
278
|
+
TradeType,
|
|
279
|
+
ZERO as ZERO2,
|
|
280
|
+
CurrencyAmount as CurrencyAmount2,
|
|
281
|
+
Fraction,
|
|
282
|
+
Price as Price3,
|
|
283
|
+
sortedInsert,
|
|
284
|
+
computePriceImpact
|
|
285
|
+
} from "@pancakeswap/swap-sdk-core";
|
|
640
286
|
function inputOutputComparator(a, b) {
|
|
641
|
-
|
|
642
|
-
|
|
287
|
+
invariant4(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
|
|
288
|
+
invariant4(a.outputAmount.currency.equals(b.outputAmount.currency), "OUTPUT_CURRENCY");
|
|
643
289
|
if (a.outputAmount.equalTo(b.outputAmount)) {
|
|
644
290
|
if (a.inputAmount.equalTo(b.inputAmount)) {
|
|
645
291
|
return 0;
|
|
646
292
|
}
|
|
647
293
|
if (a.inputAmount.lessThan(b.inputAmount)) {
|
|
648
294
|
return -1;
|
|
649
|
-
} else {
|
|
650
|
-
return 1;
|
|
651
|
-
}
|
|
652
|
-
} else {
|
|
653
|
-
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
654
|
-
return 1;
|
|
655
|
-
} else {
|
|
656
|
-
return -1;
|
|
657
295
|
}
|
|
296
|
+
return 1;
|
|
297
|
+
}
|
|
298
|
+
if (a.outputAmount.lessThan(b.outputAmount)) {
|
|
299
|
+
return 1;
|
|
658
300
|
}
|
|
301
|
+
return -1;
|
|
659
302
|
}
|
|
660
303
|
function tradeComparator(a, b) {
|
|
661
304
|
const ioComp = inputOutputComparator(a, b);
|
|
@@ -664,24 +307,25 @@ function tradeComparator(a, b) {
|
|
|
664
307
|
}
|
|
665
308
|
if (a.priceImpact.lessThan(b.priceImpact)) {
|
|
666
309
|
return -1;
|
|
667
|
-
}
|
|
310
|
+
}
|
|
311
|
+
if (a.priceImpact.greaterThan(b.priceImpact)) {
|
|
668
312
|
return 1;
|
|
669
313
|
}
|
|
670
314
|
return a.route.path.length - b.route.path.length;
|
|
671
315
|
}
|
|
672
316
|
var Trade = class {
|
|
673
317
|
static exactIn(route, amountIn) {
|
|
674
|
-
return new Trade(route, amountIn,
|
|
318
|
+
return new Trade(route, amountIn, TradeType.EXACT_INPUT);
|
|
675
319
|
}
|
|
676
320
|
static exactOut(route, amountOut) {
|
|
677
|
-
return new Trade(route, amountOut,
|
|
321
|
+
return new Trade(route, amountOut, TradeType.EXACT_OUTPUT);
|
|
678
322
|
}
|
|
679
323
|
constructor(route, amount, tradeType) {
|
|
680
324
|
this.route = route;
|
|
681
325
|
this.tradeType = tradeType;
|
|
682
326
|
const tokenAmounts = new Array(route.path.length);
|
|
683
|
-
if (tradeType ===
|
|
684
|
-
|
|
327
|
+
if (tradeType === TradeType.EXACT_INPUT) {
|
|
328
|
+
invariant4(amount.currency.equals(route.input), "INPUT");
|
|
685
329
|
tokenAmounts[0] = amount.wrapped;
|
|
686
330
|
for (let i = 0; i < route.path.length - 1; i++) {
|
|
687
331
|
const pair = route.pairs[i];
|
|
@@ -691,7 +335,7 @@ var Trade = class {
|
|
|
691
335
|
this.inputAmount = CurrencyAmount2.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
692
336
|
this.outputAmount = CurrencyAmount2.fromFractionalAmount(route.output, tokenAmounts[tokenAmounts.length - 1].numerator, tokenAmounts[tokenAmounts.length - 1].denominator);
|
|
693
337
|
} else {
|
|
694
|
-
|
|
338
|
+
invariant4(amount.currency.equals(route.output), "OUTPUT");
|
|
695
339
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
696
340
|
for (let i = route.path.length - 1; i > 0; i--) {
|
|
697
341
|
const pair = route.pairs[i - 1];
|
|
@@ -701,38 +345,36 @@ var Trade = class {
|
|
|
701
345
|
this.inputAmount = CurrencyAmount2.fromFractionalAmount(route.input, tokenAmounts[0].numerator, tokenAmounts[0].denominator);
|
|
702
346
|
this.outputAmount = CurrencyAmount2.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
703
347
|
}
|
|
704
|
-
this.executionPrice = new
|
|
348
|
+
this.executionPrice = new Price3(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
705
349
|
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
706
350
|
}
|
|
707
351
|
minimumAmountOut(slippageTolerance) {
|
|
708
|
-
|
|
709
|
-
if (this.tradeType ===
|
|
352
|
+
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
353
|
+
if (this.tradeType === TradeType.EXACT_OUTPUT) {
|
|
710
354
|
return this.outputAmount;
|
|
711
|
-
} else {
|
|
712
|
-
const slippageAdjustedAmountOut = new Fraction(ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
713
|
-
return CurrencyAmount2.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
714
355
|
}
|
|
356
|
+
const slippageAdjustedAmountOut = new Fraction(ONE2).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
357
|
+
return CurrencyAmount2.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
715
358
|
}
|
|
716
359
|
maximumAmountIn(slippageTolerance) {
|
|
717
|
-
|
|
718
|
-
if (this.tradeType ===
|
|
360
|
+
invariant4(!slippageTolerance.lessThan(ZERO2), "SLIPPAGE_TOLERANCE");
|
|
361
|
+
if (this.tradeType === TradeType.EXACT_INPUT) {
|
|
719
362
|
return this.inputAmount;
|
|
720
|
-
} else {
|
|
721
|
-
const slippageAdjustedAmountIn = new Fraction(ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
722
|
-
return CurrencyAmount2.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
723
363
|
}
|
|
364
|
+
const slippageAdjustedAmountIn = new Fraction(ONE2).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
365
|
+
return CurrencyAmount2.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
724
366
|
}
|
|
725
367
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
368
|
+
invariant4(pairs.length > 0, "PAIRS");
|
|
369
|
+
invariant4(maxHops > 0, "MAX_HOPS");
|
|
370
|
+
invariant4(currencyAmountIn === nextAmountIn || currentPairs.length > 0, "INVALID_RECURSION");
|
|
729
371
|
const amountIn = nextAmountIn.wrapped;
|
|
730
372
|
const tokenOut = currencyOut.wrapped;
|
|
731
373
|
for (let i = 0; i < pairs.length; i++) {
|
|
732
374
|
const pair = pairs[i];
|
|
733
375
|
if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
|
|
734
376
|
continue;
|
|
735
|
-
if (pair.reserve0.equalTo(
|
|
377
|
+
if (pair.reserve0.equalTo(ZERO2) || pair.reserve1.equalTo(ZERO2))
|
|
736
378
|
continue;
|
|
737
379
|
let amountOut;
|
|
738
380
|
try {
|
|
@@ -745,7 +387,7 @@ var Trade = class {
|
|
|
745
387
|
throw error;
|
|
746
388
|
}
|
|
747
389
|
if (amountOut.currency.equals(tokenOut)) {
|
|
748
|
-
sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn,
|
|
390
|
+
sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn, TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
|
|
749
391
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
750
392
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
751
393
|
Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
|
|
@@ -757,19 +399,19 @@ var Trade = class {
|
|
|
757
399
|
return bestTrades;
|
|
758
400
|
}
|
|
759
401
|
worstExecutionPrice(slippageTolerance) {
|
|
760
|
-
return new
|
|
402
|
+
return new Price3(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
761
403
|
}
|
|
762
404
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
405
|
+
invariant4(pairs.length > 0, "PAIRS");
|
|
406
|
+
invariant4(maxHops > 0, "MAX_HOPS");
|
|
407
|
+
invariant4(currencyAmountOut === nextAmountOut || currentPairs.length > 0, "INVALID_RECURSION");
|
|
766
408
|
const amountOut = nextAmountOut.wrapped;
|
|
767
409
|
const tokenIn = currencyIn.wrapped;
|
|
768
410
|
for (let i = 0; i < pairs.length; i++) {
|
|
769
411
|
const pair = pairs[i];
|
|
770
412
|
if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
|
|
771
413
|
continue;
|
|
772
|
-
if (pair.reserve0.equalTo(
|
|
414
|
+
if (pair.reserve0.equalTo(ZERO2) || pair.reserve1.equalTo(ZERO2))
|
|
773
415
|
continue;
|
|
774
416
|
let amountIn;
|
|
775
417
|
try {
|
|
@@ -782,7 +424,7 @@ var Trade = class {
|
|
|
782
424
|
throw error;
|
|
783
425
|
}
|
|
784
426
|
if (amountIn.currency.equals(tokenIn)) {
|
|
785
|
-
sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut,
|
|
427
|
+
sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut, TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
|
|
786
428
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
787
429
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
788
430
|
Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
|
|
@@ -795,17 +437,9 @@ var Trade = class {
|
|
|
795
437
|
}
|
|
796
438
|
};
|
|
797
439
|
|
|
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
440
|
// src/entities/native.ts
|
|
808
|
-
import
|
|
441
|
+
import invariant5 from "tiny-invariant";
|
|
442
|
+
import { NativeCurrency } from "@pancakeswap/swap-sdk-core";
|
|
809
443
|
var _Native = class extends NativeCurrency {
|
|
810
444
|
constructor({
|
|
811
445
|
chainId,
|
|
@@ -817,14 +451,14 @@ var _Native = class extends NativeCurrency {
|
|
|
817
451
|
}
|
|
818
452
|
get wrapped() {
|
|
819
453
|
const wnative = WNATIVE[this.chainId];
|
|
820
|
-
|
|
454
|
+
invariant5(!!wnative, "WRAPPED");
|
|
821
455
|
return wnative;
|
|
822
456
|
}
|
|
823
457
|
static onChain(chainId) {
|
|
824
458
|
if (chainId in this.cache) {
|
|
825
459
|
return this.cache[chainId];
|
|
826
460
|
}
|
|
827
|
-
|
|
461
|
+
invariant5(!!NATIVE[chainId], "NATIVE_CURRENCY");
|
|
828
462
|
const { decimals, name, symbol } = NATIVE[chainId];
|
|
829
463
|
return this.cache[chainId] = new _Native({ chainId, decimals, symbol, name });
|
|
830
464
|
}
|
|
@@ -836,7 +470,8 @@ var Native = _Native;
|
|
|
836
470
|
Native.cache = {};
|
|
837
471
|
|
|
838
472
|
// src/router.ts
|
|
839
|
-
import
|
|
473
|
+
import { TradeType as TradeType2 } from "@pancakeswap/swap-sdk-core";
|
|
474
|
+
import invariant6 from "tiny-invariant";
|
|
840
475
|
function toHex(currencyAmount) {
|
|
841
476
|
return `0x${currencyAmount.quotient.toString(16)}`;
|
|
842
477
|
}
|
|
@@ -847,8 +482,8 @@ var Router = class {
|
|
|
847
482
|
static swapCallParameters(trade, options) {
|
|
848
483
|
const etherIn = trade.inputAmount.currency.isNative;
|
|
849
484
|
const etherOut = trade.outputAmount.currency.isNative;
|
|
850
|
-
|
|
851
|
-
|
|
485
|
+
invariant6(!(etherIn && etherOut), "ETHER_IN_OUT");
|
|
486
|
+
invariant6(!("ttl" in options) || options.ttl > 0, "TTL");
|
|
852
487
|
const to = validateAndParseAddress(options.recipient);
|
|
853
488
|
const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
|
|
854
489
|
const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
|
|
@@ -859,7 +494,7 @@ var Router = class {
|
|
|
859
494
|
let args;
|
|
860
495
|
let value;
|
|
861
496
|
switch (trade.tradeType) {
|
|
862
|
-
case
|
|
497
|
+
case TradeType2.EXACT_INPUT:
|
|
863
498
|
if (etherIn) {
|
|
864
499
|
methodName = useFeeOnTransfer ? "swapExactETHForTokensSupportingFeeOnTransferTokens" : "swapExactETHForTokens";
|
|
865
500
|
args = [amountOut, path, to, deadline];
|
|
@@ -874,8 +509,8 @@ var Router = class {
|
|
|
874
509
|
value = ZERO_HEX;
|
|
875
510
|
}
|
|
876
511
|
break;
|
|
877
|
-
case
|
|
878
|
-
|
|
512
|
+
case TradeType2.EXACT_OUTPUT:
|
|
513
|
+
invariant6(!useFeeOnTransfer, "EXACT_OUT_FOT");
|
|
879
514
|
if (etherIn) {
|
|
880
515
|
methodName = "swapETHForExactTokens";
|
|
881
516
|
args = [amountOut, path, to, deadline];
|
|
@@ -898,48 +533,27 @@ var Router = class {
|
|
|
898
533
|
};
|
|
899
534
|
}
|
|
900
535
|
};
|
|
536
|
+
|
|
537
|
+
// src/index.ts
|
|
538
|
+
export * from "@pancakeswap/swap-sdk-core";
|
|
901
539
|
export {
|
|
902
|
-
BaseCurrency,
|
|
903
540
|
ChainId,
|
|
904
|
-
|
|
541
|
+
ERC20Token,
|
|
905
542
|
FACTORY_ADDRESS,
|
|
906
543
|
FACTORY_ADDRESS_MAP,
|
|
907
|
-
FIVE,
|
|
908
|
-
Fraction,
|
|
909
544
|
INIT_CODE_HASH,
|
|
910
545
|
INIT_CODE_HASH_MAP,
|
|
911
|
-
|
|
912
|
-
InsufficientReservesError,
|
|
913
|
-
JSBI8 as JSBI,
|
|
914
|
-
MINIMUM_LIQUIDITY,
|
|
915
|
-
MaxUint256,
|
|
546
|
+
JSBI2 as JSBI,
|
|
916
547
|
NATIVE,
|
|
917
548
|
Native,
|
|
918
|
-
NativeCurrency,
|
|
919
|
-
ONE,
|
|
920
549
|
Pair,
|
|
921
|
-
Percent,
|
|
922
|
-
Price2 as Price,
|
|
923
|
-
Rounding,
|
|
924
550
|
Route,
|
|
925
551
|
Router,
|
|
926
|
-
SOLIDITY_TYPE_MAXIMA,
|
|
927
|
-
SolidityType,
|
|
928
|
-
TEN,
|
|
929
|
-
THREE,
|
|
930
|
-
TWO,
|
|
931
|
-
Token,
|
|
932
552
|
Trade,
|
|
933
|
-
TradeType,
|
|
934
553
|
WBNB,
|
|
935
554
|
WETH9,
|
|
936
555
|
WNATIVE,
|
|
937
|
-
ZERO,
|
|
938
|
-
_100,
|
|
939
|
-
_10000,
|
|
940
|
-
_9975,
|
|
941
556
|
computePairAddress,
|
|
942
|
-
computePriceImpact,
|
|
943
557
|
inputOutputComparator,
|
|
944
558
|
tradeComparator
|
|
945
559
|
};
|