@paraswap/dex-lib 2.28.12-ramses.2 → 2.29.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.
Files changed (35) hide show
  1. package/build/dex/index.js +0 -2
  2. package/build/dex/index.js.map +1 -1
  3. package/build/dex/solidly/config.js +1 -11
  4. package/build/dex/solidly/config.js.map +1 -1
  5. package/build/dex/solidly/forks-override/ramses.js +2 -2
  6. package/build/dex/solidly/forks-override/ramses.js.map +1 -1
  7. package/build/dex/solidly/solidly.js +0 -1
  8. package/build/dex/solidly/solidly.js.map +1 -1
  9. package/build/dex/uniswap-v3/config.js +22 -0
  10. package/build/dex/uniswap-v3/config.js.map +1 -1
  11. package/build/dex/uniswap-v3/types.d.ts +6 -1
  12. package/build/dex/uniswap-v3/types.js.map +1 -1
  13. package/build/dex/uniswap-v3/uniswap-v3-pool.d.ts +3 -2
  14. package/build/dex/uniswap-v3/uniswap-v3-pool.js +5 -2
  15. package/build/dex/uniswap-v3/uniswap-v3-pool.js.map +1 -1
  16. package/build/dex/uniswap-v3/uniswap-v3.js +7 -3
  17. package/build/dex/uniswap-v3/uniswap-v3.js.map +1 -1
  18. package/package.json +1 -1
  19. package/src/abi/RamsesV2Pool.abi.json +1554 -0
  20. package/src/abi/RamsesV2Quoter.abi.json +274 -0
  21. package/src/abi/RamsesV2StateMulticall.abi.json +891 -0
  22. package/src/dex/index.ts +0 -2
  23. package/src/dex/solidly/config.ts +1 -13
  24. package/src/dex/solidly/solidly-e2e.test.ts +0 -95
  25. package/src/dex/solidly/solidly-integration.test.ts +1 -141
  26. package/src/dex/solidly/solidly.ts +0 -2
  27. package/src/dex/uniswap-v3/config.ts +22 -0
  28. package/src/dex/uniswap-v3/forks/ramses-v2/utils.ts +72 -0
  29. package/src/dex/uniswap-v3/types.ts +7 -1
  30. package/src/dex/uniswap-v3/uniswap-v3-e2e.test.ts +445 -354
  31. package/src/dex/uniswap-v3/uniswap-v3-integration.test.ts +171 -13
  32. package/src/dex/uniswap-v3/uniswap-v3-pool.ts +7 -2
  33. package/src/dex/uniswap-v3/uniswap-v3.ts +7 -2
  34. package/tests/constants-e2e.ts +1 -1
  35. package/src/dex/solidly/forks-override/ramses.ts +0 -224
@@ -3,13 +3,13 @@ import dotenv from 'dotenv';
3
3
  dotenv.config();
4
4
 
5
5
  import { Interface, Result } from '@ethersproject/abi';
6
- import { DummyDexHelper } from '../../dex-helper/index';
6
+ import { DummyDexHelper, IDexHelper } from '../../dex-helper/index';
7
7
  import { Network, SwapSide } from '../../constants';
8
8
  import { BI_POWS } from '../../bigint-constants';
9
9
  import { UniswapV3 } from './uniswap-v3';
10
10
  import { checkPoolPrices, checkPoolsLiquidity } from '../../../tests/utils';
11
11
  import { Tokens } from '../../../tests/constants-e2e';
12
- import UniswapV3QuoterABI from '../../abi/uniswap-v3/UniswapV3Quoter.abi.json';
12
+ import UniswapV3QuoterV2ABI from '../../abi/uniswap-v3/UniswapV3QuoterV2.abi.json';
13
13
  import { Address } from '@paraswap/core';
14
14
 
15
15
  const network = Network.POLYGON;
