@alcorexchange/alcor-swap-sdk 1.0.31 → 1.0.33

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.
@@ -7,6 +7,7 @@ import { Tick, TickConstructorArgs } from "./tick";
7
7
  import { TickDataProvider } from "./tickDataProvider";
8
8
  export interface PoolConstructorArgs {
9
9
  id: number;
10
+ active: boolean;
10
11
  tokenA: Token;
11
12
  tokenB: Token;
12
13
  fee: FeeAmount;
@@ -22,6 +23,7 @@ export interface PoolConstructorArgs {
22
23
  */
23
24
  export declare class Pool {
24
25
  readonly id: number;
26
+ readonly active: boolean;
25
27
  readonly tokenA: Token;
26
28
  readonly tokenB: Token;
27
29
  readonly fee: FeeAmount;
@@ -48,7 +50,7 @@ export declare class Pool {
48
50
  * @param tickCurrent The current tick of the pool
49
51
  * @param ticks The current state of the pool ticks or a data provider that can return tick data
50
52
  */
51
- constructor({ id, tokenA, tokenB, fee, sqrtPriceX64, liquidity, tickCurrent, ticks, feeGrowthGlobalAX64, feeGrowthGlobalBX64, }: PoolConstructorArgs);
53
+ constructor({ id, active, tokenA, tokenB, fee, sqrtPriceX64, liquidity, tickCurrent, ticks, feeGrowthGlobalAX64, feeGrowthGlobalBX64, }: PoolConstructorArgs);
52
54
  /**
53
55
  * Returns true if the token is either tokenA or tokenB
54
56
  * @param token The token to check
@@ -35,7 +35,7 @@ class Pool {
35
35
  * @param tickCurrent The current tick of the pool
36
36
  * @param ticks The current state of the pool ticks or a data provider that can return tick data
37
37
  */
38
- constructor({ id, tokenA, tokenB, fee, sqrtPriceX64, liquidity, tickCurrent, ticks = NO_TICK_DATA_PROVIDER_DEFAULT, feeGrowthGlobalAX64 = 0, feeGrowthGlobalBX64 = 0, }) {
38
+ constructor({ id, active, tokenA, tokenB, fee, sqrtPriceX64, liquidity, tickCurrent, ticks = NO_TICK_DATA_PROVIDER_DEFAULT, feeGrowthGlobalAX64 = 0, feeGrowthGlobalBX64 = 0, }) {
39
39
  (0, tiny_invariant_1.default)(Number.isInteger(fee) && fee < 1000000, "FEE");
40
40
  const tickCurrentSqrtRatioX64 = tickMath_1.TickMath.getSqrtRatioAtTick(tickCurrent);
41
41
  const nextTickSqrtRatioX64 = tickMath_1.TickMath.getSqrtRatioAtTick(tickCurrent + 1);
@@ -44,6 +44,7 @@ class Pool {
44
44
  // always create a copy of the list since we want the pool's tick list to be immutable
45
45
  this.id = id;
46
46
  this.fee = fee;
47
+ this.active = active;
47
48
  this.sqrtPriceX64 = jsbi_1.default.BigInt(sqrtPriceX64);
48
49
  this.liquidity = jsbi_1.default.BigInt(liquidity);
49
50
  this.tickCurrent = tickCurrent;
@@ -211,6 +212,7 @@ class Pool {
211
212
  id: pool.id,
212
213
  tokenA: token_1.Token.toJSON(pool.tokenA),
213
214
  tokenB: token_1.Token.toJSON(pool.tokenB),
215
+ active: pool.active,
214
216
  fee: pool.fee,
215
217
  sqrtPriceX64: pool.sqrtPriceX64.toString(),
216
218
  liquidity: pool.liquidity.toString(),
@@ -224,6 +226,7 @@ class Pool {
224
226
  static fromJSON(json) {
225
227
  return new Pool({
226
228
  id: json.id,
229
+ active: json.active,
227
230
  tokenA: token_1.Token.fromJSON(json.tokenA),
228
231
  tokenB: token_1.Token.fromJSON(json.tokenB),
229
232
  fee: json.fee,
@@ -126,6 +126,7 @@ class Position {
126
126
  // construct counterfactual pools
127
127
  const poolLower = new pool_1.Pool({
128
128
  id: this.pool.id,
129
+ active: this.pool.active,
129
130
  tokenA: this.pool.tokenA,
130
131
  tokenB: this.pool.tokenB,
131
132
  fee: this.pool.fee,
@@ -138,6 +139,7 @@ class Position {
138
139
  });
139
140
  const poolUpper = new pool_1.Pool({
140
141
  id: this.pool.id,
142
+ active: this.pool.active,
141
143
  tokenA: this.pool.tokenA,
142
144
  tokenB: this.pool.tokenB,
143
145
  fee: this.pool.fee,
@@ -203,6 +205,7 @@ class Position {
203
205
  // construct counterfactual pools
204
206
  const poolLower = new pool_1.Pool({
205
207
  id: this.pool.id,
208
+ active: this.pool.active,
206
209
  tokenA: this.pool.tokenA,
207
210
  tokenB: this.pool.tokenB,
208
211
  fee: this.pool.fee,
@@ -215,6 +218,7 @@ class Position {
215
218
  });
216
219
  const poolUpper = new pool_1.Pool({
217
220
  id: this.pool.id,
221
+ active: this.pool.active,
218
222
  tokenA: this.pool.tokenA,
219
223
  tokenB: this.pool.tokenB,
220
224
  fee: this.pool.fee,
@@ -1,7 +1,6 @@
1
1
  import { Currency } from './currency';
2
2
  import { Percent, Price, CurrencyAmount } from './fractions';
3
3
  import { TradeType } from '../internalConstants';
4
- import { Pool } from './pool';
5
4
  import { Route } from './route';
6
5
  /**
7
6
  * Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
@@ -186,6 +185,6 @@ export declare class Trade<TInput extends Currency, TOutput extends Currency, TT
186
185
  * @returns The execution price
187
186
  */
188
187
  worstExecutionPrice(slippageTolerance: Percent): Price<TInput, TOutput>;
189
- static bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(routes: Route<TInput, TOutput>[], pools: Pool[] | Map<number, Pool>, currencyAmountIn: CurrencyAmount<TInput>, maxNumResults?: number): Trade<TInput, TOutput, TradeType.EXACT_INPUT>[];
190
- static bestTradeExactOut<TInput extends Currency, TOutput extends Currency>(routes: Route<TInput, TOutput>[], pools: Pool[] | Map<number, Pool>, currencyAmountOut: CurrencyAmount<TOutput>, maxNumResults?: number): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[];
188
+ static bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(routes: Route<TInput, TOutput>[], currencyAmountIn: CurrencyAmount<TInput>, maxNumResults?: number): Trade<TInput, TOutput, TradeType.EXACT_INPUT>[];
189
+ static bestTradeExactOut<TInput extends Currency, TOutput extends Currency>(routes: Route<TInput, TOutput>[], currencyAmountOut: CurrencyAmount<TOutput>, maxNumResults?: number): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[];
191
190
  }
@@ -8,8 +8,6 @@ const tiny_invariant_1 = __importDefault(require("tiny-invariant"));
8
8
  const fractions_1 = require("./fractions");
9
9
  const utils_1 = require("../utils");
10
10
  const internalConstants_1 = require("../internalConstants");
11
- const pool_1 = require("./pool");
12
- const route_1 = require("./route");
13
11
  /**
14
12
  * Trades comparator, an extension of the input output comparator that also considers other dimensions of the trade in ranking them
15
13
  * @template TInput The input token, either Ether or an ERC-20
@@ -327,21 +325,11 @@ class Trade {
327
325
  worstExecutionPrice(slippageTolerance) {
328
326
  return new fractions_1.Price(this.inputAmount.currency, this.outputAmount.currency, this.maximumAmountIn(slippageTolerance).quotient, this.minimumAmountOut(slippageTolerance).quotient);
329
327
  }
330
- static bestTradeExactIn(routes, pools, currencyAmountIn, maxNumResults = 1) {
331
- const poolsMap = Array.isArray(pools) ? new Map(pools.map(p => [p.id, p])) : pools;
332
- (0, tiny_invariant_1.default)(poolsMap.size > 0, 'POOLS');
328
+ static bestTradeExactIn(routes, currencyAmountIn, maxNumResults = 1) {
329
+ (0, tiny_invariant_1.default)(routes.length > 0, 'ROUTES');
333
330
  const bestTrades = [];
334
331
  for (const route of routes) {
335
- const freshPools = route.pools.map(p => {
336
- const pool = poolsMap.get(p.id);
337
- if (!pool) {
338
- console.log('POOL FOR ROUTE NOT FOUND', p, route.pools);
339
- }
340
- (0, tiny_invariant_1.default)(pool, 'POOL_FOR_ROUTE');
341
- // Creating new instance
342
- return pool_1.Pool.fromBuffer(pool_1.Pool.toBuffer(pool));
343
- });
344
- const trade = Trade.fromRoute(new route_1.Route(freshPools, route.input, route.output), currencyAmountIn, internalConstants_1.TradeType.EXACT_INPUT);
332
+ const trade = Trade.fromRoute(route, currencyAmountIn, internalConstants_1.TradeType.EXACT_INPUT);
345
333
  // FIXME! Sorting bug multiple pools
346
334
  if (!trade.inputAmount.greaterThan(0) || !trade.priceImpact.greaterThan(0)) {
347
335
  continue;
@@ -350,18 +338,11 @@ class Trade {
350
338
  }
351
339
  return bestTrades;
352
340
  }
353
- static bestTradeExactOut(routes, pools, currencyAmountOut, maxNumResults = 1) {
354
- const poolsMap = Array.isArray(pools) ? new Map(pools.map(p => [p.id, p])) : pools;
355
- (0, tiny_invariant_1.default)(poolsMap.size > 0, 'POOLS');
341
+ static bestTradeExactOut(routes, currencyAmountOut, maxNumResults = 1) {
342
+ (0, tiny_invariant_1.default)(routes.length > 0, 'ROUTES');
356
343
  const bestTrades = [];
357
344
  for (const route of routes) {
358
- const freshPools = route.pools.map(p => {
359
- const pool = poolsMap.get(p.id);
360
- (0, tiny_invariant_1.default)(pool, 'POOL_FOR_ROUTE');
361
- // Creating new instance
362
- return pool_1.Pool.fromBuffer(pool_1.Pool.toBuffer(pool));
363
- });
364
- const trade = Trade.fromRoute(new route_1.Route(freshPools, route.input, route.output), currencyAmountOut, internalConstants_1.TradeType.EXACT_OUTPUT);
345
+ const trade = Trade.fromRoute(route, currencyAmountOut, internalConstants_1.TradeType.EXACT_OUTPUT);
365
346
  if (!trade.inputAmount.greaterThan(0) || !trade.priceImpact.greaterThan(0)) {
366
347
  continue;
367
348
  }
@@ -54,12 +54,12 @@ async function main() {
54
54
  })
55
55
 
56
56
  const pool = new Pool({
57
- ...poolRow,
57
+ ...poolRow as any,
58
58
  tokenA: parseToken(tokenA),
59
59
  tokenB: parseToken(tokenB),
60
60
  sqrtPriceX64,
61
61
  tickCurrent: tick,
62
- ticks: ticks.sort((a, b) => a.id - b.id)
62
+ ticks: ticks.sort((a: any, b: any) => a.id - b.id)
63
63
  })
64
64
 
65
65
  const { rows: positions } = await rpc.get_table_rows({
@@ -28,6 +28,7 @@ async function main() {
28
28
  const pools: Pool[] = []
29
29
 
30
30
  // We have to get all pools with fetched ticks for them
31
+ let i = 0
31
32
  for (const p of rows) {
32
33
  const { id, tokenA, tokenB, currSlot: { sqrtPriceX64, tick } } = p
33
34
 
@@ -40,13 +41,15 @@ async function main() {
40
41
  if (ticks.length == 0) continue
41
42
 
42
43
  pools.push(new Pool({
43
- ...p,
44
+ ...p as any,
44
45
  tokenA: parseToken(tokenA),
45
46
  tokenB: parseToken(tokenB),
46
47
  sqrtPriceX64,
47
48
  tickCurrent: tick,
48
- ticks: ticks.sort((a, b) => a.id - b.id)
49
+ ticks: ticks.sort((a: any, b: any) => a.id - b.id)
49
50
  }))
51
+ i += 1
52
+ process.stdout.write(`fetching ticks: ${i}/${rows.length} \r`)
50
53
  }
51
54
 
52
55
  // 1.0000 EOS
@@ -56,9 +59,8 @@ async function main() {
56
59
  const receiver = 'myaccount'
57
60
 
58
61
  // First trade sorted by biggest output
59
- //const [trade] = Trade.bestTradeExactIn(pools, amountIn, tokenOut, { maxHops: 3 })
60
- const routes = computeAllRoutes(amountIn.currency, tokenOut, pools, 3)
61
- const [trade] = Trade.bestTradeExactIn(routes, pools, amountIn, 3)
62
+ const routes = computeAllRoutes(amountIn.currency, tokenOut, pools, 2)
63
+ const [trade] = Trade.bestTradeExactIn(routes, amountIn, 1)
62
64
 
63
65
  const route = trade.route.pools.map(p => p.id)
64
66
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alcorexchange/alcor-swap-sdk",
3
- "version": "1.0.31",
3
+ "version": "1.0.33",
4
4
  "description": "",
5
5
  "main": "build/index.js",
6
6
  "scripts": {
@@ -16,6 +16,7 @@ import { TickListDataProvider } from "./tickListDataProvider";
16
16
 
17
17
  export interface PoolConstructorArgs {
18
18
  id: number,
19
+ active: boolean,
19
20
  tokenA: Token,
20
21
  tokenB: Token,
21
22
  fee: FeeAmount,
@@ -50,6 +51,7 @@ const NO_TICK_DATA_PROVIDER_DEFAULT = new NoTickDataProvider();
50
51
  export class Pool {
51
52
  // public readonly id: number;
52
53
  public readonly id: number;
54
+ public readonly active: boolean;
53
55
  public readonly tokenA: Token;
54
56
  public readonly tokenB: Token;
55
57
  public readonly fee: FeeAmount;
@@ -83,6 +85,7 @@ export class Pool {
83
85
  */
84
86
  public constructor({
85
87
  id,
88
+ active,
86
89
  tokenA,
87
90
  tokenB,
88
91
  fee,
@@ -108,6 +111,7 @@ export class Pool {
108
111
  // always create a copy of the list since we want the pool's tick list to be immutable
109
112
  this.id = id;
110
113
  this.fee = fee;
114
+ this.active = active;
111
115
  this.sqrtPriceX64 = JSBI.BigInt(sqrtPriceX64);
112
116
  this.liquidity = JSBI.BigInt(liquidity);
113
117
  this.tickCurrent = tickCurrent;
@@ -380,6 +384,7 @@ export class Pool {
380
384
  id: pool.id,
381
385
  tokenA: Token.toJSON(pool.tokenA),
382
386
  tokenB: Token.toJSON(pool.tokenB),
387
+ active: pool.active,
383
388
  fee: pool.fee,
384
389
  sqrtPriceX64: pool.sqrtPriceX64.toString(),
385
390
  liquidity: pool.liquidity.toString(),
@@ -395,6 +400,7 @@ export class Pool {
395
400
  static fromJSON(json: any): Pool {
396
401
  return new Pool({
397
402
  id: json.id,
403
+ active: json.active,
398
404
  tokenA: Token.fromJSON(json.tokenA),
399
405
  tokenB: Token.fromJSON(json.tokenB),
400
406
  fee: json.fee,
@@ -234,6 +234,7 @@ export class Position {
234
234
  // construct counterfactual pools
235
235
  const poolLower = new Pool({
236
236
  id: this.pool.id,
237
+ active: this.pool.active,
237
238
  tokenA: this.pool.tokenA,
238
239
  tokenB: this.pool.tokenB,
239
240
  fee: this.pool.fee,
@@ -246,6 +247,7 @@ export class Position {
246
247
  });
247
248
  const poolUpper = new Pool({
248
249
  id: this.pool.id,
250
+ active: this.pool.active,
249
251
  tokenA: this.pool.tokenA,
250
252
  tokenB: this.pool.tokenB,
251
253
  fee: this.pool.fee,
@@ -319,6 +321,7 @@ export class Position {
319
321
  // construct counterfactual pools
320
322
  const poolLower = new Pool({
321
323
  id: this.pool.id,
324
+ active: this.pool.active,
322
325
  tokenA: this.pool.tokenA,
323
326
  tokenB: this.pool.tokenB,
324
327
  fee: this.pool.fee,
@@ -331,6 +334,7 @@ export class Position {
331
334
  });
332
335
  const poolUpper = new Pool({
333
336
  id: this.pool.id,
337
+ active: this.pool.active,
334
338
  tokenA: this.pool.tokenA,
335
339
  tokenB: this.pool.tokenB,
336
340
  fee: this.pool.fee,
@@ -483,32 +483,14 @@ export class Trade<TInput extends Currency, TOutput extends Currency, TTradeType
483
483
 
484
484
  public static bestTradeExactIn<TInput extends Currency, TOutput extends Currency>(
485
485
  routes: Route<TInput, TOutput>[],
486
- pools: Pool[] | Map<number, Pool>,
487
486
  currencyAmountIn: CurrencyAmount<TInput>,
488
487
  maxNumResults = 1,
489
488
  ): Trade<TInput, TOutput, TradeType.EXACT_INPUT>[] {
490
- const poolsMap = Array.isArray(pools) ? new Map(pools.map(p => [p.id, p])) : pools
491
-
492
- invariant(poolsMap.size > 0, 'POOLS')
489
+ invariant(routes.length > 0, 'ROUTES')
493
490
 
494
491
  const bestTrades: Trade<TInput, TOutput, TradeType.EXACT_INPUT>[] = []
495
492
  for (const route of routes) {
496
- const freshPools = route.pools.map(p => {
497
- const pool = poolsMap.get(p.id)
498
- if (!pool) {
499
- console.log('POOL FOR ROUTE NOT FOUND', p, route.pools)
500
- }
501
- invariant(pool, 'POOL_FOR_ROUTE')
502
-
503
- // Creating new instance
504
- return Pool.fromBuffer(Pool.toBuffer(pool))
505
- })
506
-
507
- const trade = Trade.fromRoute(
508
- new Route(freshPools, route.input, route.output),
509
- currencyAmountIn,
510
- TradeType.EXACT_INPUT
511
- )
493
+ const trade = Trade.fromRoute(route, currencyAmountIn, TradeType.EXACT_INPUT)
512
494
 
513
495
  // FIXME! Sorting bug multiple pools
514
496
  if (!trade.inputAmount.greaterThan(0) || !trade.priceImpact.greaterThan(0)) {
@@ -528,28 +510,14 @@ export class Trade<TInput extends Currency, TOutput extends Currency, TTradeType
528
510
 
529
511
  public static bestTradeExactOut<TInput extends Currency, TOutput extends Currency>(
530
512
  routes: Route<TInput, TOutput>[],
531
- pools: Pool[] | Map<number, Pool>,
532
513
  currencyAmountOut: CurrencyAmount<TOutput>,
533
514
  maxNumResults = 1,
534
515
  ): Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[] {
535
- const poolsMap = Array.isArray(pools) ? new Map(pools.map(p => [p.id, p])) : pools
536
- invariant(poolsMap.size > 0, 'POOLS')
516
+ invariant(routes.length > 0, 'ROUTES')
537
517
 
538
518
  const bestTrades: Trade<TInput, TOutput, TradeType.EXACT_OUTPUT>[] = []
539
519
  for (const route of routes) {
540
- const freshPools = route.pools.map(p => {
541
- const pool = poolsMap.get(p.id)
542
- invariant(pool, 'POOL_FOR_ROUTE')
543
-
544
- // Creating new instance
545
- return Pool.fromBuffer(Pool.toBuffer(pool))
546
- })
547
-
548
- const trade = Trade.fromRoute(
549
- new Route(freshPools, route.input, route.output),
550
- currencyAmountOut,
551
- TradeType.EXACT_OUTPUT
552
- )
520
+ const trade = Trade.fromRoute(route, currencyAmountOut, TradeType.EXACT_OUTPUT)
553
521
 
554
522
  if (!trade.inputAmount.greaterThan(0) || !trade.priceImpact.greaterThan(0)) {
555
523
  continue