@paraswap/dex-lib 3.4.2-balancer-v2-single-swap.0 → 3.4.2

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 (110) hide show
  1. package/README.md +377 -377
  2. package/build/config.js +9 -5
  3. package/build/config.js.map +1 -1
  4. package/build/dex/algebra/algebra.js +25 -25
  5. package/build/dex/balancer-v1/balancer-v1.js +34 -34
  6. package/build/dex/balancer-v2/balancer-v2.d.ts +1 -1
  7. package/build/dex/balancer-v2/balancer-v2.js +65 -78
  8. package/build/dex/balancer-v2/balancer-v2.js.map +1 -1
  9. package/build/dex/camelot/camelot.js +27 -27
  10. package/build/dex/kyberdmm/kyberdmm.js +27 -27
  11. package/build/dex/pancakeswap-v3/pancakeswap-v3.js +25 -25
  12. package/build/dex/solidly/solidly.js +27 -27
  13. package/build/dex/solidly-v3/solidly-v3.js +25 -25
  14. package/build/dex/swaap-v2/config.js +15 -0
  15. package/build/dex/swaap-v2/config.js.map +1 -1
  16. package/build/dex/uniswap-v2/uniswap-v2.js +26 -26
  17. package/build/dex/uniswap-v3/uniswap-v3.js +25 -25
  18. package/funding.json +5 -5
  19. package/package.json +1 -1
  20. package/src/config.ts +500 -496
  21. package/src/constants.ts +67 -67
  22. package/src/dex/aave-v2/aave-v2.ts +409 -409
  23. package/src/dex/aave-v3/aave-v3.ts +320 -320
  24. package/src/dex/algebra/algebra.ts +1180 -1180
  25. package/src/dex/angle-staked-stable/angle-staked-stable.ts +347 -347
  26. package/src/dex/augustus-rfq.ts +180 -180
  27. package/src/dex/bProtocol/bProtocol.ts +108 -108
  28. package/src/dex/balancer-v1/balancer-v1.ts +572 -572
  29. package/src/dex/balancer-v2/balancer-v2-e2e.test.ts +1699 -1699
  30. package/src/dex/balancer-v2/balancer-v2.ts +1669 -1701
  31. package/src/dex/bancor/bancor.ts +128 -128
  32. package/src/dex/camelot/camelot.ts +873 -873
  33. package/src/dex/compound/compound.ts +107 -107
  34. package/src/dex/curve-v1/config.ts +1179 -1179
  35. package/src/dex/curve-v1/curve-v1-e2e.test.ts +507 -507
  36. package/src/dex/curve-v1/curve-v1.ts +1315 -1315
  37. package/src/dex/curve-v1-factory/config.ts +1018 -1018
  38. package/src/dex/curve-v1-factory/curve-v1-factory.ts +1288 -1288
  39. package/src/dex/curve-v2/curve-v2.ts +440 -440
  40. package/src/dex/dexalot/dexalot.ts +1100 -1100
  41. package/src/dex/dodo-v1/dodo-v1.ts +132 -132
  42. package/src/dex/dodo-v2.ts +266 -266
  43. package/src/dex/etherfi.ts +239 -239
  44. package/src/dex/generic-rfq/generic-rfq.ts +431 -431
  45. package/src/dex/gmx/gmx.ts +321 -321
  46. package/src/dex/hashflow/hashflow.ts +1135 -1135
  47. package/src/dex/idex.ts +254 -254
  48. package/src/dex/index.ts +357 -357
  49. package/src/dex/jarvis-v6/jarvis-v6.ts +509 -509
  50. package/src/dex/jarvis.ts +262 -262
  51. package/src/dex/kyberdmm/kyberdmm.ts +649 -649
  52. package/src/dex/lido/lido.ts +110 -110
  53. package/src/dex/maker-psm/maker-psm.ts +592 -592
  54. package/src/dex/maverick-v1/maverick-v1.ts +537 -537
  55. package/src/dex/nerve/config.ts +635 -635
  56. package/src/dex/nerve/nerve-e2e.test.ts +731 -731
  57. package/src/dex/nerve/nerve-events.test.ts +911 -911
  58. package/src/dex/nerve/nerve.ts +467 -467
  59. package/src/dex/onebit/onebit.ts +113 -113
  60. package/src/dex/pancakeswap-v3/pancakeswap-v3.ts +1150 -1150
  61. package/src/dex/paraswap-limit-orders/config.ts +60 -60
  62. package/src/dex/paraswap-limit-orders/paraswap-limit-orders-e2e.test.ts +137 -137
  63. package/src/dex/paraswap-limit-orders/paraswap-limit-orders-integration.test.ts +328 -328
  64. package/src/dex/paraswap-limit-orders/paraswap-limit-orders.ts +631 -631
  65. package/src/dex/polygon-migrator/polygon-migrator.ts +194 -194
  66. package/src/dex/quick-perps/quick-perps.ts +337 -337
  67. package/src/dex/quickswap/config.ts +16 -16
  68. package/src/dex/quickswap/quickswap-e2e.test.ts +384 -384
  69. package/src/dex/smoothy/smoothy.ts +110 -110
  70. package/src/dex/solidly/config.ts +225 -225
  71. package/src/dex/solidly/solidly-e2e.test.ts +1421 -1421
  72. package/src/dex/solidly/solidly-integration.test.ts +1753 -1753
  73. package/src/dex/solidly/solidly.ts +687 -687
  74. package/src/dex/solidly-v3/solidly-v3.ts +968 -968
  75. package/src/dex/spark/spark.ts +284 -284
  76. package/src/dex/stable-pool/stable-pool.ts +129 -129
  77. package/src/dex/swaap-v2/config.ts +15 -0
  78. package/src/dex/swaap-v2/swaap-v2-e2e.test.ts +233 -17
  79. package/src/dex/swaap-v2/swaap-v2-integration.test.ts +1 -1
  80. package/src/dex/swaap-v2/swaap-v2.ts +1056 -1056
  81. package/src/dex/swell/swell.ts +312 -312
  82. package/src/dex/synthetix/synthetix.ts +420 -420
  83. package/src/dex/trader-joe-v2.1/trader-joe-v2.1.ts +180 -180
  84. package/src/dex/uniswap-v2/config.ts +637 -637
  85. package/src/dex/uniswap-v2/constants.ts +58 -58
  86. package/src/dex/uniswap-v2/uniswap-v2-e2e-avalanche.test.ts +1257 -1257
  87. package/src/dex/uniswap-v2/uniswap-v2-e2e-base.test.ts +42 -42
  88. package/src/dex/uniswap-v2/uniswap-v2-e2e-bsc.test.ts +1295 -1295
  89. package/src/dex/uniswap-v2/uniswap-v2-e2e-mainnet.test.ts +1834 -1834
  90. package/src/dex/uniswap-v2/uniswap-v2-e2e-polygon.test.ts +871 -871
  91. package/src/dex/uniswap-v2/uniswap-v2.ts +1117 -1117
  92. package/src/dex/uniswap-v3/uniswap-v3.ts +1501 -1501
  93. package/src/dex/uniswap-v3.ts +242 -242
  94. package/src/dex/weth/config.ts +73 -73
  95. package/src/dex/weth/weth.ts +230 -230
  96. package/src/dex/wombat/wombat.ts +467 -467
  97. package/src/dex/woo-fi-v2/woo-fi-v2.ts +529 -529
  98. package/src/dex/wsteth/wsteth.ts +304 -304
  99. package/src/dex/zerox/index.ts +328 -328
  100. package/src/executor/Executor01BytecodeBuilder.ts +474 -474
  101. package/src/executor/Executor02BytecodeBuilder.ts +1392 -1392
  102. package/src/executor/ExecutorBytecodeBuilder.ts +390 -390
  103. package/src/executor/ExecutorDetector.ts +131 -131
  104. package/src/executor/constants.ts +66 -66
  105. package/src/executor/executor01-bytecode-builder-e2e.test.ts +1478 -1478
  106. package/src/executor/utils.ts +62 -62
  107. package/src/router/simpleswap.ts +366 -366
  108. package/src/types.ts +342 -342
  109. package/tests/constants-e2e.ts +1632 -1622
  110. package/tests/generate-new-limit-order.ts +107 -107