@@ -28,10 +28,8 @@ const amounts = [
28
28
 
29
29
  const amountsBuy = [0n, 1n * BI_POWS[18], 2n * BI_POWS[18], 3n * BI_POWS[18]];
30
30
 
31
- const dexHelper = new DummyDexHelper(network);
32
- const dexKey = 'UniswapV3';
33
31
 
34
- const quoterIface = new Interface(UniswapV3QuoterABI);
32
+ const quoterIface = new Interface(UniswapV3QuoterV2ABI);
35
33
 
36
34
  function getReaderCalldata(
37
35
  exchangeAddress: string,
@@ -45,11 +43,13 @@ function getReaderCalldata(
45
43
  return amounts.map(amount => ({
46
44
  target: exchangeAddress,
47
45
  callData: readerIface.encodeFunctionData(funcName, [
48
- tokenIn,
49
- tokenOut,
50
- fee,
51
- amount,
52
- 0n,
46
+ [
47
+ tokenIn,
48
+ tokenOut,
49
+ amount.toString(),
50
+ fee.toString(),
51
+ 0,
52
+ ]
53
53
  ]),
54
54
  }));
55
55
  }
@@ -66,9 +66,11 @@ function decodeReaderResult(
66
66
  }
67
67
 
68
68
  async function checkOnChainPricing(
69
+ dexHelper: IDexHelper,
69
70
  uniswapV3: UniswapV3,
70
71
  funcName: string,
71
72
  blockNumber: number,
73
+ exchangeAddress: string,
72
74
  prices: bigint[],
73
75
  tokenIn: Address,
74
76
  tokenOut: Address,
@@ -76,7 +78,7 @@ async function checkOnChainPricing(
76
78
  _amounts: bigint[],
77
79
  ) {
78
80
  // Quoter address
79
- const exchangeAddress = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6';
81
+ // const exchangeAddress = '0xb27308f9F90D607463bb33eA1BeBb41C27CE5AB6';
80
82
  const readerIface = quoterIface;
81
83
 
82
84
  const sum = prices.reduce((acc, curr) => (acc += curr), 0n);
@@ -117,6 +119,8 @@ async function checkOnChainPricing(
117
119
  decodeReaderResult(readerResult, readerIface, funcName),
118
120
  );
119
121
 
122
+ console.log('EXPECTED PRICES: ', expectedPrices);
123
+
120
124
  let firstZeroIndex = prices.slice(1).indexOf(0n);
121
125
 
122
126
  // we skipped first, so add +1 on result
@@ -130,6 +134,9 @@ async function checkOnChainPricing(
130
134
  }
131
135
 
132
136
  describe('UniswapV3', function () {
137
+ const dexHelper = new DummyDexHelper(network);
138
+ const dexKey = 'UniswapV3';
139
+
133
140
  let blockNumber: number;
134
141
  let uniswapV3: UniswapV3;
135
142
  let uniswapV3Mainnet: UniswapV3;
@@ -138,9 +145,9 @@ describe('UniswapV3', function () {
138
145
  blockNumber = await dexHelper.web3Provider.eth.getBlockNumber();
139
146
  uniswapV3 = new UniswapV3(network, dexKey, dexHelper);
140
147
  uniswapV3Mainnet = new UniswapV3(
141
- Network.MAINNET,
148
+ Network.ARBITRUM,
142
149
  dexKey,
143
- new DummyDexHelper(Network.MAINNET),
150
+ new DummyDexHelper(Network.ARBITRUM),
144
151
  );
145
152
  });
146
153
 
@@ -173,9 +180,11 @@ describe('UniswapV3', function () {
173
180
  poolPrices!.map(async price => {
174
181
  const fee = uniswapV3.eventPools[price.poolIdentifier!]!.feeCode;
175
182
  const res = await checkOnChainPricing(
183
+ dexHelper,
176
184
  uniswapV3,
177
185
  'quoteExactInputSingle',
178
186
  blockNumber,
187
+ '0x61fFE014bA17989E743c5F6cB21bF9697530B21e',
179
188
  price.prices,
180
189
  TokenA.address,
181
190
  TokenB.address,
@@ -219,9 +228,11 @@ describe('UniswapV3', function () {
219
228
  poolPrices!.map(async price => {
220
229
  const fee = uniswapV3.eventPools[price.poolIdentifier!]!.feeCode;
221
230
  const res = await checkOnChainPricing(
231
+ dexHelper,
222
232
  uniswapV3,
223
233
  'quoteExactOutputSingle',
224
234
  blockNumber,
235
+ '0x61fFE014bA17989E743c5F6cB21bF9697530B21e',
225
236
  price.prices,
226
237
  TokenA.address,
227
238
  TokenB.address,
@@ -329,9 +340,11 @@ describe('UniswapV3', function () {
329
340
  poolPrices!.map(async price => {
330
341
  const fee = uniswapV3.eventPools[price.poolIdentifier!]!.feeCode;
331
342
  const res = await checkOnChainPricing(
343
+ dexHelper,
332
344
  uniswapV3,
333
345
  'quoteExactInputSingle',
334
346
  blockNumber,
347
+ '0x61fFE014bA17989E743c5F6cB21bF9697530B21e',
335
348
  price.prices,
336
349
  TokenA.address,
337
350
  TokenB.address,
@@ -439,9 +452,11 @@ describe('UniswapV3', function () {
439
452
  poolPrices!.map(async price => {
440
453
  const fee = uniswapV3.eventPools[price.poolIdentifier!]!.feeCode;
441
454
  const res = await checkOnChainPricing(
455
+ dexHelper,
442
456
  uniswapV3,
443
457
  'quoteExactOutputSingle',
444
458
  blockNumber,
459
+ '0x61fFE014bA17989E743c5F6cB21bF9697530B21e',
445
460
  price.prices,
446
461
  TokenA.address,
447
462
  TokenB.address,
@@ -466,3 +481,146 @@ describe('UniswapV3', function () {
466
481
  }
467
482
  });
468
483
  });
484
+
485
+ describe('RamsesV2', () => {
486
+ const dexKey = 'RamsesV2';
487
+ let blockNumber: number;
488
+ let uniswapV3: UniswapV3;
489
+ let uniswapV3Mainnet: UniswapV3;
490
+
491
+ const network = Network.ARBITRUM;
492
+ const dexHelper = new DummyDexHelper(network);
493
+ const TokenASymbol = 'USDCe';
494
+ const TokenA = Tokens[network][TokenASymbol];
495
+
496
+ const TokenBSymbol = 'USDC';
497
+ const TokenB = Tokens[network][TokenBSymbol];
498
+
499
+ beforeEach(async () => {
500
+ blockNumber = await dexHelper.web3Provider.eth.getBlockNumber();
501
+ uniswapV3 = new UniswapV3(network, dexKey, dexHelper);
502
+ uniswapV3Mainnet = new UniswapV3(
503
+ Network.ARBITRUM,
504
+ dexKey,
505
+ dexHelper,
506
+ );
507
+ });
508
+
509
+ it('getPoolIdentifiers and getPricesVolume SELL', async function () {
510
+
511
+ const amounts = [
512
+ 0n,
513
+ 6000000n,
514
+ 12000000n,
515
+ 18000000n,
516
+ 24000000n,
517
+ 30000000n,
518
+ 36000000n,
519
+ 42000000n,
520
+ ];
521
+
522
+ const pools = await uniswapV3.getPoolIdentifiers(
523
+ TokenA,
524
+ TokenB,
525
+ SwapSide.SELL,
526
+ blockNumber,
527
+ );
528
+ console.log(`${TokenASymbol} <> ${TokenBSymbol} Pool Identifiers: `, pools);
529
+
530
+ expect(pools.length).toBeGreaterThan(0);
531
+
532
+ const poolPrices = await uniswapV3.getPricesVolume(
533
+ TokenA,
534
+ TokenB,
535
+ amounts,
536
+ SwapSide.SELL,
537
+ blockNumber,
538
+ pools,
539
+ );
540
+ console.log(`${TokenASymbol} <> ${TokenBSymbol} Pool Prices: `, poolPrices);
541
+
542
+ expect(poolPrices).not.toBeNull();
543
+ checkPoolPrices(poolPrices!, amounts, SwapSide.SELL, dexKey);
544
+
545
+ let falseChecksCounter = 0;
546
+ await Promise.all(
547
+ poolPrices!.map(async price => {
548
+ const fee = uniswapV3.eventPools[price.poolIdentifier!]!.feeCode;
549
+ const res = await checkOnChainPricing(
550
+ dexHelper,
551
+ uniswapV3,
552
+ 'quoteExactInputSingle',
553
+ blockNumber,
554
+ '0xAA20EFF7ad2F523590dE6c04918DaAE0904E3b20',
555
+ price.prices,
556
+ TokenA.address,
557
+ TokenB.address,
558
+ fee,
559
+ amounts,
560
+ );
561
+ if (res === false) falseChecksCounter++;
562
+ }),
563
+ );
564
+
565
+ expect(falseChecksCounter).toBeLessThan(poolPrices!.length);
566
+ });
567
+
568
+ it('getPoolIdentifiers and getPricesVolume BUY', async function () {
569
+
570
+ const amounts = [
571
+ 0n,
572
+ 6000000n,
573
+ 12000000n,
574
+ 18000000n,
575
+ 24000000n,
576
+ 30000000n,
577
+ 36000000n,
578
+ 42000000n,
579
+ ];
580
+
581
+ const pools = await uniswapV3.getPoolIdentifiers(
582
+ TokenA,
583
+ TokenB,
584
+ SwapSide.BUY,
585
+ blockNumber,
586
+ );
587
+ console.log(`${TokenASymbol} <> ${TokenBSymbol} Pool Identifiers: `, pools);
588
+
589
+ expect(pools.length).toBeGreaterThan(0);
590
+
591
+ const poolPrices = await uniswapV3.getPricesVolume(
592
+ TokenA,
593
+ TokenB,
594
+ amounts,
595
+ SwapSide.BUY,
596
+ blockNumber,
597
+ pools,
598
+ );
599
+ console.log(`${TokenASymbol} <> ${TokenBSymbol} Pool Prices: `, poolPrices);
600
+
601
+ expect(poolPrices).not.toBeNull();
602
+ checkPoolPrices(poolPrices!, amounts, SwapSide.SELL, dexKey);
603
+
604
+ let falseChecksCounter = 0;
605
+ await Promise.all(
606
+ poolPrices!.map(async price => {
607
+ const fee = uniswapV3.eventPools[price.poolIdentifier!]!.feeCode;
608
+ const res = await checkOnChainPricing(
609
+ dexHelper,
610
+ uniswapV3,
611
+ 'quoteExactOutputSingle',
612
+ blockNumber,
613
+ '0xAA20EFF7ad2F523590dE6c04918DaAE0904E3b20',
614
+ price.prices,
615
+ TokenA.address,
616
+ TokenB.address,
617
+ fee,
618
+ amounts,
619
+ );
620
+ if (res === false) falseChecksCounter++;
621
+ }),
622
+ );
623
+
624
+ expect(falseChecksCounter).toBeLessThan(poolPrices!.length);
625
+ });
626
+ });
@@ -11,7 +11,7 @@ import {
11
11
  import { IDexHelper } from '../../dex-helper/idex-helper';
12
12
  import {
13
13
  PoolState,
14
- DecodedStateMultiCallResultWithRelativeBitmaps,
14
+ DecodedStateMultiCallResultWithRelativeBitmaps, DecodeStateMultiCallFunc,
15
15
  } from './types';
16
16
  import UniswapV3PoolABI from '../../abi/uniswap-v3/UniswapV3Pool.abi.json';
17
17
  import { bigIntify, catchParseLogError, isSampled } from '../../utils';
@@ -60,6 +60,7 @@ export class UniswapV3EventPool extends StatefulEventSubscriber<PoolState> {
60
60
  readonly dexHelper: IDexHelper,
61
61
  parentName: string,
62
62
  readonly stateMultiContract: Contract,
63
+ readonly decodeStateMultiCallResultWithRelativeBitmaps: DecodeStateMultiCallFunc | undefined,
63
64
  readonly erc20Interface: Interface,
64
65
  protected readonly factoryAddress: Address,
65
66
  public readonly feeCode: bigint,
@@ -226,9 +227,13 @@ export class UniswapV3EventPool extends StatefulEventSubscriber<PoolState> {
226
227
  this.getBitmapRangeToRequest(),
227
228
  )
228
229
  .encodeABI(),
229
- decodeFunction: decodeStateMultiCallResultWithRelativeBitmaps,
230
+ decodeFunction:
231
+ this.decodeStateMultiCallResultWithRelativeBitmaps !== undefined
232
+ ? this.decodeStateMultiCallResultWithRelativeBitmaps
233
+ : decodeStateMultiCallResultWithRelativeBitmaps,
230
234
  },
231
235
  ];
236
+
232
237
  this._stateRequestCallData = callData;
233
238
  }
234
239
  return this._stateRequestCallData;
@@ -91,7 +91,7 @@ export class UniswapV3
91
91
 
92
92
  public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
93
93
  getDexKeysWithNetwork(
94
- _.pick(UniswapV3Config, ['UniswapV3', 'QuickSwapV3.1']),
94
+ _.pick(UniswapV3Config, ['UniswapV3', 'QuickSwapV3.1', 'RamsesV2']),
95
95
  );
96
96
 
97
97
  logger: Logger;
@@ -118,7 +118,9 @@ export class UniswapV3
118
118
  this.config.uniswapMulticall,
119
119
  );
120
120
  this.stateMultiContract = new this.dexHelper.web3Provider.eth.Contract(
121
- UniswapV3StateMulticallABI as AbiItem[],
121
+ this.config.stateMultiCallAbi !== undefined
122
+ ? this.config.stateMultiCallAbi
123
+ : UniswapV3StateMulticallABI as AbiItem[],
122
124
  this.config.stateMulticall,
123
125
  );
124
126
 
@@ -239,6 +241,7 @@ export class UniswapV3
239
241
  this.dexHelper,
240
242
  this.dexKey,
241
243
  this.stateMultiContract,
244
+ this.config.decodeStateMultiCallResultWithRelativeBitmaps,
242
245
  this.erc20Interface,
243
246
  this.config.factory,
244
247
  fee,
@@ -1055,6 +1058,8 @@ export class UniswapV3
1055
1058
  deployer: this.config.deployer?.toLowerCase(),
1056
1059
  initHash: this.config.initHash,
1057
1060
  subgraphURL: this.config.subgraphURL,
1061
+ stateMultiCallAbi: this.config.stateMultiCallAbi,
1062
+ decodeStateMultiCallResultWithRelativeBitmaps: this.config.decodeStateMultiCallResultWithRelativeBitmaps,
1058
1063
  };
1059
1064
  return newConfig;
1060
1065
  }
@@ -1020,7 +1020,7 @@ export const Holders: {
1020
1020
  USDCe: '0x62383739d68dd0f844103db8dfb05a7eded5bbe6',
1021
1021
  USDC: '0xa843392198862f98d17e3aa1421b08f2c2020cff',
1022
1022
  OHM: '0xebce5f29ff5ca9aa330ebdf7ec6b5f474bff271e',
1023
- USDT: '0x62383739d68dd0f844103db8dfb05a7eded5bbe6',
1023
+ USDT: '0x5ff47d4ab75bcaff6807c81f1367abb53439883c',
1024
1024
  POPS: '0x4b78b52e7de4d8b7d367297cb8a87c1875a9d591',
1025
1025
  FRAX: '0x59bf0545fca0e5ad48e13da269facd2e8c886ba4',
1026
1026
  nUSD: '0x9dd329f5411466d9e0c488ff72519ca9fef0cb40',
@@ -1,224 +0,0 @@
1
- import { Network, SUBGRAPH_TIMEOUT } from '../../../constants';
2
- import { getDexKeysWithNetwork } from '../../../utils';
3
- import { SolidlyConfig } from '../config';
4
- import _ from 'lodash';
5
- import { Address, PoolLiquidity } from '../../../types';
6
- import BigNumber from 'bignumber.js';
7
- import { Solidly } from '../solidly';
8
- import { IDexHelper } from '../../../dex-helper';
9
- import { SolidlyPair } from '../types';
10
- import { Interface } from '@ethersproject/abi';
11
-
12
-
13
- const RamsesFactoryABI = [
14
- {
15
- inputs: [{internalType: 'address', name: '', type: 'address'}],
16
- name: 'pairFee',
17
- outputs: [{internalType: 'uint256', 'name': '', type: 'uint256'}],
18
- stateMutability: 'view',
19
- type: 'function',
20
- },
21
- ];
22
-
23
- const ramsesFactoryIface = new Interface(RamsesFactoryABI);
24
-
25
- export type RamsesSubgraphPool = {
26
- id: string;
27
- isStable: boolean;
28
- token0: string;
29
- reserve0: string;
30
- reserve1: string;
31
- token1: string;
32
- };
33
-
34
- export type RamsesSubgraphToken = {
35
- id: string;
36
- decimals: string;
37
- }
38
-
39
- export class Ramses extends Solidly {
40
-
41
- public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
42
- getDexKeysWithNetwork(_.pick(SolidlyConfig, ['Ramses']));
43
-
44
- constructor(
45
- protected network: Network,
46
- dexKey: string,
47
- protected dexHelper: IDexHelper,
48
- ) {
49
- super(
50
- network,
51
- dexKey,
52
- dexHelper,
53
- true, // dynamic fees
54
- );
55
- }
56
-
57
- protected getFeesMultiCallData(pair: SolidlyPair) {
58
- const callEntry = {
59
- target: this.factoryAddress,
60
- callData: ramsesFactoryIface.encodeFunctionData('pairFee', [pair.exchange]),
61
- };
62
- const callDecoder = (values: any[]) =>
63
- parseInt(
64
- ramsesFactoryIface
65
- .decodeFunctionResult('pairFee', values)[0]
66
- .toString(),
67
- );
68
-
69
- return {
70
- callEntry,
71
- callDecoder,
72
- };
73
- }
74
-
75
- async getTopPoolsForToken(
76
- tokenAddress: Address,
77
- count: number,
78
- ): Promise<PoolLiquidity[]> {
79
- if (!this.subgraphURL) return [];
80
-
81
- const query = `query ($token: Bytes!, $count: Int) {
82
- pools0: pairs(first: $count, orderBy: reserve0, orderDirection: desc, where: {token0: $token, reserve0_gt: 1, reserve1_gt: 1}) {
83
- id
84
- isStable
85
- token0
86
- token1
87
- reserve0,
88
- reserve1,
89
- }
90
- pools1: pairs(first: $count, orderBy: reserve1, orderDirection: desc, where: {token1: $token, reserve0_gt: 1, reserve1_gt: 1}) {
91
- id
92
- isStable
93
- token0
94
- token1
95
- reserve0,
96
- reserve1,
97
- }
98
- }`;
99
-
100
- const { data } = await this.dexHelper.httpRequest.post(
101
- this.subgraphURL,
102
- {
103
- query,
104
- variables: { token: tokenAddress.toLowerCase(), count },
105
- },
106
- SUBGRAPH_TIMEOUT,
107
- );
108
-
109
- if (!(data && data.pools0 && data.pools1))
110
- throw new Error("Couldn't fetch the pools from the subgraph");
111
-
112
- const tokenIds = _.uniq([].concat(data.pools0, data.pools1).map(
113
- (pool: RamsesSubgraphPool) => [ pool.token0, pool.token1]).flat(),
114
- );
115
-
116
- const tokensQuery = `
117
- query ($tokenIds: [String!]) {
118
- tokens(where: {id_in: $tokenIds}) {
119
- id
120
- decimals
121
- }
122
- }`;
123
-
124
-
125
- const { data: tokensData } = await this.dexHelper.httpRequest.post(
126
- this.subgraphURL,
127
- {
128
- query: tokensQuery,
129
- variables: { tokenIds },
130
- },
131
- SUBGRAPH_TIMEOUT,
132
- );
133
-
134
- const pools0 = await this.prepareSubgraphPools(tokensData.tokens, data.pools0, (pool,{
135
- address1, decimals1, liquidityUSDToken0, liquidityUSDToken1,
136
- }) => ({
137
- exchange: this.dexKey,
138
- stable: pool.isStable,
139
- address: pool.id.toLowerCase(),
140
- connectorTokens: [
141
- {
142
- address: address1,
143
- decimals: decimals1,
144
- },
145
- ],
146
- liquidityUSD: liquidityUSDToken0 + liquidityUSDToken1,
147
- }));
148
-
149
- const pools1 = await this.prepareSubgraphPools(tokensData.tokens, data.pools1, (pool,{
150
- address0, decimals0, liquidityUSDToken0, liquidityUSDToken1,
151
- }) => ({
152
- exchange: this.dexKey,
153
- stable: pool.isStable,
154
- address: pool.id.toLowerCase(),
155
- connectorTokens: [
156
- {
157
- address: address0,
158
- decimals: decimals0,
159
- },
160
- ],
161
- liquidityUSD: liquidityUSDToken0 + liquidityUSDToken1,
162
- }));
163
-
164
- return _.slice(
165
- _.sortBy(_.concat(pools0, pools1), [pool => -1 * pool.liquidityUSD]),
166
- 0,
167
- count,
168
- );
169
- }
170
-
171
- protected async prepareSubgraphPools(
172
- tokens: RamsesSubgraphToken[],
173
- pools: RamsesSubgraphPool[],
174
- iterator: (
175
- pool: RamsesSubgraphPool, {
176
- address0,
177
- address1,
178
- decimals0,
179
- decimals1,
180
- reserve0,
181
- reserve1,
182
- liquidityUSDToken0,
183
- liquidityUSDToken1,
184
- }: {
185
- address0: string,
186
- address1: string,
187
- decimals0: number,
188
- decimals1: number,
189
- reserve0: bigint,
190
- reserve1: bigint,
191
- liquidityUSDToken0: number,
192
- liquidityUSDToken1: number,
193
- }) => PoolLiquidity
194
- ): Promise<PoolLiquidity[]> {
195
- return Promise.all(pools.map(async (
196
- pool: RamsesSubgraphPool,
197
- ) => {
198
- const address0 = pool.token0.toLowerCase();
199
- const address1 = pool.token1.toLowerCase();
200
-
201
- const decimals0 = parseInt(tokens.find((t) => t.id === address0)!.decimals);
202
- const decimals1 = parseInt(tokens.find((t) => t.id === address1)!.decimals);
203
-
204
- const reserve0 = BigInt(new BigNumber(pool.reserve0).multipliedBy(10 ** decimals0).toFixed());
205
- const reserve1 = BigInt(new BigNumber(pool.reserve1).multipliedBy(10 ** decimals1).toFixed());
206
-
207
- const liquidityUSDToken0 = await this.dexHelper.getTokenUSDPrice({
208
- address: address0,
209
- decimals: decimals0,
210
- }, reserve0);
211
-
212
- const liquidityUSDToken1 = await this.dexHelper.getTokenUSDPrice({
213
- address: address1,
214
- decimals: decimals1,
215
- }, reserve1);
216
-
217
- return iterator(
218
- pool, {
219
- address0, address1, decimals0, decimals1, reserve0, reserve1, liquidityUSDToken0, liquidityUSDToken1,
220
- },
221
- );
222
- }));
223
- }
224
- }