@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.js
CHANGED
|
@@ -17,74 +17,46 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
17
|
}
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
|
+
var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
|
|
20
21
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
|
|
21
22
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
22
23
|
|
|
23
24
|
// src/index.ts
|
|
24
25
|
var src_exports = {};
|
|
25
26
|
__export(src_exports, {
|
|
26
|
-
BaseCurrency: () => BaseCurrency,
|
|
27
27
|
ChainId: () => ChainId,
|
|
28
|
-
|
|
28
|
+
ERC20Token: () => ERC20Token,
|
|
29
29
|
FACTORY_ADDRESS: () => FACTORY_ADDRESS,
|
|
30
30
|
FACTORY_ADDRESS_MAP: () => FACTORY_ADDRESS_MAP,
|
|
31
|
-
FIVE: () => FIVE,
|
|
32
|
-
Fraction: () => Fraction,
|
|
33
31
|
INIT_CODE_HASH: () => INIT_CODE_HASH,
|
|
34
32
|
INIT_CODE_HASH_MAP: () => INIT_CODE_HASH_MAP,
|
|
35
|
-
|
|
36
|
-
InsufficientReservesError: () => InsufficientReservesError,
|
|
37
|
-
JSBI: () => import_jsbi8.default,
|
|
38
|
-
MINIMUM_LIQUIDITY: () => MINIMUM_LIQUIDITY,
|
|
39
|
-
MaxUint256: () => MaxUint256,
|
|
33
|
+
JSBI: () => import_jsbi3.default,
|
|
40
34
|
NATIVE: () => NATIVE,
|
|
41
35
|
Native: () => Native,
|
|
42
|
-
NativeCurrency: () => NativeCurrency,
|
|
43
|
-
ONE: () => ONE,
|
|
44
36
|
Pair: () => Pair,
|
|
45
|
-
Percent: () => Percent,
|
|
46
|
-
Price: () => Price2,
|
|
47
|
-
Rounding: () => Rounding,
|
|
48
37
|
Route: () => Route,
|
|
49
38
|
Router: () => Router,
|
|
50
|
-
SOLIDITY_TYPE_MAXIMA: () => SOLIDITY_TYPE_MAXIMA,
|
|
51
|
-
SolidityType: () => SolidityType,
|
|
52
|
-
TEN: () => TEN,
|
|
53
|
-
THREE: () => THREE,
|
|
54
|
-
TWO: () => TWO,
|
|
55
|
-
Token: () => Token,
|
|
56
39
|
Trade: () => Trade,
|
|
57
|
-
TradeType: () => TradeType,
|
|
58
40
|
WBNB: () => WBNB,
|
|
59
41
|
WETH9: () => WETH9,
|
|
60
42
|
WNATIVE: () => WNATIVE,
|
|
61
|
-
ZERO: () => ZERO,
|
|
62
|
-
_100: () => _100,
|
|
63
|
-
_10000: () => _10000,
|
|
64
|
-
_9975: () => _9975,
|
|
65
43
|
computePairAddress: () => computePairAddress,
|
|
66
44
|
computePriceImpact: () => computePriceImpact,
|
|
67
45
|
inputOutputComparator: () => inputOutputComparator,
|
|
68
46
|
tradeComparator: () => tradeComparator
|
|
69
47
|
});
|
|
70
48
|
module.exports = __toCommonJS(src_exports);
|
|
71
|
-
var
|
|
72
|
-
|
|
73
|
-
// src/constants.ts
|
|
74
|
-
var import_jsbi2 = __toESM(require("jsbi"));
|
|
49
|
+
var import_jsbi3 = __toESM(require("jsbi"));
|
|
75
50
|
|
|
76
51
|
// src/entities/token.ts
|
|
77
|
-
var
|
|
52
|
+
var import_swap_sdk_core2 = require("@pancakeswap/swap-sdk-core");
|
|
78
53
|
|
|
79
54
|
// src/utils.ts
|
|
55
|
+
var import_address = require("@ethersproject/address");
|
|
56
|
+
var import_jsbi = __toESM(require("jsbi"));
|
|
80
57
|
var import_tiny_invariant = __toESM(require("tiny-invariant"));
|
|
81
58
|
var import_tiny_warning = __toESM(require("tiny-warning"));
|
|
82
|
-
var
|
|
83
|
-
var import_address = require("@ethersproject/address");
|
|
84
|
-
function validateSolidityTypeInstance(value, solidityType) {
|
|
85
|
-
(0, import_tiny_invariant.default)(import_jsbi.default.greaterThanOrEqual(value, ZERO), `${value} is not a ${solidityType}.`);
|
|
86
|
-
(0, import_tiny_invariant.default)(import_jsbi.default.lessThanOrEqual(value, SOLIDITY_TYPE_MAXIMA[solidityType]), `${value} is not a ${solidityType}.`);
|
|
87
|
-
}
|
|
59
|
+
var import_swap_sdk_core = require("@pancakeswap/swap-sdk-core");
|
|
88
60
|
function validateAndParseAddress(address) {
|
|
89
61
|
try {
|
|
90
62
|
const checksummedAddress = (0, import_address.getAddress)(address);
|
|
@@ -94,22 +66,6 @@ function validateAndParseAddress(address) {
|
|
|
94
66
|
(0, import_tiny_invariant.default)(false, `${address} is not a valid address.`);
|
|
95
67
|
}
|
|
96
68
|
}
|
|
97
|
-
function sqrt(y) {
|
|
98
|
-
validateSolidityTypeInstance(y, "uint256" /* uint256 */);
|
|
99
|
-
let z = ZERO;
|
|
100
|
-
let x;
|
|
101
|
-
if (import_jsbi.default.greaterThan(y, THREE)) {
|
|
102
|
-
z = y;
|
|
103
|
-
x = import_jsbi.default.add(import_jsbi.default.divide(y, TWO), ONE);
|
|
104
|
-
while (import_jsbi.default.lessThan(x, z)) {
|
|
105
|
-
z = x;
|
|
106
|
-
x = import_jsbi.default.divide(import_jsbi.default.add(import_jsbi.default.divide(y, x), x), TWO);
|
|
107
|
-
}
|
|
108
|
-
} else if (import_jsbi.default.notEqual(y, ZERO)) {
|
|
109
|
-
z = ONE;
|
|
110
|
-
}
|
|
111
|
-
return z;
|
|
112
|
-
}
|
|
113
69
|
function sortedInsert(items, add, maxSize, comparator) {
|
|
114
70
|
(0, import_tiny_invariant.default)(maxSize > 0, "MAX_SIZE_ZERO");
|
|
115
71
|
(0, import_tiny_invariant.default)(items.length <= maxSize, "ITEMS_SIZE");
|
|
@@ -137,51 +93,13 @@ function sortedInsert(items, add, maxSize, comparator) {
|
|
|
137
93
|
function computePriceImpact(midPrice, inputAmount, outputAmount) {
|
|
138
94
|
const quotedOutputAmount = midPrice.quote(inputAmount);
|
|
139
95
|
const priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount);
|
|
140
|
-
return new Percent(priceImpact.numerator, priceImpact.denominator);
|
|
96
|
+
return new import_swap_sdk_core.Percent(priceImpact.numerator, priceImpact.denominator);
|
|
141
97
|
}
|
|
142
98
|
|
|
143
|
-
// src/entities/baseCurrency.ts
|
|
144
|
-
var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
|
|
145
|
-
var BaseCurrency = class {
|
|
146
|
-
constructor(chainId, decimals, symbol, name) {
|
|
147
|
-
(0, import_tiny_invariant2.default)(Number.isSafeInteger(chainId), "CHAIN_ID");
|
|
148
|
-
(0, import_tiny_invariant2.default)(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
|
|
149
|
-
this.chainId = chainId;
|
|
150
|
-
this.decimals = decimals;
|
|
151
|
-
this.symbol = symbol;
|
|
152
|
-
this.name = name;
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
|
|
156
99
|
// src/entities/token.ts
|
|
157
|
-
var
|
|
100
|
+
var ERC20Token = class extends import_swap_sdk_core2.Token {
|
|
158
101
|
constructor(chainId, address, decimals, symbol, name, projectLink) {
|
|
159
|
-
super(chainId, decimals, symbol, name);
|
|
160
|
-
this.isNative = false;
|
|
161
|
-
this.isToken = true;
|
|
162
|
-
this.address = validateAndParseAddress(address);
|
|
163
|
-
this.projectLink = projectLink;
|
|
164
|
-
}
|
|
165
|
-
equals(other) {
|
|
166
|
-
return other.isToken && this.chainId === other.chainId && this.address === other.address;
|
|
167
|
-
}
|
|
168
|
-
sortsBefore(other) {
|
|
169
|
-
(0, import_tiny_invariant3.default)(this.chainId === other.chainId, "CHAIN_IDS");
|
|
170
|
-
(0, import_tiny_invariant3.default)(this.address !== other.address, "ADDRESSES");
|
|
171
|
-
return this.address.toLowerCase() < other.address.toLowerCase();
|
|
172
|
-
}
|
|
173
|
-
get wrapped() {
|
|
174
|
-
return this;
|
|
175
|
-
}
|
|
176
|
-
get serialize() {
|
|
177
|
-
return {
|
|
178
|
-
address: this.address,
|
|
179
|
-
chainId: this.chainId,
|
|
180
|
-
decimals: this.decimals,
|
|
181
|
-
symbol: this.symbol,
|
|
182
|
-
name: this.name,
|
|
183
|
-
projectLink: this.projectLink
|
|
184
|
-
};
|
|
102
|
+
super(chainId, validateAndParseAddress(address), decimals, symbol, name, projectLink);
|
|
185
103
|
}
|
|
186
104
|
};
|
|
187
105
|
|
|
@@ -194,17 +112,6 @@ var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
|
194
112
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
195
113
|
return ChainId2;
|
|
196
114
|
})(ChainId || {});
|
|
197
|
-
var TradeType = /* @__PURE__ */ ((TradeType2) => {
|
|
198
|
-
TradeType2[TradeType2["EXACT_INPUT"] = 0] = "EXACT_INPUT";
|
|
199
|
-
TradeType2[TradeType2["EXACT_OUTPUT"] = 1] = "EXACT_OUTPUT";
|
|
200
|
-
return TradeType2;
|
|
201
|
-
})(TradeType || {});
|
|
202
|
-
var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
203
|
-
Rounding2[Rounding2["ROUND_DOWN"] = 0] = "ROUND_DOWN";
|
|
204
|
-
Rounding2[Rounding2["ROUND_HALF_UP"] = 1] = "ROUND_HALF_UP";
|
|
205
|
-
Rounding2[Rounding2["ROUND_UP"] = 2] = "ROUND_UP";
|
|
206
|
-
return Rounding2;
|
|
207
|
-
})(Rounding || {});
|
|
208
115
|
var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
209
116
|
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
210
117
|
var FACTORY_ADDRESS_MAP = {
|
|
@@ -223,35 +130,15 @@ var INIT_CODE_HASH_MAP = {
|
|
|
223
130
|
[56 /* BSC */]: INIT_CODE_HASH,
|
|
224
131
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
225
132
|
};
|
|
226
|
-
var MINIMUM_LIQUIDITY = import_jsbi2.default.BigInt(1e3);
|
|
227
|
-
var ZERO = import_jsbi2.default.BigInt(0);
|
|
228
|
-
var ONE = import_jsbi2.default.BigInt(1);
|
|
229
|
-
var TWO = import_jsbi2.default.BigInt(2);
|
|
230
|
-
var THREE = import_jsbi2.default.BigInt(3);
|
|
231
|
-
var FIVE = import_jsbi2.default.BigInt(5);
|
|
232
|
-
var TEN = import_jsbi2.default.BigInt(10);
|
|
233
|
-
var _100 = import_jsbi2.default.BigInt(100);
|
|
234
|
-
var _9975 = import_jsbi2.default.BigInt(9975);
|
|
235
|
-
var _10000 = import_jsbi2.default.BigInt(1e4);
|
|
236
|
-
var MaxUint256 = import_jsbi2.default.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
237
|
-
var SolidityType = /* @__PURE__ */ ((SolidityType2) => {
|
|
238
|
-
SolidityType2["uint8"] = "uint8";
|
|
239
|
-
SolidityType2["uint256"] = "uint256";
|
|
240
|
-
return SolidityType2;
|
|
241
|
-
})(SolidityType || {});
|
|
242
|
-
var SOLIDITY_TYPE_MAXIMA = {
|
|
243
|
-
["uint8" /* uint8 */]: import_jsbi2.default.BigInt("0xff"),
|
|
244
|
-
["uint256" /* uint256 */]: import_jsbi2.default.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
|
245
|
-
};
|
|
246
133
|
var WETH9 = {
|
|
247
|
-
[1 /* ETHEREUM */]: new
|
|
248
|
-
[4 /* RINKEBY */]: new
|
|
249
|
-
[5 /* GOERLI */]: new
|
|
134
|
+
[1 /* ETHEREUM */]: new ERC20Token(1 /* ETHEREUM */, "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", 18, "WETH", "Wrapped Ether", "https://weth.io"),
|
|
135
|
+
[4 /* RINKEBY */]: new ERC20Token(4 /* RINKEBY */, "0xc778417E063141139Fce010982780140Aa0cD5Ab", 18, "WETH", "Wrapped Ether", "https://weth.io"),
|
|
136
|
+
[5 /* GOERLI */]: new ERC20Token(5 /* GOERLI */, "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6", 18, "WETH", "Wrapped Ether", "https://weth.io")
|
|
250
137
|
};
|
|
251
138
|
var WBNB = {
|
|
252
|
-
[1 /* ETHEREUM */]: new
|
|
253
|
-
[56 /* BSC */]: new
|
|
254
|
-
[97 /* BSC_TESTNET */]: new
|
|
139
|
+
[1 /* ETHEREUM */]: new ERC20Token(1 /* ETHEREUM */, "0x418D75f65a02b3D53B2418FB8E1fe493759c7605", 18, "WBNB", "Wrapped BNB", "https://www.binance.org"),
|
|
140
|
+
[56 /* BSC */]: new ERC20Token(56 /* BSC */, "0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c", 18, "WBNB", "Wrapped BNB", "https://www.binance.org"),
|
|
141
|
+
[97 /* BSC_TESTNET */]: new ERC20Token(97 /* BSC_TESTNET */, "0xae13d989daC2f0dEbFf460aC112a837C89BAa7cd", 18, "WBNB", "Wrapped BNB", "https://www.binance.org")
|
|
255
142
|
};
|
|
256
143
|
var WNATIVE = {
|
|
257
144
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
@@ -276,261 +163,12 @@ var NATIVE = {
|
|
|
276
163
|
}
|
|
277
164
|
};
|
|
278
165
|
|
|
279
|
-
// src/errors.ts
|
|
280
|
-
var CAN_SET_PROTOTYPE = "setPrototypeOf" in Object;
|
|
281
|
-
var InsufficientReservesError = class extends Error {
|
|
282
|
-
constructor() {
|
|
283
|
-
super();
|
|
284
|
-
this.isInsufficientReservesError = true;
|
|
285
|
-
this.name = this.constructor.name;
|
|
286
|
-
if (CAN_SET_PROTOTYPE)
|
|
287
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
288
|
-
}
|
|
289
|
-
};
|
|
290
|
-
var InsufficientInputAmountError = class extends Error {
|
|
291
|
-
constructor() {
|
|
292
|
-
super();
|
|
293
|
-
this.isInsufficientInputAmountError = true;
|
|
294
|
-
this.name = this.constructor.name;
|
|
295
|
-
if (CAN_SET_PROTOTYPE)
|
|
296
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
297
|
-
}
|
|
298
|
-
};
|
|
299
|
-
|
|
300
166
|
// src/entities/pair.ts
|
|
167
|
+
var import_swap_sdk_core3 = require("@pancakeswap/swap-sdk-core");
|
|
301
168
|
var import_address2 = require("@ethersproject/address");
|
|
302
169
|
var import_solidity = require("@ethersproject/solidity");
|
|
303
|
-
var
|
|
304
|
-
var
|
|
305
|
-
|
|
306
|
-
// src/entities/fractions/price.ts
|
|
307
|
-
var import_jsbi5 = __toESM(require("jsbi"));
|
|
308
|
-
var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
|
|
309
|
-
|
|
310
|
-
// src/entities/fractions/fraction.ts
|
|
311
|
-
var import_jsbi3 = __toESM(require("jsbi"));
|
|
312
|
-
var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
|
|
313
|
-
var import_decimal = __toESM(require("decimal.js-light"));
|
|
314
|
-
var import_big = __toESM(require("big.js"));
|
|
315
|
-
var import_toformat = __toESM(require("toformat"));
|
|
316
|
-
var Decimal = (0, import_toformat.default)(import_decimal.default);
|
|
317
|
-
var Big = (0, import_toformat.default)(import_big.default);
|
|
318
|
-
var toSignificantRounding = {
|
|
319
|
-
[0 /* ROUND_DOWN */]: Decimal.ROUND_DOWN,
|
|
320
|
-
[1 /* ROUND_HALF_UP */]: Decimal.ROUND_HALF_UP,
|
|
321
|
-
[2 /* ROUND_UP */]: Decimal.ROUND_UP
|
|
322
|
-
};
|
|
323
|
-
var toFixedRounding = {
|
|
324
|
-
[0 /* ROUND_DOWN */]: 0 /* RoundDown */,
|
|
325
|
-
[1 /* ROUND_HALF_UP */]: 1 /* RoundHalfUp */,
|
|
326
|
-
[2 /* ROUND_UP */]: 3 /* RoundUp */
|
|
327
|
-
};
|
|
328
|
-
var Fraction = class {
|
|
329
|
-
constructor(numerator, denominator = import_jsbi3.default.BigInt(1)) {
|
|
330
|
-
this.numerator = import_jsbi3.default.BigInt(numerator);
|
|
331
|
-
this.denominator = import_jsbi3.default.BigInt(denominator);
|
|
332
|
-
}
|
|
333
|
-
static tryParseFraction(fractionish) {
|
|
334
|
-
if (fractionish instanceof import_jsbi3.default || typeof fractionish === "number" || typeof fractionish === "string")
|
|
335
|
-
return new Fraction(fractionish);
|
|
336
|
-
if ("numerator" in fractionish && "denominator" in fractionish)
|
|
337
|
-
return fractionish;
|
|
338
|
-
throw new Error("Could not parse fraction");
|
|
339
|
-
}
|
|
340
|
-
get quotient() {
|
|
341
|
-
return import_jsbi3.default.divide(this.numerator, this.denominator);
|
|
342
|
-
}
|
|
343
|
-
get remainder() {
|
|
344
|
-
return new Fraction(import_jsbi3.default.remainder(this.numerator, this.denominator), this.denominator);
|
|
345
|
-
}
|
|
346
|
-
invert() {
|
|
347
|
-
return new Fraction(this.denominator, this.numerator);
|
|
348
|
-
}
|
|
349
|
-
add(other) {
|
|
350
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
351
|
-
if (import_jsbi3.default.equal(this.denominator, otherParsed.denominator)) {
|
|
352
|
-
return new Fraction(import_jsbi3.default.add(this.numerator, otherParsed.numerator), this.denominator);
|
|
353
|
-
}
|
|
354
|
-
return new Fraction(import_jsbi3.default.add(import_jsbi3.default.multiply(this.numerator, otherParsed.denominator), import_jsbi3.default.multiply(otherParsed.numerator, this.denominator)), import_jsbi3.default.multiply(this.denominator, otherParsed.denominator));
|
|
355
|
-
}
|
|
356
|
-
subtract(other) {
|
|
357
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
358
|
-
if (import_jsbi3.default.equal(this.denominator, otherParsed.denominator)) {
|
|
359
|
-
return new Fraction(import_jsbi3.default.subtract(this.numerator, otherParsed.numerator), this.denominator);
|
|
360
|
-
}
|
|
361
|
-
return new Fraction(import_jsbi3.default.subtract(import_jsbi3.default.multiply(this.numerator, otherParsed.denominator), import_jsbi3.default.multiply(otherParsed.numerator, this.denominator)), import_jsbi3.default.multiply(this.denominator, otherParsed.denominator));
|
|
362
|
-
}
|
|
363
|
-
lessThan(other) {
|
|
364
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
365
|
-
return import_jsbi3.default.lessThan(import_jsbi3.default.multiply(this.numerator, otherParsed.denominator), import_jsbi3.default.multiply(otherParsed.numerator, this.denominator));
|
|
366
|
-
}
|
|
367
|
-
equalTo(other) {
|
|
368
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
369
|
-
return import_jsbi3.default.equal(import_jsbi3.default.multiply(this.numerator, otherParsed.denominator), import_jsbi3.default.multiply(otherParsed.numerator, this.denominator));
|
|
370
|
-
}
|
|
371
|
-
greaterThan(other) {
|
|
372
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
373
|
-
return import_jsbi3.default.greaterThan(import_jsbi3.default.multiply(this.numerator, otherParsed.denominator), import_jsbi3.default.multiply(otherParsed.numerator, this.denominator));
|
|
374
|
-
}
|
|
375
|
-
multiply(other) {
|
|
376
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
377
|
-
return new Fraction(import_jsbi3.default.multiply(this.numerator, otherParsed.numerator), import_jsbi3.default.multiply(this.denominator, otherParsed.denominator));
|
|
378
|
-
}
|
|
379
|
-
divide(other) {
|
|
380
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
381
|
-
return new Fraction(import_jsbi3.default.multiply(this.numerator, otherParsed.denominator), import_jsbi3.default.multiply(this.denominator, otherParsed.numerator));
|
|
382
|
-
}
|
|
383
|
-
toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
384
|
-
(0, import_tiny_invariant4.default)(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
|
|
385
|
-
(0, import_tiny_invariant4.default)(significantDigits > 0, `${significantDigits} is not positive.`);
|
|
386
|
-
Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });
|
|
387
|
-
const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
|
|
388
|
-
return quotient.toFormat(quotient.decimalPlaces(), format);
|
|
389
|
-
}
|
|
390
|
-
toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
391
|
-
(0, import_tiny_invariant4.default)(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
|
|
392
|
-
(0, import_tiny_invariant4.default)(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
|
|
393
|
-
Big.DP = decimalPlaces;
|
|
394
|
-
Big.RM = toFixedRounding[rounding];
|
|
395
|
-
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
|
|
396
|
-
}
|
|
397
|
-
get asFraction() {
|
|
398
|
-
return new Fraction(this.numerator, this.denominator);
|
|
399
|
-
}
|
|
400
|
-
};
|
|
401
|
-
|
|
402
|
-
// src/entities/fractions/currencyAmount.ts
|
|
403
|
-
var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
|
|
404
|
-
var import_jsbi4 = __toESM(require("jsbi"));
|
|
405
|
-
var import_big2 = __toESM(require("big.js"));
|
|
406
|
-
var import_toformat2 = __toESM(require("toformat"));
|
|
407
|
-
var Big2 = (0, import_toformat2.default)(import_big2.default);
|
|
408
|
-
var CurrencyAmount2 = class extends Fraction {
|
|
409
|
-
constructor(currency, numerator, denominator) {
|
|
410
|
-
super(numerator, denominator);
|
|
411
|
-
(0, import_tiny_invariant5.default)(import_jsbi4.default.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT");
|
|
412
|
-
this.currency = currency;
|
|
413
|
-
this.decimalScale = import_jsbi4.default.exponentiate(import_jsbi4.default.BigInt(10), import_jsbi4.default.BigInt(currency.decimals));
|
|
414
|
-
}
|
|
415
|
-
static fromRawAmount(currency, rawAmount) {
|
|
416
|
-
return new CurrencyAmount2(currency, rawAmount);
|
|
417
|
-
}
|
|
418
|
-
static fromFractionalAmount(currency, numerator, denominator) {
|
|
419
|
-
return new CurrencyAmount2(currency, numerator, denominator);
|
|
420
|
-
}
|
|
421
|
-
add(other) {
|
|
422
|
-
(0, import_tiny_invariant5.default)(this.currency.equals(other.currency), "CURRENCY");
|
|
423
|
-
const added = super.add(other);
|
|
424
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, added.numerator, added.denominator);
|
|
425
|
-
}
|
|
426
|
-
subtract(other) {
|
|
427
|
-
(0, import_tiny_invariant5.default)(this.currency.equals(other.currency), "CURRENCY");
|
|
428
|
-
const subtracted = super.subtract(other);
|
|
429
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
|
|
430
|
-
}
|
|
431
|
-
multiply(other) {
|
|
432
|
-
const multiplied = super.multiply(other);
|
|
433
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
|
|
434
|
-
}
|
|
435
|
-
divide(other) {
|
|
436
|
-
const divided = super.divide(other);
|
|
437
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
|
|
438
|
-
}
|
|
439
|
-
toSignificant(significantDigits = 6, format, rounding = 0 /* ROUND_DOWN */) {
|
|
440
|
-
return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
|
|
441
|
-
}
|
|
442
|
-
toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
|
|
443
|
-
(0, import_tiny_invariant5.default)(decimalPlaces <= this.currency.decimals, "DECIMALS");
|
|
444
|
-
return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
|
|
445
|
-
}
|
|
446
|
-
toExact(format = { groupSeparator: "" }) {
|
|
447
|
-
Big2.DP = this.currency.decimals;
|
|
448
|
-
return new Big2(this.quotient.toString()).div(this.decimalScale.toString()).toFormat(format);
|
|
449
|
-
}
|
|
450
|
-
get wrapped() {
|
|
451
|
-
if (this.currency.isToken)
|
|
452
|
-
return this;
|
|
453
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator);
|
|
454
|
-
}
|
|
455
|
-
};
|
|
456
|
-
|
|
457
|
-
// src/entities/fractions/price.ts
|
|
458
|
-
var Price2 = class extends Fraction {
|
|
459
|
-
constructor(...args) {
|
|
460
|
-
let baseCurrency, quoteCurrency, denominator, numerator;
|
|
461
|
-
if (args.length === 4) {
|
|
462
|
-
;
|
|
463
|
-
[baseCurrency, quoteCurrency, denominator, numerator] = args;
|
|
464
|
-
} else {
|
|
465
|
-
const result = args[0].quoteAmount.divide(args[0].baseAmount);
|
|
466
|
-
[baseCurrency, quoteCurrency, denominator, numerator] = [
|
|
467
|
-
args[0].baseAmount.currency,
|
|
468
|
-
args[0].quoteAmount.currency,
|
|
469
|
-
result.denominator,
|
|
470
|
-
result.numerator
|
|
471
|
-
];
|
|
472
|
-
}
|
|
473
|
-
super(numerator, denominator);
|
|
474
|
-
this.baseCurrency = baseCurrency;
|
|
475
|
-
this.quoteCurrency = quoteCurrency;
|
|
476
|
-
this.scalar = new Fraction(import_jsbi5.default.exponentiate(import_jsbi5.default.BigInt(10), import_jsbi5.default.BigInt(baseCurrency.decimals)), import_jsbi5.default.exponentiate(import_jsbi5.default.BigInt(10), import_jsbi5.default.BigInt(quoteCurrency.decimals)));
|
|
477
|
-
}
|
|
478
|
-
invert() {
|
|
479
|
-
return new Price2(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
|
|
480
|
-
}
|
|
481
|
-
multiply(other) {
|
|
482
|
-
(0, import_tiny_invariant6.default)(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
|
|
483
|
-
const fraction = super.multiply(other);
|
|
484
|
-
return new Price2(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
|
|
485
|
-
}
|
|
486
|
-
quote(currencyAmount) {
|
|
487
|
-
(0, import_tiny_invariant6.default)(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
|
|
488
|
-
const result = super.multiply(currencyAmount);
|
|
489
|
-
return CurrencyAmount2.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
|
|
490
|
-
}
|
|
491
|
-
get adjustedForDecimals() {
|
|
492
|
-
return super.multiply(this.scalar);
|
|
493
|
-
}
|
|
494
|
-
toSignificant(significantDigits = 6, format, rounding) {
|
|
495
|
-
return this.adjustedForDecimals.toSignificant(significantDigits, format, rounding);
|
|
496
|
-
}
|
|
497
|
-
toFixed(decimalPlaces = 4, format, rounding) {
|
|
498
|
-
return this.adjustedForDecimals.toFixed(decimalPlaces, format, rounding);
|
|
499
|
-
}
|
|
500
|
-
};
|
|
501
|
-
|
|
502
|
-
// src/entities/fractions/percent.ts
|
|
503
|
-
var import_jsbi6 = __toESM(require("jsbi"));
|
|
504
|
-
var ONE_HUNDRED = new Fraction(import_jsbi6.default.BigInt(100));
|
|
505
|
-
function toPercent(fraction) {
|
|
506
|
-
return new Percent(fraction.numerator, fraction.denominator);
|
|
507
|
-
}
|
|
508
|
-
var Percent = class extends Fraction {
|
|
509
|
-
constructor() {
|
|
510
|
-
super(...arguments);
|
|
511
|
-
this.isPercent = true;
|
|
512
|
-
}
|
|
513
|
-
add(other) {
|
|
514
|
-
return toPercent(super.add(other));
|
|
515
|
-
}
|
|
516
|
-
subtract(other) {
|
|
517
|
-
return toPercent(super.subtract(other));
|
|
518
|
-
}
|
|
519
|
-
multiply(other) {
|
|
520
|
-
return toPercent(super.multiply(other));
|
|
521
|
-
}
|
|
522
|
-
divide(other) {
|
|
523
|
-
return toPercent(super.divide(other));
|
|
524
|
-
}
|
|
525
|
-
toSignificant(significantDigits = 5, format, rounding) {
|
|
526
|
-
return super.multiply(ONE_HUNDRED).toSignificant(significantDigits, format, rounding);
|
|
527
|
-
}
|
|
528
|
-
toFixed(decimalPlaces = 2, format, rounding) {
|
|
529
|
-
return super.multiply(ONE_HUNDRED).toFixed(decimalPlaces, format, rounding);
|
|
530
|
-
}
|
|
531
|
-
};
|
|
532
|
-
|
|
533
|
-
// src/entities/pair.ts
|
|
170
|
+
var import_jsbi2 = __toESM(require("jsbi"));
|
|
171
|
+
var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
|
|
534
172
|
var PAIR_ADDRESS_CACHE = {};
|
|
535
173
|
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
536
174
|
var computePairAddress = ({
|
|
@@ -554,7 +192,7 @@ var Pair = class {
|
|
|
554
192
|
}
|
|
555
193
|
constructor(currencyAmountA, tokenAmountB) {
|
|
556
194
|
const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) ? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
|
|
557
|
-
this.liquidityToken = new
|
|
195
|
+
this.liquidityToken = new ERC20Token(tokenAmounts[0].currency.chainId, Pair.getAddress(tokenAmounts[0].currency, tokenAmounts[1].currency), 18, "Cake-LP", "Pancake LPs");
|
|
558
196
|
this.tokenAmounts = tokenAmounts;
|
|
559
197
|
}
|
|
560
198
|
involvesToken(token) {
|
|
@@ -562,14 +200,14 @@ var Pair = class {
|
|
|
562
200
|
}
|
|
563
201
|
get token0Price() {
|
|
564
202
|
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
565
|
-
return new
|
|
203
|
+
return new import_swap_sdk_core3.Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
566
204
|
}
|
|
567
205
|
get token1Price() {
|
|
568
206
|
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
569
|
-
return new
|
|
207
|
+
return new import_swap_sdk_core3.Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
570
208
|
}
|
|
571
209
|
priceOf(token) {
|
|
572
|
-
(0,
|
|
210
|
+
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
573
211
|
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
574
212
|
}
|
|
575
213
|
get chainId() {
|
|
@@ -588,73 +226,73 @@ var Pair = class {
|
|
|
588
226
|
return this.tokenAmounts[1];
|
|
589
227
|
}
|
|
590
228
|
reserveOf(token) {
|
|
591
|
-
(0,
|
|
229
|
+
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
592
230
|
return token.equals(this.token0) ? this.reserve0 : this.reserve1;
|
|
593
231
|
}
|
|
594
232
|
getOutputAmount(inputAmount) {
|
|
595
|
-
(0,
|
|
596
|
-
if (
|
|
597
|
-
throw new InsufficientReservesError();
|
|
233
|
+
(0, import_tiny_invariant2.default)(this.involvesToken(inputAmount.currency), "TOKEN");
|
|
234
|
+
if (import_jsbi2.default.equal(this.reserve0.quotient, import_swap_sdk_core3.ZERO) || import_jsbi2.default.equal(this.reserve1.quotient, import_swap_sdk_core3.ZERO)) {
|
|
235
|
+
throw new import_swap_sdk_core3.InsufficientReservesError();
|
|
598
236
|
}
|
|
599
237
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
600
238
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
601
|
-
const inputAmountWithFee =
|
|
602
|
-
const numerator =
|
|
603
|
-
const denominator =
|
|
604
|
-
const outputAmount =
|
|
605
|
-
if (
|
|
606
|
-
throw new InsufficientInputAmountError();
|
|
239
|
+
const inputAmountWithFee = import_jsbi2.default.multiply(inputAmount.quotient, import_swap_sdk_core3._9975);
|
|
240
|
+
const numerator = import_jsbi2.default.multiply(inputAmountWithFee, outputReserve.quotient);
|
|
241
|
+
const denominator = import_jsbi2.default.add(import_jsbi2.default.multiply(inputReserve.quotient, import_swap_sdk_core3._10000), inputAmountWithFee);
|
|
242
|
+
const outputAmount = import_swap_sdk_core3.CurrencyAmount.fromRawAmount(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0, import_jsbi2.default.divide(numerator, denominator));
|
|
243
|
+
if (import_jsbi2.default.equal(outputAmount.quotient, import_swap_sdk_core3.ZERO)) {
|
|
244
|
+
throw new import_swap_sdk_core3.InsufficientInputAmountError();
|
|
607
245
|
}
|
|
608
246
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
609
247
|
}
|
|
610
248
|
getInputAmount(outputAmount) {
|
|
611
|
-
(0,
|
|
612
|
-
if (
|
|
613
|
-
throw new InsufficientReservesError();
|
|
249
|
+
(0, import_tiny_invariant2.default)(this.involvesToken(outputAmount.currency), "TOKEN");
|
|
250
|
+
if (import_jsbi2.default.equal(this.reserve0.quotient, import_swap_sdk_core3.ZERO) || import_jsbi2.default.equal(this.reserve1.quotient, import_swap_sdk_core3.ZERO) || import_jsbi2.default.greaterThanOrEqual(outputAmount.quotient, this.reserveOf(outputAmount.currency).quotient)) {
|
|
251
|
+
throw new import_swap_sdk_core3.InsufficientReservesError();
|
|
614
252
|
}
|
|
615
253
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
616
254
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
617
|
-
const numerator =
|
|
618
|
-
const denominator =
|
|
619
|
-
const inputAmount =
|
|
255
|
+
const numerator = import_jsbi2.default.multiply(import_jsbi2.default.multiply(inputReserve.quotient, outputAmount.quotient), import_swap_sdk_core3._10000);
|
|
256
|
+
const denominator = import_jsbi2.default.multiply(import_jsbi2.default.subtract(outputReserve.quotient, outputAmount.quotient), import_swap_sdk_core3._9975);
|
|
257
|
+
const inputAmount = import_swap_sdk_core3.CurrencyAmount.fromRawAmount(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0, import_jsbi2.default.add(import_jsbi2.default.divide(numerator, denominator), import_swap_sdk_core3.ONE));
|
|
620
258
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
621
259
|
}
|
|
622
260
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
623
|
-
(0,
|
|
261
|
+
(0, import_tiny_invariant2.default)(totalSupply.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
624
262
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
625
|
-
(0,
|
|
263
|
+
(0, import_tiny_invariant2.default)(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
626
264
|
let liquidity;
|
|
627
|
-
if (
|
|
628
|
-
liquidity =
|
|
265
|
+
if (import_jsbi2.default.equal(totalSupply.quotient, import_swap_sdk_core3.ZERO)) {
|
|
266
|
+
liquidity = import_jsbi2.default.subtract((0, import_swap_sdk_core3.sqrt)(import_jsbi2.default.multiply(tokenAmounts[0].quotient, tokenAmounts[1].quotient)), import_swap_sdk_core3.MINIMUM_LIQUIDITY);
|
|
629
267
|
} else {
|
|
630
|
-
const amount0 =
|
|
631
|
-
const amount1 =
|
|
632
|
-
liquidity =
|
|
268
|
+
const amount0 = import_jsbi2.default.divide(import_jsbi2.default.multiply(tokenAmounts[0].quotient, totalSupply.quotient), this.reserve0.quotient);
|
|
269
|
+
const amount1 = import_jsbi2.default.divide(import_jsbi2.default.multiply(tokenAmounts[1].quotient, totalSupply.quotient), this.reserve1.quotient);
|
|
270
|
+
liquidity = import_jsbi2.default.lessThanOrEqual(amount0, amount1) ? amount0 : amount1;
|
|
633
271
|
}
|
|
634
|
-
if (!
|
|
635
|
-
throw new InsufficientInputAmountError();
|
|
272
|
+
if (!import_jsbi2.default.greaterThan(liquidity, import_swap_sdk_core3.ZERO)) {
|
|
273
|
+
throw new import_swap_sdk_core3.InsufficientInputAmountError();
|
|
636
274
|
}
|
|
637
|
-
return
|
|
275
|
+
return import_swap_sdk_core3.CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
638
276
|
}
|
|
639
277
|
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
640
|
-
(0,
|
|
641
|
-
(0,
|
|
642
|
-
(0,
|
|
643
|
-
(0,
|
|
278
|
+
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
279
|
+
(0, import_tiny_invariant2.default)(totalSupply.currency.equals(this.liquidityToken), "TOTAL_SUPPLY");
|
|
280
|
+
(0, import_tiny_invariant2.default)(liquidity.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
281
|
+
(0, import_tiny_invariant2.default)(import_jsbi2.default.lessThanOrEqual(liquidity.quotient, totalSupply.quotient), "LIQUIDITY");
|
|
644
282
|
let totalSupplyAdjusted;
|
|
645
283
|
if (!feeOn) {
|
|
646
284
|
totalSupplyAdjusted = totalSupply;
|
|
647
285
|
} else {
|
|
648
|
-
(0,
|
|
649
|
-
const kLastParsed =
|
|
650
|
-
if (!
|
|
651
|
-
const rootK = sqrt(
|
|
652
|
-
const rootKLast = sqrt(kLastParsed);
|
|
653
|
-
if (
|
|
654
|
-
const numerator =
|
|
655
|
-
const denominator =
|
|
656
|
-
const feeLiquidity =
|
|
657
|
-
totalSupplyAdjusted = totalSupply.add(
|
|
286
|
+
(0, import_tiny_invariant2.default)(!!kLast, "K_LAST");
|
|
287
|
+
const kLastParsed = import_jsbi2.default.BigInt(kLast);
|
|
288
|
+
if (!import_jsbi2.default.equal(kLastParsed, import_swap_sdk_core3.ZERO)) {
|
|
289
|
+
const rootK = (0, import_swap_sdk_core3.sqrt)(import_jsbi2.default.multiply(this.reserve0.quotient, this.reserve1.quotient));
|
|
290
|
+
const rootKLast = (0, import_swap_sdk_core3.sqrt)(kLastParsed);
|
|
291
|
+
if (import_jsbi2.default.greaterThan(rootK, rootKLast)) {
|
|
292
|
+
const numerator = import_jsbi2.default.multiply(totalSupply.quotient, import_jsbi2.default.subtract(rootK, rootKLast));
|
|
293
|
+
const denominator = import_jsbi2.default.add(import_jsbi2.default.multiply(rootK, import_swap_sdk_core3.FIVE), rootKLast);
|
|
294
|
+
const feeLiquidity = import_jsbi2.default.divide(numerator, denominator);
|
|
295
|
+
totalSupplyAdjusted = totalSupply.add(import_swap_sdk_core3.CurrencyAmount.fromRawAmount(this.liquidityToken, feeLiquidity));
|
|
658
296
|
} else {
|
|
659
297
|
totalSupplyAdjusted = totalSupply;
|
|
660
298
|
}
|
|
@@ -662,25 +300,26 @@ var Pair = class {
|
|
|
662
300
|
totalSupplyAdjusted = totalSupply;
|
|
663
301
|
}
|
|
664
302
|
}
|
|
665
|
-
return
|
|
303
|
+
return import_swap_sdk_core3.CurrencyAmount.fromRawAmount(token, import_jsbi2.default.divide(import_jsbi2.default.multiply(liquidity.quotient, this.reserveOf(token).quotient), totalSupplyAdjusted.quotient));
|
|
666
304
|
}
|
|
667
305
|
};
|
|
668
306
|
|
|
669
307
|
// src/entities/route.ts
|
|
670
|
-
var
|
|
308
|
+
var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
|
|
309
|
+
var import_swap_sdk_core4 = require("@pancakeswap/swap-sdk-core");
|
|
671
310
|
var Route = class {
|
|
672
311
|
constructor(pairs, input, output) {
|
|
673
312
|
this._midPrice = null;
|
|
674
|
-
(0,
|
|
313
|
+
(0, import_tiny_invariant3.default)(pairs.length > 0, "PAIRS");
|
|
675
314
|
const chainId = pairs[0].chainId;
|
|
676
|
-
(0,
|
|
315
|
+
(0, import_tiny_invariant3.default)(pairs.every((pair) => pair.chainId === chainId), "CHAIN_IDS");
|
|
677
316
|
const wrappedInput = input.wrapped;
|
|
678
|
-
(0,
|
|
679
|
-
(0,
|
|
317
|
+
(0, import_tiny_invariant3.default)(pairs[0].involvesToken(wrappedInput), "INPUT");
|
|
318
|
+
(0, import_tiny_invariant3.default)(typeof output === "undefined" || pairs[pairs.length - 1].involvesToken(output.wrapped), "OUTPUT");
|
|
680
319
|
const path = [wrappedInput];
|
|
681
320
|
for (const [i, pair] of pairs.entries()) {
|
|
682
321
|
const currentInput = path[i];
|
|
683
|
-
(0,
|
|
322
|
+
(0, import_tiny_invariant3.default)(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), "PATH");
|
|
684
323
|
const output2 = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
|
|
685
324
|
path.push(output2);
|
|
686
325
|
}
|
|
@@ -694,10 +333,10 @@ var Route = class {
|
|
|
694
333
|
return this._midPrice;
|
|
695
334
|
const prices = [];
|
|
696
335
|
for (const [i, pair] of this.pairs.entries()) {
|
|
697
|
-
prices.push(this.path[i].equals(pair.token0) ? new
|
|
336
|
+
prices.push(this.path[i].equals(pair.token0) ? new import_swap_sdk_core4.Price(pair.reserve0.currency, pair.reserve1.currency, pair.reserve0.quotient, pair.reserve1.quotient) : new import_swap_sdk_core4.Price(pair.reserve1.currency, pair.reserve0.currency, pair.reserve1.quotient, pair.reserve0.quotient));
|
|
698
337
|
}
|
|
699
338
|
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
700
|
-
return this._midPrice = new
|
|
339
|
+
return this._midPrice = new import_swap_sdk_core4.Price(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
701
340
|
}
|
|
702
341
|
get chainId() {
|
|
703
342
|
return this.pairs[0].chainId;
|
|
@@ -705,10 +344,11 @@ var Route = class {
|
|
|
705
344
|
};
|
|
706
345
|
|
|
707
346
|
// src/entities/trade.ts
|
|
708
|
-
var
|
|
347
|
+
var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
|
|
348
|
+
var import_swap_sdk_core5 = require("@pancakeswap/swap-sdk-core");
|
|
709
349
|
function inputOutputComparator(a, b) {
|
|
710
|
-
(0,
|
|
711
|
-
(0,
|
|
350
|
+
(0, import_tiny_invariant4.default)(a.inputAmount.currency.equals(b.inputAmount.currency), "INPUT_CURRENCY");
|
|
351
|
+
(0, import_tiny_invariant4.default)(a.outputAmount.currency.equals(b.outputAmount.currency), "OUTPUT_CURRENCY");
|
|
712
352
|
if (a.outputAmount.equalTo(b.outputAmount)) {
|
|
713
353
|
if (a.inputAmount.equalTo(b.inputAmount)) {
|
|
714
354
|
return 0;
|
|
@@ -740,68 +380,68 @@ function tradeComparator(a, b) {
|
|
|
740
380
|
}
|
|
741
381
|
var Trade = class {
|
|
742
382
|
static exactIn(route, amountIn) {
|
|
743
|
-
return new Trade(route, amountIn,
|
|
383
|
+
return new Trade(route, amountIn, import_swap_sdk_core5.TradeType.EXACT_INPUT);
|
|
744
384
|
}
|
|
745
385
|
static exactOut(route, amountOut) {
|
|
746
|
-
return new Trade(route, amountOut,
|
|
386
|
+
return new Trade(route, amountOut, import_swap_sdk_core5.TradeType.EXACT_OUTPUT);
|
|
747
387
|
}
|
|
748
388
|
constructor(route, amount, tradeType) {
|
|
749
389
|
this.route = route;
|
|
750
390
|
this.tradeType = tradeType;
|
|
751
391
|
const tokenAmounts = new Array(route.path.length);
|
|
752
|
-
if (tradeType ===
|
|
753
|
-
(0,
|
|
392
|
+
if (tradeType === import_swap_sdk_core5.TradeType.EXACT_INPUT) {
|
|
393
|
+
(0, import_tiny_invariant4.default)(amount.currency.equals(route.input), "INPUT");
|
|
754
394
|
tokenAmounts[0] = amount.wrapped;
|
|
755
395
|
for (let i = 0; i < route.path.length - 1; i++) {
|
|
756
396
|
const pair = route.pairs[i];
|
|
757
397
|
const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
|
|
758
398
|
tokenAmounts[i + 1] = outputAmount;
|
|
759
399
|
}
|
|
760
|
-
this.inputAmount =
|
|
761
|
-
this.outputAmount =
|
|
400
|
+
this.inputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.input, amount.numerator, amount.denominator);
|
|
401
|
+
this.outputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.output, tokenAmounts[tokenAmounts.length - 1].numerator, tokenAmounts[tokenAmounts.length - 1].denominator);
|
|
762
402
|
} else {
|
|
763
|
-
(0,
|
|
403
|
+
(0, import_tiny_invariant4.default)(amount.currency.equals(route.output), "OUTPUT");
|
|
764
404
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
765
405
|
for (let i = route.path.length - 1; i > 0; i--) {
|
|
766
406
|
const pair = route.pairs[i - 1];
|
|
767
407
|
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
768
408
|
tokenAmounts[i - 1] = inputAmount;
|
|
769
409
|
}
|
|
770
|
-
this.inputAmount =
|
|
771
|
-
this.outputAmount =
|
|
410
|
+
this.inputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.input, tokenAmounts[0].numerator, tokenAmounts[0].denominator);
|
|
411
|
+
this.outputAmount = import_swap_sdk_core5.CurrencyAmount.fromFractionalAmount(route.output, amount.numerator, amount.denominator);
|
|
772
412
|
}
|
|
773
|
-
this.executionPrice = new
|
|
413
|
+
this.executionPrice = new import_swap_sdk_core5.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
774
414
|
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
775
415
|
}
|
|
776
416
|
minimumAmountOut(slippageTolerance) {
|
|
777
|
-
(0,
|
|
778
|
-
if (this.tradeType ===
|
|
417
|
+
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(import_swap_sdk_core5.ZERO), "SLIPPAGE_TOLERANCE");
|
|
418
|
+
if (this.tradeType === import_swap_sdk_core5.TradeType.EXACT_OUTPUT) {
|
|
779
419
|
return this.outputAmount;
|
|
780
420
|
} else {
|
|
781
|
-
const slippageAdjustedAmountOut = new Fraction(ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
782
|
-
return
|
|
421
|
+
const slippageAdjustedAmountOut = new import_swap_sdk_core5.Fraction(import_swap_sdk_core5.ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
422
|
+
return import_swap_sdk_core5.CurrencyAmount.fromRawAmount(this.outputAmount.currency, slippageAdjustedAmountOut);
|
|
783
423
|
}
|
|
784
424
|
}
|
|
785
425
|
maximumAmountIn(slippageTolerance) {
|
|
786
|
-
(0,
|
|
787
|
-
if (this.tradeType ===
|
|
426
|
+
(0, import_tiny_invariant4.default)(!slippageTolerance.lessThan(import_swap_sdk_core5.ZERO), "SLIPPAGE_TOLERANCE");
|
|
427
|
+
if (this.tradeType === import_swap_sdk_core5.TradeType.EXACT_INPUT) {
|
|
788
428
|
return this.inputAmount;
|
|
789
429
|
} else {
|
|
790
|
-
const slippageAdjustedAmountIn = new Fraction(ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
791
|
-
return
|
|
430
|
+
const slippageAdjustedAmountIn = new import_swap_sdk_core5.Fraction(import_swap_sdk_core5.ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
431
|
+
return import_swap_sdk_core5.CurrencyAmount.fromRawAmount(this.inputAmount.currency, slippageAdjustedAmountIn);
|
|
792
432
|
}
|
|
793
433
|
}
|
|
794
434
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
795
|
-
(0,
|
|
796
|
-
(0,
|
|
797
|
-
(0,
|
|
435
|
+
(0, import_tiny_invariant4.default)(pairs.length > 0, "PAIRS");
|
|
436
|
+
(0, import_tiny_invariant4.default)(maxHops > 0, "MAX_HOPS");
|
|
437
|
+
(0, import_tiny_invariant4.default)(currencyAmountIn === nextAmountIn || currentPairs.length > 0, "INVALID_RECURSION");
|
|
798
438
|
const amountIn = nextAmountIn.wrapped;
|
|
799
439
|
const tokenOut = currencyOut.wrapped;
|
|
800
440
|
for (let i = 0; i < pairs.length; i++) {
|
|
801
441
|
const pair = pairs[i];
|
|
802
442
|
if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
|
|
803
443
|
continue;
|
|
804
|
-
if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO))
|
|
444
|
+
if (pair.reserve0.equalTo(import_swap_sdk_core5.ZERO) || pair.reserve1.equalTo(import_swap_sdk_core5.ZERO))
|
|
805
445
|
continue;
|
|
806
446
|
let amountOut;
|
|
807
447
|
try {
|
|
@@ -814,7 +454,7 @@ var Trade = class {
|
|
|
814
454
|
throw error;
|
|
815
455
|
}
|
|
816
456
|
if (amountOut.currency.equals(tokenOut)) {
|
|
817
|
-
sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn,
|
|
457
|
+
sortedInsert(bestTrades, new Trade(new Route([...currentPairs, pair], currencyAmountIn.currency, currencyOut), currencyAmountIn, import_swap_sdk_core5.TradeType.EXACT_INPUT), maxNumResults, tradeComparator);
|
|
818
458
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
819
459
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
820
460
|
Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
|
|
@@ -826,19 +466,19 @@ var Trade = class {
|
|
|
826
466
|
return bestTrades;
|
|
827
467
|
}
|
|
828
468
|
worstExecutionPrice(slippageTolerance) {
|
|
829
|
-
return new
|
|
469
|
+
return new import_swap_sdk_core5.Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
830
470
|
}
|
|
831
471
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
832
|
-
(0,
|
|
833
|
-
(0,
|
|
834
|
-
(0,
|
|
472
|
+
(0, import_tiny_invariant4.default)(pairs.length > 0, "PAIRS");
|
|
473
|
+
(0, import_tiny_invariant4.default)(maxHops > 0, "MAX_HOPS");
|
|
474
|
+
(0, import_tiny_invariant4.default)(currencyAmountOut === nextAmountOut || currentPairs.length > 0, "INVALID_RECURSION");
|
|
835
475
|
const amountOut = nextAmountOut.wrapped;
|
|
836
476
|
const tokenIn = currencyIn.wrapped;
|
|
837
477
|
for (let i = 0; i < pairs.length; i++) {
|
|
838
478
|
const pair = pairs[i];
|
|
839
479
|
if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
|
|
840
480
|
continue;
|
|
841
|
-
if (pair.reserve0.equalTo(ZERO) || pair.reserve1.equalTo(ZERO))
|
|
481
|
+
if (pair.reserve0.equalTo(import_swap_sdk_core5.ZERO) || pair.reserve1.equalTo(import_swap_sdk_core5.ZERO))
|
|
842
482
|
continue;
|
|
843
483
|
let amountIn;
|
|
844
484
|
try {
|
|
@@ -851,7 +491,7 @@ var Trade = class {
|
|
|
851
491
|
throw error;
|
|
852
492
|
}
|
|
853
493
|
if (amountIn.currency.equals(tokenIn)) {
|
|
854
|
-
sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut,
|
|
494
|
+
sortedInsert(bestTrades, new Trade(new Route([pair, ...currentPairs], currencyIn, currencyAmountOut.currency), currencyAmountOut, import_swap_sdk_core5.TradeType.EXACT_OUTPUT), maxNumResults, tradeComparator);
|
|
855
495
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
856
496
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
857
497
|
Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
|
|
@@ -864,18 +504,10 @@ var Trade = class {
|
|
|
864
504
|
}
|
|
865
505
|
};
|
|
866
506
|
|
|
867
|
-
// src/entities/nativeCurrency.ts
|
|
868
|
-
var NativeCurrency = class extends BaseCurrency {
|
|
869
|
-
constructor() {
|
|
870
|
-
super(...arguments);
|
|
871
|
-
this.isNative = true;
|
|
872
|
-
this.isToken = false;
|
|
873
|
-
}
|
|
874
|
-
};
|
|
875
|
-
|
|
876
507
|
// src/entities/native.ts
|
|
877
|
-
var
|
|
878
|
-
var
|
|
508
|
+
var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
|
|
509
|
+
var import_swap_sdk_core6 = require("@pancakeswap/swap-sdk-core");
|
|
510
|
+
var _Native = class extends import_swap_sdk_core6.NativeCurrency {
|
|
879
511
|
constructor({
|
|
880
512
|
chainId,
|
|
881
513
|
decimals,
|
|
@@ -886,14 +518,14 @@ var _Native = class extends NativeCurrency {
|
|
|
886
518
|
}
|
|
887
519
|
get wrapped() {
|
|
888
520
|
const wnative = WNATIVE[this.chainId];
|
|
889
|
-
(0,
|
|
521
|
+
(0, import_tiny_invariant5.default)(!!wnative, "WRAPPED");
|
|
890
522
|
return wnative;
|
|
891
523
|
}
|
|
892
524
|
static onChain(chainId) {
|
|
893
525
|
if (chainId in this.cache) {
|
|
894
526
|
return this.cache[chainId];
|
|
895
527
|
}
|
|
896
|
-
(0,
|
|
528
|
+
(0, import_tiny_invariant5.default)(!!NATIVE[chainId], "NATIVE_CURRENCY");
|
|
897
529
|
const { decimals, name, symbol } = NATIVE[chainId];
|
|
898
530
|
return this.cache[chainId] = new _Native({ chainId, decimals, symbol, name });
|
|
899
531
|
}
|
|
@@ -905,7 +537,8 @@ var Native = _Native;
|
|
|
905
537
|
Native.cache = {};
|
|
906
538
|
|
|
907
539
|
// src/router.ts
|
|
908
|
-
var
|
|
540
|
+
var import_swap_sdk_core7 = require("@pancakeswap/swap-sdk-core");
|
|
541
|
+
var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
|
|
909
542
|
function toHex(currencyAmount) {
|
|
910
543
|
return `0x${currencyAmount.quotient.toString(16)}`;
|
|
911
544
|
}
|
|
@@ -916,8 +549,8 @@ var Router = class {
|
|
|
916
549
|
static swapCallParameters(trade, options) {
|
|
917
550
|
const etherIn = trade.inputAmount.currency.isNative;
|
|
918
551
|
const etherOut = trade.outputAmount.currency.isNative;
|
|
919
|
-
(0,
|
|
920
|
-
(0,
|
|
552
|
+
(0, import_tiny_invariant6.default)(!(etherIn && etherOut), "ETHER_IN_OUT");
|
|
553
|
+
(0, import_tiny_invariant6.default)(!("ttl" in options) || options.ttl > 0, "TTL");
|
|
921
554
|
const to = validateAndParseAddress(options.recipient);
|
|
922
555
|
const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
|
|
923
556
|
const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
|
|
@@ -928,7 +561,7 @@ var Router = class {
|
|
|
928
561
|
let args;
|
|
929
562
|
let value;
|
|
930
563
|
switch (trade.tradeType) {
|
|
931
|
-
case
|
|
564
|
+
case import_swap_sdk_core7.TradeType.EXACT_INPUT:
|
|
932
565
|
if (etherIn) {
|
|
933
566
|
methodName = useFeeOnTransfer ? "swapExactETHForTokensSupportingFeeOnTransferTokens" : "swapExactETHForTokens";
|
|
934
567
|
args = [amountOut, path, to, deadline];
|
|
@@ -943,8 +576,8 @@ var Router = class {
|
|
|
943
576
|
value = ZERO_HEX;
|
|
944
577
|
}
|
|
945
578
|
break;
|
|
946
|
-
case
|
|
947
|
-
(0,
|
|
579
|
+
case import_swap_sdk_core7.TradeType.EXACT_OUTPUT:
|
|
580
|
+
(0, import_tiny_invariant6.default)(!useFeeOnTransfer, "EXACT_OUT_FOT");
|
|
948
581
|
if (etherIn) {
|
|
949
582
|
methodName = "swapETHForExactTokens";
|
|
950
583
|
args = [amountOut, path, to, deadline];
|
|
@@ -967,47 +600,27 @@ var Router = class {
|
|
|
967
600
|
};
|
|
968
601
|
}
|
|
969
602
|
};
|
|
603
|
+
|
|
604
|
+
// src/index.ts
|
|
605
|
+
__reExport(src_exports, require("@pancakeswap/swap-sdk-core"), module.exports);
|
|
970
606
|
// Annotate the CommonJS export names for ESM import in node:
|
|
971
607
|
0 && (module.exports = {
|
|
972
|
-
BaseCurrency,
|
|
973
608
|
ChainId,
|
|
974
|
-
|
|
609
|
+
ERC20Token,
|
|
975
610
|
FACTORY_ADDRESS,
|
|
976
611
|
FACTORY_ADDRESS_MAP,
|
|
977
|
-
FIVE,
|
|
978
|
-
Fraction,
|
|
979
612
|
INIT_CODE_HASH,
|
|
980
613
|
INIT_CODE_HASH_MAP,
|
|
981
|
-
InsufficientInputAmountError,
|
|
982
|
-
InsufficientReservesError,
|
|
983
614
|
JSBI,
|
|
984
|
-
MINIMUM_LIQUIDITY,
|
|
985
|
-
MaxUint256,
|
|
986
615
|
NATIVE,
|
|
987
616
|
Native,
|
|
988
|
-
NativeCurrency,
|
|
989
|
-
ONE,
|
|
990
617
|
Pair,
|
|
991
|
-
Percent,
|
|
992
|
-
Price,
|
|
993
|
-
Rounding,
|
|
994
618
|
Route,
|
|
995
619
|
Router,
|
|
996
|
-
SOLIDITY_TYPE_MAXIMA,
|
|
997
|
-
SolidityType,
|
|
998
|
-
TEN,
|
|
999
|
-
THREE,
|
|
1000
|
-
TWO,
|
|
1001
|
-
Token,
|
|
1002
620
|
Trade,
|
|
1003
|
-
TradeType,
|
|
1004
621
|
WBNB,
|
|
1005
622
|
WETH9,
|
|
1006
623
|
WNATIVE,
|
|
1007
|
-
ZERO,
|
|
1008
|
-
_100,
|
|
1009
|
-
_10000,
|
|
1010
|
-
_9975,
|
|
1011
624
|
computePairAddress,
|
|
1012
625
|
computePriceImpact,
|
|
1013
626
|
inputOutputComparator,
|