package/src/dex/index.ts CHANGED
@@ -1,357 +1,357 @@
1
- import _ from 'lodash';
2
- import { UnoptimizedRate } from '../types';
3
- import { CurveV2 } from './curve-v2/curve-v2';
4
- import { IDexTxBuilder, DexConstructor, IDex, IRouteOptimizer } from './idex';
5
- import { Jarvis } from './jarvis';
6
- import { JarvisV6 } from './jarvis-v6/jarvis-v6';
7
- import { StablePool } from './stable-pool/stable-pool';
8
- import { Weth } from './weth/weth';
9
- import { PolygonMigrator } from './polygon-migrator/polygon-migrator';
10
- import { ZeroX } from './zerox';
11
- import { UniswapV3 } from './uniswap-v3/uniswap-v3';
12
- import { BalancerV2 } from './balancer-v2/balancer-v2';
13
- import { balancerV2Merge } from './balancer-v2/optimizer';
14
- import { UniswapV2 } from './uniswap-v2/uniswap-v2';
15
- import { UniswapV2Alias } from './uniswap-v2/constants';
16
- import { uniswapMerge } from './uniswap-v2/optimizer';
17
- import { BiSwap } from './uniswap-v2/biswap';
18
- import { MDEX } from './uniswap-v2/mdex';
19
- import { Dfyn } from './uniswap-v2/dfyn';
20
- import { Bancor } from './bancor/bancor';
21
- import { BProtocol } from './bProtocol/bProtocol';
22
- import { MStable } from './mStable';
23
- import { Shell } from './shell';
24
- import { Onebit } from './onebit/onebit';
25
- import { Compound } from './compound/compound';
26
- import { AaveV2 } from './aave-v2/aave-v2';
27
- import { AaveV3 } from './aave-v3/aave-v3';
28
- import { OneInchLp } from './OneInchLp';
29
- import { DodoV1 } from './dodo-v1/dodo-v1';
30
- import { DodoV2 } from './dodo-v2';
31
- import { Smoothy } from './smoothy/smoothy';
32
- import { Nerve } from './nerve/nerve';
33
- import { IDexHelper } from '../dex-helper';
34
- import { SwapSide } from '../constants';
35
- import { Adapters } from '../types';
36
- import { Lido } from './lido/lido';
37
- import { Excalibur } from './uniswap-v2/excalibur';
38
- import { MakerPsm } from './maker-psm/maker-psm';
39
- import { KyberDmm } from './kyberdmm/kyberdmm';
40
- import { GMX } from './gmx/gmx';
41
- import { WooFiV2 } from './woo-fi-v2/woo-fi-v2';
42
- import { ParaSwapLimitOrders } from './paraswap-limit-orders/paraswap-limit-orders';
43
- import { AugustusRFQOrder } from './augustus-rfq';
44
- import { Solidly } from './solidly/solidly';
45
- import { SolidlyV3 } from './solidly-v3/solidly-v3';
46
- import { Ramses } from './solidly/forks-override/ramses';
47
- import { Thena } from './solidly/forks-override/thena';
48
- import { Chronos } from './solidly/forks-override/chronos';
49
- import { Velodrome } from './solidly/forks-override/velodrome';
50
- import { VelodromeV2 } from './solidly/forks-override/velodromeV2';
51
- import { Aerodrome } from './solidly/forks-override/aerodrome';
52
- import { SpiritSwapV2 } from './solidly/forks-override/spiritSwapV2';
53
- import { Synthetix } from './synthetix/synthetix';
54
- import { Usdfi } from './solidly/forks-override/usdfi';
55
- import { Equalizer } from './solidly/forks-override/equalizer';
56
- import { Velocimeter } from './solidly/forks-override/velocimeter';
57
- import { BalancerV1 } from './balancer-v1/balancer-v1';
58
- import { balancerV1Merge } from './balancer-v1/optimizer';
59
- import { CurveV1 } from './curve-v1/curve-v1';
60
- import { CurveFork } from './curve-v1/forks/curve-forks/curve-forks';
61
- import { Swerve } from './curve-v1/forks/swerve/swerve';
62
- import { CurveV1Factory } from './curve-v1-factory/curve-v1-factory';
63
- import { GenericRFQ } from './generic-rfq/generic-rfq';
64
- import { WstETH } from './wsteth/wsteth';
65
- import { Camelot } from './camelot/camelot';
66
- import { Hashflow } from './hashflow/hashflow';
67
- import { SolidlyEthereum } from './solidly/solidly-ethereum';
68
- import { MaverickV1 } from './maverick-v1/maverick-v1';
69
- import { QuickSwapV3 } from './quickswap/quickswap-v3';
70
- import { ThenaFusion } from './quickswap/thena-fusion';
71
- import { TraderJoeV2 } from './trader-joe-v2';
72
- import { SwaapV2 } from './swaap-v2/swaap-v2';
73
- import { TraderJoeV21 } from './trader-joe-v2.1/trader-joe-v2.1';
74
- import { PancakeswapV3 } from './pancakeswap-v3/pancakeswap-v3';
75
- import { Algebra } from './algebra/algebra';
76
- import { AngleStakedStable } from './angle-staked-stable/angle-staked-stable';
77
- import { QuickPerps } from './quick-perps/quick-perps';
78
- import { NomiswapV2 } from './uniswap-v2/nomiswap-v2';
79
- import { Dexalot } from './dexalot/dexalot';
80
- import { Wombat } from './wombat/wombat';
81
- import { Swell } from './swell/swell';
82
- import { PharaohV1 } from './solidly/forks-override/pharaohV1';
83
- import { EtherFi } from './etherfi';
84
- import { Spark } from './spark/spark';
85
- import { VelodromeSlipstream } from './uniswap-v3/forks/velodrome-slipstream/velodrome-slipstream';
86
-
87
- const LegacyDexes = [
88
- CurveV2,
89
- StablePool,
90
- Smoothy,
91
- ZeroX,
92
- Bancor,
93
- BProtocol,
94
- MStable,
95
- Shell,
96
- Onebit,
97
- Compound,
98
- OneInchLp,
99
- DodoV1,
100
- DodoV2,
101
- QuickSwapV3,
102
- ThenaFusion,
103
- TraderJoeV2,
104
- TraderJoeV21,
105
- Jarvis,
106
- Lido,
107
- AugustusRFQOrder,
108
- EtherFi,
109
- ];
110
-
111
- const Dexes = [
112
- Dexalot,
113
- CurveV1,
114
- CurveFork,
115
- Swerve,
116
- BalancerV1,
117
- BalancerV2,
118
- UniswapV2,
119
- UniswapV3,
120
- Algebra,
121
- PancakeswapV3,
122
- VelodromeSlipstream,
123
- BiSwap,
124
- MDEX,
125
- Dfyn,
126
- Excalibur,
127
- AaveV2,
128
- AaveV3,
129
- KyberDmm,
130
- Weth,
131
- PolygonMigrator,
132
- MakerPsm,
133
- Nerve,
134
- GMX,
135
- JarvisV6,
136
- WooFiV2,
137
- ParaSwapLimitOrders,
138
- Solidly,
139
- SolidlyEthereum,
140
- SpiritSwapV2,
141
- Ramses,
142
- Thena,
143
- Chronos,
144
- Velodrome,
145
- VelodromeV2,
146
- Aerodrome,
147
- Equalizer,
148
- Velocimeter,
149
- Usdfi,
150
- Synthetix,
151
- CurveV1Factory,
152
- WstETH,
153
- Hashflow,
154
- MaverickV1,
155
- Camelot,
156
- SwaapV2,
157
- AngleStakedStable,
158
- QuickPerps,
159
- NomiswapV2,
160
- SolidlyV3,
161
- Wombat,
162
- Swell,
163
- PharaohV1,
164
- Spark,
165
- ];
166
-
167
- export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
168
- any,
169
- any
170
- >;
171
-
172
- interface IGetDirectFunctionName {
173
- getDirectFunctionName?(): string[];
174
- getDirectFunctionNameV6?(): string[];
175
- }
176
-
177
- export class DexAdapterService {
178
- dexToKeyMap: {
179
- [key: string]: LegacyDexConstructor | DexConstructor<any, any, any>;
180
- } = {};
181
- directFunctionsNames: string[];
182
- directFunctionsNamesV6: string[];
183
- dexInstances: {
184
- [key: string]: IDexTxBuilder<any, any> | IDex<any, any, any>;
185
- } = {};
186
- isLegacy: { [dexKey: string]: boolean } = {};
187
- // dexKeys only has keys for non legacy dexes
188
- dexKeys: string[] = [];
189
- genericRFQDexKeys: Set<string> = new Set();
190
- uniswapV2Alias: string | null;
191
-
192
- public routeOptimizers: IRouteOptimizer<UnoptimizedRate>[] = [
193
- balancerV1Merge,
194
- balancerV2Merge,
195
- uniswapMerge,
196
- ];
197
-
198
- constructor(
199
- public dexHelper: IDexHelper,
200
- public network: number,
201
- protected sellAdapters: Adapters = {},
202
- protected buyAdapters: Adapters = {},
203
- ) {
204
- LegacyDexes.forEach(DexAdapter => {
205
- DexAdapter.dexKeys.forEach(key => {
206
- this.dexToKeyMap[key.toLowerCase()] = DexAdapter;
207
- this.isLegacy[key.toLowerCase()] = true;
208
- });
209
- });
210
-
211
- const handleDex = (newDex: IDex<any, any, any>, key: string) => {
212
- const _key = key.toLowerCase();
213
- this.isLegacy[_key] = false;
214
- this.dexKeys.push(key);
215
- this.dexInstances[_key] = newDex;
216
-
217
- const sellAdaptersDex = (
218
- this.dexInstances[_key] as IDex<any, any, any>
219
- ).getAdapters(SwapSide.SELL);
220
- if (sellAdaptersDex)
221
- this.sellAdapters[_key] = sellAdaptersDex.map(({ name, index }) => ({
222
- adapter: this.dexHelper.config.data.adapterAddresses[name],
223
- index,
224
- }));
225
-
226
- const buyAdaptersDex = (
227
- this.dexInstances[_key] as IDex<any, any, any>
228
- ).getAdapters(SwapSide.BUY);
229
- if (buyAdaptersDex)
230
- this.buyAdapters[_key] = buyAdaptersDex.map(({ name, index }) => ({
231
- adapter: this.dexHelper.config.data.adapterAddresses[name],
232
- index,
233
- }));
234
- };
235
-
236
- Dexes.forEach(DexAdapter => {
237
- DexAdapter.dexKeysWithNetwork.forEach(({ key, networks }) => {
238
- if (networks.includes(network)) {
239
- const dex = new DexAdapter(network, key, dexHelper);
240
- handleDex(dex, key);
241
- }
242
- });
243
- });
244
-
245
- const rfqConfigs = dexHelper.config.data.rfqConfigs;
246
- Object.keys(dexHelper.config.data.rfqConfigs).forEach(rfqName => {
247
- const dex = new GenericRFQ(
248
- network,
249
- rfqName,
250
- dexHelper,
251
- rfqConfigs[rfqName],
252
- );
253
- handleDex(dex, rfqName);
254
- this.genericRFQDexKeys.add(rfqName.toLowerCase());
255
- });
256
-
257
- this.directFunctionsNames = [...LegacyDexes, ...Dexes]
258
- .flatMap(dexAdapter => {
259
- const _dexAdapter = dexAdapter as IGetDirectFunctionName;
260
- return _dexAdapter.getDirectFunctionName
261
- ? _dexAdapter.getDirectFunctionName()
262
- : [];
263
- })
264
- .filter(x => !!x)
265
- .map(v => v.toLowerCase());
266
-
267
- // include GenericRFQ, because it has direct method for v6
268
- this.directFunctionsNamesV6 = [...LegacyDexes, ...Dexes, GenericRFQ]
269
- .flatMap(dexAdapter => {
270
- const _dexAdapter = dexAdapter as IGetDirectFunctionName;
271
- return _dexAdapter.getDirectFunctionNameV6
272
- ? _dexAdapter.getDirectFunctionNameV6()
273
- : [];
274
- })
275
- .filter(x => !!x)
276
- .map(v => v.toLowerCase());
277
-
278
- this.uniswapV2Alias =
279
- this.network in UniswapV2Alias
280
- ? UniswapV2Alias[this.network].toLowerCase()
281
- : null;
282
- }
283
-
284
- getTxBuilderDexByKey(dexKey: string): IDexTxBuilder<any, any> {
285
- let _dexKey = this.getDexKeySpecial(dexKey);
286
-
287
- if (!this.dexInstances[_dexKey]) {
288
- const DexAdapter = this.dexToKeyMap[_dexKey];
289
- if (!DexAdapter)
290
- throw new Error(
291
- `${dexKey} dex is not supported for network(${this.network})!`,
292
- );
293
-
294
- this.dexInstances[_dexKey] = new (DexAdapter as LegacyDexConstructor)(
295
- this.dexHelper,
296
- );
297
- }
298
-
299
- return this.dexInstances[_dexKey];
300
- }
301
-
302
- isDirectFunctionName(functionName: string): boolean {
303
- return this.directFunctionsNames.includes(functionName.toLowerCase());
304
- }
305
-
306
- isDirectFunctionNameV6(functionName: string): boolean {
307
- return this.directFunctionsNamesV6.includes(functionName.toLowerCase());
308
- }
309
-
310
- getAllDexKeys() {
311
- return _.uniq(this.dexKeys);
312
- }
313
-
314
- getDexByKey(key: string): IDex<any, any, any> {
315
- const _key = key.toLowerCase();
316
- if (!(_key in this.isLegacy) || this.isLegacy[_key])
317
- throw new Error(`Invalid Dex Key ${key}`);
318
-
319
- return this.dexInstances[_key] as IDex<any, any, any>;
320
- }
321
-
322
- getAllDexAdapters(side: SwapSide = SwapSide.SELL) {
323
- return side === SwapSide.SELL ? this.sellAdapters : this.buyAdapters;
324
- }
325
-
326
- getDexKeySpecial(dexKey: string, isAdapters: boolean = false) {
327
- dexKey = dexKey.toLowerCase();
328
- if (this.genericRFQDexKeys.has(dexKey)) {
329
- return dexKey;
330
- }
331
- if (!isAdapters && /^paraswappool(.*)/i.test(dexKey)) return 'zerox';
332
- else if ('uniswapforkoptimized' === dexKey) {
333
- if (!this.uniswapV2Alias)
334
- throw new Error(
335
- `${dexKey} dex is not supported for network(${this.network})!`,
336
- );
337
- return this.uniswapV2Alias;
338
- }
339
- return dexKey;
340
- }
341
-
342
- getAdapter(dexKey: string, side: SwapSide) {
343
- const specialDexKey = this.getDexKeySpecial(dexKey, true);
344
- return side === SwapSide.SELL
345
- ? this.sellAdapters[specialDexKey]
346
- : this.buyAdapters[specialDexKey];
347
- }
348
-
349
- doesPreProcessingRequireSequentiality(dexKey: string): boolean {
350
- try {
351
- const dex = this.getDexByKey(dexKey);
352
- return !!dex.needsSequentialPreprocessing;
353
- } catch (e) {
354
- return false;
355
- }
356
- }
357
- }
1
+ import _ from 'lodash';
2
+ import { UnoptimizedRate } from '../types';
3
+ import { CurveV2 } from './curve-v2/curve-v2';
4
+ import { IDexTxBuilder, DexConstructor, IDex, IRouteOptimizer } from './idex';
5
+ import { Jarvis } from './jarvis';
6
+ import { JarvisV6 } from './jarvis-v6/jarvis-v6';
7
+ import { StablePool } from './stable-pool/stable-pool';
8
+ import { Weth } from './weth/weth';
9
+ import { PolygonMigrator } from './polygon-migrator/polygon-migrator';
10
+ import { ZeroX } from './zerox';
11
+ import { UniswapV3 } from './uniswap-v3/uniswap-v3';
12
+ import { BalancerV2 } from './balancer-v2/balancer-v2';
13
+ import { balancerV2Merge } from './balancer-v2/optimizer';
14
+ import { UniswapV2 } from './uniswap-v2/uniswap-v2';
15
+ import { UniswapV2Alias } from './uniswap-v2/constants';
16
+ import { uniswapMerge } from './uniswap-v2/optimizer';
17
+ import { BiSwap } from './uniswap-v2/biswap';
18
+ import { MDEX } from './uniswap-v2/mdex';
19
+ import { Dfyn } from './uniswap-v2/dfyn';
20
+ import { Bancor } from './bancor/bancor';
21
+ import { BProtocol } from './bProtocol/bProtocol';
22
+ import { MStable } from './mStable';
23
+ import { Shell } from './shell';
24
+ import { Onebit } from './onebit/onebit';
25
+ import { Compound } from './compound/compound';
26
+ import { AaveV2 } from './aave-v2/aave-v2';
27
+ import { AaveV3 } from './aave-v3/aave-v3';
28
+ import { OneInchLp } from './OneInchLp';
29
+ import { DodoV1 } from './dodo-v1/dodo-v1';
30
+ import { DodoV2 } from './dodo-v2';
31
+ import { Smoothy } from './smoothy/smoothy';
32
+ import { Nerve } from './nerve/nerve';
33
+ import { IDexHelper } from '../dex-helper';
34
+ import { SwapSide } from '../constants';
35
+ import { Adapters } from '../types';
36
+ import { Lido } from './lido/lido';
37
+ import { Excalibur } from './uniswap-v2/excalibur';
38
+ import { MakerPsm } from './maker-psm/maker-psm';
39
+ import { KyberDmm } from './kyberdmm/kyberdmm';
40
+ import { GMX } from './gmx/gmx';
41
+ import { WooFiV2 } from './woo-fi-v2/woo-fi-v2';
42
+ import { ParaSwapLimitOrders } from './paraswap-limit-orders/paraswap-limit-orders';
43
+ import { AugustusRFQOrder } from './augustus-rfq';
44
+ import { Solidly } from './solidly/solidly';
45
+ import { SolidlyV3 } from './solidly-v3/solidly-v3';
46
+ import { Ramses } from './solidly/forks-override/ramses';
47
+ import { Thena } from './solidly/forks-override/thena';
48
+ import { Chronos } from './solidly/forks-override/chronos';
49
+ import { Velodrome } from './solidly/forks-override/velodrome';
50
+ import { VelodromeV2 } from './solidly/forks-override/velodromeV2';
51
+ import { Aerodrome } from './solidly/forks-override/aerodrome';
52
+ import { SpiritSwapV2 } from './solidly/forks-override/spiritSwapV2';
53
+ import { Synthetix } from './synthetix/synthetix';
54
+ import { Usdfi } from './solidly/forks-override/usdfi';
55
+ import { Equalizer } from './solidly/forks-override/equalizer';
56
+ import { Velocimeter } from './solidly/forks-override/velocimeter';
57
+ import { BalancerV1 } from './balancer-v1/balancer-v1';
58
+ import { balancerV1Merge } from './balancer-v1/optimizer';
59
+ import { CurveV1 } from './curve-v1/curve-v1';
60
+ import { CurveFork } from './curve-v1/forks/curve-forks/curve-forks';
61
+ import { Swerve } from './curve-v1/forks/swerve/swerve';
62
+ import { CurveV1Factory } from './curve-v1-factory/curve-v1-factory';
63
+ import { GenericRFQ } from './generic-rfq/generic-rfq';
64
+ import { WstETH } from './wsteth/wsteth';
65
+ import { Camelot } from './camelot/camelot';
66
+ import { Hashflow } from './hashflow/hashflow';
67
+ import { SolidlyEthereum } from './solidly/solidly-ethereum';
68
+ import { MaverickV1 } from './maverick-v1/maverick-v1';
69
+ import { QuickSwapV3 } from './quickswap/quickswap-v3';
70
+ import { ThenaFusion } from './quickswap/thena-fusion';
71
+ import { TraderJoeV2 } from './trader-joe-v2';
72
+ import { SwaapV2 } from './swaap-v2/swaap-v2';
73
+ import { TraderJoeV21 } from './trader-joe-v2.1/trader-joe-v2.1';
74
+ import { PancakeswapV3 } from './pancakeswap-v3/pancakeswap-v3';
75
+ import { Algebra } from './algebra/algebra';
76
+ import { AngleStakedStable } from './angle-staked-stable/angle-staked-stable';
77
+ import { QuickPerps } from './quick-perps/quick-perps';
78
+ import { NomiswapV2 } from './uniswap-v2/nomiswap-v2';
79
+ import { Dexalot } from './dexalot/dexalot';
80
+ import { Wombat } from './wombat/wombat';
81
+ import { Swell } from './swell/swell';
82
+ import { PharaohV1 } from './solidly/forks-override/pharaohV1';
83
+ import { EtherFi } from './etherfi';
84
+ import { Spark } from './spark/spark';
85
+ import { VelodromeSlipstream } from './uniswap-v3/forks/velodrome-slipstream/velodrome-slipstream';
86
+
87
+ const LegacyDexes = [
88
+ CurveV2,
89
+ StablePool,
90
+ Smoothy,
91
+ ZeroX,
92
+ Bancor,
93
+ BProtocol,
94
+ MStable,
95
+ Shell,
96
+ Onebit,
97
+ Compound,
98
+ OneInchLp,
99
+ DodoV1,
100
+ DodoV2,
101
+ QuickSwapV3,
102
+ ThenaFusion,
103
+ TraderJoeV2,
104
+ TraderJoeV21,
105
+ Jarvis,
106
+ Lido,
107
+ AugustusRFQOrder,
108
+ EtherFi,
109
+ ];
110
+
111
+ const Dexes = [
112
+ Dexalot,
113
+ CurveV1,
114
+ CurveFork,
115
+ Swerve,
116
+ BalancerV1,
117
+ BalancerV2,
118
+ UniswapV2,
119
+ UniswapV3,
120
+ Algebra,
121
+ PancakeswapV3,
122
+ VelodromeSlipstream,
123
+ BiSwap,
124
+ MDEX,
125
+ Dfyn,
126
+ Excalibur,
127
+ AaveV2,
128
+ AaveV3,
129
+ KyberDmm,
130
+ Weth,
131
+ PolygonMigrator,
132
+ MakerPsm,
133
+ Nerve,
134
+ GMX,
135
+ JarvisV6,
136
+ WooFiV2,
137
+ ParaSwapLimitOrders,
138
+ Solidly,
139
+ SolidlyEthereum,
140
+ SpiritSwapV2,
141
+ Ramses,
142
+ Thena,
143
+ Chronos,
144
+ Velodrome,
145
+ VelodromeV2,
146
+ Aerodrome,
147
+ Equalizer,
148
+ Velocimeter,
149
+ Usdfi,
150
+ Synthetix,
151
+ CurveV1Factory,
152
+ WstETH,
153
+ Hashflow,
154
+ MaverickV1,
155
+ Camelot,
156
+ SwaapV2,
157
+ AngleStakedStable,
158
+ QuickPerps,
159
+ NomiswapV2,
160
+ SolidlyV3,
161
+ Wombat,
162
+ Swell,
163
+ PharaohV1,
164
+ Spark,
165
+ ];
166
+
167
+ export type LegacyDexConstructor = new (dexHelper: IDexHelper) => IDexTxBuilder<
168
+ any,
169
+ any
170
+ >;
171
+
172
+ interface IGetDirectFunctionName {
173
+ getDirectFunctionName?(): string[];
174
+ getDirectFunctionNameV6?(): string[];
175
+ }
176
+
177
+ export class DexAdapterService {
178
+ dexToKeyMap: {
179
+ [key: string]: LegacyDexConstructor | DexConstructor<any, any, any>;
180
+ } = {};
181
+ directFunctionsNames: string[];
182
+ directFunctionsNamesV6: string[];
183
+ dexInstances: {
184
+ [key: string]: IDexTxBuilder<any, any> | IDex<any, any, any>;
185
+ } = {};
186
+ isLegacy: { [dexKey: string]: boolean } = {};
187
+ // dexKeys only has keys for non legacy dexes
188
+ dexKeys: string[] = [];
189
+ genericRFQDexKeys: Set<string> = new Set();
190
+ uniswapV2Alias: string | null;
191
+
192
+ public routeOptimizers: IRouteOptimizer<UnoptimizedRate>[] = [
193
+ balancerV1Merge,
194
+ balancerV2Merge,
195
+ uniswapMerge,
196
+ ];
197
+
198
+ constructor(
199
+ public dexHelper: IDexHelper,
200
+ public network: number,
201
+ protected sellAdapters: Adapters = {},
202
+ protected buyAdapters: Adapters = {},
203
+ ) {
204
+ LegacyDexes.forEach(DexAdapter => {
205
+ DexAdapter.dexKeys.forEach(key => {
206
+ this.dexToKeyMap[key.toLowerCase()] = DexAdapter;
207
+ this.isLegacy[key.toLowerCase()] = true;
208
+ });
209
+ });
210
+
211
+ const handleDex = (newDex: IDex<any, any, any>, key: string) => {
212
+ const _key = key.toLowerCase();
213
+ this.isLegacy[_key] = false;
214
+ this.dexKeys.push(key);
215
+ this.dexInstances[_key] = newDex;
216
+
217
+ const sellAdaptersDex = (
218
+ this.dexInstances[_key] as IDex<any, any, any>
219
+ ).getAdapters(SwapSide.SELL);
220
+ if (sellAdaptersDex)
221
+ this.sellAdapters[_key] = sellAdaptersDex.map(({ name, index }) => ({
222
+ adapter: this.dexHelper.config.data.adapterAddresses[name],
223
+ index,
224
+ }));
225
+
226
+ const buyAdaptersDex = (
227
+ this.dexInstances[_key] as IDex<any, any, any>
228
+ ).getAdapters(SwapSide.BUY);
229
+ if (buyAdaptersDex)
230
+ this.buyAdapters[_key] = buyAdaptersDex.map(({ name, index }) => ({
231
+ adapter: this.dexHelper.config.data.adapterAddresses[name],
232
+ index,
233
+ }));
234
+ };
235
+
236
+ Dexes.forEach(DexAdapter => {
237
+ DexAdapter.dexKeysWithNetwork.forEach(({ key, networks }) => {
238
+ if (networks.includes(network)) {
239
+ const dex = new DexAdapter(network, key, dexHelper);
240
+ handleDex(dex, key);
241
+ }
242
+ });
243
+ });
244
+
245
+ const rfqConfigs = dexHelper.config.data.rfqConfigs;
246
+ Object.keys(dexHelper.config.data.rfqConfigs).forEach(rfqName => {
247
+ const dex = new GenericRFQ(
248
+ network,
249
+ rfqName,
250
+ dexHelper,
251
+ rfqConfigs[rfqName],
252
+ );
253
+ handleDex(dex, rfqName);
254
+ this.genericRFQDexKeys.add(rfqName.toLowerCase());
255
+ });
256
+
257
+ this.directFunctionsNames = [...LegacyDexes, ...Dexes]
258
+ .flatMap(dexAdapter => {
259
+ const _dexAdapter = dexAdapter as IGetDirectFunctionName;
260
+ return _dexAdapter.getDirectFunctionName
261
+ ? _dexAdapter.getDirectFunctionName()
262
+ : [];
263
+ })
264
+ .filter(x => !!x)
265
+ .map(v => v.toLowerCase());
266
+
267
+ // include GenericRFQ, because it has direct method for v6
268
+ this.directFunctionsNamesV6 = [...LegacyDexes, ...Dexes, GenericRFQ]
269
+ .flatMap(dexAdapter => {
270
+ const _dexAdapter = dexAdapter as IGetDirectFunctionName;
271
+ return _dexAdapter.getDirectFunctionNameV6
272
+ ? _dexAdapter.getDirectFunctionNameV6()
273
+ : [];
274
+ })
275
+ .filter(x => !!x)
276
+ .map(v => v.toLowerCase());
277
+
278
+ this.uniswapV2Alias =
279
+ this.network in UniswapV2Alias
280
+ ? UniswapV2Alias[this.network].toLowerCase()
281
+ : null;
282
+ }
283
+
284
+ getTxBuilderDexByKey(dexKey: string): IDexTxBuilder<any, any> {
285
+ let _dexKey = this.getDexKeySpecial(dexKey);
286
+
287
+ if (!this.dexInstances[_dexKey]) {
288
+ const DexAdapter = this.dexToKeyMap[_dexKey];
289
+ if (!DexAdapter)
290
+ throw new Error(
291
+ `${dexKey} dex is not supported for network(${this.network})!`,
292
+ );
293
+
294
+ this.dexInstances[_dexKey] = new (DexAdapter as LegacyDexConstructor)(
295
+ this.dexHelper,
296
+ );
297
+ }
298
+
299
+ return this.dexInstances[_dexKey];
300
+ }
301
+
302
+ isDirectFunctionName(functionName: string): boolean {
303
+ return this.directFunctionsNames.includes(functionName.toLowerCase());
304
+ }
305
+
306
+ isDirectFunctionNameV6(functionName: string): boolean {
307
+ return this.directFunctionsNamesV6.includes(functionName.toLowerCase());
308
+ }
309
+
310
+ getAllDexKeys() {
311
+ return _.uniq(this.dexKeys);
312
+ }
313
+
314
+ getDexByKey(key: string): IDex<any, any, any> {
315
+ const _key = key.toLowerCase();
316
+ if (!(_key in this.isLegacy) || this.isLegacy[_key])
317
+ throw new Error(`Invalid Dex Key ${key}`);
318
+
319
+ return this.dexInstances[_key] as IDex<any, any, any>;
320
+ }
321
+
322
+ getAllDexAdapters(side: SwapSide = SwapSide.SELL) {
323
+ return side === SwapSide.SELL ? this.sellAdapters : this.buyAdapters;
324
+ }
325
+
326
+ getDexKeySpecial(dexKey: string, isAdapters: boolean = false) {
327
+ dexKey = dexKey.toLowerCase();
328
+ if (this.genericRFQDexKeys.has(dexKey)) {
329
+ return dexKey;
330
+ }
331
+ if (!isAdapters && /^paraswappool(.*)/i.test(dexKey)) return 'zerox';
332
+ else if ('uniswapforkoptimized' === dexKey) {
333
+ if (!this.uniswapV2Alias)
334
+ throw new Error(
335
+ `${dexKey} dex is not supported for network(${this.network})!`,
336
+ );
337
+ return this.uniswapV2Alias;
338
+ }
339
+ return dexKey;
340
+ }
341
+
342
+ getAdapter(dexKey: string, side: SwapSide) {
343
+ const specialDexKey = this.getDexKeySpecial(dexKey, true);
344
+ return side === SwapSide.SELL
345
+ ? this.sellAdapters[specialDexKey]
346
+ : this.buyAdapters[specialDexKey];
347
+ }
348
+
349
+ doesPreProcessingRequireSequentiality(dexKey: string): boolean {
350
+ try {
351
+ const dex = this.getDexByKey(dexKey);
352
+ return !!dex.needsSequentialPreprocessing;
353
+ } catch (e) {
354
+ return false;
355
+ }
356
+ }
357
+ }