@pancakeswap/swap-sdk-core 0.0.1 → 0.1.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/index.d.ts +6 -3
- package/dist/index.js +227 -185
- package/dist/index.mjs +174 -117
- package/package.json +13 -6
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import JSBI from 'jsbi';
|
|
2
2
|
export { default as JSBI } from 'jsbi';
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
type BigintIsh = JSBI | number | string;
|
|
5
5
|
declare enum TradeType {
|
|
6
6
|
EXACT_INPUT = 0,
|
|
7
7
|
EXACT_OUTPUT = 1
|
|
@@ -78,7 +78,7 @@ declare class Token extends BaseCurrency {
|
|
|
78
78
|
get serialize(): SerializedToken;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
type Currency = NativeCurrency | Token;
|
|
82
82
|
|
|
83
83
|
/**
|
|
84
84
|
* A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
|
|
@@ -253,5 +253,8 @@ declare function sortedInsert<T>(items: T[], add: T, maxSize: number, comparator
|
|
|
253
253
|
* @param outputAmount the output amount of the trade
|
|
254
254
|
*/
|
|
255
255
|
declare function computePriceImpact<TBase extends Currency, TQuote extends Currency>(midPrice: Price<TBase, TQuote>, inputAmount: CurrencyAmount<TBase>, outputAmount: CurrencyAmount<TQuote>): Percent;
|
|
256
|
+
declare function getTokenComparator(balances: {
|
|
257
|
+
[tokenAddress: string]: CurrencyAmount<Token> | undefined;
|
|
258
|
+
}): (tokenA: Token, tokenB: Token) => number;
|
|
256
259
|
|
|
257
|
-
export { BaseCurrency, BigintIsh, Currency, CurrencyAmount, FIVE, Fraction, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, MaxUint256, NativeCurrency, ONE, Percent, Price, Rounding, SerializedToken, TEN, THREE, TWO, Token, TradeType, VMType, VM_TYPE_MAXIMA, ZERO, _100, _10000, _9975, computePriceImpact, sortedInsert, sqrt, validateVMTypeInstance };
|
|
260
|
+
export { BaseCurrency, BigintIsh, Currency, CurrencyAmount, FIVE, Fraction, InsufficientInputAmountError, InsufficientReservesError, MINIMUM_LIQUIDITY, MaxUint256, NativeCurrency, ONE, Percent, Price, Rounding, SerializedToken, TEN, THREE, TWO, Token, TradeType, VMType, VM_TYPE_MAXIMA, ZERO, _100, _10000, _9975, computePriceImpact, getTokenComparator, sortedInsert, sqrt, validateVMTypeInstance };
|
package/dist/index.js
CHANGED
|
@@ -1,63 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
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
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
1
|
+
'use strict';
|
|
22
2
|
|
|
23
|
-
|
|
24
|
-
var
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
InsufficientInputAmountError: () => InsufficientInputAmountError,
|
|
31
|
-
InsufficientReservesError: () => InsufficientReservesError,
|
|
32
|
-
JSBI: () => import_jsbi7.default,
|
|
33
|
-
MINIMUM_LIQUIDITY: () => MINIMUM_LIQUIDITY,
|
|
34
|
-
MaxUint256: () => MaxUint256,
|
|
35
|
-
NativeCurrency: () => NativeCurrency,
|
|
36
|
-
ONE: () => ONE,
|
|
37
|
-
Percent: () => Percent,
|
|
38
|
-
Price: () => Price,
|
|
39
|
-
Rounding: () => Rounding,
|
|
40
|
-
TEN: () => TEN,
|
|
41
|
-
THREE: () => THREE,
|
|
42
|
-
TWO: () => TWO,
|
|
43
|
-
Token: () => Token,
|
|
44
|
-
TradeType: () => TradeType,
|
|
45
|
-
VMType: () => VMType,
|
|
46
|
-
VM_TYPE_MAXIMA: () => VM_TYPE_MAXIMA,
|
|
47
|
-
ZERO: () => ZERO,
|
|
48
|
-
_100: () => _100,
|
|
49
|
-
_10000: () => _10000,
|
|
50
|
-
_9975: () => _9975,
|
|
51
|
-
computePriceImpact: () => computePriceImpact,
|
|
52
|
-
sortedInsert: () => sortedInsert,
|
|
53
|
-
sqrt: () => sqrt,
|
|
54
|
-
validateVMTypeInstance: () => validateVMTypeInstance
|
|
55
|
-
});
|
|
56
|
-
module.exports = __toCommonJS(src_exports);
|
|
57
|
-
var import_jsbi7 = __toESM(require("jsbi"));
|
|
3
|
+
var JSBI2 = require('jsbi');
|
|
4
|
+
var invariant6 = require('tiny-invariant');
|
|
5
|
+
var _Decimal = require('decimal.js-light');
|
|
6
|
+
var _Big = require('big.js');
|
|
7
|
+
var toFormat = require('toformat');
|
|
8
|
+
|
|
9
|
+
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
58
10
|
|
|
59
|
-
|
|
60
|
-
var
|
|
11
|
+
var JSBI2__default = /*#__PURE__*/_interopDefault(JSBI2);
|
|
12
|
+
var invariant6__default = /*#__PURE__*/_interopDefault(invariant6);
|
|
13
|
+
var _Decimal__default = /*#__PURE__*/_interopDefault(_Decimal);
|
|
14
|
+
var _Big__default = /*#__PURE__*/_interopDefault(_Big);
|
|
15
|
+
var toFormat__default = /*#__PURE__*/_interopDefault(toFormat);
|
|
16
|
+
|
|
17
|
+
// src/index.ts
|
|
61
18
|
var TradeType = /* @__PURE__ */ ((TradeType2) => {
|
|
62
19
|
TradeType2[TradeType2["EXACT_INPUT"] = 0] = "EXACT_INPUT";
|
|
63
20
|
TradeType2[TradeType2["EXACT_OUTPUT"] = 1] = "EXACT_OUTPUT";
|
|
@@ -69,48 +26,45 @@ var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
|
69
26
|
Rounding2[Rounding2["ROUND_UP"] = 2] = "ROUND_UP";
|
|
70
27
|
return Rounding2;
|
|
71
28
|
})(Rounding || {});
|
|
72
|
-
var MINIMUM_LIQUIDITY =
|
|
73
|
-
var ZERO =
|
|
74
|
-
var ONE =
|
|
75
|
-
var TWO =
|
|
76
|
-
var THREE =
|
|
77
|
-
var FIVE =
|
|
78
|
-
var TEN =
|
|
79
|
-
var _100 =
|
|
80
|
-
var _9975 =
|
|
81
|
-
var _10000 =
|
|
82
|
-
var MaxUint256 =
|
|
83
|
-
var VMType = /* @__PURE__ */ ((
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return
|
|
29
|
+
var MINIMUM_LIQUIDITY = JSBI2__default.default.BigInt(1e3);
|
|
30
|
+
var ZERO = JSBI2__default.default.BigInt(0);
|
|
31
|
+
var ONE = JSBI2__default.default.BigInt(1);
|
|
32
|
+
var TWO = JSBI2__default.default.BigInt(2);
|
|
33
|
+
var THREE = JSBI2__default.default.BigInt(3);
|
|
34
|
+
var FIVE = JSBI2__default.default.BigInt(5);
|
|
35
|
+
var TEN = JSBI2__default.default.BigInt(10);
|
|
36
|
+
var _100 = JSBI2__default.default.BigInt(100);
|
|
37
|
+
var _9975 = JSBI2__default.default.BigInt(9975);
|
|
38
|
+
var _10000 = JSBI2__default.default.BigInt(1e4);
|
|
39
|
+
var MaxUint256 = JSBI2__default.default.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
40
|
+
var VMType = /* @__PURE__ */ ((VMType3) => {
|
|
41
|
+
VMType3["uint8"] = "uint8";
|
|
42
|
+
VMType3["uint256"] = "uint256";
|
|
43
|
+
return VMType3;
|
|
87
44
|
})(VMType || {});
|
|
88
45
|
var VM_TYPE_MAXIMA = {
|
|
89
|
-
["uint8" /* uint8 */]:
|
|
90
|
-
["uint256" /* uint256 */]:
|
|
46
|
+
["uint8" /* uint8 */]: JSBI2__default.default.BigInt("0xff"),
|
|
47
|
+
["uint256" /* uint256 */]: JSBI2__default.default.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
|
91
48
|
};
|
|
92
|
-
|
|
93
|
-
// src/baseCurrency.ts
|
|
94
|
-
var import_tiny_invariant = __toESM(require("tiny-invariant"));
|
|
95
49
|
var BaseCurrency = class {
|
|
50
|
+
/**
|
|
51
|
+
* Constructs an instance of the base class `BaseCurrency`.
|
|
52
|
+
* @param chainId the chain ID on which this currency resides
|
|
53
|
+
* @param decimals decimals of the currency
|
|
54
|
+
* @param symbol symbol of the currency
|
|
55
|
+
* @param name of the currency
|
|
56
|
+
*/
|
|
96
57
|
constructor(chainId, decimals, symbol, name) {
|
|
97
|
-
|
|
98
|
-
|
|
58
|
+
invariant6__default.default(Number.isSafeInteger(chainId), "CHAIN_ID");
|
|
59
|
+
invariant6__default.default(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
|
|
99
60
|
this.chainId = chainId;
|
|
100
61
|
this.decimals = decimals;
|
|
101
62
|
this.symbol = symbol;
|
|
102
63
|
this.name = name;
|
|
103
64
|
}
|
|
104
65
|
};
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
var import_jsbi2 = __toESM(require("jsbi"));
|
|
108
|
-
var import_tiny_invariant2 = __toESM(require("tiny-invariant"));
|
|
109
|
-
var import_decimal = __toESM(require("decimal.js-light"));
|
|
110
|
-
var import_big = __toESM(require("big.js"));
|
|
111
|
-
var import_toformat = __toESM(require("toformat"));
|
|
112
|
-
var Decimal = (0, import_toformat.default)(import_decimal.default);
|
|
113
|
-
var Big = (0, import_toformat.default)(import_big.default);
|
|
66
|
+
var Decimal = toFormat__default.default(_Decimal__default.default);
|
|
67
|
+
var Big = toFormat__default.default(_Big__default.default);
|
|
114
68
|
var toSignificantRounding = {
|
|
115
69
|
[0 /* ROUND_DOWN */]: Decimal.ROUND_DOWN,
|
|
116
70
|
[1 /* ROUND_HALF_UP */]: Decimal.ROUND_HALF_UP,
|
|
@@ -122,88 +76,120 @@ var toFixedRounding = {
|
|
|
122
76
|
[2 /* ROUND_UP */]: 3 /* RoundUp */
|
|
123
77
|
};
|
|
124
78
|
var Fraction = class {
|
|
125
|
-
constructor(numerator, denominator =
|
|
126
|
-
this.numerator =
|
|
127
|
-
this.denominator =
|
|
79
|
+
constructor(numerator, denominator = JSBI2__default.default.BigInt(1)) {
|
|
80
|
+
this.numerator = JSBI2__default.default.BigInt(numerator);
|
|
81
|
+
this.denominator = JSBI2__default.default.BigInt(denominator);
|
|
128
82
|
}
|
|
129
83
|
static tryParseFraction(fractionish) {
|
|
130
|
-
if (fractionish instanceof
|
|
84
|
+
if (fractionish instanceof JSBI2__default.default || typeof fractionish === "number" || typeof fractionish === "string")
|
|
131
85
|
return new Fraction(fractionish);
|
|
132
86
|
if ("numerator" in fractionish && "denominator" in fractionish)
|
|
133
87
|
return fractionish;
|
|
134
88
|
throw new Error("Could not parse fraction");
|
|
135
89
|
}
|
|
90
|
+
// performs floor division
|
|
136
91
|
get quotient() {
|
|
137
|
-
return
|
|
92
|
+
return JSBI2__default.default.divide(this.numerator, this.denominator);
|
|
138
93
|
}
|
|
94
|
+
// remainder after floor division
|
|
139
95
|
get remainder() {
|
|
140
|
-
return new Fraction(
|
|
96
|
+
return new Fraction(JSBI2__default.default.remainder(this.numerator, this.denominator), this.denominator);
|
|
141
97
|
}
|
|
142
98
|
invert() {
|
|
143
99
|
return new Fraction(this.denominator, this.numerator);
|
|
144
100
|
}
|
|
145
101
|
add(other) {
|
|
146
102
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
147
|
-
if (
|
|
148
|
-
return new Fraction(
|
|
103
|
+
if (JSBI2__default.default.equal(this.denominator, otherParsed.denominator)) {
|
|
104
|
+
return new Fraction(JSBI2__default.default.add(this.numerator, otherParsed.numerator), this.denominator);
|
|
149
105
|
}
|
|
150
|
-
return new Fraction(
|
|
106
|
+
return new Fraction(
|
|
107
|
+
JSBI2__default.default.add(
|
|
108
|
+
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
|
|
109
|
+
JSBI2__default.default.multiply(otherParsed.numerator, this.denominator)
|
|
110
|
+
),
|
|
111
|
+
JSBI2__default.default.multiply(this.denominator, otherParsed.denominator)
|
|
112
|
+
);
|
|
151
113
|
}
|
|
152
114
|
subtract(other) {
|
|
153
115
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
154
|
-
if (
|
|
155
|
-
return new Fraction(
|
|
116
|
+
if (JSBI2__default.default.equal(this.denominator, otherParsed.denominator)) {
|
|
117
|
+
return new Fraction(JSBI2__default.default.subtract(this.numerator, otherParsed.numerator), this.denominator);
|
|
156
118
|
}
|
|
157
|
-
return new Fraction(
|
|
119
|
+
return new Fraction(
|
|
120
|
+
JSBI2__default.default.subtract(
|
|
121
|
+
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
|
|
122
|
+
JSBI2__default.default.multiply(otherParsed.numerator, this.denominator)
|
|
123
|
+
),
|
|
124
|
+
JSBI2__default.default.multiply(this.denominator, otherParsed.denominator)
|
|
125
|
+
);
|
|
158
126
|
}
|
|
159
127
|
lessThan(other) {
|
|
160
128
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
161
|
-
return
|
|
129
|
+
return JSBI2__default.default.lessThan(
|
|
130
|
+
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
|
|
131
|
+
JSBI2__default.default.multiply(otherParsed.numerator, this.denominator)
|
|
132
|
+
);
|
|
162
133
|
}
|
|
163
134
|
equalTo(other) {
|
|
164
135
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
165
|
-
return
|
|
136
|
+
return JSBI2__default.default.equal(
|
|
137
|
+
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
|
|
138
|
+
JSBI2__default.default.multiply(otherParsed.numerator, this.denominator)
|
|
139
|
+
);
|
|
166
140
|
}
|
|
167
141
|
greaterThan(other) {
|
|
168
142
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
169
|
-
return
|
|
143
|
+
return JSBI2__default.default.greaterThan(
|
|
144
|
+
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
|
|
145
|
+
JSBI2__default.default.multiply(otherParsed.numerator, this.denominator)
|
|
146
|
+
);
|
|
170
147
|
}
|
|
171
148
|
multiply(other) {
|
|
172
149
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
173
|
-
return new Fraction(
|
|
150
|
+
return new Fraction(
|
|
151
|
+
JSBI2__default.default.multiply(this.numerator, otherParsed.numerator),
|
|
152
|
+
JSBI2__default.default.multiply(this.denominator, otherParsed.denominator)
|
|
153
|
+
);
|
|
174
154
|
}
|
|
175
155
|
divide(other) {
|
|
176
156
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
177
|
-
return new Fraction(
|
|
157
|
+
return new Fraction(
|
|
158
|
+
JSBI2__default.default.multiply(this.numerator, otherParsed.denominator),
|
|
159
|
+
JSBI2__default.default.multiply(this.denominator, otherParsed.numerator)
|
|
160
|
+
);
|
|
178
161
|
}
|
|
179
162
|
toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
180
|
-
|
|
181
|
-
|
|
163
|
+
invariant6__default.default(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
|
|
164
|
+
invariant6__default.default(significantDigits > 0, `${significantDigits} is not positive.`);
|
|
182
165
|
Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });
|
|
183
166
|
const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
|
|
184
167
|
return quotient.toFormat(quotient.decimalPlaces(), format);
|
|
185
168
|
}
|
|
186
169
|
toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
187
|
-
|
|
188
|
-
|
|
170
|
+
invariant6__default.default(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
|
|
171
|
+
invariant6__default.default(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
|
|
189
172
|
Big.DP = decimalPlaces;
|
|
190
173
|
Big.RM = toFixedRounding[rounding];
|
|
191
174
|
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
|
|
192
175
|
}
|
|
176
|
+
/**
|
|
177
|
+
* Helper method for converting any super class back to a fraction
|
|
178
|
+
*/
|
|
193
179
|
get asFraction() {
|
|
194
180
|
return new Fraction(this.numerator, this.denominator);
|
|
195
181
|
}
|
|
196
182
|
};
|
|
197
|
-
|
|
198
|
-
// src/fractions/percent.ts
|
|
199
|
-
var import_jsbi3 = __toESM(require("jsbi"));
|
|
200
|
-
var ONE_HUNDRED = new Fraction(import_jsbi3.default.BigInt(100));
|
|
183
|
+
var ONE_HUNDRED = new Fraction(JSBI2__default.default.BigInt(100));
|
|
201
184
|
function toPercent(fraction) {
|
|
202
185
|
return new Percent(fraction.numerator, fraction.denominator);
|
|
203
186
|
}
|
|
204
187
|
var Percent = class extends Fraction {
|
|
205
188
|
constructor() {
|
|
206
189
|
super(...arguments);
|
|
190
|
+
/**
|
|
191
|
+
* This boolean prevents a fraction from being interpreted as a Percent
|
|
192
|
+
*/
|
|
207
193
|
this.isPercent = true;
|
|
208
194
|
}
|
|
209
195
|
add(other) {
|
|
@@ -225,33 +211,38 @@ var Percent = class extends Fraction {
|
|
|
225
211
|
return super.multiply(ONE_HUNDRED).toFixed(decimalPlaces, format, rounding);
|
|
226
212
|
}
|
|
227
213
|
};
|
|
228
|
-
|
|
229
|
-
// src/fractions/currencyAmount.ts
|
|
230
|
-
var import_tiny_invariant3 = __toESM(require("tiny-invariant"));
|
|
231
|
-
var import_jsbi4 = __toESM(require("jsbi"));
|
|
232
|
-
var import_big2 = __toESM(require("big.js"));
|
|
233
|
-
var import_toformat2 = __toESM(require("toformat"));
|
|
234
|
-
var Big2 = (0, import_toformat2.default)(import_big2.default);
|
|
214
|
+
var Big2 = toFormat__default.default(_Big__default.default);
|
|
235
215
|
var CurrencyAmount = class extends Fraction {
|
|
236
216
|
constructor(currency, numerator, denominator) {
|
|
237
217
|
super(numerator, denominator);
|
|
238
|
-
|
|
218
|
+
invariant6__default.default(JSBI2__default.default.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT");
|
|
239
219
|
this.currency = currency;
|
|
240
|
-
this.decimalScale =
|
|
220
|
+
this.decimalScale = JSBI2__default.default.exponentiate(JSBI2__default.default.BigInt(10), JSBI2__default.default.BigInt(currency.decimals));
|
|
241
221
|
}
|
|
222
|
+
/**
|
|
223
|
+
* Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
|
|
224
|
+
* @param currency the currency in the amount
|
|
225
|
+
* @param rawAmount the raw token or ether amount
|
|
226
|
+
*/
|
|
242
227
|
static fromRawAmount(currency, rawAmount) {
|
|
243
228
|
return new CurrencyAmount(currency, rawAmount);
|
|
244
229
|
}
|
|
230
|
+
/**
|
|
231
|
+
* Construct a currency amount with a denominator that is not equal to 1
|
|
232
|
+
* @param currency the currency
|
|
233
|
+
* @param numerator the numerator of the fractional token amount
|
|
234
|
+
* @param denominator the denominator of the fractional token amount
|
|
235
|
+
*/
|
|
245
236
|
static fromFractionalAmount(currency, numerator, denominator) {
|
|
246
237
|
return new CurrencyAmount(currency, numerator, denominator);
|
|
247
238
|
}
|
|
248
239
|
add(other) {
|
|
249
|
-
|
|
240
|
+
invariant6__default.default(this.currency.equals(other.currency), "CURRENCY");
|
|
250
241
|
const added = super.add(other);
|
|
251
242
|
return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
|
|
252
243
|
}
|
|
253
244
|
subtract(other) {
|
|
254
|
-
|
|
245
|
+
invariant6__default.default(this.currency.equals(other.currency), "CURRENCY");
|
|
255
246
|
const subtracted = super.subtract(other);
|
|
256
247
|
return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
|
|
257
248
|
}
|
|
@@ -267,7 +258,7 @@ var CurrencyAmount = class extends Fraction {
|
|
|
267
258
|
return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
|
|
268
259
|
}
|
|
269
260
|
toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
|
|
270
|
-
|
|
261
|
+
invariant6__default.default(decimalPlaces <= this.currency.decimals, "DECIMALS");
|
|
271
262
|
return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
|
|
272
263
|
}
|
|
273
264
|
toExact(format = { groupSeparator: "" }) {
|
|
@@ -280,18 +271,18 @@ var CurrencyAmount = class extends Fraction {
|
|
|
280
271
|
return CurrencyAmount.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator);
|
|
281
272
|
}
|
|
282
273
|
};
|
|
283
|
-
|
|
284
|
-
// src/fractions/price.ts
|
|
285
|
-
var import_jsbi5 = __toESM(require("jsbi"));
|
|
286
|
-
var import_tiny_invariant4 = __toESM(require("tiny-invariant"));
|
|
287
274
|
var Price = class extends Fraction {
|
|
275
|
+
// used to adjust the raw fraction w/r/t the decimals of the {base,quote}Token
|
|
276
|
+
/**
|
|
277
|
+
* Construct a price, either with the base and quote currency amount, or the
|
|
278
|
+
* @param args
|
|
279
|
+
*/
|
|
288
280
|
constructor(...args) {
|
|
289
281
|
let baseCurrency;
|
|
290
282
|
let quoteCurrency;
|
|
291
283
|
let denominator;
|
|
292
284
|
let numerator;
|
|
293
285
|
if (args.length === 4) {
|
|
294
|
-
;
|
|
295
286
|
[baseCurrency, quoteCurrency, denominator, numerator] = args;
|
|
296
287
|
} else {
|
|
297
288
|
const result = args[0].quoteAmount.divide(args[0].baseAmount);
|
|
@@ -305,21 +296,39 @@ var Price = class extends Fraction {
|
|
|
305
296
|
super(numerator, denominator);
|
|
306
297
|
this.baseCurrency = baseCurrency;
|
|
307
298
|
this.quoteCurrency = quoteCurrency;
|
|
308
|
-
this.scalar = new Fraction(
|
|
309
|
-
|
|
299
|
+
this.scalar = new Fraction(
|
|
300
|
+
JSBI2__default.default.exponentiate(JSBI2__default.default.BigInt(10), JSBI2__default.default.BigInt(baseCurrency.decimals)),
|
|
301
|
+
JSBI2__default.default.exponentiate(JSBI2__default.default.BigInt(10), JSBI2__default.default.BigInt(quoteCurrency.decimals))
|
|
302
|
+
);
|
|
303
|
+
}
|
|
304
|
+
/**
|
|
305
|
+
* Flip the price, switching the base and quote currency
|
|
306
|
+
*/
|
|
310
307
|
invert() {
|
|
311
308
|
return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
|
|
312
309
|
}
|
|
310
|
+
/**
|
|
311
|
+
* 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
|
|
312
|
+
* @param other the other price
|
|
313
|
+
*/
|
|
313
314
|
multiply(other) {
|
|
314
|
-
|
|
315
|
+
invariant6__default.default(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
|
|
315
316
|
const fraction = super.multiply(other);
|
|
316
317
|
return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
|
|
317
318
|
}
|
|
319
|
+
/**
|
|
320
|
+
* Return the amount of quote currency corresponding to a given amount of the base currency
|
|
321
|
+
* @param currencyAmount the amount of base currency to quote against the price
|
|
322
|
+
*/
|
|
318
323
|
quote(currencyAmount) {
|
|
319
|
-
|
|
324
|
+
invariant6__default.default(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
|
|
320
325
|
const result = super.multiply(currencyAmount);
|
|
321
326
|
return CurrencyAmount.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
|
|
322
327
|
}
|
|
328
|
+
/**
|
|
329
|
+
* Get the value scaled by decimals for formatting
|
|
330
|
+
* @private
|
|
331
|
+
*/
|
|
323
332
|
get adjustedForDecimals() {
|
|
324
333
|
return super.multiply(this.scalar);
|
|
325
334
|
}
|
|
@@ -339,9 +348,6 @@ var NativeCurrency = class extends BaseCurrency {
|
|
|
339
348
|
this.isToken = false;
|
|
340
349
|
}
|
|
341
350
|
};
|
|
342
|
-
|
|
343
|
-
// src/token.ts
|
|
344
|
-
var import_tiny_invariant5 = __toESM(require("tiny-invariant"));
|
|
345
351
|
var Token = class extends BaseCurrency {
|
|
346
352
|
constructor(chainId, address, decimals, symbol, name, projectLink) {
|
|
347
353
|
super(chainId, decimals, symbol, name);
|
|
@@ -350,14 +356,27 @@ var Token = class extends BaseCurrency {
|
|
|
350
356
|
this.address = address;
|
|
351
357
|
this.projectLink = projectLink;
|
|
352
358
|
}
|
|
359
|
+
/**
|
|
360
|
+
* Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
|
|
361
|
+
* @param other other token to compare
|
|
362
|
+
*/
|
|
353
363
|
equals(other) {
|
|
354
364
|
return other.isToken && this.chainId === other.chainId && this.address === other.address;
|
|
355
365
|
}
|
|
366
|
+
/**
|
|
367
|
+
* Returns true if the address of this token sorts before the address of the other token
|
|
368
|
+
* @param other other token to compare
|
|
369
|
+
* @throws if the tokens have the same address
|
|
370
|
+
* @throws if the tokens are on different chains
|
|
371
|
+
*/
|
|
356
372
|
sortsBefore(other) {
|
|
357
|
-
|
|
358
|
-
|
|
373
|
+
invariant6__default.default(this.chainId === other.chainId, "CHAIN_IDS");
|
|
374
|
+
invariant6__default.default(this.address !== other.address, "ADDRESSES");
|
|
359
375
|
return this.address.toLowerCase() < other.address.toLowerCase();
|
|
360
376
|
}
|
|
377
|
+
/**
|
|
378
|
+
* Return this token, which does not need to be wrapped
|
|
379
|
+
*/
|
|
361
380
|
get wrapped() {
|
|
362
381
|
return this;
|
|
363
382
|
}
|
|
@@ -393,33 +412,29 @@ var InsufficientInputAmountError = class extends Error {
|
|
|
393
412
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
394
413
|
}
|
|
395
414
|
};
|
|
396
|
-
|
|
397
|
-
// src/utils.ts
|
|
398
|
-
var import_jsbi6 = __toESM(require("jsbi"));
|
|
399
|
-
var import_tiny_invariant6 = __toESM(require("tiny-invariant"));
|
|
400
415
|
function validateVMTypeInstance(value, vmType) {
|
|
401
|
-
|
|
402
|
-
|
|
416
|
+
invariant6__default.default(JSBI2__default.default.greaterThanOrEqual(value, ZERO), `${value} is not a ${vmType}.`);
|
|
417
|
+
invariant6__default.default(JSBI2__default.default.lessThanOrEqual(value, VM_TYPE_MAXIMA[vmType]), `${value} is not a ${vmType}.`);
|
|
403
418
|
}
|
|
404
419
|
function sqrt(y) {
|
|
405
|
-
|
|
420
|
+
invariant6__default.default(JSBI2__default.default.greaterThanOrEqual(y, ZERO), "NEGATIVE");
|
|
406
421
|
let z = ZERO;
|
|
407
422
|
let x;
|
|
408
|
-
if (
|
|
423
|
+
if (JSBI2__default.default.greaterThan(y, THREE)) {
|
|
409
424
|
z = y;
|
|
410
|
-
x =
|
|
411
|
-
while (
|
|
425
|
+
x = JSBI2__default.default.add(JSBI2__default.default.divide(y, TWO), ONE);
|
|
426
|
+
while (JSBI2__default.default.lessThan(x, z)) {
|
|
412
427
|
z = x;
|
|
413
|
-
x =
|
|
428
|
+
x = JSBI2__default.default.divide(JSBI2__default.default.add(JSBI2__default.default.divide(y, x), x), TWO);
|
|
414
429
|
}
|
|
415
|
-
} else if (
|
|
430
|
+
} else if (JSBI2__default.default.notEqual(y, ZERO)) {
|
|
416
431
|
z = ONE;
|
|
417
432
|
}
|
|
418
433
|
return z;
|
|
419
434
|
}
|
|
420
435
|
function sortedInsert(items, add, maxSize, comparator) {
|
|
421
|
-
|
|
422
|
-
|
|
436
|
+
invariant6__default.default(maxSize > 0, "MAX_SIZE_ZERO");
|
|
437
|
+
invariant6__default.default(items.length <= maxSize, "ITEMS_SIZE");
|
|
423
438
|
if (items.length === 0) {
|
|
424
439
|
items.push(add);
|
|
425
440
|
return null;
|
|
@@ -446,35 +461,62 @@ function computePriceImpact(midPrice, inputAmount, outputAmount) {
|
|
|
446
461
|
const priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount);
|
|
447
462
|
return new Percent(priceImpact.numerator, priceImpact.denominator);
|
|
448
463
|
}
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
sqrt,
|
|
479
|
-
validateVMTypeInstance
|
|
464
|
+
function balanceComparator(balanceA, balanceB) {
|
|
465
|
+
if (balanceA && balanceB) {
|
|
466
|
+
return balanceA.greaterThan(balanceB) ? -1 : balanceA.equalTo(balanceB) ? 0 : 1;
|
|
467
|
+
}
|
|
468
|
+
if (balanceA && balanceA.greaterThan("0")) {
|
|
469
|
+
return -1;
|
|
470
|
+
}
|
|
471
|
+
if (balanceB && balanceB.greaterThan("0")) {
|
|
472
|
+
return 1;
|
|
473
|
+
}
|
|
474
|
+
return 0;
|
|
475
|
+
}
|
|
476
|
+
function getTokenComparator(balances) {
|
|
477
|
+
return function sortTokens(tokenA, tokenB) {
|
|
478
|
+
const balanceA = balances[tokenA.address];
|
|
479
|
+
const balanceB = balances[tokenB.address];
|
|
480
|
+
const balanceComp = balanceComparator(balanceA, balanceB);
|
|
481
|
+
if (balanceComp !== 0)
|
|
482
|
+
return balanceComp;
|
|
483
|
+
if (tokenA.symbol && tokenB.symbol) {
|
|
484
|
+
return tokenA.symbol.toLowerCase() < tokenB.symbol.toLowerCase() ? -1 : 1;
|
|
485
|
+
}
|
|
486
|
+
return tokenA.symbol ? -1 : tokenB.symbol ? -1 : 0;
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
Object.defineProperty(exports, 'JSBI', {
|
|
491
|
+
enumerable: true,
|
|
492
|
+
get: function () { return JSBI2__default.default; }
|
|
480
493
|
});
|
|
494
|
+
exports.BaseCurrency = BaseCurrency;
|
|
495
|
+
exports.CurrencyAmount = CurrencyAmount;
|
|
496
|
+
exports.FIVE = FIVE;
|
|
497
|
+
exports.Fraction = Fraction;
|
|
498
|
+
exports.InsufficientInputAmountError = InsufficientInputAmountError;
|
|
499
|
+
exports.InsufficientReservesError = InsufficientReservesError;
|
|
500
|
+
exports.MINIMUM_LIQUIDITY = MINIMUM_LIQUIDITY;
|
|
501
|
+
exports.MaxUint256 = MaxUint256;
|
|
502
|
+
exports.NativeCurrency = NativeCurrency;
|
|
503
|
+
exports.ONE = ONE;
|
|
504
|
+
exports.Percent = Percent;
|
|
505
|
+
exports.Price = Price;
|
|
506
|
+
exports.Rounding = Rounding;
|
|
507
|
+
exports.TEN = TEN;
|
|
508
|
+
exports.THREE = THREE;
|
|
509
|
+
exports.TWO = TWO;
|
|
510
|
+
exports.Token = Token;
|
|
511
|
+
exports.TradeType = TradeType;
|
|
512
|
+
exports.VMType = VMType;
|
|
513
|
+
exports.VM_TYPE_MAXIMA = VM_TYPE_MAXIMA;
|
|
514
|
+
exports.ZERO = ZERO;
|
|
515
|
+
exports._100 = _100;
|
|
516
|
+
exports._10000 = _10000;
|
|
517
|
+
exports._9975 = _9975;
|
|
518
|
+
exports.computePriceImpact = computePriceImpact;
|
|
519
|
+
exports.getTokenComparator = getTokenComparator;
|
|
520
|
+
exports.sortedInsert = sortedInsert;
|
|
521
|
+
exports.sqrt = sqrt;
|
|
522
|
+
exports.validateVMTypeInstance = validateVMTypeInstance;
|
package/dist/index.mjs
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import JSBI2 from 'jsbi';
|
|
2
|
+
export { default as JSBI } from 'jsbi';
|
|
3
|
+
import invariant6 from 'tiny-invariant';
|
|
4
|
+
import _Decimal from 'decimal.js-light';
|
|
5
|
+
import _Big from 'big.js';
|
|
6
|
+
import toFormat from 'toformat';
|
|
3
7
|
|
|
4
|
-
// src/
|
|
5
|
-
import JSBI from "jsbi";
|
|
8
|
+
// src/index.ts
|
|
6
9
|
var TradeType = /* @__PURE__ */ ((TradeType2) => {
|
|
7
10
|
TradeType2[TradeType2["EXACT_INPUT"] = 0] = "EXACT_INPUT";
|
|
8
11
|
TradeType2[TradeType2["EXACT_OUTPUT"] = 1] = "EXACT_OUTPUT";
|
|
@@ -14,46 +17,43 @@ var Rounding = /* @__PURE__ */ ((Rounding2) => {
|
|
|
14
17
|
Rounding2[Rounding2["ROUND_UP"] = 2] = "ROUND_UP";
|
|
15
18
|
return Rounding2;
|
|
16
19
|
})(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
|
|
20
|
+
var MINIMUM_LIQUIDITY = JSBI2.BigInt(1e3);
|
|
21
|
+
var ZERO = JSBI2.BigInt(0);
|
|
22
|
+
var ONE = JSBI2.BigInt(1);
|
|
23
|
+
var TWO = JSBI2.BigInt(2);
|
|
24
|
+
var THREE = JSBI2.BigInt(3);
|
|
25
|
+
var FIVE = JSBI2.BigInt(5);
|
|
26
|
+
var TEN = JSBI2.BigInt(10);
|
|
27
|
+
var _100 = JSBI2.BigInt(100);
|
|
28
|
+
var _9975 = JSBI2.BigInt(9975);
|
|
29
|
+
var _10000 = JSBI2.BigInt(1e4);
|
|
30
|
+
var MaxUint256 = JSBI2.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
31
|
+
var VMType = /* @__PURE__ */ ((VMType3) => {
|
|
32
|
+
VMType3["uint8"] = "uint8";
|
|
33
|
+
VMType3["uint256"] = "uint256";
|
|
34
|
+
return VMType3;
|
|
32
35
|
})(VMType || {});
|
|
33
36
|
var VM_TYPE_MAXIMA = {
|
|
34
|
-
["uint8" /* uint8 */]:
|
|
35
|
-
["uint256" /* uint256 */]:
|
|
37
|
+
["uint8" /* uint8 */]: JSBI2.BigInt("0xff"),
|
|
38
|
+
["uint256" /* uint256 */]: JSBI2.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
|
|
36
39
|
};
|
|
37
|
-
|
|
38
|
-
// src/baseCurrency.ts
|
|
39
|
-
import invariant from "tiny-invariant";
|
|
40
40
|
var BaseCurrency = class {
|
|
41
|
+
/**
|
|
42
|
+
* Constructs an instance of the base class `BaseCurrency`.
|
|
43
|
+
* @param chainId the chain ID on which this currency resides
|
|
44
|
+
* @param decimals decimals of the currency
|
|
45
|
+
* @param symbol symbol of the currency
|
|
46
|
+
* @param name of the currency
|
|
47
|
+
*/
|
|
41
48
|
constructor(chainId, decimals, symbol, name) {
|
|
42
|
-
|
|
43
|
-
|
|
49
|
+
invariant6(Number.isSafeInteger(chainId), "CHAIN_ID");
|
|
50
|
+
invariant6(decimals >= 0 && decimals < 255 && Number.isInteger(decimals), "DECIMALS");
|
|
44
51
|
this.chainId = chainId;
|
|
45
52
|
this.decimals = decimals;
|
|
46
53
|
this.symbol = symbol;
|
|
47
54
|
this.name = name;
|
|
48
55
|
}
|
|
49
56
|
};
|
|
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
57
|
var Decimal = toFormat(_Decimal);
|
|
58
58
|
var Big = toFormat(_Big);
|
|
59
59
|
var toSignificantRounding = {
|
|
@@ -78,9 +78,11 @@ var Fraction = class {
|
|
|
78
78
|
return fractionish;
|
|
79
79
|
throw new Error("Could not parse fraction");
|
|
80
80
|
}
|
|
81
|
+
// performs floor division
|
|
81
82
|
get quotient() {
|
|
82
83
|
return JSBI2.divide(this.numerator, this.denominator);
|
|
83
84
|
}
|
|
85
|
+
// remainder after floor division
|
|
84
86
|
get remainder() {
|
|
85
87
|
return new Fraction(JSBI2.remainder(this.numerator, this.denominator), this.denominator);
|
|
86
88
|
}
|
|
@@ -92,63 +94,93 @@ var Fraction = class {
|
|
|
92
94
|
if (JSBI2.equal(this.denominator, otherParsed.denominator)) {
|
|
93
95
|
return new Fraction(JSBI2.add(this.numerator, otherParsed.numerator), this.denominator);
|
|
94
96
|
}
|
|
95
|
-
return new Fraction(
|
|
97
|
+
return new Fraction(
|
|
98
|
+
JSBI2.add(
|
|
99
|
+
JSBI2.multiply(this.numerator, otherParsed.denominator),
|
|
100
|
+
JSBI2.multiply(otherParsed.numerator, this.denominator)
|
|
101
|
+
),
|
|
102
|
+
JSBI2.multiply(this.denominator, otherParsed.denominator)
|
|
103
|
+
);
|
|
96
104
|
}
|
|
97
105
|
subtract(other) {
|
|
98
106
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
99
107
|
if (JSBI2.equal(this.denominator, otherParsed.denominator)) {
|
|
100
108
|
return new Fraction(JSBI2.subtract(this.numerator, otherParsed.numerator), this.denominator);
|
|
101
109
|
}
|
|
102
|
-
return new Fraction(
|
|
110
|
+
return new Fraction(
|
|
111
|
+
JSBI2.subtract(
|
|
112
|
+
JSBI2.multiply(this.numerator, otherParsed.denominator),
|
|
113
|
+
JSBI2.multiply(otherParsed.numerator, this.denominator)
|
|
114
|
+
),
|
|
115
|
+
JSBI2.multiply(this.denominator, otherParsed.denominator)
|
|
116
|
+
);
|
|
103
117
|
}
|
|
104
118
|
lessThan(other) {
|
|
105
119
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
106
|
-
return JSBI2.lessThan(
|
|
120
|
+
return JSBI2.lessThan(
|
|
121
|
+
JSBI2.multiply(this.numerator, otherParsed.denominator),
|
|
122
|
+
JSBI2.multiply(otherParsed.numerator, this.denominator)
|
|
123
|
+
);
|
|
107
124
|
}
|
|
108
125
|
equalTo(other) {
|
|
109
126
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
110
|
-
return JSBI2.equal(
|
|
127
|
+
return JSBI2.equal(
|
|
128
|
+
JSBI2.multiply(this.numerator, otherParsed.denominator),
|
|
129
|
+
JSBI2.multiply(otherParsed.numerator, this.denominator)
|
|
130
|
+
);
|
|
111
131
|
}
|
|
112
132
|
greaterThan(other) {
|
|
113
133
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
114
|
-
return JSBI2.greaterThan(
|
|
134
|
+
return JSBI2.greaterThan(
|
|
135
|
+
JSBI2.multiply(this.numerator, otherParsed.denominator),
|
|
136
|
+
JSBI2.multiply(otherParsed.numerator, this.denominator)
|
|
137
|
+
);
|
|
115
138
|
}
|
|
116
139
|
multiply(other) {
|
|
117
140
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
118
|
-
return new Fraction(
|
|
141
|
+
return new Fraction(
|
|
142
|
+
JSBI2.multiply(this.numerator, otherParsed.numerator),
|
|
143
|
+
JSBI2.multiply(this.denominator, otherParsed.denominator)
|
|
144
|
+
);
|
|
119
145
|
}
|
|
120
146
|
divide(other) {
|
|
121
147
|
const otherParsed = Fraction.tryParseFraction(other);
|
|
122
|
-
return new Fraction(
|
|
148
|
+
return new Fraction(
|
|
149
|
+
JSBI2.multiply(this.numerator, otherParsed.denominator),
|
|
150
|
+
JSBI2.multiply(this.denominator, otherParsed.numerator)
|
|
151
|
+
);
|
|
123
152
|
}
|
|
124
153
|
toSignificant(significantDigits, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
125
|
-
|
|
126
|
-
|
|
154
|
+
invariant6(Number.isInteger(significantDigits), `${significantDigits} is not an integer.`);
|
|
155
|
+
invariant6(significantDigits > 0, `${significantDigits} is not positive.`);
|
|
127
156
|
Decimal.set({ precision: significantDigits + 1, rounding: toSignificantRounding[rounding] });
|
|
128
157
|
const quotient = new Decimal(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(significantDigits);
|
|
129
158
|
return quotient.toFormat(quotient.decimalPlaces(), format);
|
|
130
159
|
}
|
|
131
160
|
toFixed(decimalPlaces, format = { groupSeparator: "" }, rounding = 1 /* ROUND_HALF_UP */) {
|
|
132
|
-
|
|
133
|
-
|
|
161
|
+
invariant6(Number.isInteger(decimalPlaces), `${decimalPlaces} is not an integer.`);
|
|
162
|
+
invariant6(decimalPlaces >= 0, `${decimalPlaces} is negative.`);
|
|
134
163
|
Big.DP = decimalPlaces;
|
|
135
164
|
Big.RM = toFixedRounding[rounding];
|
|
136
165
|
return new Big(this.numerator.toString()).div(this.denominator.toString()).toFormat(decimalPlaces, format);
|
|
137
166
|
}
|
|
167
|
+
/**
|
|
168
|
+
* Helper method for converting any super class back to a fraction
|
|
169
|
+
*/
|
|
138
170
|
get asFraction() {
|
|
139
171
|
return new Fraction(this.numerator, this.denominator);
|
|
140
172
|
}
|
|
141
173
|
};
|
|
142
|
-
|
|
143
|
-
// src/fractions/percent.ts
|
|
144
|
-
import JSBI3 from "jsbi";
|
|
145
|
-
var ONE_HUNDRED = new Fraction(JSBI3.BigInt(100));
|
|
174
|
+
var ONE_HUNDRED = new Fraction(JSBI2.BigInt(100));
|
|
146
175
|
function toPercent(fraction) {
|
|
147
176
|
return new Percent(fraction.numerator, fraction.denominator);
|
|
148
177
|
}
|
|
149
178
|
var Percent = class extends Fraction {
|
|
150
179
|
constructor() {
|
|
151
180
|
super(...arguments);
|
|
181
|
+
/**
|
|
182
|
+
* This boolean prevents a fraction from being interpreted as a Percent
|
|
183
|
+
*/
|
|
152
184
|
this.isPercent = true;
|
|
153
185
|
}
|
|
154
186
|
add(other) {
|
|
@@ -170,33 +202,38 @@ var Percent = class extends Fraction {
|
|
|
170
202
|
return super.multiply(ONE_HUNDRED).toFixed(decimalPlaces, format, rounding);
|
|
171
203
|
}
|
|
172
204
|
};
|
|
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);
|
|
205
|
+
var Big2 = toFormat(_Big);
|
|
180
206
|
var CurrencyAmount = class extends Fraction {
|
|
181
207
|
constructor(currency, numerator, denominator) {
|
|
182
208
|
super(numerator, denominator);
|
|
183
|
-
|
|
209
|
+
invariant6(JSBI2.lessThanOrEqual(this.quotient, MaxUint256), "AMOUNT");
|
|
184
210
|
this.currency = currency;
|
|
185
|
-
this.decimalScale =
|
|
211
|
+
this.decimalScale = JSBI2.exponentiate(JSBI2.BigInt(10), JSBI2.BigInt(currency.decimals));
|
|
186
212
|
}
|
|
213
|
+
/**
|
|
214
|
+
* Returns a new currency amount instance from the unitless amount of token, i.e. the raw amount
|
|
215
|
+
* @param currency the currency in the amount
|
|
216
|
+
* @param rawAmount the raw token or ether amount
|
|
217
|
+
*/
|
|
187
218
|
static fromRawAmount(currency, rawAmount) {
|
|
188
219
|
return new CurrencyAmount(currency, rawAmount);
|
|
189
220
|
}
|
|
221
|
+
/**
|
|
222
|
+
* Construct a currency amount with a denominator that is not equal to 1
|
|
223
|
+
* @param currency the currency
|
|
224
|
+
* @param numerator the numerator of the fractional token amount
|
|
225
|
+
* @param denominator the denominator of the fractional token amount
|
|
226
|
+
*/
|
|
190
227
|
static fromFractionalAmount(currency, numerator, denominator) {
|
|
191
228
|
return new CurrencyAmount(currency, numerator, denominator);
|
|
192
229
|
}
|
|
193
230
|
add(other) {
|
|
194
|
-
|
|
231
|
+
invariant6(this.currency.equals(other.currency), "CURRENCY");
|
|
195
232
|
const added = super.add(other);
|
|
196
233
|
return CurrencyAmount.fromFractionalAmount(this.currency, added.numerator, added.denominator);
|
|
197
234
|
}
|
|
198
235
|
subtract(other) {
|
|
199
|
-
|
|
236
|
+
invariant6(this.currency.equals(other.currency), "CURRENCY");
|
|
200
237
|
const subtracted = super.subtract(other);
|
|
201
238
|
return CurrencyAmount.fromFractionalAmount(this.currency, subtracted.numerator, subtracted.denominator);
|
|
202
239
|
}
|
|
@@ -212,7 +249,7 @@ var CurrencyAmount = class extends Fraction {
|
|
|
212
249
|
return super.divide(this.decimalScale).toSignificant(significantDigits, format, rounding);
|
|
213
250
|
}
|
|
214
251
|
toFixed(decimalPlaces = this.currency.decimals, format, rounding = 0 /* ROUND_DOWN */) {
|
|
215
|
-
|
|
252
|
+
invariant6(decimalPlaces <= this.currency.decimals, "DECIMALS");
|
|
216
253
|
return super.divide(this.decimalScale).toFixed(decimalPlaces, format, rounding);
|
|
217
254
|
}
|
|
218
255
|
toExact(format = { groupSeparator: "" }) {
|
|
@@ -225,18 +262,18 @@ var CurrencyAmount = class extends Fraction {
|
|
|
225
262
|
return CurrencyAmount.fromFractionalAmount(this.currency.wrapped, this.numerator, this.denominator);
|
|
226
263
|
}
|
|
227
264
|
};
|
|
228
|
-
|
|
229
|
-
// src/fractions/price.ts
|
|
230
|
-
import JSBI5 from "jsbi";
|
|
231
|
-
import invariant4 from "tiny-invariant";
|
|
232
265
|
var Price = class extends Fraction {
|
|
266
|
+
// used to adjust the raw fraction w/r/t the decimals of the {base,quote}Token
|
|
267
|
+
/**
|
|
268
|
+
* Construct a price, either with the base and quote currency amount, or the
|
|
269
|
+
* @param args
|
|
270
|
+
*/
|
|
233
271
|
constructor(...args) {
|
|
234
272
|
let baseCurrency;
|
|
235
273
|
let quoteCurrency;
|
|
236
274
|
let denominator;
|
|
237
275
|
let numerator;
|
|
238
276
|
if (args.length === 4) {
|
|
239
|
-
;
|
|
240
277
|
[baseCurrency, quoteCurrency, denominator, numerator] = args;
|
|
241
278
|
} else {
|
|
242
279
|
const result = args[0].quoteAmount.divide(args[0].baseAmount);
|
|
@@ -250,21 +287,39 @@ var Price = class extends Fraction {
|
|
|
250
287
|
super(numerator, denominator);
|
|
251
288
|
this.baseCurrency = baseCurrency;
|
|
252
289
|
this.quoteCurrency = quoteCurrency;
|
|
253
|
-
this.scalar = new Fraction(
|
|
254
|
-
|
|
290
|
+
this.scalar = new Fraction(
|
|
291
|
+
JSBI2.exponentiate(JSBI2.BigInt(10), JSBI2.BigInt(baseCurrency.decimals)),
|
|
292
|
+
JSBI2.exponentiate(JSBI2.BigInt(10), JSBI2.BigInt(quoteCurrency.decimals))
|
|
293
|
+
);
|
|
294
|
+
}
|
|
295
|
+
/**
|
|
296
|
+
* Flip the price, switching the base and quote currency
|
|
297
|
+
*/
|
|
255
298
|
invert() {
|
|
256
299
|
return new Price(this.quoteCurrency, this.baseCurrency, this.numerator, this.denominator);
|
|
257
300
|
}
|
|
301
|
+
/**
|
|
302
|
+
* 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
|
|
303
|
+
* @param other the other price
|
|
304
|
+
*/
|
|
258
305
|
multiply(other) {
|
|
259
|
-
|
|
306
|
+
invariant6(this.quoteCurrency.equals(other.baseCurrency), "TOKEN");
|
|
260
307
|
const fraction = super.multiply(other);
|
|
261
308
|
return new Price(this.baseCurrency, other.quoteCurrency, fraction.denominator, fraction.numerator);
|
|
262
309
|
}
|
|
310
|
+
/**
|
|
311
|
+
* Return the amount of quote currency corresponding to a given amount of the base currency
|
|
312
|
+
* @param currencyAmount the amount of base currency to quote against the price
|
|
313
|
+
*/
|
|
263
314
|
quote(currencyAmount) {
|
|
264
|
-
|
|
315
|
+
invariant6(currencyAmount.currency.equals(this.baseCurrency), "TOKEN");
|
|
265
316
|
const result = super.multiply(currencyAmount);
|
|
266
317
|
return CurrencyAmount.fromFractionalAmount(this.quoteCurrency, result.numerator, result.denominator);
|
|
267
318
|
}
|
|
319
|
+
/**
|
|
320
|
+
* Get the value scaled by decimals for formatting
|
|
321
|
+
* @private
|
|
322
|
+
*/
|
|
268
323
|
get adjustedForDecimals() {
|
|
269
324
|
return super.multiply(this.scalar);
|
|
270
325
|
}
|
|
@@ -284,9 +339,6 @@ var NativeCurrency = class extends BaseCurrency {
|
|
|
284
339
|
this.isToken = false;
|
|
285
340
|
}
|
|
286
341
|
};
|
|
287
|
-
|
|
288
|
-
// src/token.ts
|
|
289
|
-
import invariant5 from "tiny-invariant";
|
|
290
342
|
var Token = class extends BaseCurrency {
|
|
291
343
|
constructor(chainId, address, decimals, symbol, name, projectLink) {
|
|
292
344
|
super(chainId, decimals, symbol, name);
|
|
@@ -295,14 +347,27 @@ var Token = class extends BaseCurrency {
|
|
|
295
347
|
this.address = address;
|
|
296
348
|
this.projectLink = projectLink;
|
|
297
349
|
}
|
|
350
|
+
/**
|
|
351
|
+
* Returns true if the two tokens are equivalent, i.e. have the same chainId and address.
|
|
352
|
+
* @param other other token to compare
|
|
353
|
+
*/
|
|
298
354
|
equals(other) {
|
|
299
355
|
return other.isToken && this.chainId === other.chainId && this.address === other.address;
|
|
300
356
|
}
|
|
357
|
+
/**
|
|
358
|
+
* Returns true if the address of this token sorts before the address of the other token
|
|
359
|
+
* @param other other token to compare
|
|
360
|
+
* @throws if the tokens have the same address
|
|
361
|
+
* @throws if the tokens are on different chains
|
|
362
|
+
*/
|
|
301
363
|
sortsBefore(other) {
|
|
302
|
-
|
|
303
|
-
|
|
364
|
+
invariant6(this.chainId === other.chainId, "CHAIN_IDS");
|
|
365
|
+
invariant6(this.address !== other.address, "ADDRESSES");
|
|
304
366
|
return this.address.toLowerCase() < other.address.toLowerCase();
|
|
305
367
|
}
|
|
368
|
+
/**
|
|
369
|
+
* Return this token, which does not need to be wrapped
|
|
370
|
+
*/
|
|
306
371
|
get wrapped() {
|
|
307
372
|
return this;
|
|
308
373
|
}
|
|
@@ -338,26 +403,22 @@ var InsufficientInputAmountError = class extends Error {
|
|
|
338
403
|
Object.setPrototypeOf(this, new.target.prototype);
|
|
339
404
|
}
|
|
340
405
|
};
|
|
341
|
-
|
|
342
|
-
// src/utils.ts
|
|
343
|
-
import JSBI6 from "jsbi";
|
|
344
|
-
import invariant6 from "tiny-invariant";
|
|
345
406
|
function validateVMTypeInstance(value, vmType) {
|
|
346
|
-
invariant6(
|
|
347
|
-
invariant6(
|
|
407
|
+
invariant6(JSBI2.greaterThanOrEqual(value, ZERO), `${value} is not a ${vmType}.`);
|
|
408
|
+
invariant6(JSBI2.lessThanOrEqual(value, VM_TYPE_MAXIMA[vmType]), `${value} is not a ${vmType}.`);
|
|
348
409
|
}
|
|
349
410
|
function sqrt(y) {
|
|
350
|
-
|
|
411
|
+
invariant6(JSBI2.greaterThanOrEqual(y, ZERO), "NEGATIVE");
|
|
351
412
|
let z = ZERO;
|
|
352
413
|
let x;
|
|
353
|
-
if (
|
|
414
|
+
if (JSBI2.greaterThan(y, THREE)) {
|
|
354
415
|
z = y;
|
|
355
|
-
x =
|
|
356
|
-
while (
|
|
416
|
+
x = JSBI2.add(JSBI2.divide(y, TWO), ONE);
|
|
417
|
+
while (JSBI2.lessThan(x, z)) {
|
|
357
418
|
z = x;
|
|
358
|
-
x =
|
|
419
|
+
x = JSBI2.divide(JSBI2.add(JSBI2.divide(y, x), x), TWO);
|
|
359
420
|
}
|
|
360
|
-
} else if (
|
|
421
|
+
} else if (JSBI2.notEqual(y, ZERO)) {
|
|
361
422
|
z = ONE;
|
|
362
423
|
}
|
|
363
424
|
return z;
|
|
@@ -391,34 +452,30 @@ function computePriceImpact(midPrice, inputAmount, outputAmount) {
|
|
|
391
452
|
const priceImpact = quotedOutputAmount.subtract(outputAmount).divide(quotedOutputAmount);
|
|
392
453
|
return new Percent(priceImpact.numerator, priceImpact.denominator);
|
|
393
454
|
}
|
|
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
|
-
};
|
|
455
|
+
function balanceComparator(balanceA, balanceB) {
|
|
456
|
+
if (balanceA && balanceB) {
|
|
457
|
+
return balanceA.greaterThan(balanceB) ? -1 : balanceA.equalTo(balanceB) ? 0 : 1;
|
|
458
|
+
}
|
|
459
|
+
if (balanceA && balanceA.greaterThan("0")) {
|
|
460
|
+
return -1;
|
|
461
|
+
}
|
|
462
|
+
if (balanceB && balanceB.greaterThan("0")) {
|
|
463
|
+
return 1;
|
|
464
|
+
}
|
|
465
|
+
return 0;
|
|
466
|
+
}
|
|
467
|
+
function getTokenComparator(balances) {
|
|
468
|
+
return function sortTokens(tokenA, tokenB) {
|
|
469
|
+
const balanceA = balances[tokenA.address];
|
|
470
|
+
const balanceB = balances[tokenB.address];
|
|
471
|
+
const balanceComp = balanceComparator(balanceA, balanceB);
|
|
472
|
+
if (balanceComp !== 0)
|
|
473
|
+
return balanceComp;
|
|
474
|
+
if (tokenA.symbol && tokenB.symbol) {
|
|
475
|
+
return tokenA.symbol.toLowerCase() < tokenB.symbol.toLowerCase() ? -1 : 1;
|
|
476
|
+
}
|
|
477
|
+
return tokenA.symbol ? -1 : tokenB.symbol ? -1 : 0;
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
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 };
|
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": "0.1.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,11 +25,10 @@
|
|
|
16
25
|
"keywords": [
|
|
17
26
|
"pancakeswap"
|
|
18
27
|
],
|
|
19
|
-
"module": "dist/index.mjs",
|
|
20
28
|
"scripts": {
|
|
21
29
|
"lint": "eslint src test",
|
|
22
|
-
"build": "tsup
|
|
23
|
-
"dev": "tsup
|
|
30
|
+
"build": "tsup",
|
|
31
|
+
"dev": "tsup --watch",
|
|
24
32
|
"test": "jest",
|
|
25
33
|
"prepublishOnly": "yarn run build",
|
|
26
34
|
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist"
|
|
@@ -39,8 +47,7 @@
|
|
|
39
47
|
"@swc/jest": "^0.2.21",
|
|
40
48
|
"@types/big.js": "^4.0.5",
|
|
41
49
|
"@types/jest": "^24.0.25",
|
|
42
|
-
"babel-plugin-transform-jsbi-to-bigint": "^1.3.1"
|
|
43
|
-
"tsup": "^5.10.1"
|
|
50
|
+
"babel-plugin-transform-jsbi-to-bigint": "^1.3.1"
|
|
44
51
|
},
|
|
45
52
|
"engines": {
|
|
46
53
|
"node": ">=10"
|