@alcorexchange/alcor-swap-sdk 1.0.404 → 1.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/build/entities/fractions/currencyAmount.d.ts +2 -4
- package/build/entities/fractions/currencyAmount.js +12 -19
- package/build/entities/fractions/fraction.d.ts +3 -4
- package/build/entities/fractions/fraction.js +7 -12
- package/build/entities/fractions/percent.js +4 -13
- package/build/entities/fractions/price.js +4 -13
- package/build/entities/pool.d.ts +14 -16
- package/build/entities/pool.js +51 -28
- package/build/entities/position.d.ts +9 -10
- package/build/entities/position.js +57 -61
- package/build/entities/route.d.ts +2 -3
- package/build/entities/route.js +10 -11
- package/build/entities/tick.d.ts +7 -8
- package/build/entities/tick.js +12 -13
- package/build/entities/trade.js +44 -69
- package/build/esm/entities/baseCurrency.js +44 -0
- package/build/esm/entities/currency.js +1 -0
- package/build/esm/entities/fractions/currencyAmount.js +158 -0
- package/build/esm/entities/fractions/fraction.js +147 -0
- package/build/esm/entities/fractions/index.js +4 -0
- package/build/esm/entities/fractions/percent.js +70 -0
- package/build/esm/entities/fractions/price.js +103 -0
- package/build/esm/entities/index.js +10 -0
- package/build/esm/entities/pool.js +412 -0
- package/build/esm/entities/position.js +513 -0
- package/build/esm/entities/route.js +151 -0
- package/build/esm/entities/tick.js +68 -0
- package/build/esm/entities/tickDataProvider.js +31 -0
- package/build/esm/entities/tickListDataProvider.js +58 -0
- package/build/esm/entities/token.js +80 -0
- package/build/esm/entities/trade.js +632 -0
- package/build/esm/errors.js +54 -0
- package/build/esm/index.js +3 -0
- package/build/esm/internalConstants.js +54 -0
- package/build/esm/utils/common.js +52 -0
- package/build/esm/utils/computeAllRoutes.js +118 -0
- package/build/esm/utils/computeAllRoutesFast.js +139 -0
- package/build/esm/utils/computeAllRoutesWASM.js +160 -0
- package/build/esm/utils/encodeSqrtRatioX64.js +14 -0
- package/build/esm/utils/fullMath.js +32 -0
- package/build/esm/utils/getBestSwapRoute.js +173 -0
- package/build/esm/utils/index.js +18 -0
- package/build/esm/utils/isSorted.js +14 -0
- package/build/esm/utils/liquidityMath.js +24 -0
- package/build/esm/utils/maxLiquidityForAmounts.js +78 -0
- package/build/esm/utils/mostSignificantBit.js +16 -0
- package/build/esm/utils/nearestUsableTick.js +15 -0
- package/build/esm/utils/positionLibrary.js +25 -0
- package/build/esm/utils/priceTickConversions.js +39 -0
- package/build/esm/utils/sortedInsert.js +33 -0
- package/build/esm/utils/sqrt.js +27 -0
- package/build/esm/utils/sqrtPriceMath.js +99 -0
- package/build/esm/utils/swapMath.js +78 -0
- package/build/esm/utils/tickLibrary.js +54 -0
- package/build/esm/utils/tickList.js +189 -0
- package/build/esm/utils/tickMath.js +124 -0
- package/build/esm/utils/tradeCalculatorWASM.js +254 -0
- package/build/esm/utils/wasm_route_finder.js +639 -0
- package/build/esm/utils/wasm_route_finder_bg.wasm +0 -0
- package/build/internalConstants.d.ts +13 -14
- package/build/internalConstants.js +8 -8
- package/build/utils/common.js +6 -7
- package/build/utils/computeAllRoutesFast.js +2 -4
- package/build/utils/encodeSqrtRatioX64.d.ts +1 -2
- package/build/utils/encodeSqrtRatioX64.js +3 -3
- package/build/utils/fullMath.d.ts +1 -2
- package/build/utils/fullMath.js +2 -2
- package/build/utils/getBestSwapRoute.js +4 -5
- package/build/utils/liquidityMath.d.ts +1 -2
- package/build/utils/maxLiquidityForAmounts.d.ts +1 -2
- package/build/utils/mostSignificantBit.d.ts +1 -2
- package/build/utils/mostSignificantBit.js +1 -1
- package/build/utils/positionLibrary.d.ts +1 -2
- package/build/utils/positionLibrary.js +3 -3
- package/build/utils/sqrt.d.ts +2 -3
- package/build/utils/sqrt.js +3 -3
- package/build/utils/sqrtPriceMath.d.ts +4 -5
- package/build/utils/sqrtPriceMath.js +3 -3
- package/build/utils/swapMath.d.ts +1 -2
- package/build/utils/swapMath.js +1 -1
- package/build/utils/tickLibrary.d.ts +5 -6
- package/build/utils/tickList.js +10 -18
- package/build/utils/tickMath.d.ts +4 -5
- package/build/utils/tickMath.js +11 -11
- package/build/utils/tradeCalculatorWASM.js +5 -6
- package/build/utils/wasm_route_finder.js +77 -4
- package/build/utils/wasm_route_finder_bg.wasm +0 -0
- package/package.json +19 -7
|
@@ -13,7 +13,7 @@ var _sqrtPriceMath = require("../utils/sqrtPriceMath");
|
|
|
13
13
|
var _tickMath = require("../utils/tickMath");
|
|
14
14
|
var _encodeSqrtRatioX = require("../utils/encodeSqrtRatioX64");
|
|
15
15
|
var _pool = require("./pool");
|
|
16
|
-
var
|
|
16
|
+
var _tickLibrary = require("../utils/tickLibrary");
|
|
17
17
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
18
18
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
19
19
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
@@ -31,19 +31,18 @@ let Position = exports.Position = /*#__PURE__*/function () {
|
|
|
31
31
|
* @param lower The lower tick of the position
|
|
32
32
|
* @param upper The upper tick of the position
|
|
33
33
|
*/
|
|
34
|
-
function Position(
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
} = _ref;
|
|
34
|
+
function Position({
|
|
35
|
+
id,
|
|
36
|
+
owner,
|
|
37
|
+
pool,
|
|
38
|
+
liquidity,
|
|
39
|
+
tickLower,
|
|
40
|
+
tickUpper,
|
|
41
|
+
feeGrowthInsideALastX64 = 0,
|
|
42
|
+
feeGrowthInsideBLastX64 = 0,
|
|
43
|
+
feesA = 0,
|
|
44
|
+
feesB = 0
|
|
45
|
+
}) {
|
|
47
46
|
_classCallCheck(this, Position);
|
|
48
47
|
_defineProperty(this, "id", void 0);
|
|
49
48
|
_defineProperty(this, "owner", void 0);
|
|
@@ -145,11 +144,11 @@ let Position = exports.Position = /*#__PURE__*/function () {
|
|
|
145
144
|
const priceUpper = this.pool.tokenAPrice.asFraction.multiply(slippageTolerance.add(1));
|
|
146
145
|
let sqrtPriceX64Lower = (0, _encodeSqrtRatioX.encodeSqrtRatioX64)(priceLower.numerator, priceLower.denominator);
|
|
147
146
|
if (sqrtPriceX64Lower <= _tickMath.TickMath.MIN_SQRT_RATIO) {
|
|
148
|
-
sqrtPriceX64Lower = _tickMath.TickMath.MIN_SQRT_RATIO +
|
|
147
|
+
sqrtPriceX64Lower = _tickMath.TickMath.MIN_SQRT_RATIO + BigInt(1);
|
|
149
148
|
}
|
|
150
149
|
let sqrtPriceX64Upper = (0, _encodeSqrtRatioX.encodeSqrtRatioX64)(priceUpper.numerator, priceUpper.denominator);
|
|
151
150
|
if (sqrtPriceX64Upper >= _tickMath.TickMath.MAX_SQRT_RATIO) {
|
|
152
|
-
sqrtPriceX64Upper = _tickMath.TickMath.MAX_SQRT_RATIO -
|
|
151
|
+
sqrtPriceX64Upper = _tickMath.TickMath.MAX_SQRT_RATIO - BigInt(1);
|
|
153
152
|
}
|
|
154
153
|
return {
|
|
155
154
|
sqrtPriceX64Lower,
|
|
@@ -397,9 +396,9 @@ let Position = exports.Position = /*#__PURE__*/function () {
|
|
|
397
396
|
feeGrowthGlobalAX64,
|
|
398
397
|
feeGrowthGlobalBX64
|
|
399
398
|
} = pool;
|
|
400
|
-
const [feeGrowthInsideAX64, feeGrowthInsideBX64] =
|
|
401
|
-
const tokensOwedA = (0,
|
|
402
|
-
const tokensOwedB = (0,
|
|
399
|
+
const [feeGrowthInsideAX64, feeGrowthInsideBX64] = _tickLibrary.TickLibrary.getFeeGrowthInside(lower, upper, tickLower, tickUpper, pool.tickCurrent, feeGrowthGlobalAX64, feeGrowthGlobalBX64);
|
|
400
|
+
const tokensOwedA = (0, _tickLibrary.subIn128)(feeGrowthInsideAX64, feeGrowthInsideALastX64) * liquidity / _internalConstants.Q64;
|
|
401
|
+
const tokensOwedB = (0, _tickLibrary.subIn128)(feeGrowthInsideBX64, feeGrowthInsideBLastX64) * liquidity / _internalConstants.Q64;
|
|
403
402
|
return {
|
|
404
403
|
feesA: _fractions.CurrencyAmount.fromRawAmount(this.pool.tokenA, tokensOwedA + this.feesA),
|
|
405
404
|
feesB: _fractions.CurrencyAmount.fromRawAmount(this.pool.tokenB, tokensOwedB + this.feesB)
|
|
@@ -407,21 +406,20 @@ let Position = exports.Position = /*#__PURE__*/function () {
|
|
|
407
406
|
}
|
|
408
407
|
}], [{
|
|
409
408
|
key: "fromAmounts",
|
|
410
|
-
value: function fromAmounts(
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
} = _ref2;
|
|
409
|
+
value: function fromAmounts({
|
|
410
|
+
id,
|
|
411
|
+
owner,
|
|
412
|
+
pool,
|
|
413
|
+
tickLower,
|
|
414
|
+
tickUpper,
|
|
415
|
+
amountA,
|
|
416
|
+
amountB,
|
|
417
|
+
useFullPrecision,
|
|
418
|
+
feeGrowthInsideALastX64,
|
|
419
|
+
feeGrowthInsideBLastX64,
|
|
420
|
+
feesA,
|
|
421
|
+
feesB
|
|
422
|
+
}) {
|
|
425
423
|
const sqrtRatioLX64 = _tickMath.TickMath.getSqrtRatioAtTick(tickLower);
|
|
426
424
|
const sqrtRatioUX64 = _tickMath.TickMath.getSqrtRatioAtTick(tickUpper);
|
|
427
425
|
return new Position({
|
|
@@ -450,20 +448,19 @@ let Position = exports.Position = /*#__PURE__*/function () {
|
|
|
450
448
|
*/
|
|
451
449
|
}, {
|
|
452
450
|
key: "fromAmountA",
|
|
453
|
-
value: function fromAmountA(
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
} = _ref3;
|
|
451
|
+
value: function fromAmountA({
|
|
452
|
+
id,
|
|
453
|
+
owner,
|
|
454
|
+
pool,
|
|
455
|
+
tickLower,
|
|
456
|
+
tickUpper,
|
|
457
|
+
amountA,
|
|
458
|
+
useFullPrecision,
|
|
459
|
+
feeGrowthInsideALastX64,
|
|
460
|
+
feeGrowthInsideBLastX64,
|
|
461
|
+
feesA,
|
|
462
|
+
feesB
|
|
463
|
+
}) {
|
|
467
464
|
return Position.fromAmounts({
|
|
468
465
|
id,
|
|
469
466
|
owner,
|
|
@@ -490,19 +487,18 @@ let Position = exports.Position = /*#__PURE__*/function () {
|
|
|
490
487
|
*/
|
|
491
488
|
}, {
|
|
492
489
|
key: "fromAmountB",
|
|
493
|
-
value: function fromAmountB(
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
} = _ref4;
|
|
490
|
+
value: function fromAmountB({
|
|
491
|
+
id,
|
|
492
|
+
owner,
|
|
493
|
+
pool,
|
|
494
|
+
tickLower,
|
|
495
|
+
tickUpper,
|
|
496
|
+
amountB,
|
|
497
|
+
feeGrowthInsideALastX64,
|
|
498
|
+
feeGrowthInsideBLastX64,
|
|
499
|
+
feesA,
|
|
500
|
+
feesB
|
|
501
|
+
}) {
|
|
506
502
|
// this function always uses full precision,
|
|
507
503
|
return Position.fromAmounts({
|
|
508
504
|
id,
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { Currency } from './currency';
|
|
3
2
|
import { Price } from './fractions';
|
|
4
3
|
import { Token } from './token';
|
|
@@ -26,7 +25,7 @@ export declare class Route<TInput extends Currency, TOutput extends Currency> {
|
|
|
26
25
|
*/
|
|
27
26
|
get midPrice(): Price<TInput, TOutput>;
|
|
28
27
|
static toJSON(route: Route<Currency, Currency>, lightWeightVersion?: boolean): {
|
|
29
|
-
pools: (number |
|
|
28
|
+
pools: (number | Uint8Array)[];
|
|
30
29
|
input: {
|
|
31
30
|
contract: string;
|
|
32
31
|
decimals: number;
|
|
@@ -41,7 +40,7 @@ export declare class Route<TInput extends Currency, TOutput extends Currency> {
|
|
|
41
40
|
};
|
|
42
41
|
static fromJSON(json: any): Route<Token, Token>;
|
|
43
42
|
static toBuffer(route: Route<Currency, Currency>, lightWeightVersion?: boolean): any;
|
|
44
|
-
static fromBuffer(buffer:
|
|
43
|
+
static fromBuffer(buffer: Uint8Array): Route<Token, Token>;
|
|
45
44
|
static toBufferAdvanced(route: Route<Currency, Currency>, pools: any[]): any;
|
|
46
45
|
equals(other: Route<Currency, Currency>): boolean;
|
|
47
46
|
}
|
package/build/entities/route.js
CHANGED
|
@@ -63,11 +63,10 @@ let Route = exports.Route = /*#__PURE__*/function () {
|
|
|
63
63
|
key: "midPrice",
|
|
64
64
|
get: function () {
|
|
65
65
|
if (this._midPrice !== null) return this._midPrice;
|
|
66
|
-
const price = this.pools.slice(1).reduce((
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
} = _ref;
|
|
66
|
+
const price = this.pools.slice(1).reduce(({
|
|
67
|
+
nextInput,
|
|
68
|
+
price
|
|
69
|
+
}, pool) => {
|
|
71
70
|
return nextInput.equals(pool.tokenA) ? {
|
|
72
71
|
nextInput: pool.tokenB,
|
|
73
72
|
price: price.multiply(pool.tokenAPrice)
|
|
@@ -95,8 +94,7 @@ let Route = exports.Route = /*#__PURE__*/function () {
|
|
|
95
94
|
}
|
|
96
95
|
}], [{
|
|
97
96
|
key: "toJSON",
|
|
98
|
-
value: function toJSON(route) {
|
|
99
|
-
let lightWeightVersion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
97
|
+
value: function toJSON(route, lightWeightVersion = false) {
|
|
100
98
|
return {
|
|
101
99
|
pools: route.pools.map(pool => {
|
|
102
100
|
if (lightWeightVersion) {
|
|
@@ -117,7 +115,8 @@ let Route = exports.Route = /*#__PURE__*/function () {
|
|
|
117
115
|
if (typeof pool === 'number') {
|
|
118
116
|
return _pool.Pool.fromId(pool);
|
|
119
117
|
} else {
|
|
120
|
-
|
|
118
|
+
const bytes = pool instanceof Uint8Array ? pool : typeof Buffer !== 'undefined' && typeof Buffer.isBuffer === 'function' && Buffer.isBuffer(pool) ? pool : pool instanceof ArrayBuffer ? new Uint8Array(pool) : pool && pool.buffer instanceof ArrayBuffer ? new Uint8Array(pool.buffer) : pool;
|
|
119
|
+
return _pool.Pool.fromBuffer(bytes);
|
|
121
120
|
}
|
|
122
121
|
});
|
|
123
122
|
const input = _token.Token.fromJSON(json.input);
|
|
@@ -126,8 +125,7 @@ let Route = exports.Route = /*#__PURE__*/function () {
|
|
|
126
125
|
}
|
|
127
126
|
}, {
|
|
128
127
|
key: "toBuffer",
|
|
129
|
-
value: function toBuffer(route) {
|
|
130
|
-
let lightWeightVersion = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
|
|
128
|
+
value: function toBuffer(route, lightWeightVersion = false) {
|
|
131
129
|
const json = this.toJSON(route, lightWeightVersion);
|
|
132
130
|
return _msgpackLite.default.encode(json);
|
|
133
131
|
}
|
|
@@ -142,7 +140,8 @@ let Route = exports.Route = /*#__PURE__*/function () {
|
|
|
142
140
|
value: function toBufferAdvanced(route, pools) {
|
|
143
141
|
const json = {
|
|
144
142
|
pools: pools.map(pool => {
|
|
145
|
-
|
|
143
|
+
const isBuffer = typeof Buffer !== 'undefined' && typeof Buffer.isBuffer === 'function' && Buffer.isBuffer(pool);
|
|
144
|
+
if (typeof pool === 'number' || pool instanceof Uint8Array || isBuffer) {
|
|
146
145
|
return pool;
|
|
147
146
|
} else {
|
|
148
147
|
return _pool.Pool.toBuffer(pool);
|
package/build/entities/tick.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import JSBI from "jsbi";
|
|
2
1
|
import { BigintIsh } from "../internalConstants";
|
|
3
2
|
export interface TickConstructorArgs {
|
|
4
3
|
id: number;
|
|
@@ -12,13 +11,13 @@ export interface TickConstructorArgs {
|
|
|
12
11
|
}
|
|
13
12
|
export declare class Tick {
|
|
14
13
|
readonly id: number;
|
|
15
|
-
readonly liquidityGross:
|
|
16
|
-
readonly liquidityNet:
|
|
17
|
-
readonly feeGrowthOutsideAX64:
|
|
18
|
-
readonly feeGrowthOutsideBX64:
|
|
19
|
-
readonly tickCumulativeOutside:
|
|
20
|
-
readonly secondsOutside:
|
|
21
|
-
readonly secondsPerLiquidityOutsideX64:
|
|
14
|
+
readonly liquidityGross: bigint;
|
|
15
|
+
readonly liquidityNet: bigint;
|
|
16
|
+
readonly feeGrowthOutsideAX64: bigint;
|
|
17
|
+
readonly feeGrowthOutsideBX64: bigint;
|
|
18
|
+
readonly tickCumulativeOutside: bigint;
|
|
19
|
+
readonly secondsOutside: bigint;
|
|
20
|
+
readonly secondsPerLiquidityOutsideX64: bigint;
|
|
22
21
|
constructor({ id, liquidityGross, liquidityNet, feeGrowthOutsideAX64, feeGrowthOutsideBX64, tickCumulativeOutside, secondsOutside, secondsPerLiquidityOutsideX64, }: TickConstructorArgs);
|
|
23
22
|
static toJSON(tick: Tick): object;
|
|
24
23
|
static fromJSON(json: any): Tick;
|
package/build/entities/tick.js
CHANGED
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.Tick = void 0;
|
|
7
7
|
var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));
|
|
8
|
-
var
|
|
8
|
+
var _tickMath = require("../utils/tickMath");
|
|
9
9
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
10
10
|
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
11
11
|
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
@@ -14,17 +14,16 @@ function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object
|
|
|
14
14
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
15
15
|
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
16
16
|
let Tick = exports.Tick = /*#__PURE__*/function () {
|
|
17
|
-
function Tick(
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
} = _ref;
|
|
17
|
+
function Tick({
|
|
18
|
+
id,
|
|
19
|
+
liquidityGross,
|
|
20
|
+
liquidityNet,
|
|
21
|
+
feeGrowthOutsideAX64 = 0,
|
|
22
|
+
feeGrowthOutsideBX64 = 0,
|
|
23
|
+
tickCumulativeOutside = 0,
|
|
24
|
+
secondsOutside = 0,
|
|
25
|
+
secondsPerLiquidityOutsideX64 = 0
|
|
26
|
+
}) {
|
|
28
27
|
_classCallCheck(this, Tick);
|
|
29
28
|
_defineProperty(this, "id", void 0);
|
|
30
29
|
_defineProperty(this, "liquidityGross", void 0);
|
|
@@ -34,7 +33,7 @@ let Tick = exports.Tick = /*#__PURE__*/function () {
|
|
|
34
33
|
_defineProperty(this, "tickCumulativeOutside", void 0);
|
|
35
34
|
_defineProperty(this, "secondsOutside", void 0);
|
|
36
35
|
_defineProperty(this, "secondsPerLiquidityOutsideX64", void 0);
|
|
37
|
-
(0, _tinyInvariant.default)(id >=
|
|
36
|
+
(0, _tinyInvariant.default)(id >= _tickMath.TickMath.MIN_TICK && id <= _tickMath.TickMath.MAX_TICK, "TICK");
|
|
38
37
|
this.id = id;
|
|
39
38
|
this.liquidityGross = BigInt(liquidityGross);
|
|
40
39
|
this.liquidityNet = BigInt(liquidityNet);
|
package/build/entities/trade.js
CHANGED
|
@@ -8,14 +8,13 @@ exports.tradeComparator = tradeComparator;
|
|
|
8
8
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
9
9
|
var _tinyInvariant = _interopRequireDefault(require("tiny-invariant"));
|
|
10
10
|
var _fractions = require("./fractions");
|
|
11
|
-
var
|
|
11
|
+
var _sortedInsert = require("../utils/sortedInsert");
|
|
12
12
|
var _internalConstants = require("../internalConstants");
|
|
13
13
|
var _getBestSwapRoute = require("../utils/getBestSwapRoute");
|
|
14
14
|
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
|
|
15
15
|
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
|
|
16
16
|
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
|
|
17
|
-
function
|
|
18
|
-
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
17
|
+
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
|
|
19
18
|
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
20
19
|
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
21
20
|
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
@@ -75,11 +74,10 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
75
74
|
* @param routes The routes through which the trade occurs
|
|
76
75
|
* @param tradeType The type of trade, exact input or exact output
|
|
77
76
|
*/
|
|
78
|
-
function Trade(
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
} = _ref;
|
|
77
|
+
function Trade({
|
|
78
|
+
routes,
|
|
79
|
+
tradeType
|
|
80
|
+
}) {
|
|
83
81
|
_classCallCheck(this, Trade);
|
|
84
82
|
/**
|
|
85
83
|
* The swaps of the trade, i.e. which routes and how much is swapped in each that
|
|
@@ -112,24 +110,15 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
112
110
|
_defineProperty(this, "_priceImpact", void 0);
|
|
113
111
|
const inputCurrency = routes[0].inputAmount.currency;
|
|
114
112
|
const outputCurrency = routes[0].outputAmount.currency;
|
|
115
|
-
(0, _tinyInvariant.default)(routes.every(
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
}), '
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
} = _ref3;
|
|
125
|
-
return outputCurrency.equals(route.output);
|
|
126
|
-
}), 'OUTPUT_CURRENCY_MATCH');
|
|
127
|
-
const numPools = routes.map(_ref4 => {
|
|
128
|
-
let {
|
|
129
|
-
route
|
|
130
|
-
} = _ref4;
|
|
131
|
-
return route.pools.length;
|
|
132
|
-
}).reduce((total, cur) => total + cur, 0);
|
|
113
|
+
(0, _tinyInvariant.default)(routes.every(({
|
|
114
|
+
route
|
|
115
|
+
}) => inputCurrency.equals(route.input)), 'INPUT_CURRENCY_MATCH');
|
|
116
|
+
(0, _tinyInvariant.default)(routes.every(({
|
|
117
|
+
route
|
|
118
|
+
}) => outputCurrency.equals(route.output)), 'OUTPUT_CURRENCY_MATCH');
|
|
119
|
+
const numPools = routes.map(({
|
|
120
|
+
route
|
|
121
|
+
}) => route.pools.length).reduce((total, cur) => total + cur, 0);
|
|
133
122
|
const poolAddressSet = new Set();
|
|
134
123
|
for (const {
|
|
135
124
|
route
|
|
@@ -173,12 +162,9 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
173
162
|
return this._inputAmount;
|
|
174
163
|
}
|
|
175
164
|
const inputCurrency = this.swaps[0].inputAmount.currency;
|
|
176
|
-
const totalInputFromRoutes = this.swaps.map(
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
} = _ref5;
|
|
180
|
-
return inputAmount;
|
|
181
|
-
}).reduce((total, cur) => total.add(cur), _fractions.CurrencyAmount.fromRawAmount(inputCurrency, 0));
|
|
165
|
+
const totalInputFromRoutes = this.swaps.map(({
|
|
166
|
+
inputAmount
|
|
167
|
+
}) => inputAmount).reduce((total, cur) => total.add(cur), _fractions.CurrencyAmount.fromRawAmount(inputCurrency, 0));
|
|
182
168
|
this._inputAmount = totalInputFromRoutes;
|
|
183
169
|
return this._inputAmount;
|
|
184
170
|
}
|
|
@@ -193,12 +179,9 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
193
179
|
return this._outputAmount;
|
|
194
180
|
}
|
|
195
181
|
const outputCurrency = this.swaps[0].outputAmount.currency;
|
|
196
|
-
const totalOutputFromRoutes = this.swaps.map(
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
} = _ref6;
|
|
200
|
-
return outputAmount;
|
|
201
|
-
}).reduce((total, cur) => total.add(cur), _fractions.CurrencyAmount.fromRawAmount(outputCurrency, 0));
|
|
182
|
+
const totalOutputFromRoutes = this.swaps.map(({
|
|
183
|
+
outputAmount
|
|
184
|
+
}) => outputAmount).reduce((total, cur) => total.add(cur), _fractions.CurrencyAmount.fromRawAmount(outputCurrency, 0));
|
|
202
185
|
this._outputAmount = totalOutputFromRoutes;
|
|
203
186
|
return this._outputAmount;
|
|
204
187
|
}
|
|
@@ -245,8 +228,7 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
245
228
|
*/
|
|
246
229
|
}, {
|
|
247
230
|
key: "minimumAmountOut",
|
|
248
|
-
value: function minimumAmountOut(slippageTolerance) {
|
|
249
|
-
let amountOut = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.outputAmount;
|
|
231
|
+
value: function minimumAmountOut(slippageTolerance, amountOut = this.outputAmount) {
|
|
250
232
|
(0, _tinyInvariant.default)(!slippageTolerance.lessThan(_internalConstants.ZERO), 'SLIPPAGE_TOLERANCE');
|
|
251
233
|
if (this.tradeType === _internalConstants.TradeType.EXACT_OUTPUT) {
|
|
252
234
|
return amountOut;
|
|
@@ -263,8 +245,7 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
263
245
|
*/
|
|
264
246
|
}, {
|
|
265
247
|
key: "maximumAmountIn",
|
|
266
|
-
value: function maximumAmountIn(slippageTolerance) {
|
|
267
|
-
let amountIn = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.inputAmount;
|
|
248
|
+
value: function maximumAmountIn(slippageTolerance, amountIn = this.inputAmount) {
|
|
268
249
|
(0, _tinyInvariant.default)(!slippageTolerance.lessThan(_internalConstants.ZERO), 'SLIPPAGE_TOLERANCE');
|
|
269
250
|
if (this.tradeType === _internalConstants.TradeType.EXACT_INPUT) {
|
|
270
251
|
return amountIn;
|
|
@@ -332,8 +313,7 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
332
313
|
*/
|
|
333
314
|
}, {
|
|
334
315
|
key: "fromRoute",
|
|
335
|
-
value: function fromRoute(route, amount, tradeType) {
|
|
336
|
-
let percent = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 100;
|
|
316
|
+
value: function fromRoute(route, amount, tradeType, percent = 100) {
|
|
337
317
|
const amounts = new Array(route.tokenPath.length);
|
|
338
318
|
let inputAmount;
|
|
339
319
|
let outputAmount;
|
|
@@ -457,14 +437,13 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
457
437
|
}
|
|
458
438
|
}, {
|
|
459
439
|
key: "bestTradeExactIn",
|
|
460
|
-
value: function bestTradeExactIn(routes, currencyAmountIn) {
|
|
461
|
-
let maxNumResults = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
|
|
440
|
+
value: function bestTradeExactIn(routes, currencyAmountIn, maxNumResults = 1) {
|
|
462
441
|
(0, _tinyInvariant.default)(routes.length > 0, 'ROUTES');
|
|
463
442
|
|
|
464
443
|
// Pre-filter: remove routes with zero-liquidity pools
|
|
465
444
|
const validRoutes = routes.filter(route => route.pools.every(pool => pool.active && pool.liquidity > _internalConstants.ZERO));
|
|
466
445
|
|
|
467
|
-
// Helper: compute min liquidity
|
|
446
|
+
// Helper: compute min liquidity (no overflow)
|
|
468
447
|
const getMinLiquidity = route => {
|
|
469
448
|
let min = route.pools[0].liquidity;
|
|
470
449
|
for (let i = 1; i < route.pools.length; i++) {
|
|
@@ -505,14 +484,13 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
505
484
|
|
|
506
485
|
// Only check outputAmount > 0, skip expensive priceImpact calculation
|
|
507
486
|
if (!trade.outputAmount.greaterThan(0)) continue;
|
|
508
|
-
(0,
|
|
487
|
+
(0, _sortedInsert.sortedInsert)(bestTrades, trade, maxNumResults, tradeComparator);
|
|
509
488
|
}
|
|
510
489
|
return bestTrades;
|
|
511
490
|
}
|
|
512
491
|
}, {
|
|
513
492
|
key: "bestTradeExactOut",
|
|
514
|
-
value: function bestTradeExactOut(routes, currencyAmountOut) {
|
|
515
|
-
let maxNumResults = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
|
|
493
|
+
value: function bestTradeExactOut(routes, currencyAmountOut, maxNumResults = 1) {
|
|
516
494
|
(0, _tinyInvariant.default)(routes.length > 0, 'ROUTES');
|
|
517
495
|
const bestTrades = [];
|
|
518
496
|
for (const route of routes) {
|
|
@@ -527,7 +505,7 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
527
505
|
throw error;
|
|
528
506
|
}
|
|
529
507
|
if (!trade.inputAmount.greaterThan(0) || !trade.priceImpact.greaterThan(0)) continue;
|
|
530
|
-
(0,
|
|
508
|
+
(0, _sortedInsert.sortedInsert)(bestTrades, trade, maxNumResults, tradeComparator);
|
|
531
509
|
}
|
|
532
510
|
return bestTrades;
|
|
533
511
|
}
|
|
@@ -544,11 +522,10 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
544
522
|
*/
|
|
545
523
|
}, {
|
|
546
524
|
key: "bestTradeWithSplitWASM",
|
|
547
|
-
value: async function bestTradeWithSplitWASM(_routes, amount, percents, tradeType, pools
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
};
|
|
525
|
+
value: async function bestTradeWithSplitWASM(_routes, amount, percents, tradeType, pools, swapConfig = {
|
|
526
|
+
minSplits: 1,
|
|
527
|
+
maxSplits: 10
|
|
528
|
+
}) {
|
|
552
529
|
const {
|
|
553
530
|
bestTradeWithSplitWASM
|
|
554
531
|
} = await Promise.resolve().then(() => _interopRequireWildcard(require('../utils/tradeCalculatorWASM')));
|
|
@@ -556,18 +533,17 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
556
533
|
}
|
|
557
534
|
}, {
|
|
558
535
|
key: "bestTradeWithSplit",
|
|
559
|
-
value: function bestTradeWithSplit(_routes, amount, percents, tradeType
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
};
|
|
536
|
+
value: function bestTradeWithSplit(_routes, amount, percents, tradeType, swapConfig = {
|
|
537
|
+
minSplits: 1,
|
|
538
|
+
maxSplits: 10
|
|
539
|
+
}) {
|
|
564
540
|
(0, _tinyInvariant.default)(_routes.length > 0, 'ROUTES');
|
|
565
541
|
(0, _tinyInvariant.default)(percents.length > 0, 'PERCENTS');
|
|
566
542
|
|
|
567
543
|
// Pre-filter: remove routes with zero-liquidity or inactive pools
|
|
568
544
|
const validRoutes = _routes.filter(route => route.pools.every(pool => pool.active && pool.liquidity > _internalConstants.ZERO));
|
|
569
545
|
|
|
570
|
-
// Helper: compute min liquidity for a route
|
|
546
|
+
// Helper: compute min liquidity for a route (no overflow)
|
|
571
547
|
const getMinLiquidity = route => {
|
|
572
548
|
let min = route.pools[0].liquidity;
|
|
573
549
|
for (let i = 1; i < route.pools.length; i++) {
|
|
@@ -632,13 +608,12 @@ let Trade = exports.Trade = /*#__PURE__*/function () {
|
|
|
632
608
|
});
|
|
633
609
|
const bestTrades = (0, _getBestSwapRoute.getBestSwapRoute)(tradeType, percentToTradesObj, percents, swapConfig);
|
|
634
610
|
if (!bestTrades) return null;
|
|
635
|
-
const routes = bestTrades.map(
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
} = _ref7;
|
|
611
|
+
const routes = bestTrades.map(({
|
|
612
|
+
inputAmount,
|
|
613
|
+
outputAmount,
|
|
614
|
+
route,
|
|
615
|
+
swaps
|
|
616
|
+
}) => {
|
|
642
617
|
return {
|
|
643
618
|
inputAmount,
|
|
644
619
|
outputAmount,
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
|
|
2
|
+
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
|
|
3
|
+
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
|
|
4
|
+
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
|
|
5
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; }
|
|
6
|
+
function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
7
|
+
import invariant from "tiny-invariant";
|
|
8
|
+
/**
|
|
9
|
+
* A currency is any fungible financial instrument, including Ether, all ERC20 tokens, and other chain-native currencies
|
|
10
|
+
*/
|
|
11
|
+
export let BaseCurrency = /*#__PURE__*/_createClass(
|
|
12
|
+
/**
|
|
13
|
+
* Constructs an instance of the base class `BaseCurrency`.
|
|
14
|
+
* @param chainId the chain ID on which this currency resides
|
|
15
|
+
* @param decimals decimals of the currency
|
|
16
|
+
* @param symbol symbol of the currency
|
|
17
|
+
* @param name of the currency
|
|
18
|
+
*/
|
|
19
|
+
function BaseCurrency(contract, decimals, symbol) {
|
|
20
|
+
_classCallCheck(this, BaseCurrency);
|
|
21
|
+
/**
|
|
22
|
+
* The contract address of the currency
|
|
23
|
+
*/
|
|
24
|
+
_defineProperty(this, "contract", void 0);
|
|
25
|
+
/**
|
|
26
|
+
* The decimals used in representing currency amounts
|
|
27
|
+
*/
|
|
28
|
+
_defineProperty(this, "decimals", void 0);
|
|
29
|
+
/**
|
|
30
|
+
* The symbol of the currency, i.e. a short textual non-unique identifier
|
|
31
|
+
*/
|
|
32
|
+
_defineProperty(this, "symbol", void 0);
|
|
33
|
+
_defineProperty(this, "id", void 0);
|
|
34
|
+
invariant(decimals >= 0 && decimals < 19 && Number.isInteger(decimals), "DECIMALS");
|
|
35
|
+
this.contract = contract;
|
|
36
|
+
this.decimals = decimals;
|
|
37
|
+
this.symbol = symbol;
|
|
38
|
+
this.id = symbol.toLowerCase() + '-' + contract;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Returns whether this currency is functionally equivalent to the other currency
|
|
43
|
+
* @param other the other currency
|
|
44
|
+
*/);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|