@pancakeswap/sdk 3.0.0 → 3.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.d.ts +30 -257
- package/dist/index.js +134 -502
- package/dist/index.mjs +140 -467
- package/package.json +10 -5
package/dist/index.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,41 +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;
|
|
102
|
+
super(chainId, validateAndParseAddress(address), decimals, symbol, name, projectLink);
|
|
175
103
|
}
|
|
176
104
|
};
|
|
177
105
|
|
|
@@ -184,19 +112,8 @@ var ChainId = /* @__PURE__ */ ((ChainId2) => {
|
|
|
184
112
|
ChainId2[ChainId2["BSC_TESTNET"] = 97] = "BSC_TESTNET";
|
|
185
113
|
return ChainId2;
|
|
186
114
|
})(ChainId || {});
|
|
187
|
-
var TradeType = /* @__PURE__ */ ((TradeType2) => {
|
|
188
|
-
TradeType2[TradeType2["EXACT_INPUT"] = 0] = "EXACT_INPUT";
|
|
189
|
-
TradeType2[TradeType2["EXACT_OUTPUT"] = 1] = "EXACT_OUTPUT";
|
|
190
|
-
return TradeType2;
|
|
191
|
-
})(TradeType || {});
|
|
192
|
-
var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
193
|
-
Rounding2[Rounding2["ROUND_DOWN"] = 0] = "ROUND_DOWN";
|
|
194
|
-
Rounding2[Rounding2["ROUND_HALF_UP"] = 1] = "ROUND_HALF_UP";
|
|
195
|
-
Rounding2[Rounding2["ROUND_UP"] = 2] = "ROUND_UP";
|
|
196
|
-
return Rounding2;
|
|
197
|
-
})(Rounding || {});
|
|
198
115
|
var FACTORY_ADDRESS = "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73";
|
|
199
|
-
var FACTORY_ADDRESS_ETH = "
|
|
116
|
+
var FACTORY_ADDRESS_ETH = "0x1097053Fd2ea711dad45caCcc45EfF7548fCB362";
|
|
200
117
|
var FACTORY_ADDRESS_MAP = {
|
|
201
118
|
[1 /* ETHEREUM */]: FACTORY_ADDRESS_ETH,
|
|
202
119
|
[4 /* RINKEBY */]: FACTORY_ADDRESS_ETH,
|
|
@@ -213,35 +130,15 @@ var INIT_CODE_HASH_MAP = {
|
|
|
213
130
|
[56 /* BSC */]: INIT_CODE_HASH,
|
|
214
131
|
[97 /* BSC_TESTNET */]: "0xd0d4c4cd0848c93cb4fd1f498d7013ee6bfb25783ea21593d5834f5d250ece66"
|
|
215
132
|
};
|
|
216
|
-
var MINIMUM_LIQUIDITY = import_jsbi2.default.BigInt(1e3);
|
|
217
|
-
var ZERO = import_jsbi2.default.BigInt(0);
|
|
218
|
-
var ONE = import_jsbi2.default.BigInt(1);
|
|
219
|
-
var TWO = import_jsbi2.default.BigInt(2);
|
|
220
|
-
var THREE = import_jsbi2.default.BigInt(3);
|
|
221
|
-
var FIVE = import_jsbi2.default.BigInt(5);
|
|
222
|
-
var TEN = import_jsbi2.default.BigInt(10);
|
|
223
|
-
var _100 = import_jsbi2.default.BigInt(100);
|
|
224
|
-
var _9975 = import_jsbi2.default.BigInt(9975);
|
|
225
|
-
var _10000 = import_jsbi2.default.BigInt(1e4);
|
|
226
|
-
var MaxUint256 = import_jsbi2.default.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
227
|
-
var SolidityType = /* @__PURE__ */ ((SolidityType2) => {
|
|
228
|
-
SolidityType2["uint8"] = "uint8";
|
|
229
|
-
SolidityType2["uint256"] = "uint256";
|
|
230
|
-
return SolidityType2;
|
|
231
|
-
})(SolidityType || {});
|
|
232
|
-
var SOLIDITY_TYPE_MAXIMA = {
|
|
233
|
-
["uint8" /* uint8 */]: import_jsbi2.default.BigInt("0xff"),
|
|
234
|
-
["uint256" /* uint256 */]: import_jsbi2.default.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
|
235
|
-
};
|
|
236
133
|
var WETH9 = {
|
|
237
|
-
[1 /* ETHEREUM */]: new
|
|
238
|
-
[4 /* RINKEBY */]: new
|
|
239
|
-
[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")
|
|
240
137
|
};
|
|
241
138
|
var WBNB = {
|
|
242
|
-
[1 /* ETHEREUM */]: new
|
|
243
|
-
[56 /* BSC */]: new
|
|
244
|
-
[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")
|
|
245
142
|
};
|
|
246
143
|
var WNATIVE = {
|
|
247
144
|
[1 /* ETHEREUM */]: WETH9[1 /* ETHEREUM */],
|
|
@@ -266,268 +163,28 @@ var NATIVE = {
|
|
|
266
163
|
}
|
|
267
164
|
};
|
|
268
165
|
|
|
269
|
-
// src/errors.ts
|
|
270
|
-
var CAN_SET_PROTOTYPE = "setPrototypeOf" in Object;
|
|
271
|
-
var InsufficientReservesError = class extends Error {
|
|
272
|
-
constructor() {
|
|
273
|
-
super();
|
|
274
|
-
this.isInsufficientReservesError = true;
|
|
275
|
-
this.name = this.constructor.name;
|
|
276
|
-
if (CAN_SET_PROTOTYPE)
|
|
277
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
278
|
-
}
|
|
279
|
-
};
|
|
280
|
-
var InsufficientInputAmountError = class extends Error {
|
|
281
|
-
constructor() {
|
|
282
|
-
super();
|
|
283
|
-
this.isInsufficientInputAmountError = true;
|
|
284
|
-
this.name = this.constructor.name;
|
|
285
|
-
if (CAN_SET_PROTOTYPE)
|
|
286
|
-
Object.setPrototypeOf(this, new.target.prototype);
|
|
287
|
-
}
|
|
288
|
-
};
|
|
289
|
-
|
|
290
166
|
// src/entities/pair.ts
|
|
167
|
+
var import_swap_sdk_core3 = require("@pancakeswap/swap-sdk-core");
|
|
291
168
|
var import_address2 = require("@ethersproject/address");
|
|
292
169
|
var import_solidity = require("@ethersproject/solidity");
|
|
293
|
-
var
|
|
294
|
-
var
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
var import_jsbi5 = __toESM(require("jsbi"));
|
|
298
|
-
var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
|
|
299
|
-
|
|
300
|
-
// src/entities/fractions/fraction.ts
|
|
301
|
-
var import_jsbi3 = __toESM(require("jsbi"));
|
|
302
|
-
var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
|
|
303
|
-
var import_decimal = __toESM(require("decimal.js-light"));
|
|
304
|
-
var import_big = __toESM(require("big.js"));
|
|
305
|
-
var import_toformat = __toESM(require("toformat"));
|
|
306
|
-
var Decimal = (0, import_toformat.default)(import_decimal.default);
|
|
307
|
-
var Big = (0, import_toformat.default)(import_big.default);
|
|
308
|
-
var toSignificantRounding = {
|
|
309
|
-
[0 /* ROUND_DOWN */]: Decimal.ROUND_DOWN,
|
|
310
|
-
[1 /* ROUND_HALF_UP */]: Decimal.ROUND_HALF_UP,
|
|
311
|
-
[2 /* ROUND_UP */]: Decimal.ROUND_UP
|
|
312
|
-
};
|
|
313
|
-
var toFixedRounding = {
|
|
314
|
-
[0 /* ROUND_DOWN */]: 0 /* RoundDown */,
|
|
315
|
-
[1 /* ROUND_HALF_UP */]: 1 /* RoundHalfUp */,
|
|
316
|
-
[2 /* ROUND_UP */]: 3 /* RoundUp */
|
|
317
|
-
};
|
|
318
|
-
var Fraction = class {
|
|
319
|
-
constructor(numerator, denominator = import_jsbi3.default.BigInt(1)) {
|
|
320
|
-
this.numerator = import_jsbi3.default.BigInt(numerator);
|
|
321
|
-
this.denominator = import_jsbi3.default.BigInt(denominator);
|
|
322
|
-
}
|
|
323
|
-
static tryParseFraction(fractionish) {
|
|
324
|
-
if (fractionish instanceof import_jsbi3.default || typeof fractionish === "number" || typeof fractionish === "string")
|
|
325
|
-
return new Fraction(fractionish);
|
|
326
|
-
if ("numerator" in fractionish && "denominator" in fractionish)
|
|
327
|
-
return fractionish;
|
|
328
|
-
throw new Error("Could not parse fraction");
|
|
329
|
-
}
|
|
330
|
-
get quotient() {
|
|
331
|
-
return import_jsbi3.default.divide(this.numerator, this.denominator);
|
|
332
|
-
}
|
|
333
|
-
get remainder() {
|
|
334
|
-
return new Fraction(import_jsbi3.default.remainder(this.numerator, this.denominator), this.denominator);
|
|
335
|
-
}
|
|
336
|
-
invert() {
|
|
337
|
-
return new Fraction(this.denominator, this.numerator);
|
|
338
|
-
}
|
|
339
|
-
add(other) {
|
|
340
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
341
|
-
if (import_jsbi3.default.equal(this.denominator, otherParsed.denominator)) {
|
|
342
|
-
return new Fraction(import_jsbi3.default.add(this.numerator, otherParsed.numerator), this.denominator);
|
|
343
|
-
}
|
|
344
|
-
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));
|
|
345
|
-
}
|
|
346
|
-
subtract(other) {
|
|
347
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
348
|
-
if (import_jsbi3.default.equal(this.denominator, otherParsed.denominator)) {
|
|
349
|
-
return new Fraction(import_jsbi3.default.subtract(this.numerator, otherParsed.numerator), this.denominator);
|
|
350
|
-
}
|
|
351
|
-
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));
|
|
352
|
-
}
|
|
353
|
-
lessThan(other) {
|
|
354
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
355
|
-
return import_jsbi3.default.lessThan(import_jsbi3.default.multiply(this.numerator, otherParsed.denominator), import_jsbi3.default.multiply(otherParsed.numerator, this.denominator));
|
|
356
|
-
}
|
|
357
|
-
equalTo(other) {
|
|
358
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
359
|
-
return import_jsbi3.default.equal(import_jsbi3.default.multiply(this.numerator, otherParsed.denominator), import_jsbi3.default.multiply(otherParsed.numerator, this.denominator));
|
|
360
|
-
}
|
|
361
|
-
greaterThan(other) {
|
|
362
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
363
|
-
return import_jsbi3.default.greaterThan(import_jsbi3.default.multiply(this.numerator, otherParsed.denominator), import_jsbi3.default.multiply(otherParsed.numerator, this.denominator));
|
|
364
|
-
}
|
|
365
|
-
multiply(other) {
|
|
366
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
367
|
-
return new Fraction(import_jsbi3.default.multiply(this.numerator, otherParsed.numerator), import_jsbi3.default.multiply(this.denominator, otherParsed.denominator));
|
|
368
|
-
}
|
|
369
|
-
divide(other) {
|
|
370
|
-
const otherParsed = Fraction.tryParseFraction(other);
|
|
371
|
-
return new Fraction(import_jsbi3.default.multiply(this.numerator, otherParsed.denominator), import_jsbi3.default.multiply(this.denominator, otherParsed.numerator));
|
|
372
|
-
}
|
|
373
|
-
toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
374
|
-
(0, import_tiny_invariant4.default)(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
|
|
375
|
-
(0, import_tiny_invariant4.default)(significantDigits > 0, `${significantDigits} is not positive.`);
|
|
376
|
-
Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });
|
|
377
|
-
const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
|
|
378
|
-
return quotient.toFormat(quotient.decimalPlaces(), format);
|
|
379
|
-
}
|
|
380
|
-
toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
381
|
-
(0, import_tiny_invariant4.default)(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
|
|
382
|
-
(0, import_tiny_invariant4.default)(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
|
|
383
|
-
Big.DP = decimalPlaces;
|
|
384
|
-
Big.RM = toFixedRounding[rounding];
|
|
385
|
-
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
|
|
386
|
-
}
|
|
387
|
-
get asFraction() {
|
|
388
|
-
return new Fraction(this.numerator, this.denominator);
|
|
389
|
-
}
|
|
390
|
-
};
|
|
391
|
-
|
|
392
|
-
// src/entities/fractions/currencyAmount.ts
|
|
393
|
-
var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
|
|
394
|
-
var import_jsbi4 = __toESM(require("jsbi"));
|
|
395
|
-
var import_big2 = __toESM(require("big.js"));
|
|
396
|
-
var import_toformat2 = __toESM(require("toformat"));
|
|
397
|
-
var Big2 = (0, import_toformat2.default)(import_big2.default);
|
|
398
|
-
var CurrencyAmount2 = class extends Fraction {
|
|
399
|
-
constructor(currency, numerator, denominator) {
|
|
400
|
-
super(numerator, denominator);
|
|
401
|
-
(0, import_tiny_invariant5.default)(import_jsbi4.default.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT");
|
|
402
|
-
this.currency = currency;
|
|
403
|
-
this.decimalScale = import_jsbi4.default.exponentiate(import_jsbi4.default.BigInt(10), import_jsbi4.default.BigInt(currency.decimals));
|
|
404
|
-
}
|
|
405
|
-
static fromRawAmount(currency, rawAmount) {
|
|
406
|
-
return new CurrencyAmount2(currency, rawAmount);
|
|
407
|
-
}
|
|
408
|
-
static fromFractionalAmount(currency, numerator, denominator) {
|
|
409
|
-
return new CurrencyAmount2(currency, numerator, denominator);
|
|
410
|
-
}
|
|
411
|
-
add(other) {
|
|
412
|
-
(0, import_tiny_invariant5.default)(this.currency.equals(other.currency), "CURRENCY");
|
|
413
|
-
const added = super.add(other);
|
|
414
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, added.numerator, added.denominator);
|
|
415
|
-
}
|
|
416
|
-
subtract(other) {
|
|
417
|
-
(0, import_tiny_invariant5.default)(this.currency.equals(other.currency), "CURRENCY");
|
|
418
|
-
const subtracted = super.subtract(other);
|
|
419
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
|
|
420
|
-
}
|
|
421
|
-
multiply(other) {
|
|
422
|
-
const multiplied = super.multiply(other);
|
|
423
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, multiplied.numerator, multiplied.denominator);
|
|
424
|
-
}
|
|
425
|
-
divide(other) {
|
|
426
|
-
const divided = super.divide(other);
|
|
427
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency, divided.numerator, divided.denominator);
|
|
428
|
-
}
|
|
429
|
-
toSignificant(significantDigits = 6, format, rounding = 0 /* ROUND_DOWN */) {
|
|
430
|
-
return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
|
|
431
|
-
}
|
|
432
|
-
toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
|
|
433
|
-
(0, import_tiny_invariant5.default)(decimalPlaces <= this.currency.decimals, "DECIMALS");
|
|
434
|
-
return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
|
|
435
|
-
}
|
|
436
|
-
toExact(format = { groupSeparator: "" }) {
|
|
437
|
-
Big2.DP = this.currency.decimals;
|
|
438
|
-
return new Big2(this.quotient.toString()).div(this.decimalScale.toString()).toFormat(format);
|
|
439
|
-
}
|
|
440
|
-
get wrapped() {
|
|
441
|
-
if (this.currency.isToken)
|
|
442
|
-
return this;
|
|
443
|
-
return CurrencyAmount2.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator);
|
|
444
|
-
}
|
|
445
|
-
};
|
|
446
|
-
|
|
447
|
-
// src/entities/fractions/price.ts
|
|
448
|
-
var Price2 = class extends Fraction {
|
|
449
|
-
constructor(...args) {
|
|
450
|
-
let baseCurrency, quoteCurrency, denominator, numerator;
|
|
451
|
-
if (args.length === 4) {
|
|
452
|
-
;
|
|
453
|
-
[baseCurrency, quoteCurrency, denominator, numerator] = args;
|
|
454
|
-
} else {
|
|
455
|
-
const result = args[0].quoteAmount.divide(args[0].baseAmount);
|
|
456
|
-
[baseCurrency, quoteCurrency, denominator, numerator] = [
|
|
457
|
-
args[0].baseAmount.currency,
|
|
458
|
-
args[0].quoteAmount.currency,
|
|
459
|
-
result.denominator,
|
|
460
|
-
result.numerator
|
|
461
|
-
];
|
|
462
|
-
}
|
|
463
|
-
super(numerator, denominator);
|
|
464
|
-
this.baseCurrency = baseCurrency;
|
|
465
|
-
this.quoteCurrency = quoteCurrency;
|
|
466
|
-
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)));
|
|
467
|
-
}
|
|
468
|
-
invert() {
|
|
469
|
-
return new Price2(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
|
|
470
|
-
}
|
|
471
|
-
multiply(other) {
|
|
472
|
-
(0, import_tiny_invariant6.default)(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
|
|
473
|
-
const fraction = super.multiply(other);
|
|
474
|
-
return new Price2(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
|
|
475
|
-
}
|
|
476
|
-
quote(currencyAmount) {
|
|
477
|
-
(0, import_tiny_invariant6.default)(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
|
|
478
|
-
const result = super.multiply(currencyAmount);
|
|
479
|
-
return CurrencyAmount2.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
|
|
480
|
-
}
|
|
481
|
-
get adjustedForDecimals() {
|
|
482
|
-
return super.multiply(this.scalar);
|
|
483
|
-
}
|
|
484
|
-
toSignificant(significantDigits = 6, format, rounding) {
|
|
485
|
-
return this.adjustedForDecimals.toSignificant(significantDigits, format, rounding);
|
|
486
|
-
}
|
|
487
|
-
toFixed(decimalPlaces = 4, format, rounding) {
|
|
488
|
-
return this.adjustedForDecimals.toFixed(decimalPlaces, format, rounding);
|
|
489
|
-
}
|
|
490
|
-
};
|
|
491
|
-
|
|
492
|
-
// src/entities/fractions/percent.ts
|
|
493
|
-
var import_jsbi6 = __toESM(require("jsbi"));
|
|
494
|
-
var ONE_HUNDRED = new Fraction(import_jsbi6.default.BigInt(100));
|
|
495
|
-
function toPercent(fraction) {
|
|
496
|
-
return new Percent(fraction.numerator, fraction.denominator);
|
|
497
|
-
}
|
|
498
|
-
var Percent = class extends Fraction {
|
|
499
|
-
constructor() {
|
|
500
|
-
super(...arguments);
|
|
501
|
-
this.isPercent = true;
|
|
502
|
-
}
|
|
503
|
-
add(other) {
|
|
504
|
-
return toPercent(super.add(other));
|
|
505
|
-
}
|
|
506
|
-
subtract(other) {
|
|
507
|
-
return toPercent(super.subtract(other));
|
|
508
|
-
}
|
|
509
|
-
multiply(other) {
|
|
510
|
-
return toPercent(super.multiply(other));
|
|
511
|
-
}
|
|
512
|
-
divide(other) {
|
|
513
|
-
return toPercent(super.divide(other));
|
|
514
|
-
}
|
|
515
|
-
toSignificant(significantDigits = 5, format, rounding) {
|
|
516
|
-
return super.multiply(ONE_HUNDRED).toSignificant(significantDigits, format, rounding);
|
|
517
|
-
}
|
|
518
|
-
toFixed(decimalPlaces = 2, format, rounding) {
|
|
519
|
-
return super.multiply(ONE_HUNDRED).toFixed(decimalPlaces, format, rounding);
|
|
520
|
-
}
|
|
521
|
-
};
|
|
522
|
-
|
|
523
|
-
// src/entities/pair.ts
|
|
170
|
+
var import_jsbi2 = __toESM(require("jsbi"));
|
|
171
|
+
var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
|
|
172
|
+
var PAIR_ADDRESS_CACHE = {};
|
|
173
|
+
var composeKey = (token0, token1) => `${token0.chainId}-${token0.address}-${token1.address}`;
|
|
524
174
|
var computePairAddress = ({
|
|
525
175
|
factoryAddress,
|
|
526
176
|
tokenA,
|
|
527
177
|
tokenB
|
|
528
178
|
}) => {
|
|
529
179
|
const [token0, token1] = tokenA.sortsBefore(tokenB) ? [tokenA, tokenB] : [tokenB, tokenA];
|
|
530
|
-
|
|
180
|
+
const key = composeKey(token0, token1);
|
|
181
|
+
if ((PAIR_ADDRESS_CACHE == null ? void 0 : PAIR_ADDRESS_CACHE[key]) === void 0) {
|
|
182
|
+
PAIR_ADDRESS_CACHE = {
|
|
183
|
+
...PAIR_ADDRESS_CACHE,
|
|
184
|
+
[key]: (0, import_address2.getCreate2Address)(factoryAddress, (0, import_solidity.keccak256)(["bytes"], [(0, import_solidity.pack)(["address", "address"], [token0.address, token1.address])]), INIT_CODE_HASH_MAP[token0.chainId])
|
|
185
|
+
};
|
|
186
|
+
}
|
|
187
|
+
return PAIR_ADDRESS_CACHE[key];
|
|
531
188
|
};
|
|
532
189
|
var Pair = class {
|
|
533
190
|
static getAddress(tokenA, tokenB) {
|
|
@@ -535,7 +192,7 @@ var Pair = class {
|
|
|
535
192
|
}
|
|
536
193
|
constructor(currencyAmountA, tokenAmountB) {
|
|
537
194
|
const tokenAmounts = currencyAmountA.currency.sortsBefore(tokenAmountB.currency) ? [currencyAmountA, tokenAmountB] : [tokenAmountB, currencyAmountA];
|
|
538
|
-
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");
|
|
539
196
|
this.tokenAmounts = tokenAmounts;
|
|
540
197
|
}
|
|
541
198
|
involvesToken(token) {
|
|
@@ -543,14 +200,14 @@ var Pair = class {
|
|
|
543
200
|
}
|
|
544
201
|
get token0Price() {
|
|
545
202
|
const result = this.tokenAmounts[1].divide(this.tokenAmounts[0]);
|
|
546
|
-
return new
|
|
203
|
+
return new import_swap_sdk_core3.Price(this.token0, this.token1, result.denominator, result.numerator);
|
|
547
204
|
}
|
|
548
205
|
get token1Price() {
|
|
549
206
|
const result = this.tokenAmounts[0].divide(this.tokenAmounts[1]);
|
|
550
|
-
return new
|
|
207
|
+
return new import_swap_sdk_core3.Price(this.token1, this.token0, result.denominator, result.numerator);
|
|
551
208
|
}
|
|
552
209
|
priceOf(token) {
|
|
553
|
-
(0,
|
|
210
|
+
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
554
211
|
return token.equals(this.token0) ? this.token0Price : this.token1Price;
|
|
555
212
|
}
|
|
556
213
|
get chainId() {
|
|
@@ -569,73 +226,73 @@ var Pair = class {
|
|
|
569
226
|
return this.tokenAmounts[1];
|
|
570
227
|
}
|
|
571
228
|
reserveOf(token) {
|
|
572
|
-
(0,
|
|
229
|
+
(0, import_tiny_invariant2.default)(this.involvesToken(token), "TOKEN");
|
|
573
230
|
return token.equals(this.token0) ? this.reserve0 : this.reserve1;
|
|
574
231
|
}
|
|
575
232
|
getOutputAmount(inputAmount) {
|
|
576
|
-
(0,
|
|
577
|
-
if (
|
|
578
|
-
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();
|
|
579
236
|
}
|
|
580
237
|
const inputReserve = this.reserveOf(inputAmount.currency);
|
|
581
238
|
const outputReserve = this.reserveOf(inputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
582
|
-
const inputAmountWithFee =
|
|
583
|
-
const numerator =
|
|
584
|
-
const denominator =
|
|
585
|
-
const outputAmount =
|
|
586
|
-
if (
|
|
587
|
-
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();
|
|
588
245
|
}
|
|
589
246
|
return [outputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
590
247
|
}
|
|
591
248
|
getInputAmount(outputAmount) {
|
|
592
|
-
(0,
|
|
593
|
-
if (
|
|
594
|
-
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();
|
|
595
252
|
}
|
|
596
253
|
const outputReserve = this.reserveOf(outputAmount.currency);
|
|
597
254
|
const inputReserve = this.reserveOf(outputAmount.currency.equals(this.token0) ? this.token1 : this.token0);
|
|
598
|
-
const numerator =
|
|
599
|
-
const denominator =
|
|
600
|
-
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));
|
|
601
258
|
return [inputAmount, new Pair(inputReserve.add(inputAmount), outputReserve.subtract(outputAmount))];
|
|
602
259
|
}
|
|
603
260
|
getLiquidityMinted(totalSupply, tokenAmountA, tokenAmountB) {
|
|
604
|
-
(0,
|
|
261
|
+
(0, import_tiny_invariant2.default)(totalSupply.currency.equals(this.liquidityToken), "LIQUIDITY");
|
|
605
262
|
const tokenAmounts = tokenAmountA.currency.sortsBefore(tokenAmountB.currency) ? [tokenAmountA, tokenAmountB] : [tokenAmountB, tokenAmountA];
|
|
606
|
-
(0,
|
|
263
|
+
(0, import_tiny_invariant2.default)(tokenAmounts[0].currency.equals(this.token0) && tokenAmounts[1].currency.equals(this.token1), "TOKEN");
|
|
607
264
|
let liquidity;
|
|
608
|
-
if (
|
|
609
|
-
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);
|
|
610
267
|
} else {
|
|
611
|
-
const amount0 =
|
|
612
|
-
const amount1 =
|
|
613
|
-
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;
|
|
614
271
|
}
|
|
615
|
-
if (!
|
|
616
|
-
throw new InsufficientInputAmountError();
|
|
272
|
+
if (!import_jsbi2.default.greaterThan(liquidity, import_swap_sdk_core3.ZERO)) {
|
|
273
|
+
throw new import_swap_sdk_core3.InsufficientInputAmountError();
|
|
617
274
|
}
|
|
618
|
-
return
|
|
275
|
+
return import_swap_sdk_core3.CurrencyAmount.fromRawAmount(this.liquidityToken, liquidity);
|
|
619
276
|
}
|
|
620
277
|
getLiquidityValue(token, totalSupply, liquidity, feeOn = false, kLast) {
|
|
621
|
-
(0,
|
|
622
|
-
(0,
|
|
623
|
-
(0,
|
|
624
|
-
(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");
|
|
625
282
|
let totalSupplyAdjusted;
|
|
626
283
|
if (!feeOn) {
|
|
627
284
|
totalSupplyAdjusted = totalSupply;
|
|
628
285
|
} else {
|
|
629
|
-
(0,
|
|
630
|
-
const kLastParsed =
|
|
631
|
-
if (!
|
|
632
|
-
const rootK = sqrt(
|
|
633
|
-
const rootKLast = sqrt(kLastParsed);
|
|
634
|
-
if (
|
|
635
|
-
const numerator =
|
|
636
|
-
const denominator =
|
|
637
|
-
const feeLiquidity =
|
|
638
|
-
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));
|
|
639
296
|
} else {
|
|
640
297
|
totalSupplyAdjusted = totalSupply;
|
|
641
298
|
}
|
|
@@ -643,25 +300,26 @@ var Pair = class {
|
|
|
643
300
|
totalSupplyAdjusted = totalSupply;
|
|
644
301
|
}
|
|
645
302
|
}
|
|
646
|
-
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));
|
|
647
304
|
}
|
|
648
305
|
};
|
|
649
306
|
|
|
650
307
|
// src/entities/route.ts
|
|
651
|
-
var
|
|
308
|
+
var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
|
|
309
|
+
var import_swap_sdk_core4 = require("@pancakeswap/swap-sdk-core");
|
|
652
310
|
var Route = class {
|
|
653
311
|
constructor(pairs, input, output) {
|
|
654
312
|
this._midPrice = null;
|
|
655
|
-
(0,
|
|
313
|
+
(0, import_tiny_invariant3.default)(pairs.length > 0, "PAIRS");
|
|
656
314
|
const chainId = pairs[0].chainId;
|
|
657
|
-
(0,
|
|
315
|
+
(0, import_tiny_invariant3.default)(pairs.every((pair) => pair.chainId === chainId), "CHAIN_IDS");
|
|
658
316
|
const wrappedInput = input.wrapped;
|
|
659
|
-
(0,
|
|
660
|
-
(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");
|
|
661
319
|
const path = [wrappedInput];
|
|
662
320
|
for (const [i, pair] of pairs.entries()) {
|
|
663
321
|
const currentInput = path[i];
|
|
664
|
-
(0,
|
|
322
|
+
(0, import_tiny_invariant3.default)(currentInput.equals(pair.token0) || currentInput.equals(pair.token1), "PATH");
|
|
665
323
|
const output2 = currentInput.equals(pair.token0) ? pair.token1 : pair.token0;
|
|
666
324
|
path.push(output2);
|
|
667
325
|
}
|
|
@@ -675,10 +333,10 @@ var Route = class {
|
|
|
675
333
|
return this._midPrice;
|
|
676
334
|
const prices = [];
|
|
677
335
|
for (const [i, pair] of this.pairs.entries()) {
|
|
678
|
-
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));
|
|
679
337
|
}
|
|
680
338
|
const reduced = prices.slice(1).reduce((accumulator, currentValue) => accumulator.multiply(currentValue), prices[0]);
|
|
681
|
-
return this._midPrice = new
|
|
339
|
+
return this._midPrice = new import_swap_sdk_core4.Price(this.input, this.output, reduced.denominator, reduced.numerator);
|
|
682
340
|
}
|
|
683
341
|
get chainId() {
|
|
684
342
|
return this.pairs[0].chainId;
|
|
@@ -686,10 +344,11 @@ var Route = class {
|
|
|
686
344
|
};
|
|
687
345
|
|
|
688
346
|
// src/entities/trade.ts
|
|
689
|
-
var
|
|
347
|
+
var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
|
|
348
|
+
var import_swap_sdk_core5 = require("@pancakeswap/swap-sdk-core");
|
|
690
349
|
function inputOutputComparator(a, b) {
|
|
691
|
-
(0,
|
|
692
|
-
(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");
|
|
693
352
|
if (a.outputAmount.equalTo(b.outputAmount)) {
|
|
694
353
|
if (a.inputAmount.equalTo(b.inputAmount)) {
|
|
695
354
|
return 0;
|
|
@@ -721,68 +380,68 @@ function tradeComparator(a, b) {
|
|
|
721
380
|
}
|
|
722
381
|
var Trade = class {
|
|
723
382
|
static exactIn(route, amountIn) {
|
|
724
|
-
return new Trade(route, amountIn,
|
|
383
|
+
return new Trade(route, amountIn, import_swap_sdk_core5.TradeType.EXACT_INPUT);
|
|
725
384
|
}
|
|
726
385
|
static exactOut(route, amountOut) {
|
|
727
|
-
return new Trade(route, amountOut,
|
|
386
|
+
return new Trade(route, amountOut, import_swap_sdk_core5.TradeType.EXACT_OUTPUT);
|
|
728
387
|
}
|
|
729
388
|
constructor(route, amount, tradeType) {
|
|
730
389
|
this.route = route;
|
|
731
390
|
this.tradeType = tradeType;
|
|
732
391
|
const tokenAmounts = new Array(route.path.length);
|
|
733
|
-
if (tradeType ===
|
|
734
|
-
(0,
|
|
392
|
+
if (tradeType === import_swap_sdk_core5.TradeType.EXACT_INPUT) {
|
|
393
|
+
(0, import_tiny_invariant4.default)(amount.currency.equals(route.input), "INPUT");
|
|
735
394
|
tokenAmounts[0] = amount.wrapped;
|
|
736
395
|
for (let i = 0; i < route.path.length - 1; i++) {
|
|
737
396
|
const pair = route.pairs[i];
|
|
738
397
|
const [outputAmount] = pair.getOutputAmount(tokenAmounts[i]);
|
|
739
398
|
tokenAmounts[i + 1] = outputAmount;
|
|
740
399
|
}
|
|
741
|
-
this.inputAmount =
|
|
742
|
-
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);
|
|
743
402
|
} else {
|
|
744
|
-
(0,
|
|
403
|
+
(0, import_tiny_invariant4.default)(amount.currency.equals(route.output), "OUTPUT");
|
|
745
404
|
tokenAmounts[tokenAmounts.length - 1] = amount.wrapped;
|
|
746
405
|
for (let i = route.path.length - 1; i > 0; i--) {
|
|
747
406
|
const pair = route.pairs[i - 1];
|
|
748
407
|
const [inputAmount] = pair.getInputAmount(tokenAmounts[i]);
|
|
749
408
|
tokenAmounts[i - 1] = inputAmount;
|
|
750
409
|
}
|
|
751
|
-
this.inputAmount =
|
|
752
|
-
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);
|
|
753
412
|
}
|
|
754
|
-
this.executionPrice = new
|
|
413
|
+
this.executionPrice = new import_swap_sdk_core5.Price(this.inputAmount.currency, this.outputAmount.currency, this.inputAmount.quotient, this.outputAmount.quotient);
|
|
755
414
|
this.priceImpact = computePriceImpact(route.midPrice, this.inputAmount, this.outputAmount);
|
|
756
415
|
}
|
|
757
416
|
minimumAmountOut(slippageTolerance) {
|
|
758
|
-
(0,
|
|
759
|
-
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) {
|
|
760
419
|
return this.outputAmount;
|
|
761
420
|
} else {
|
|
762
|
-
const slippageAdjustedAmountOut = new Fraction(ONE).add(slippageTolerance).invert().multiply(this.outputAmount.quotient).quotient;
|
|
763
|
-
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);
|
|
764
423
|
}
|
|
765
424
|
}
|
|
766
425
|
maximumAmountIn(slippageTolerance) {
|
|
767
|
-
(0,
|
|
768
|
-
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) {
|
|
769
428
|
return this.inputAmount;
|
|
770
429
|
} else {
|
|
771
|
-
const slippageAdjustedAmountIn = new Fraction(ONE).add(slippageTolerance).multiply(this.inputAmount.quotient).quotient;
|
|
772
|
-
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);
|
|
773
432
|
}
|
|
774
433
|
}
|
|
775
434
|
static bestTradeExactIn(pairs, currencyAmountIn, currencyOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountIn = currencyAmountIn, bestTrades = []) {
|
|
776
|
-
(0,
|
|
777
|
-
(0,
|
|
778
|
-
(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");
|
|
779
438
|
const amountIn = nextAmountIn.wrapped;
|
|
780
439
|
const tokenOut = currencyOut.wrapped;
|
|
781
440
|
for (let i = 0; i < pairs.length; i++) {
|
|
782
441
|
const pair = pairs[i];
|
|
783
442
|
if (!pair.token0.equals(amountIn.currency) && !pair.token1.equals(amountIn.currency))
|
|
784
443
|
continue;
|
|
785
|
-
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))
|
|
786
445
|
continue;
|
|
787
446
|
let amountOut;
|
|
788
447
|
try {
|
|
@@ -795,7 +454,7 @@ var Trade = class {
|
|
|
795
454
|
throw error;
|
|
796
455
|
}
|
|
797
456
|
if (amountOut.currency.equals(tokenOut)) {
|
|
798
|
-
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);
|
|
799
458
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
800
459
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
801
460
|
Trade.bestTradeExactIn(pairsExcludingThisPair, currencyAmountIn, currencyOut, {
|
|
@@ -807,19 +466,19 @@ var Trade = class {
|
|
|
807
466
|
return bestTrades;
|
|
808
467
|
}
|
|
809
468
|
worstExecutionPrice(slippageTolerance) {
|
|
810
|
-
return new
|
|
469
|
+
return new import_swap_sdk_core5.Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
|
|
811
470
|
}
|
|
812
471
|
static bestTradeExactOut(pairs, currencyIn, currencyAmountOut, { maxNumResults = 3, maxHops = 3 } = {}, currentPairs = [], nextAmountOut = currencyAmountOut, bestTrades = []) {
|
|
813
|
-
(0,
|
|
814
|
-
(0,
|
|
815
|
-
(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");
|
|
816
475
|
const amountOut = nextAmountOut.wrapped;
|
|
817
476
|
const tokenIn = currencyIn.wrapped;
|
|
818
477
|
for (let i = 0; i < pairs.length; i++) {
|
|
819
478
|
const pair = pairs[i];
|
|
820
479
|
if (!pair.token0.equals(amountOut.currency) && !pair.token1.equals(amountOut.currency))
|
|
821
480
|
continue;
|
|
822
|
-
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))
|
|
823
482
|
continue;
|
|
824
483
|
let amountIn;
|
|
825
484
|
try {
|
|
@@ -832,7 +491,7 @@ var Trade = class {
|
|
|
832
491
|
throw error;
|
|
833
492
|
}
|
|
834
493
|
if (amountIn.currency.equals(tokenIn)) {
|
|
835
|
-
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);
|
|
836
495
|
} else if (maxHops > 1 && pairs.length > 1) {
|
|
837
496
|
const pairsExcludingThisPair = pairs.slice(0, i).concat(pairs.slice(i + 1, pairs.length));
|
|
838
497
|
Trade.bestTradeExactOut(pairsExcludingThisPair, currencyIn, currencyAmountOut, {
|
|
@@ -845,18 +504,10 @@ var Trade = class {
|
|
|
845
504
|
}
|
|
846
505
|
};
|
|
847
506
|
|
|
848
|
-
// src/entities/nativeCurrency.ts
|
|
849
|
-
var NativeCurrency = class extends BaseCurrency {
|
|
850
|
-
constructor() {
|
|
851
|
-
super(...arguments);
|
|
852
|
-
this.isNative = true;
|
|
853
|
-
this.isToken = false;
|
|
854
|
-
}
|
|
855
|
-
};
|
|
856
|
-
|
|
857
507
|
// src/entities/native.ts
|
|
858
|
-
var
|
|
859
|
-
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 {
|
|
860
511
|
constructor({
|
|
861
512
|
chainId,
|
|
862
513
|
decimals,
|
|
@@ -867,14 +518,14 @@ var _Native = class extends NativeCurrency {
|
|
|
867
518
|
}
|
|
868
519
|
get wrapped() {
|
|
869
520
|
const wnative = WNATIVE[this.chainId];
|
|
870
|
-
(0,
|
|
521
|
+
(0, import_tiny_invariant5.default)(!!wnative, "WRAPPED");
|
|
871
522
|
return wnative;
|
|
872
523
|
}
|
|
873
524
|
static onChain(chainId) {
|
|
874
525
|
if (chainId in this.cache) {
|
|
875
526
|
return this.cache[chainId];
|
|
876
527
|
}
|
|
877
|
-
(0,
|
|
528
|
+
(0, import_tiny_invariant5.default)(!!NATIVE[chainId], "NATIVE_CURRENCY");
|
|
878
529
|
const { decimals, name, symbol } = NATIVE[chainId];
|
|
879
530
|
return this.cache[chainId] = new _Native({ chainId, decimals, symbol, name });
|
|
880
531
|
}
|
|
@@ -886,7 +537,8 @@ var Native = _Native;
|
|
|
886
537
|
Native.cache = {};
|
|
887
538
|
|
|
888
539
|
// src/router.ts
|
|
889
|
-
var
|
|
540
|
+
var import_swap_sdk_core7 = require("@pancakeswap/swap-sdk-core");
|
|
541
|
+
var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
|
|
890
542
|
function toHex(currencyAmount) {
|
|
891
543
|
return `0x${currencyAmount.quotient.toString(16)}`;
|
|
892
544
|
}
|
|
@@ -897,8 +549,8 @@ var Router = class {
|
|
|
897
549
|
static swapCallParameters(trade, options) {
|
|
898
550
|
const etherIn = trade.inputAmount.currency.isNative;
|
|
899
551
|
const etherOut = trade.outputAmount.currency.isNative;
|
|
900
|
-
(0,
|
|
901
|
-
(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");
|
|
902
554
|
const to = validateAndParseAddress(options.recipient);
|
|
903
555
|
const amountIn = toHex(trade.maximumAmountIn(options.allowedSlippage));
|
|
904
556
|
const amountOut = toHex(trade.minimumAmountOut(options.allowedSlippage));
|
|
@@ -909,7 +561,7 @@ var Router = class {
|
|
|
909
561
|
let args;
|
|
910
562
|
let value;
|
|
911
563
|
switch (trade.tradeType) {
|
|
912
|
-
case
|
|
564
|
+
case import_swap_sdk_core7.TradeType.EXACT_INPUT:
|
|
913
565
|
if (etherIn) {
|
|
914
566
|
methodName = useFeeOnTransfer ? "swapExactETHForTokensSupportingFeeOnTransferTokens" : "swapExactETHForTokens";
|
|
915
567
|
args = [amountOut, path, to, deadline];
|
|
@@ -924,8 +576,8 @@ var Router = class {
|
|
|
924
576
|
value = ZERO_HEX;
|
|
925
577
|
}
|
|
926
578
|
break;
|
|
927
|
-
case
|
|
928
|
-
(0,
|
|
579
|
+
case import_swap_sdk_core7.TradeType.EXACT_OUTPUT:
|
|
580
|
+
(0, import_tiny_invariant6.default)(!useFeeOnTransfer, "EXACT_OUT_FOT");
|
|
929
581
|
if (etherIn) {
|
|
930
582
|
methodName = "swapETHForExactTokens";
|
|
931
583
|
args = [amountOut, path, to, deadline];
|
|
@@ -948,47 +600,27 @@ var Router = class {
|
|
|
948
600
|
};
|
|
949
601
|
}
|
|
950
602
|
};
|
|
603
|
+
|
|
604
|
+
// src/index.ts
|
|
605
|
+
__reExport(src_exports, require("@pancakeswap/swap-sdk-core"), module.exports);
|
|
951
606
|
// Annotate the CommonJS export names for ESM import in node:
|
|
952
607
|
0 && (module.exports = {
|
|
953
|
-
BaseCurrency,
|
|
954
608
|
ChainId,
|
|
955
|
-
|
|
609
|
+
ERC20Token,
|
|
956
610
|
FACTORY_ADDRESS,
|
|
957
611
|
FACTORY_ADDRESS_MAP,
|
|
958
|
-
FIVE,
|
|
959
|
-
Fraction,
|
|
960
612
|
INIT_CODE_HASH,
|
|
961
613
|
INIT_CODE_HASH_MAP,
|
|
962
|
-
InsufficientInputAmountError,
|
|
963
|
-
InsufficientReservesError,
|
|
964
614
|
JSBI,
|
|
965
|
-
MINIMUM_LIQUIDITY,
|
|
966
|
-
MaxUint256,
|
|
967
615
|
NATIVE,
|
|
968
616
|
Native,
|
|
969
|
-
NativeCurrency,
|
|
970
|
-
ONE,
|
|
971
617
|
Pair,
|
|
972
|
-
Percent,
|
|
973
|
-
Price,
|
|
974
|
-
Rounding,
|
|
975
618
|
Route,
|
|
976
619
|
Router,
|
|
977
|
-
SOLIDITY_TYPE_MAXIMA,
|
|
978
|
-
SolidityType,
|
|
979
|
-
TEN,
|
|
980
|
-
THREE,
|
|
981
|
-
TWO,
|
|
982
|
-
Token,
|
|
983
620
|
Trade,
|
|
984
|
-
TradeType,
|
|
985
621
|
WBNB,
|
|
986
622
|
WETH9,
|
|
987
623
|
WNATIVE,
|
|
988
|
-
ZERO,
|
|
989
|
-
_100,
|
|
990
|
-
_10000,
|
|
991
|
-
_9975,
|
|
992
624
|
computePairAddress,
|
|
993
625
|
computePriceImpact,
|
|
994
626
|
inputOutputComparator,
|