@paraswap/dex-lib 2.42.30-smardex → 2.42.31

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 (55) hide show
  1. package/build/dex/index.js +2 -2
  2. package/build/dex/index.js.map +1 -1
  3. package/build/dex/smardex/config.js +0 -7
  4. package/build/dex/smardex/config.js.map +1 -1
  5. package/build/dex/smardex/constants.d.ts +1 -1
  6. package/build/dex/smardex/constants.js +2 -2
  7. package/build/dex/smardex/constants.js.map +1 -1
  8. package/build/dex/smardex/sdk/utils.js.map +1 -1
  9. package/build/dex/smardex/smardex-event-pool.d.ts +2 -2
  10. package/build/dex/smardex/smardex-event-pool.js +9 -11
  11. package/build/dex/smardex/smardex-event-pool.js.map +1 -1
  12. package/build/dex/smardex/smardex.d.ts +1 -6
  13. package/build/dex/smardex/smardex.js +95 -52
  14. package/build/dex/smardex/smardex.js.map +1 -1
  15. package/build/dex/smardex/types.d.ts +1 -12
  16. package/build/dex/wombat/utils.d.ts +1 -0
  17. package/build/dex/wombat/utils.js +14 -1
  18. package/build/dex/wombat/utils.js.map +1 -1
  19. package/build/dex/wombat/wombat-bmw.d.ts +2 -2
  20. package/build/dex/wombat/wombat-bmw.js +8 -25
  21. package/build/dex/wombat/wombat-bmw.js.map +1 -1
  22. package/build/dex/wombat/wombat-pool.d.ts +51 -17
  23. package/build/dex/wombat/wombat-pool.js +398 -112
  24. package/build/dex/wombat/wombat-pool.js.map +1 -1
  25. package/build/dex/wombat/wombat.d.ts +1 -1
  26. package/build/dex/wombat/wombat.js +12 -18
  27. package/build/dex/wombat/wombat.js.map +1 -1
  28. package/package.json +1 -1
  29. package/src/abi/smardex/layer-1/smardex-factory.json +242 -0
  30. package/src/abi/smardex/layer-1/smardex-pool.json +506 -0
  31. package/src/abi/wombat/pool-v3.json +2013 -0
  32. package/src/dex/index.ts +2 -2
  33. package/src/dex/smardex/config.ts +0 -8
  34. package/src/dex/smardex/constants.ts +1 -1
  35. package/src/dex/smardex/sdk/utils.ts +2 -0
  36. package/src/dex/smardex/smardex-e2e.test.ts +21 -22
  37. package/src/dex/smardex/smardex-event-pool.ts +9 -11
  38. package/src/dex/smardex/smardex-events.test.ts +15 -27
  39. package/src/dex/smardex/smardex-integration.test.ts +1 -7
  40. package/src/dex/smardex/smardex.ts +118 -70
  41. package/src/dex/smardex/types.ts +1 -11
  42. package/src/dex/wombat/utils.ts +20 -0
  43. package/src/dex/wombat/wombat-bmw.ts +8 -31
  44. package/src/dex/wombat/wombat-e2e.test.ts +13 -1
  45. package/src/dex/wombat/wombat-events.test.ts +147 -0
  46. package/src/dex/wombat/wombat-integration.test.ts +70 -111
  47. package/src/dex/wombat/wombat-pool.ts +623 -171
  48. package/src/dex/wombat/wombat.ts +13 -23
  49. package/tests/constants-e2e.ts +11 -7
  50. package/src/dex/smardex/subgraph-queries.ts +0 -16
  51. package/src/dex/smardex/usd-reserves.ts +0 -137
  52. /package/src/abi/smardex/{smardex-router.json → all/smardex-router.json} +0 -0
  53. /package/src/abi/smardex/{smardex-factory.json → layer-2/smardex-factory.json} +0 -0
  54. /package/src/abi/smardex/{smardex-pool.json → layer-2/smardex-pool.json} +0 -0
  55. /package/src/abi/wombat/{pool.json → pool-v2.json} +0 -0
package/src/dex/index.ts CHANGED
@@ -38,7 +38,6 @@ import { Excalibur } from './uniswap-v2/excalibur';
38
38
  import { MakerPsm } from './maker-psm/maker-psm';
39
39
  import { KyberDmm } from './kyberdmm/kyberdmm';
40
40
  import { Platypus } from './platypus/platypus';
41
- //import { Wombat } from './wombat/wombat';
42
41
  import { GMX } from './gmx/gmx';
43
42
  import { WooFiV2 } from './woo-fi-v2/woo-fi-v2';
44
43
  import { ParaSwapLimitOrders } from './paraswap-limit-orders/paraswap-limit-orders';
