@pancakeswap/swap-sdk-core 0.0.1 → 1.0.0
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/dist/baseCurrency.d.ts +50 -0
- package/dist/baseCurrency.d.ts.map +1 -0
- package/dist/constants.d.ts +30 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/currency.d.ts +4 -0
- package/dist/currency.d.ts.map +1 -0
- package/dist/errors.d.ts +17 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/fractions/currencyAmount.d.ts +31 -0
- package/dist/fractions/currencyAmount.d.ts.map +1 -0
- package/dist/fractions/fraction.d.ts +24 -0
- package/dist/fractions/fraction.d.ts.map +1 -0
- package/dist/fractions/index.d.ts +5 -0
- package/dist/fractions/index.d.ts.map +1 -0
- package/dist/fractions/percent.d.ts +15 -0
- package/dist/fractions/percent.d.ts.map +1 -0
- package/dist/fractions/price.d.ts +39 -0
- package/dist/fractions/price.d.ts.map +1 -0
- package/dist/index.d.ts +9 -257
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +196 -182
- package/dist/index.mjs +156 -123
- package/dist/nativeCurrency.d.ts +9 -0
- package/dist/nativeCurrency.d.ts.map +1 -0
- package/dist/token.d.ts +41 -0
- package/dist/token.d.ts.map +1 -0
- package/dist/utils.d.ts +18 -0
- package/dist/utils.d.ts.map +1 -0
- package/package.json +20 -14
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
import
|
|
1
|
+
import invariant6 from 'tiny-invariant';
|
|
2
|
+
import _Decimal from 'decimal.js-light';
|
|
3
|
+
import _Big from 'big.js';
|
|
4
|
+
import toFormat from 'toformat';
|
|
3
5
|
|
|
4
6
|
// src/constants.ts
|
|
5
|
-
import JSBI from "jsbi";
|
|
6
7
|
var TradeType = /* @__PURE__ */ ((TradeType2) => {
|
|
7
8
|
TradeType2[TradeType2["EXACT_INPUT"] = 0] = "EXACT_INPUT";
|
|
8
9
|
TradeType2[TradeType2["EXACT_OUTPUT"] = 1] = "EXACT_OUTPUT";
|
|
@@ -14,46 +15,43 @@ var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
|
14
15
|
Rounding2[Rounding2["ROUND_UP"] = 2] = "ROUND_UP";
|
|
15
16
|
return Rounding2;
|
|
16
17
|
})(Rounding || {});
|
|
17
|
-
var MINIMUM_LIQUIDITY =
|
|
18
|
-
var ZERO =
|
|
19
|
-
var ONE =
|
|
20
|
-
var TWO =
|
|
21
|
-
var THREE =
|
|
22
|
-
var FIVE =
|
|
23
|
-
var TEN =
|
|
24
|
-
var _100 =
|
|
25
|
-
var _9975 =
|
|
26
|
-
var _10000 =
|
|
27
|
-
var MaxUint256 =
|
|
28
|
-
var VMType = /* @__PURE__ */ ((
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
return
|
|
18
|
+
var MINIMUM_LIQUIDITY = 1000n;
|
|
19
|
+
var ZERO = 0n;
|
|
20
|
+
var ONE = 1n;
|
|
21
|
+
var TWO = 2n;
|
|
22
|
+
var THREE = 3n;
|
|
23
|
+
var FIVE = 5n;
|
|
24
|
+
var TEN = 10n;
|
|
25
|
+
var _100 = 100n;
|
|
26
|
+
var _9975 = 9975n;
|
|
27
|
+
var _10000 = 10000n;
|
|
28
|
+
var MaxUint256 = BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
29
|
+
var VMType = /* @__PURE__ */ ((VMType3) => {
|
|
30
|
+
VMType3["uint8"] = "uint8";
|
|
31
|
+
VMType3["uint256"] = "uint256";
|
|
32
|
+
return VMType3;
|
|
32
33
|
})(VMType || {});
|
|
33
34
|
var VM_TYPE_MAXIMA = {
|
|
34
|
-
["uint8" /* uint8 */]:
|
|
35
|
-
["uint256" /* uint256 */]:
|
|
35
|
+
["uint8" /* uint8 */]: BigInt("0xff"),
|
|
36
|
+
["uint256" /* uint256 */]: BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
|
36
37
|
};
|
|
37
|
-
|
|
38
|
-
// src/baseCurrency.ts
|
|
39
|
-
import invariant from "tiny-invariant";
|
|
40
38
|
var BaseCurrency = class {
|
|
39
|
+
/**
|
|
40
|
+
* Constructs an instance of the base class `BaseCurrency`.
|
|
41
|
+
* @param chainId the chain ID on which this currency resides
|
|
42
|
+
* @param decimals decimals of the currency
|
|
43
|
+
* @param symbol symbol of the currency
|
|
44
|
+
* @param name of the currency
|
|
45
|
+
*/
|
|
41
46
|
constructor(chainId, decimals, symbol, name) {
|
|
42
|
-
|
|
43
|
-
|
|
47
|
+
invariant6(Number.isSafeInteger(chainId), "CHAIN_ID");
|
|
48
|
+
invariant6(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
|
|
44
49
|
this.chainId = chainId;
|
|
45
50
|
this.decimals = decimals;
|
|
46
51
|
this.symbol = symbol;
|
|
47
52
|
this.name = name;
|
|
48
53
|
}
|
|
49
54
|
};
|
|
50
|
-
|
|
51
|
-
// src/fractions/fraction.ts
|
|
52
|
-
import JSBI2 from "jsbi";
|
|
53
|
-
import invariant2 from "tiny-invariant";
|
|
54
|
-
import _Decimal from "decimal.js-light";
|
|
55
|
-
import _Big from "big.js";
|
|
56
|
-
import toFormat from "toformat";
|
|
57
55
|
var Decimal = toFormat(_Decimal);
|
|
58
56
|
var Big = toFormat(_Big);
|
|
59
57
|
var toSignificantRounding = {
|
|
@@ -67,88 +65,101 @@ var toFixedRounding = {
|
|
|
67
65
|
[2 /* ROUND_UP */]: 3 /* RoundUp */
|
|
68
66
|
};
|
|
69
67
|
var Fraction = class {
|
|
70
|
-
constructor(numerator, denominator =
|
|
71
|
-
this.numerator =
|
|
72
|
-
this.denominator =
|
|
68
|
+
constructor(numerator, denominator = 1n) {
|
|
69
|
+
this.numerator = BigInt(numerator);
|
|
70
|
+
this.denominator = BigInt(denominator);
|
|
73
71
|
}
|
|
74
72
|
static tryParseFraction(fractionish) {
|
|
75
|
-
if (fractionish
|
|
73
|
+
if (typeof fractionish === "bigint" || typeof fractionish === "number" || typeof fractionish === "string")
|
|
76
74
|
return new Fraction(fractionish);
|
|
77
75
|
if ("numerator" in fractionish && "denominator" in fractionish)
|
|
78
76
|
return fractionish;
|
|
79
77
|
throw new Error("Could not parse fraction");
|
|
80
78
|
}
|
|
79
|
+
// performs floor division
|
|
81
80
|
get quotient() {
|
|
82
|
-
return
|
|
81
|
+
return this.numerator / this.denominator;
|
|
83
82
|
}
|
|
83
|
+
// remainder after floor division
|
|
84
84
|
get remainder() {
|
|
85
|
-
return new Fraction(
|
|
85
|
+
return new Fraction(this.numerator % this.denominator, this.denominator);
|
|
86
86
|
}
|
|
87
87
|
invert() {
|
|
88
88
|
return new Fraction(this.denominator, this.numerator);
|
|
89
89
|
}
|
|
90
90
|
add(other) {
|
|
91
91
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
92
|
-
if (
|
|
93
|
-
return new Fraction(
|
|
92
|
+
if (this.denominator === otherParsed.denominator) {
|
|
93
|
+
return new Fraction(this.numerator + otherParsed.numerator, this.denominator);
|
|
94
94
|
}
|
|
95
|
-
return new Fraction(
|
|
95
|
+
return new Fraction(
|
|
96
|
+
this.numerator * otherParsed.denominator + otherParsed.numerator * this.denominator,
|
|
97
|
+
this.denominator * otherParsed.denominator
|
|
98
|
+
);
|
|
96
99
|
}
|
|
97
100
|
subtract(other) {
|
|
98
101
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
99
|
-
if (
|
|
100
|
-
return new Fraction(
|
|
102
|
+
if (this.denominator === otherParsed.denominator) {
|
|
103
|
+
return new Fraction(this.numerator - otherParsed.numerator, this.denominator);
|
|
101
104
|
}
|
|
102
|
-
return new Fraction(
|
|
105
|
+
return new Fraction(
|
|
106
|
+
this.numerator * otherParsed.denominator - otherParsed.numerator * this.denominator,
|
|
107
|
+
this.denominator * otherParsed.denominator
|
|
108
|
+
);
|
|
103
109
|
}
|
|
104
110
|
lessThan(other) {
|
|
105
111
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
106
|
-
return
|
|
112
|
+
return this.numerator * otherParsed.denominator < otherParsed.numerator * this.denominator;
|
|
107
113
|
}
|
|
108
114
|
equalTo(other) {
|
|
109
115
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
110
|
-
return
|
|
116
|
+
return this.numerator * otherParsed.denominator === otherParsed.numerator * this.denominator;
|
|
111
117
|
}
|
|
112
118
|
greaterThan(other) {
|
|
113
119
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
114
|
-
return
|
|
120
|
+
return this.numerator * otherParsed.denominator > otherParsed.numerator * this.denominator;
|
|
115
121
|
}
|
|
116
122
|
multiply(other) {
|
|
117
123
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
118
|
-
return new Fraction(
|
|
124
|
+
return new Fraction(this.numerator * otherParsed.numerator, this.denominator * otherParsed.denominator);
|
|
119
125
|
}
|
|
120
126
|
divide(other) {
|
|
121
127
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
122
|
-
return new Fraction(
|
|
128
|
+
return new Fraction(this.numerator * otherParsed.denominator, this.denominator * otherParsed.numerator);
|
|
123
129
|
}
|
|
124
130
|
toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
125
|
-
|
|
126
|
-
|
|
131
|
+
invariant6(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
|
|
132
|
+
invariant6(significantDigits > 0, `${significantDigits} is not positive.`);
|
|
127
133
|
Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });
|
|
128
134
|
const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
|
|
129
135
|
return quotient.toFormat(quotient.decimalPlaces(), format);
|
|
130
136
|
}
|
|
131
137
|
toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
132
|
-
|
|
133
|
-
|
|
138
|
+
invariant6(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
|
|
139
|
+
invariant6(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
|
|
134
140
|
Big.DP = decimalPlaces;
|
|
135
141
|
Big.RM = toFixedRounding[rounding];
|
|
136
142
|
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
|
|
137
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Helper method for converting any super class back to a fraction
|
|
146
|
+
*/
|
|
138
147
|
get asFraction() {
|
|
139
148
|
return new Fraction(this.numerator, this.denominator);
|
|
140
149
|
}
|
|
141
150
|
};
|
|
142
151
|
|
|
143
152
|
// src/fractions/percent.ts
|
|
144
|
-
|
|
145
|
-
var ONE_HUNDRED = new Fraction(JSBI3.BigInt(100));
|
|
153
|
+
var ONE_HUNDRED = new Fraction(100n);
|
|
146
154
|
function toPercent(fraction) {
|
|
147
155
|
return new Percent(fraction.numerator, fraction.denominator);
|
|
148
156
|
}
|
|
149
157
|
var Percent = class extends Fraction {
|
|
150
158
|
constructor() {
|
|
151
159
|
super(...arguments);
|
|
160
|
+
/**
|
|
161
|
+
* This boolean prevents a fraction from being interpreted as a Percent
|
|
162
|
+
*/
|
|
152
163
|
this.isPercent = true;
|
|
153
164
|
}
|
|
154
165
|
add(other) {
|
|
@@ -170,33 +181,38 @@ var Percent = class extends Fraction {
|
|
|
170
181
|
return super.multiply(ONE_HUNDRED).toFixed(decimalPlaces, format, rounding);
|
|
171
182
|
}
|
|
172
183
|
};
|
|
173
|
-
|
|
174
|
-
// src/fractions/currencyAmount.ts
|
|
175
|
-
import invariant3 from "tiny-invariant";
|
|
176
|
-
import JSBI4 from "jsbi";
|
|
177
|
-
import _Big2 from "big.js";
|
|
178
|
-
import toFormat2 from "toformat";
|
|
179
|
-
var Big2 = toFormat2(_Big2);
|
|
184
|
+
var Big2 = toFormat(_Big);
|
|
180
185
|
var CurrencyAmount = class extends Fraction {
|
|
181
186
|
constructor(currency, numerator, denominator) {
|
|
182
187
|
super(numerator, denominator);
|
|
183
|
-
|
|
188
|
+
invariant6(this.quotient <= MaxUint256, "AMOUNT");
|
|
184
189
|
this.currency = currency;
|
|
185
|
-
this.decimalScale =
|
|
190
|
+
this.decimalScale = 10n ** BigInt(currency.decimals);
|
|
186
191
|
}
|
|
192
|
+
/**
|
|
193
|
+
* Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
|
|
194
|
+
* @param currency the currency in the amount
|
|
195
|
+
* @param rawAmount the raw token or ether amount
|
|
196
|
+
*/
|
|
187
197
|
static fromRawAmount(currency, rawAmount) {
|
|
188
198
|
return new CurrencyAmount(currency, rawAmount);
|
|
189
199
|
}
|
|
200
|
+
/**
|
|
201
|
+
* Construct a currency amount with a denominator that is not equal to 1
|
|
202
|
+
* @param currency the currency
|
|
203
|
+
* @param numerator the numerator of the fractional token amount
|
|
204
|
+
* @param denominator the denominator of the fractional token amount
|
|
205
|
+
*/
|
|
190
206
|
static fromFractionalAmount(currency, numerator, denominator) {
|
|
191
207
|
return new CurrencyAmount(currency, numerator, denominator);
|
|
192
208
|
}
|
|
193
209
|
add(other) {
|
|
194
|
-
|
|
210
|
+
invariant6(this.currency.equals(other.currency), "CURRENCY");
|
|
195
211
|
const added = super.add(other);
|
|
196
212
|
return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
|
|
197
213
|
}
|
|
198
214
|
subtract(other) {
|
|
199
|
-
|
|
215
|
+
invariant6(this.currency.equals(other.currency), "CURRENCY");
|
|
200
216
|
const subtracted = super.subtract(other);
|
|
201
217
|
return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
|
|
202
218
|
}
|
|
@@ -212,7 +228,7 @@ var CurrencyAmount = class extends Fraction {
|
|
|
212
228
|
return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
|
|
213
229
|
}
|
|
214
230
|
toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
|
|
215
|
-
|
|
231
|
+
invariant6(decimalPlaces <= this.currency.decimals, "DECIMALS");
|
|
216
232
|
return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
|
|
217
233
|
}
|
|
218
234
|
toExact(format = { groupSeparator: "" }) {
|
|
@@ -225,18 +241,18 @@ var CurrencyAmount = class extends Fraction {
|
|
|
225
241
|
return CurrencyAmount.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator);
|
|
226
242
|
}
|
|
227
243
|
};
|
|
228
|
-
|
|
229
|
-
// src/fractions/price.ts
|
|
230
|
-
import JSBI5 from "jsbi";
|
|
231
|
-
import invariant4 from "tiny-invariant";
|
|
232
244
|
var Price = class extends Fraction {
|
|
245
|
+
// used to adjust the raw fraction w/r/t the decimals of the {base,quote}Token
|
|
246
|
+
/**
|
|
247
|
+
* Construct a price, either with the base and quote currency amount, or the
|
|
248
|
+
* @param args
|
|
249
|
+
*/
|
|
233
250
|
constructor(...args) {
|
|
234
251
|
let baseCurrency;
|
|
235
252
|
let quoteCurrency;
|
|
236
253
|
let denominator;
|
|
237
254
|
let numerator;
|
|
238
255
|
if (args.length === 4) {
|
|
239
|
-
;
|
|
240
256
|
[baseCurrency, quoteCurrency, denominator, numerator] = args;
|
|
241
257
|
} else {
|
|
242
258
|
const result = args[0].quoteAmount.divide(args[0].baseAmount);
|
|
@@ -250,21 +266,36 @@ var Price = class extends Fraction {
|
|
|
250
266
|
super(numerator, denominator);
|
|
251
267
|
this.baseCurrency = baseCurrency;
|
|
252
268
|
this.quoteCurrency = quoteCurrency;
|
|
253
|
-
this.scalar = new Fraction(
|
|
269
|
+
this.scalar = new Fraction(10n ** BigInt(baseCurrency.decimals), 10n ** BigInt(quoteCurrency.decimals));
|
|
254
270
|
}
|
|
271
|
+
/**
|
|
272
|
+
* Flip the price, switching the base and quote currency
|
|
273
|
+
*/
|
|
255
274
|
invert() {
|
|
256
275
|
return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
|
|
257
276
|
}
|
|
277
|
+
/**
|
|
278
|
+
* Multiply the price by another price, returning a new price. The other price must have the same base currency as this price's quote currency
|
|
279
|
+
* @param other the other price
|
|
280
|
+
*/
|
|
258
281
|
multiply(other) {
|
|
259
|
-
|
|
282
|
+
invariant6(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
|
|
260
283
|
const fraction = super.multiply(other);
|
|
261
284
|
return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
|
|
262
285
|
}
|
|
286
|
+
/**
|
|
287
|
+
* Return the amount of quote currency corresponding to a given amount of the base currency
|
|
288
|
+
* @param currencyAmount the amount of base currency to quote against the price
|
|
289
|
+
*/
|
|
263
290
|
quote(currencyAmount) {
|
|
264
|
-
|
|
291
|
+
invariant6(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
|
|
265
292
|
const result = super.multiply(currencyAmount);
|
|
266
293
|
return CurrencyAmount.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
|
|
267
294
|
}
|
|
295
|
+
/**
|
|
296
|
+
* Get the value scaled by decimals for formatting
|
|
297
|
+
* @private
|
|
298
|
+
*/
|
|
268
299
|
get adjustedForDecimals() {
|
|
269
300
|
return super.multiply(this.scalar);
|
|
270
301
|
}
|
|
@@ -284,9 +315,6 @@ var NativeCurrency = class extends BaseCurrency {
|
|
|
284
315
|
this.isToken = false;
|
|
285
316
|
}
|
|
286
317
|
};
|
|
287
|
-
|
|
288
|
-
// src/token.ts
|
|
289
|
-
import invariant5 from "tiny-invariant";
|
|
290
318
|
var Token = class extends BaseCurrency {
|
|
291
319
|
constructor(chainId, address, decimals, symbol, name, projectLink) {
|
|
292
320
|
super(chainId, decimals, symbol, name);
|
|
@@ -295,14 +323,27 @@ var Token = class extends BaseCurrency {
|
|
|
295
323
|
this.address = address;
|
|
296
324
|
this.projectLink = projectLink;
|
|
297
325
|
}
|
|
326
|
+
/**
|
|
327
|
+
* Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
|
|
328
|
+
* @param other other token to compare
|
|
329
|
+
*/
|
|
298
330
|
equals(other) {
|
|
299
331
|
return other.isToken && this.chainId === other.chainId && this.address === other.address;
|
|
300
332
|
}
|
|
333
|
+
/**
|
|
334
|
+
* Returns true if the address of this token sorts before the address of the other token
|
|
335
|
+
* @param other other token to compare
|
|
336
|
+
* @throws if the tokens have the same address
|
|
337
|
+
* @throws if the tokens are on different chains
|
|
338
|
+
*/
|
|
301
339
|
sortsBefore(other) {
|
|
302
|
-
|
|
303
|
-
|
|
340
|
+
invariant6(this.chainId === other.chainId, "CHAIN_IDS");
|
|
341
|
+
invariant6(this.address !== other.address, "ADDRESSES");
|
|
304
342
|
return this.address.toLowerCase() < other.address.toLowerCase();
|
|
305
343
|
}
|
|
344
|
+
/**
|
|
345
|
+
* Return this token, which does not need to be wrapped
|
|
346
|
+
*/
|
|
306
347
|
get wrapped() {
|
|
307
348
|
return this;
|
|
308
349
|
}
|
|
@@ -338,26 +379,22 @@ var InsufficientInputAmountError = class extends Error {
|
|
|
338
379
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
339
380
|
}
|
|
340
381
|
};
|
|
341
|
-
|
|
342
|
-
// src/utils.ts
|
|
343
|
-
import JSBI6 from "jsbi";
|
|
344
|
-
import invariant6 from "tiny-invariant";
|
|
345
382
|
function validateVMTypeInstance(value, vmType) {
|
|
346
|
-
invariant6(
|
|
347
|
-
invariant6(
|
|
383
|
+
invariant6(value >= ZERO, `${value} is not a ${vmType}.`);
|
|
384
|
+
invariant6(value <= VM_TYPE_MAXIMA[vmType], `${value} is not a ${vmType}.`);
|
|
348
385
|
}
|
|
349
386
|
function sqrt(y) {
|
|
350
|
-
|
|
387
|
+
invariant6(y >= ZERO, "NEGATIVE");
|
|
351
388
|
let z = ZERO;
|
|
352
389
|
let x;
|
|
353
|
-
if (
|
|
390
|
+
if (y > THREE) {
|
|
354
391
|
z = y;
|
|
355
|
-
x =
|
|
356
|
-
while (
|
|
392
|
+
x = y / TWO + ONE;
|
|
393
|
+
while (x < z) {
|
|
357
394
|
z = x;
|
|
358
|
-
x =
|
|
395
|
+
x = (y / x + x) / TWO;
|
|
359
396
|
}
|
|
360
|
-
} else if (
|
|
397
|
+
} else if (y !== ZERO) {
|
|
361
398
|
z = ONE;
|
|
362
399
|
}
|
|
363
400
|
return z;
|
|
@@ -391,34 +428,30 @@ function computePriceImpact(midPrice, inputAmount, outputAmount) {
|
|
|
391
428
|
const priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount);
|
|
392
429
|
return new Percent(priceImpact.numerator, priceImpact.denominator);
|
|
393
430
|
}
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
sortedInsert,
|
|
422
|
-
sqrt,
|
|
423
|
-
validateVMTypeInstance
|
|
424
|
-
};
|
|
431
|
+
function balanceComparator(balanceA, balanceB) {
|
|
432
|
+
if (balanceA && balanceB) {
|
|
433
|
+
return balanceA.greaterThan(balanceB) ? -1 : balanceA.equalTo(balanceB) ? 0 : 1;
|
|
434
|
+
}
|
|
435
|
+
if (balanceA && balanceA.greaterThan("0")) {
|
|
436
|
+
return -1;
|
|
437
|
+
}
|
|
438
|
+
if (balanceB && balanceB.greaterThan("0")) {
|
|
439
|
+
return 1;
|
|
440
|
+
}
|
|
441
|
+
return 0;
|
|
442
|
+
}
|
|
443
|
+
function getTokenComparator(balances) {
|
|
444
|
+
return function sortTokens(tokenA, tokenB) {
|
|
445
|
+
const balanceA = balances[tokenA.address];
|
|
446
|
+
const balanceB = balances[tokenB.address];
|
|
447
|
+
const balanceComp = balanceComparator(balanceA, balanceB);
|
|
448
|
+
if (balanceComp !== 0)
|
|
449
|
+
return balanceComp;
|
|
450
|
+
if (tokenA.symbol && tokenB.symbol) {
|
|
451
|
+
return tokenA.symbol.toLowerCase() < tokenB.symbol.toLowerCase() ? -1 : 1;
|
|
452
|
+
}
|
|
453
|
+
return tokenA.symbol ? -1 : tokenB.symbol ? -1 : 0;
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
export { BaseCurrency, CurrencyAmount, FIVE, Fraction, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, MaxUint256, NativeCurrency, ONE, Percent, Price, Rounding, TEN, THREE, TWO, Token, TradeType, VMType, VM_TYPE_MAXIMA, ZERO, _100, _10000, _9975, computePriceImpact, getTokenComparator, sortedInsert, sqrt, validateVMTypeInstance };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseCurrency } from './baseCurrency';
|
|
2
|
+
/**
|
|
3
|
+
* Represents the native currency of the chain on which it resides, e.g.
|
|
4
|
+
*/
|
|
5
|
+
export declare abstract class NativeCurrency extends BaseCurrency {
|
|
6
|
+
readonly isNative: true;
|
|
7
|
+
readonly isToken: false;
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=nativeCurrency.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"nativeCurrency.d.ts","sourceRoot":"","sources":["../src/nativeCurrency.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAE7C;;GAEG;AACH,8BAAsB,cAAe,SAAQ,YAAY;IACvD,SAAgB,QAAQ,OAAgB;IAExC,SAAgB,OAAO,QAAiB;CACzC"}
|
package/dist/token.d.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { BaseCurrency } from './baseCurrency';
|
|
2
|
+
import { Currency } from './currency';
|
|
3
|
+
export interface SerializedToken {
|
|
4
|
+
chainId: number;
|
|
5
|
+
address: string;
|
|
6
|
+
decimals: number;
|
|
7
|
+
symbol: string;
|
|
8
|
+
name?: string;
|
|
9
|
+
projectLink?: string;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Represents an ERC20 token with a unique address and some metadata.
|
|
13
|
+
*/
|
|
14
|
+
export declare class Token extends BaseCurrency {
|
|
15
|
+
readonly isNative: false;
|
|
16
|
+
readonly isToken: true;
|
|
17
|
+
/**
|
|
18
|
+
* The contract address on the chain on which this token lives
|
|
19
|
+
*/
|
|
20
|
+
readonly address: string;
|
|
21
|
+
readonly projectLink?: string;
|
|
22
|
+
constructor(chainId: number, address: string, decimals: number, symbol: string, name?: string, projectLink?: string);
|
|
23
|
+
/**
|
|
24
|
+
* Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
|
|
25
|
+
* @param other other token to compare
|
|
26
|
+
*/
|
|
27
|
+
equals(other: Currency): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Returns true if the address of this token sorts before the address of the other token
|
|
30
|
+
* @param other other token to compare
|
|
31
|
+
* @throws if the tokens have the same address
|
|
32
|
+
* @throws if the tokens are on different chains
|
|
33
|
+
*/
|
|
34
|
+
sortsBefore(other: Token): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Return this token, which does not need to be wrapped
|
|
37
|
+
*/
|
|
38
|
+
get wrapped(): Token;
|
|
39
|
+
get serialize(): SerializedToken;
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=token.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token.d.ts","sourceRoot":"","sources":["../src/token.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAErC,MAAM,WAAW,eAAe;IAC9B,OAAO,EAAE,MAAM,CAAA;IACf,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,CAAA;IAChB,MAAM,EAAE,MAAM,CAAA;IACd,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,WAAW,CAAC,EAAE,MAAM,CAAA;CACrB;AAED;;GAEG;AACH,qBAAa,KAAM,SAAQ,YAAY;IACrC,SAAgB,QAAQ,EAAE,KAAK,CAAiB;IAEhD,SAAgB,OAAO,EAAE,IAAI,CAAgB;IAE7C;;OAEG;IACH,SAAgB,OAAO,EAAE,MAAM,CAAA;IAE/B,SAAgB,WAAW,CAAC,EAAE,MAAM,CAAA;gBAGlC,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,EACf,QAAQ,EAAE,MAAM,EAChB,MAAM,EAAE,MAAM,EACd,IAAI,CAAC,EAAE,MAAM,EACb,WAAW,CAAC,EAAE,MAAM;IAOtB;;;OAGG;IACI,MAAM,CAAC,KAAK,EAAE,QAAQ,GAAG,OAAO;IAIvC;;;;;OAKG;IACI,WAAW,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO;IAMzC;;OAEG;IACH,IAAW,OAAO,IAAI,KAAK,CAE1B;IAED,IAAW,SAAS,IAAI,eAAe,CAStC;CACF"}
|
package/dist/utils.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { VMType } from './constants';
|
|
2
|
+
import { Currency } from './currency';
|
|
3
|
+
import { CurrencyAmount, Percent, Price } from './fractions';
|
|
4
|
+
import { Token } from './token';
|
|
5
|
+
export declare function validateVMTypeInstance(value: bigint, vmType: VMType): void;
|
|
6
|
+
export declare function sqrt(y: bigint): bigint;
|
|
7
|
+
export declare function sortedInsert<T>(items: T[], add: T, maxSize: number, comparator: (a: T, b: T) => number): T | null;
|
|
8
|
+
/**
|
|
9
|
+
* Returns the percent difference between the mid price and the execution price, i.e. price impact.
|
|
10
|
+
* @param midPrice mid price before the trade
|
|
11
|
+
* @param inputAmount the input amount of the trade
|
|
12
|
+
* @param outputAmount the output amount of the trade
|
|
13
|
+
*/
|
|
14
|
+
export declare function computePriceImpact<TBase extends Currency, TQuote extends Currency>(midPrice: Price<TBase, TQuote>, inputAmount: CurrencyAmount<TBase>, outputAmount: CurrencyAmount<TQuote>): Percent;
|
|
15
|
+
export declare function getTokenComparator(balances: {
|
|
16
|
+
[tokenAddress: string]: CurrencyAmount<Token> | undefined;
|
|
17
|
+
}): (tokenA: Token, tokenB: Token) => number;
|
|
18
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":"AACA,OAAO,EAAyB,MAAM,EAAkB,MAAM,aAAa,CAAA;AAC3E,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,OAAO,EAAE,cAAc,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAC5D,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,IAAI,CAG1E;AAGD,wBAAgB,IAAI,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,CAgBtC;AAKD,wBAAgB,YAAY,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,EAAE,GAAG,EAAE,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,KAAK,MAAM,GAAG,CAAC,GAAG,IAAI,CA8BjH;AAGD;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,SAAS,QAAQ,EAAE,MAAM,SAAS,QAAQ,EAChF,QAAQ,EAAE,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,EAC9B,WAAW,EAAE,cAAc,CAAC,KAAK,CAAC,EAClC,YAAY,EAAE,cAAc,CAAC,MAAM,CAAC,GACnC,OAAO,CAKT;AAgBD,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE;IAC3C,CAAC,YAAY,EAAE,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC,GAAG,SAAS,CAAA;CAC1D,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,KAAK,MAAM,CAkB3C"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pancakeswap/swap-sdk-core",
|
|
3
3
|
"license": "MIT",
|
|
4
|
-
"version": "0.0
|
|
4
|
+
"version": "1.0.0",
|
|
5
5
|
"description": "🛠 An SDK for building applications on top of Pancakeswap.",
|
|
6
6
|
"main": "dist/index.js",
|
|
7
7
|
"typings": "dist/index.d.ts",
|
|
8
|
+
"module": "dist/index.mjs",
|
|
8
9
|
"files": [
|
|
9
10
|
"dist"
|
|
10
11
|
],
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
16
|
+
"require": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"./package.json": "./package.json"
|
|
19
|
+
},
|
|
11
20
|
"repository": {
|
|
12
21
|
"type": "git",
|
|
13
22
|
"url": "https://github.com/pancakeswap/pancake-frontend.git",
|
|
@@ -16,31 +25,21 @@
|
|
|
16
25
|
"keywords": [
|
|
17
26
|
"pancakeswap"
|
|
18
27
|
],
|
|
19
|
-
"module": "dist/index.mjs",
|
|
20
|
-
"scripts": {
|
|
21
|
-
"lint": "eslint src test",
|
|
22
|
-
"build": "tsup src/index.ts --format esm,cjs --dts",
|
|
23
|
-
"dev": "tsup src/index.ts --format esm,cjs --watch --dts",
|
|
24
|
-
"test": "jest",
|
|
25
|
-
"prepublishOnly": "yarn run build",
|
|
26
|
-
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
27
|
-
},
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"big.js": "^5.2.2",
|
|
30
30
|
"decimal.js-light": "^2.5.0",
|
|
31
|
-
"jsbi": "^3.1.4",
|
|
32
31
|
"tiny-invariant": "^1.1.0",
|
|
33
32
|
"tiny-warning": "^1.0.3",
|
|
34
33
|
"toformat": "^2.0.0"
|
|
35
34
|
},
|
|
36
35
|
"peerDependencies": {},
|
|
37
36
|
"devDependencies": {
|
|
37
|
+
"vitest": "^0.27.2",
|
|
38
38
|
"@swc/core": "^1.2.215",
|
|
39
39
|
"@swc/jest": "^0.2.21",
|
|
40
40
|
"@types/big.js": "^4.0.5",
|
|
41
41
|
"@types/jest": "^24.0.25",
|
|
42
|
-
"
|
|
43
|
-
"tsup": "^5.10.1"
|
|
42
|
+
"tsup": "^6.7.0"
|
|
44
43
|
},
|
|
45
44
|
"engines": {
|
|
46
45
|
"node": ">=10"
|
|
@@ -49,5 +48,12 @@
|
|
|
49
48
|
"printWidth": 120,
|
|
50
49
|
"semi": false,
|
|
51
50
|
"singleQuote": true
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"lint": "eslint src test",
|
|
54
|
+
"build": "tsup",
|
|
55
|
+
"dev": "tsup --watch",
|
|
56
|
+
"test": "vitest --globals --run",
|
|
57
|
+
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
52
58
|
}
|
|
53
|
-
}
|
|
59
|
+
}
|