@@ -83,6 +82,7 @@ import { QuickPerps } from './quick-perps/quick-perps';
83
82
  import { NomiswapV2 } from './uniswap-v2/nomiswap-v2';
84
83
  import { Dexalot } from './dexalot/dexalot';
85
84
  import { Smardex } from './smardex/smardex';
85
+ import { Wombat } from './wombat/wombat';
86
86
 
87
87
  const LegacyDexes = [
88
88
  CurveV2,
@@ -162,7 +162,7 @@ const Dexes = [
162
162
  NomiswapV2,
163
163
  SolidlyV3,
164
164
  Smardex,
165
- //Wombat,
165
+ Wombat,
166
166
  ];
167
167
 
168
168
  export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
@@ -13,14 +13,6 @@ export const SmardexConfig: DexConfigMap<DexParams> = {
13
13
  [Network.MAINNET]: {
14
14
  factoryAddress: '0xB878DC600550367e14220d4916Ff678fB284214F',
15
15
  router: '0xC33984ABcAe20f47a754eF78f6526FeF266c0C6F',
16
- legacyInitCode:
17
- '0xb477a06204165d50e6d795c7c216306290eff5d6015f8b65bb46002a8775b548',
18
- legacyPairs: [
19
- '0xd2bf378cea07fe117ffdfd3f5b7e53c2b0b78c05',
20
- '0xf3a4b8efe3e3049f6bc71b47ccb7ce6665420179',
21
- '0xe547ff1c20a6fb24f4afc20a3a02c6d7b0210799',
22
- '0xd2df60073c0a9c0b607ca6ac809acecc0d9250bd',
23
- ],
24
16
  initCode: MAINNET_INIT_HASH,
25
17
  subgraphURL: `${GATEWAY_SUBGRAPH}/ethereum`,
26
18
  },
@@ -29,7 +29,7 @@ export const SUBGRAPH_TIMEOUT = 20 * 1000;
29
29
 
30
30
  export const DefaultSmardexPoolGasCost = 130 * 1000;
31
31
 
32
- export const FEES_LEGACY_LAYER_ONE: SmardexFees = {
32
+ export const FEES_LAYER_ONE: SmardexFees = {
33
33
  feesLP: 500n,
34
34
  feesPool: 200n,
35
35
  };
@@ -1,5 +1,7 @@
1
1
  import { parseUnits } from 'ethers/lib/utils';
2
2
 
3
+ import { CurrencyAmount, Trade } from './types';
4
+
3
5
  // Constants to compute approximate equality
4
6
  const APPROX_EQ_PRECISION = 1n;
5
7
  const APPROX_EQ_BASE_PRECISION = 1000000n;
@@ -79,39 +79,27 @@ describe('SmarDex E2E', () => {
79
79
  [
80
80
  {
81
81
  name: 'USDT',
82
- sellAmount: '1200000000', // USDT to sell
83
- buyAmount: '1200000000000000000000000', // SDEX to buy
82
+ sellAmount: '1200000000',
83
+ buyAmount: '120000',
84
84
  },
85
85
  {
86
86
  name: 'SDEX',
87
- sellAmount: '300000000000000000000000', // SDEX to sell
88
- buyAmount: '30000000000', // USDT to buy
87
+ sellAmount: '300000000000000000000000',
88
+ buyAmount: '30000000000',
89
89
  },
90
90
  ],
91
91
  [
92
92
  {
93
93
  name: 'SDEX',
94
94
  sellAmount: '300000000000000000000000',
95
- buyAmount: '3000000000',
95
+ buyAmount: '30000000000',
96
96
  },
97
97
  {
98
98
  name: 'ETH',
99
99
  sellAmount: '11000000000000000',
100
- buyAmount: '1100000000000000000000000',
100
+ buyAmount: '1100000000',
101
101
  },
102
102
  ],
103
- [
104
- {
105
- name: 'WASSIE',
106
- sellAmount: '567000789000000000000000000',
107
- buyAmount: '1200000000000000000',
108
- },
109
- {
110
- name: 'WETH',
111
- sellAmount: '1200000000000000000',
112
- buyAmount: '567000789000000000000000000',
113
- },
114
- ]
115
103
  ];
116
104
 
117
105
  testForNetwork(network, dexKey, pairs);
@@ -125,7 +113,7 @@ describe('SmarDex E2E', () => {
125
113
  {
126
114
  name: 'USDC',
127
115
  sellAmount: '1200000000',
128
- buyAmount: '1200000000000000000000000',
116
+ buyAmount: '120000',
129
117
  },
130
118
  {
131
119
  name: 'SDEX',
@@ -137,12 +125,12 @@ describe('SmarDex E2E', () => {
137
125
  {
138
126
  name: 'SDEX',
139
127
  sellAmount: '300000000000000000000000',
140
- buyAmount: '800000000000000000',
128
+ buyAmount: '11000000000000000',
141
129
  },
142
130
  {
143
131
  name: 'ETH',
144
132
  sellAmount: '11000000000000000',
145
- buyAmount: '1100000000000000000000000',
133
+ buyAmount: '11000000',
146
134
  },
147
135
  ],
148
136
  ];
@@ -235,6 +223,18 @@ describe('SmarDex E2E', () => {
235
223
  buyAmount: '2500000000',
236
224
  },
237
225
  ],
226
+ [
227
+ {
228
+ name: 'SDEX',
229
+ sellAmount: '190000000000000000000000',
230
+ buyAmount: '8000000',
231
+ },
232
+ {
233
+ name: 'WBTC',
234
+ sellAmount: '8000000',
235
+ buyAmount: '190000000000000000000000',
236
+ },
237
+ ],
238
238
  ];
239
239
 
240
240
  testForNetwork(network, dexKey, pairs);
@@ -245,7 +245,6 @@ describe('SmarDex E2E', () => {
245
245
 
246
246
  const pairs = [
247
247
  [
248
- // Success when holder has enough balance. eg: USDbC: '0x13a13869b814be8f13b86e9875ab51bda882e391'
249
248
  {
250
249
  name: 'WETH',
251
250
  sellAmount: '1500000000000000000',
@@ -4,7 +4,7 @@ import { AbiCoder, Interface } from '@ethersproject/abi';
4
4
  import { IDexHelper } from '../../dex-helper';
5
5
  import { Address, Log, Logger, Token } from '../../types';
6
6
  import { DeepReadonly } from 'ts-essentials';
7
- import { FEES_LEGACY_LAYER_ONE, TOPICS } from './constants';
7
+ import { FEES_LAYER_ONE, TOPICS } from './constants';
8
8
 
9
9
  const coder = new AbiCoder();
10
10
 
@@ -21,7 +21,7 @@ export class SmardexEventPool extends StatefulEventSubscriber<SmardexPoolState>
21
21
  callData: string;
22
22
  },
23
23
  protected smardexFeesMulticallDecoder?: (values: any[]) => SmardexFees,
24
- private legacyPairs?: string[],
24
+ private isLayerOne = true,
25
25
  ) {
26
26
  super('Smardex', `${token0.address}_${token1.address}`, dexHelper, logger);
27
27
  }
@@ -37,8 +37,7 @@ export class SmardexEventPool extends StatefulEventSubscriber<SmardexPoolState>
37
37
  callData: this.poolInterface.encodeFunctionData('getPriceAverage', []),
38
38
  },
39
39
  ];
40
- const isLegacyPool = this.legacyPairs?.includes(this.poolAddress.toLowerCase());
41
- if (!isLegacyPool) {
40
+ if (!this.isLayerOne) {
42
41
  calldata.push(this.smardexFeesMultiCallEntry!);
43
42
  }
44
43
 
@@ -52,8 +51,8 @@ export class SmardexEventPool extends StatefulEventSubscriber<SmardexPoolState>
52
51
  data.returnData[0],
53
52
  )[2];
54
53
 
55
- const fees = isLegacyPool
56
- ? FEES_LEGACY_LAYER_ONE
54
+ const fees = this.isLayerOne
55
+ ? FEES_LAYER_ONE
57
56
  : this.smardexFeesMulticallDecoder!(data.returnData[1]);
58
57
  return {
59
58
  feesLP: fees.feesLP,
@@ -87,7 +86,7 @@ export class SmardexEventPool extends StatefulEventSubscriber<SmardexPoolState>
87
86
  feesLP: fetchedSync.feesLP,
88
87
  feesPool: fetchedSync.feesPool,
89
88
  };
90
- case 'FeesChanged':
89
+ case 'FeesChanged': // only triggerd on L2
91
90
  return {
92
91
  reserves0: state.reserves0,
93
92
  reserves1: state.reserves1,
@@ -124,8 +123,7 @@ export class SmardexEventPool extends StatefulEventSubscriber<SmardexPoolState>
124
123
  callData: this.poolInterface.encodeFunctionData('getPriceAverage', []),
125
124
  },
126
125
  ];
127
- const isLegacyPool = this.legacyPairs?.includes(this.poolAddress.toLowerCase());
128
- if (!isLegacyPool) {
126
+ if (!this.isLayerOne) {
129
127
  calldata.push(this.smardexFeesMultiCallEntry!);
130
128
  }
131
129
 
@@ -147,8 +145,8 @@ export class SmardexEventPool extends StatefulEventSubscriber<SmardexPoolState>
147
145
  const [priceAverage0, priceAverage1, priceAverageLastTimestamp] =
148
146
  coder.decode(['uint256', 'uint256', 'uint256'], data.returnData[2]);
149
147
 
150
- const fees = isLegacyPool
151
- ? FEES_LEGACY_LAYER_ONE
148
+ const fees = this.isLayerOne
149
+ ? FEES_LAYER_ONE
152
150
  : this.smardexFeesMulticallDecoder!(data.returnData[3]);
153
151
 
154
152
  return {
@@ -4,7 +4,8 @@ import { Interface } from '@ethersproject/abi';
4
4
  import { Tokens } from '../../../tests/constants-e2e';
5
5
  dotenv.config();
6
6
 
7
- import SmardexPoolABI from '../../abi/smardex/smardex-pool.json';
7
+ import SmardexPoolLayerOneABI from '../../abi/smardex/layer-1/smardex-pool.json';
8
+ import SmardexPoolLayerTwoABI from '../../abi/smardex/layer-2/smardex-pool.json';
8
9
  import { Smardex } from './smardex';
9
10
  import { SmardexEventPool } from './smardex-event-pool';
10
11
  import { Network } from '../../constants';
@@ -49,7 +50,7 @@ const NETWORK_CONFIGS: NetworkConfig[] = [
49
50
  ],
50
51
  ['Burn']: [17231921, 17762042, 17762668],
51
52
  ['Mint']: [17739609, 17973926, 18062443],
52
- // ['FeesChanged']: [], // none on Legacy pair
53
+ // ['FeesChanged']: [], // none on L1
53
54
  ['Sync']: [
54
55
  18064045,
55
56
  18064060,
@@ -60,6 +61,7 @@ const NETWORK_CONFIGS: NetworkConfig[] = [
60
61
  ['Transfer']: [18064025, 18064045, 18065266],
61
62
  },
62
63
  },
64
+
63
65
  {
64
66
  address: '0xf3a4B8eFe3e3049F6BC71B47ccB7Ce6665420179',
65
67
  symbol0: 'SDEX',
@@ -74,7 +76,7 @@ const NETWORK_CONFIGS: NetworkConfig[] = [
74
76
  ],
75
77
  ['Burn']: [18114357, 18089610, 18026115],
76
78
  ['Mint']: [18115825, 18109975, 18093408],
77
- // ['FeesChanged']: [], // none on Legacy pair
79
+ // ['FeesChanged']: [], // none on L1
78
80
  ['Sync']: [
79
81
  18120396,
80
82
  18120367, // multiple Sync events
@@ -85,24 +87,6 @@ const NETWORK_CONFIGS: NetworkConfig[] = [
85
87
  ['Transfer']: [18108643, 18105778, 18105793],
86
88
  },
87
89
  },
88
- {
89
- // New pool created after the Factory migration (it has configurable fees enabled)
90
- address: '0xf178ADc7D707Cea43FBB3206b3364CB416F044A2',
91
- symbol0: 'WASSIE',
92
- symbol1: 'WETH',
93
- events: {
94
- ['Swap']: [18588928, 18572677, 18571765],
95
- ['Burn']: [18572334, 18569122],
96
- ['Mint']: [18571808, 18688592],
97
- ['FeesChanged']: [18562522],
98
- ['Sync']: [
99
- 18588928, 18572677, 18572334, 18571808, 18571765, 18569122,
100
- ],
101
- ['Transfer']: [
102
- 18588928, 18572677, 18572334, 18571808, 18571765, 18569122,
103
- ],
104
- },
105
- },
106
90
  ],
107
91
  },
108
92
  {
@@ -131,7 +115,7 @@ const NETWORK_CONFIGS: NetworkConfig[] = [
131
115
  ['Swap']: [47448993, 47449888, 47449861, 47448993, 47449214],
132
116
  ['Burn']: [47434291, 47433030, 47426957],
133
117
  ['Mint']: [47354112, 47268128, 47252146],
134
- ['FeesChanged']: [50575166],
118
+ ['FeesChanged']: [], // Fees didn't change on this pool
135
119
  ['Sync']: [47448993, 47449888, 47449861, 47448993, 47449214],
136
120
  ['Transfer']: [47338250, 47338178, 47333469],
137
121
  },
@@ -151,7 +135,7 @@ const NETWORK_CONFIGS: NetworkConfig[] = [
151
135
  ['Swap']: [130424308, 130422563, 130418994, 130415253, 130382808],
152
136
  ['Burn']: [129969327, 129138853, 126200497],
153
137
  ['Mint']: [130028302, 129940404, 129728771],
154
- ['FeesChanged']: [117755384],
138
+ ['FeesChanged']: [], // Fees didn't change on this pool
155
139
  ['Sync']: [130424308, 130422563, 130418994, 130415253, 130382808],
156
140
  ['Transfer']: [130028373, 129918802, 129668897],
157
141
  },
@@ -164,7 +148,7 @@ const NETWORK_CONFIGS: NetworkConfig[] = [
164
148
  ['Swap']: [130429535, 130419879, 130417306, 130414469, 130416990],
165
149
  ['Burn']: [130200966, 127933595, 125698828],
166
150
  ['Mint']: [130263032, 130331095, 128594710],
167
- ['FeesChanged']: [155564828],
151
+ ['FeesChanged']: [], // Fees didn't change on this pool
168
152
  ['Sync']: [130429535, 130419879, 130417306, 130414469, 130416990],
169
153
  ['Transfer']: [130423567, 130423514, 130331095],
170
154
  },
@@ -197,7 +181,7 @@ const NETWORK_CONFIGS: NetworkConfig[] = [
197
181
  ['Swap']: [31678534, 31675409, 31678534, 31675014, 31674391],
198
182
  ['Burn']: [31666734, 31586726, 31462652],
199
183
  ['Mint']: [31662048, 31554796, 31524123],
200
- ['FeesChanged']: [33946671],
184
+ ['FeesChanged']: [], // Fees didn't change on this pool
201
185
  ['Sync']: [31678534, 31675409, 31678534, 31675014, 31674391],
202
186
  ['Transfer']: [31666734, 31632200, 31586970],
203
187
  },
@@ -271,7 +255,11 @@ NETWORK_CONFIGS.forEach(({ name, network, pools, tokens }) => {
271
255
  const smardex = new Smardex(network, dexKey, dexHelper);
272
256
  const multicall = smardex.getFeesMultiCallData(poolAddress);
273
257
  const SmardexPool = new SmardexEventPool(
274
- new Interface(SmardexPoolABI),
258
+ new Interface(
259
+ smardex.isLayer1()
260
+ ? SmardexPoolLayerOneABI
261
+ : SmardexPoolLayerTwoABI,
262
+ ),
275
263
  dexHelper,
276
264
  poolAddress,
277
265
  token0,
@@ -279,7 +267,7 @@ NETWORK_CONFIGS.forEach(({ name, network, pools, tokens }) => {
279
267
  logger,
280
268
  multicall?.callEntry,
281
269
  multicall?.callDecoder,
282
- smardex.legacyPairs,
270
+ smardex.isLayer1(),
283
271
  );
284
272
 
285
273
  // It is done in generateState. But here have to make it manually
@@ -70,7 +70,7 @@ const networkConfigs: Array<NetworkConfig> = [
70
70
  src: 'SDEX',
71
71
  dest: 'WBTC',
72
72
  sell: [0, 100_000, 200_000, 300_000],
73
- buy: [0, 1, 2],
73
+ buy: [0, 1, 2, 3],
74
74
  },
75
75
  {
76
76
  src: 'ARB',
@@ -237,18 +237,12 @@ networkConfigs.forEach(({ name, network, tokens, tokenPairs }) => {
237
237
 
238
238
  it('getTopPoolsForToken', async () => {
239
239
  const smardex = new Smardex(network, dexKey, dexHelper);
240
- // Initialize all smardex pairs
241
- for (const pair of tokenPairs) {
242
- await smardex.findPair(tokens[pair.src], tokens[pair.dest]);
243
- }
244
240
  // Get all distinct tokens of the pairs
245
241
  const distinctTokens = tokenPairs
246
242
  .reduce((acc, pair) => [...acc, pair.src, pair.dest], [] as string[])
247
243
  .filter((value, index, self) => self.indexOf(value) === index)
248
244
  // Native token doesn't have a pool, avoid it
249
245
  .filter(token => tokens[token].address !== ETHER_ADDRESS);
250
- // First getTopPoolsForToken for reserves caching (at least 1 token initializes the whole cache)
251
- await smardex.getTopPoolsForToken(tokens['USDC'].address, 10);
252
246
  // Get top pools for each token and check liquidity
253
247
  await Promise.all(
254
248
  distinctTokens.map(async token =>
@@ -20,25 +20,28 @@ import {
20
20
  SimpleExchangeParam,
21
21
  Token,
22
22
  TransferFeeParams,
23
+ TxInfo,
23
24
  } from '../../types';
24
25
  import { IDexHelper } from '../../dex-helper/index';
25
26
  import {
26
27
  SmardexFees,
27
28
  SmardexPair,
29
+ SmardexPool,
28
30
  SmardexPoolOrderedParams,
29
31
  SmardexPoolState,
30
32
  } from './types';
31
33
  import { getBigIntPow, getDexKeysWithNetwork, isETHAddress } from '../../utils';
32
- import SmardexFactoryABI from '../../abi/smardex/smardex-factory.json';
33
- import SmardexPoolABI from '../../abi/smardex/smardex-pool.json';
34
- import SmardexRouterABI from '../../abi/smardex/smardex-router.json';
34
+ import SmardexFactoryLayerOneABI from '../../abi/smardex/layer-1/smardex-factory.json';
35
+ import SmardexFactoryLayerTwoABI from '../../abi/smardex/layer-2/smardex-factory.json';
35
36
 
37
+ import SmardexRouterABI from '../../abi/smardex/all/smardex-router.json';
36
38
  import { SimpleExchange } from '../simple-exchange';
37
39
  import {
38
40
  SmardexRouterFunctions,
39
41
  directSmardexFunctionName,
40
42
  DefaultSmardexPoolGasCost,
41
- FEES_LEGACY_LAYER_ONE,
43
+ SUBGRAPH_TIMEOUT,
44
+ FEES_LAYER_ONE,
42
45
  } from '../smardex/constants';
43
46
  import { SmardexData, SmardexParam } from '../smardex/types';
44
47
  import { IDex } from '../..';
@@ -48,12 +51,24 @@ import ParaSwapABI from '../../abi/IParaswap.json';
48
51
  import { applyTransferFee } from '../../lib/token-transfer-fee';
49
52
  import { computeAmountIn, computeAmountOut } from './sdk/core';
50
53
  import { SmardexEventPool } from './smardex-event-pool';
51
- import { USDReservesService } from './usd-reserves';
54
+ import SmardexPoolLayerOneABI from '../../abi/smardex/layer-1/smardex-pool.json';
55
+ import SmardexPoolLayerTwoABI from '../../abi/smardex/layer-2/smardex-pool.json';
52
56
 
53
- const smardexPool = new Interface(SmardexPoolABI);
57
+ const smardexPoolL1 = new Interface(SmardexPoolLayerOneABI);
58
+ const smardexPoolL2 = new Interface(SmardexPoolLayerTwoABI);
54
59
 
55
60
  const coder = new AbiCoder();
56
61
 
62
+ function encodePools(pools: SmardexPool[]): NumberAsString[] {
63
+ return pools.map(({ fee, direction, address }) => {
64
+ return (
65
+ (BigInt(fee) << 161n) +
66
+ ((direction ? 0n : 1n) << 160n) +
67
+ BigInt(address)
68
+ ).toString();
69
+ });
70
+ }
71
+
57
72
  export class Smardex
58
73
  extends SimpleExchange
59
74
  implements IDex<SmardexData, SmardexParam>
@@ -70,22 +85,13 @@ export class Smardex
70
85
 
71
86
  protected subgraphURL: string | undefined;
72
87
  protected initCode: string;
73
- protected legacyInitCode: string;
74
-
75
- public legacyPairs: string[];
76
88
 
77
89
  logger: Logger;
78
90
  readonly hasConstantPriceLargeAmounts = false;
79
- readonly isFeeOnTransferSupported: boolean = false;
91
+ readonly isFeeOnTransferSupported: boolean = true;
80
92
  readonly SRC_TOKEN_DEX_TRANSFERS = 1;
81
93
  readonly DEST_TOKEN_DEX_TRANSFERS = 1;
82
94
 
83
- // Constants for top pools caching
84
- readonly CACHED_RESERVES_USD_TTL = 3000;
85
- readonly CACHED_RESERVES_USD_KEY = 'cachedReservesUSD';
86
-
87
- readonly usdReserves: USDReservesService;
88
-
89
95
  public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
90
96
  getDexKeysWithNetwork(SmardexConfig);
91
97
 
@@ -102,21 +108,15 @@ export class Smardex
102
108
  this.factoryAddress = config[network].factoryAddress;
103
109
  this.subgraphURL = config[network].subgraphURL;
104
110
  this.initCode = config[network].initCode;
105
- this.legacyInitCode = config[network].legacyInitCode || '';
106
- this.legacyPairs = config[network].legacyPairs || [];
107
- const factoryAbi = SmardexFactoryABI;
111
+ const factoryAbi = this.isLayer1()
112
+ ? SmardexFactoryLayerOneABI
113
+ : SmardexFactoryLayerTwoABI;
108
114
  this.factory = new dexHelper.web3Provider.eth.Contract(
109
115
  factoryAbi as any,
110
116
  this.factoryAddress,
111
117
  );
112
118
  this.routerInterface = new Interface(ParaSwapABI);
113
119
  this.exchangeRouterInterface = new Interface(SmardexRouterABI);
114
- this.usdReserves = new USDReservesService(
115
- this.dexKey,
116
- this.network,
117
- this.dexHelper,
118
- this.subgraphURL!,
119
- );
120
120
  }
121
121
 
122
122
  getAdapters(side: SwapSide): { name: string; index: number }[] | null {
@@ -237,9 +237,7 @@ export class Smardex
237
237
  router: this.routerAddress,
238
238
  path: [from.address.toLowerCase(), to.address.toLowerCase()],
239
239
  factory: this.factoryAddress,
240
- initCode: this.legacyPairs.includes(pairParam.exchange)
241
- ? this.legacyInitCode
242
- : this.initCode,
240
+ initCode: this.initCode,
243
241
  pools: [
244
242
  {
245
243
  address: pairParam.exchange,
@@ -420,15 +418,18 @@ export class Smardex
420
418
 
421
419
  // On Smardex the fees are upgradable on layer 2.
422
420
  public getFeesMultiCallData(pairAddress: string) {
423
- if (this.legacyPairs.includes(pairAddress.toLowerCase())) {
421
+ if (this.isLayer1()) {
424
422
  return null;
425
423
  }
426
424
  const callEntry = {
427
425
  target: pairAddress,
428
- callData: smardexPool.encodeFunctionData('getPairFees'),
426
+ callData: smardexPoolL2.encodeFunctionData('getPairFees'),
429
427
  };
430
428
  const callDecoder = (values: any[]): SmardexFees => {
431
- const feesData = smardexPool.decodeFunctionResult('getPairFees', values);
429
+ const feesData = smardexPoolL2.decodeFunctionResult(
430
+ 'getPairFees',
431
+ values,
432
+ );
432
433
  return {
433
434
  feesLP: feesData.feesLP_.toBigInt(),
434
435
  feesPool: feesData.feesPool_.toBigInt(),
@@ -455,15 +456,16 @@ export class Smardex
455
456
  ) {
456
457
  const multiCallFeeData = this.getFeesMultiCallData(pair.exchange!);
457
458
  pair.pool = new SmardexEventPool(
458
- smardexPool,
459
+ this.isLayer1() ? smardexPoolL1 : smardexPoolL2,
459
460
  this.dexHelper,
460
461
  pair.exchange!,
461
462
  pair.token0,
462
463
  pair.token1,
463
464
  this.logger,
465
+ // For layer 2 we need to fetch the fees
464
466
  multiCallFeeData?.callEntry,
465
467
  multiCallFeeData?.callDecoder,
466
- this.legacyPairs,
468
+ this.isLayer1(),
467
469
  );
468
470
  pair.pool.addressesSubscribed.push(pair.exchange!);
469
471
 
@@ -495,20 +497,19 @@ export class Smardex
495
497
  let calldata = [
496
498
  {
497
499
  target: pair.exchange!,
498
- callData: smardexPool.encodeFunctionData('getReserves'),
500
+ callData: smardexPoolL1.encodeFunctionData('getReserves'),
499
501
  },
500
502
  {
501
503
  target: pair.exchange!,
502
- callData: smardexPool.encodeFunctionData('getFictiveReserves'),
504
+ callData: smardexPoolL1.encodeFunctionData('getFictiveReserves'),
503
505
  },
504
506
  {
505
507
  target: pair.exchange!,
506
- callData: smardexPool.encodeFunctionData('getPriceAverage'),
508
+ callData: smardexPoolL1.encodeFunctionData('getPriceAverage'),
507
509
  },
508
510
  ];
509
- // Exclude legacy pairs from fees call
510
- !this.legacyPairs.includes(pair.exchange!.toLowerCase()) &&
511
- calldata.push(multiCallFeeData[i]!.callEntry);
511
+ // Fetch fees only on layer 2
512
+ !this.isLayer1() && calldata.push(multiCallFeeData[i]!.callEntry);
512
513
  return calldata;
513
514
  })
514
515
  .flat();
@@ -541,11 +542,11 @@ export class Smardex
541
542
  priceAverageLastTimestamp: coder
542
543
  .decode(['uint256', 'uint256', 'uint256'], returnData[i][2])[2]
543
544
  .toString(),
544
- feesLP: this.legacyPairs.includes(_pair.exchange!.toLowerCase())
545
- ? FEES_LEGACY_LAYER_ONE.feesLP
545
+ feesLP: this.isLayer1()
546
+ ? FEES_LAYER_ONE.feesLP
546
547
  : multiCallFeeData[i]!.callDecoder(returnData[i][3]).feesLP,
547
- feesPool: this.legacyPairs.includes(_pair.exchange!.toLowerCase())
548
- ? FEES_LEGACY_LAYER_ONE.feesPool
548
+ feesPool: this.isLayer1()
549
+ ? FEES_LAYER_ONE.feesPool
549
550
  : multiCallFeeData[i]!.callDecoder(returnData[i][3]).feesPool,
550
551
  }));
551
552
  } catch (e) {
@@ -687,36 +688,83 @@ export class Smardex
687
688
  );
688
689
  }
689
690
 
691
+ isLayer1(): boolean {
692
+ return this.network === Network.MAINNET;
693
+ }
694
+
690
695
  async getTopPoolsForToken(
691
696
  tokenAddress: Address,
692
697
  limit: number,
693
698
  ): Promise<PoolLiquidity[]> {
694
- tokenAddress = tokenAddress.toLowerCase();
695
- const liquidityPromises = Object.values(this.pairs)
696
- .filter(
697
- pair =>
698
- pair.token0.address.toLowerCase() === tokenAddress ||
699
- pair.token1.address.toLowerCase() === tokenAddress,
700
- )
701
- .filter(pair => pair.exchange)
702
- .map(async pair => {
703
- const usdReserve = await this.usdReserves.getUSDReserveForPair(
704
- pair.exchange!,
705
- );
706
- return {
707
- exchange: this.dexKey,
708
- address: pair.exchange!,
709
- liquidityUSD: usdReserve,
710
- connectorTokens: [
711
- tokenAddress === pair.token0.address.toLowerCase()
712
- ? pair.token1
713
- : pair.token0,
714
- ],
715
- };
716
- });
717
- const resolvedLiquidity = await Promise.all(liquidityPromises);
718
- return resolvedLiquidity
719
- .sort((a, b) => b.liquidityUSD - a.liquidityUSD)
720
- .slice(0, limit);
699
+ if (!this.subgraphURL) return [];
700
+ const query = `
701
+ query ($token: Bytes!, $limit: Int) {
702
+ pools0: pairs(first: $limit, orderBy: reserveUSD, orderDirection: desc, where: {token0: $token, reserve0_gt: 1, reserve1_gt: 1}) {
703
+ id
704
+ token0 {
705
+ id
706
+ decimals
707
+ }
708
+ token1 {
709
+ id
710
+ decimals
711
+ }
712
+ reserveUSD
713
+ }
714
+ pools1: pairs(first: $limit, orderBy: reserveUSD, orderDirection: desc, where: {token1: $token, reserve0_gt: 1, reserve1_gt: 1}) {
715
+ id
716
+ token0 {
717
+ id
718
+ decimals
719
+ }
720
+ token1 {
721
+ id
722
+ decimals
723
+ }
724
+ reserveUSD
725
+ }
726
+ }`;
727
+
728
+ const { data } = await this.dexHelper.httpRequest.post(
729
+ this.subgraphURL,
730
+ {
731
+ query,
732
+ variables: { token: tokenAddress.toLowerCase(), limit },
733
+ },
734
+ SUBGRAPH_TIMEOUT,
735
+ { 'x-api-key': this.dexHelper.config.data.smardexSubgraphAuthToken! },
736
+ );
737
+
738
+ if (!(data && data.pools0 && data.pools1))
739
+ throw new Error("Couldn't fetch the pools from the subgraph");
740
+ const pools0 = _.map(data.pools0, pool => ({
741
+ exchange: this.dexKey,
742
+ address: pool.id.toLowerCase(),
743
+ connectorTokens: [
744
+ {
745
+ address: pool.token1.id.toLowerCase(),
746
+ decimals: parseInt(pool.token1.decimals),
747
+ },
748
+ ],
749
+ liquidityUSD: parseFloat(pool.reserveUSD),
750
+ }));
751
+
752
+ const pools1 = _.map(data.pools1, pool => ({
753
+ exchange: this.dexKey,
754
+ address: pool.id.toLowerCase(),
755
+ connectorTokens: [
756
+ {
757
+ address: pool.token0.id.toLowerCase(),
758
+ decimals: parseInt(pool.token0.decimals),
759
+ },
760
+ ],
761
+ liquidityUSD: parseFloat(pool.reserveUSD),
762
+ }));
763
+
764
+ return _.slice(
765
+ _.sortBy(_.concat(pools0, pools1), [pool => -1 * pool.liquidityUSD]),
766
+ 0,
767
+ limit,
768
+ );
721
769
  }
722
770
  }