@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
@@ -1,409 +1,409 @@
1
- import { Interface } from '@ethersproject/abi';
2
- import {
3
- Token,
4
- Address,
5
- ExchangePrices,
6
- PoolPrices,
7
- AdapterExchangeParam,
8
- SimpleExchangeParam,
9
- PoolLiquidity,
10
- Logger,
11
- NumberAsString,
12
- DexExchangeParam,
13
- } from '../../types';
14
- import { SwapSide, Network, NULL_ADDRESS } from '../../constants';
15
- import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
16
- import { isETHAddress, getDexKeysWithNetwork, getBigIntPow } from '../../utils';
17
- import { AaveV2Data, AaveV2Param, AaveV2PoolAndWethFunctions } from './types';
18
-
19
- import WETH_GATEWAY_ABI_MAINNET from '../../abi/aave-weth-gateway.json';
20
- import WETH_GATEWAY_ABI_POLYGON from '../../abi/aave-weth-gateway-polygon.json';
21
- import WETH_GATEWAY_ABI_AVALANCHE from '../../abi/aave-weth-gateway-avalanche.json';
22
- import AAVE_LENDING_POOL_ABI_V2 from '../../abi/AaveV2_lending_pool.json';
23
-
24
- import { IDex } from '../../dex/idex';
25
- import { IDexHelper } from '../../dex-helper/idex-helper';
26
-
27
- import { SimpleExchange } from '../simple-exchange';
28
- import {
29
- AaveV2Config,
30
- Adapters,
31
- WETH_GATEWAY,
32
- aaveLendingPool,
33
- } from './config';
34
- import { isAaveV2Pair } from './tokens';
35
-
36
- const WETH_GATEWAY_ABI: any = {
37
- [Network.MAINNET]: WETH_GATEWAY_ABI_MAINNET,
38
- [Network.POLYGON]: WETH_GATEWAY_ABI_POLYGON,
39
- [Network.AVALANCHE]: WETH_GATEWAY_ABI_AVALANCHE,
40
- };
41
-
42
- const REF_CODE = 1;
43
-
44
- const Aave2ETHGasCost = 246 * 100;
45
- const Aave2LendingGasCost = 328 * 1000;
46
-
47
- export class AaveV2
48
- extends SimpleExchange
49
- implements IDex<AaveV2Data, AaveV2Param>
50
- {
51
- readonly hasConstantPriceLargeAmounts = true;
52
- readonly isFeeOnTransferSupported = false;
53
-
54
- public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
55
- getDexKeysWithNetwork(AaveV2Config);
56
-
57
- logger: Logger;
58
-
59
- private aavePool: Interface;
60
- private wethGateway: Interface;
61
- constructor(
62
- protected network: Network,
63
- dexKey: string,
64
- protected dexHelper: IDexHelper,
65
- ) {
66
- super(dexHelper, dexKey);
67
- this.logger = dexHelper.getLogger(dexKey);
68
- this.wethGateway = new Interface(WETH_GATEWAY_ABI[network]);
69
- this.aavePool = new Interface(AAVE_LENDING_POOL_ABI_V2);
70
- }
71
-
72
- // Initialize pricing is called once in the start of
73
- // pricing service. It is intended to setup the integration
74
- // for pricing requests. It is optional for a DEX to
75
- async initializePricing(blockNumber: number) {}
76
-
77
- // Returns the list of contract adapters (name and index)
78
- // for a buy/sell. Return null if there are no adapters.
79
- getAdapters(side: SwapSide): { name: string; index: number }[] | null {
80
- return Adapters[this.network][side];
81
- }
82
-
83
- // Returns list of pool identifiers that can be used
84
- // for a given swap. poolIdentifiers must be unique
85
- // across DEXes. It is recommended to use
86
- // ${dexKey}_${poolAddress} as a poolIdentifier
87
- async getPoolIdentifiers(
88
- srcToken: Token,
89
- destToken: Token,
90
- side: SwapSide,
91
- blockNumber: number,
92
- ): Promise<string[]> {
93
- const aToken = isAaveV2Pair(
94
- this.network,
95
- this.dexHelper.config.wrapETH(srcToken),
96
- this.dexHelper.config.wrapETH(destToken),
97
- );
98
- if (aToken === null) {
99
- return [];
100
- }
101
- const tokenAddress = [
102
- srcToken.address.toLowerCase(),
103
- destToken.address.toLowerCase(),
104
- ]
105
- .sort((a, b) => (a > b ? 1 : -1))
106
- .join('_');
107
- return [`${this.dexKey}_${tokenAddress}`];
108
- }
109
-
110
- // Returns pool prices for amounts.
111
- // If limitPools is defined only pools in limitPools
112
- // should be used. If limitPools is undefined then
113
- // any pools can be used.
114
- async getPricesVolume(
115
- srcToken: Token,
116
- destToken: Token,
117
- amounts: bigint[],
118
- side: SwapSide,
119
- blockNumber: number,
120
- limitPools?: string[],
121
- ): Promise<null | ExchangePrices<AaveV2Data>> {
122
- const _src = this.dexHelper.config.wrapETH(srcToken);
123
- const _dst = this.dexHelper.config.wrapETH(destToken);
124
- const aToken = isAaveV2Pair(this.network, _src, _dst);
125
- if (!aToken) {
126
- return null;
127
- }
128
- const fromAToken = aToken == _src;
129
- return [
130
- {
131
- prices: amounts,
132
- unit: getBigIntPow(
133
- (side === SwapSide.SELL ? destToken : srcToken).decimals,
134
- ),
135
- gasCost: isETHAddress(srcToken.address)
136
- ? Aave2ETHGasCost
137
- : Aave2LendingGasCost,
138
- exchange: this.dexKey,
139
- data: {
140
- isV2: true,
141
- fromAToken,
142
- },
143
- poolAddresses: [fromAToken ? srcToken.address : destToken.address],
144
- },
145
- ];
146
- }
147
-
148
- // Returns estimated gas cost of calldata for this DEX in multiSwap
149
- getCalldataGasCost(poolPrices: PoolPrices<AaveV2Data>): number | number[] {
150
- return (
151
- CALLDATA_GAS_COST.DEX_OVERHEAD +
152
- CALLDATA_GAS_COST.LENGTH_SMALL +
153
- CALLDATA_GAS_COST.ADDRESS
154
- );
155
- }
156
-
157
- // Encode params required by the exchange adapter
158
- // Used for multiSwap, buy & megaSwap
159
- getAdapterParam(
160
- srcToken: string,
161
- destToken: string,
162
- srcAmount: string,
163
- destAmount: string,
164
- data: AaveV2Data,
165
- side: SwapSide,
166
- ): AdapterExchangeParam {
167
- const aToken = data.fromAToken ? srcToken : destToken; // Warning
168
- const payload = this.abiCoder.encodeParameter(
169
- {
170
- ParentStruct: {
171
- aToken: 'address',
172
- },
173
- },
174
- { aToken: aToken },
175
- );
176
-
177
- return {
178
- // target exchange is not used by the contract
179
- targetExchange: NULL_ADDRESS,
180
- payload,
181
- networkFee: '0',
182
- };
183
- }
184
-
185
- // Encode call data used by simpleSwap like routers
186
- // Used for simpleSwap & simpleBuy
187
- async getSimpleParam(
188
- srcToken: string,
189
- destToken: string,
190
- srcAmount: string,
191
- destAmount: string,
192
- data: AaveV2Data,
193
- side: SwapSide,
194
- ): Promise<SimpleExchangeParam> {
195
- const amount = side === SwapSide.SELL ? srcAmount : destAmount;
196
- const [Interface, swapCallee, swapFunction, swapFunctionParams] = ((): [
197
- Interface,
198
- Address,
199
- AaveV2PoolAndWethFunctions,
200
- AaveV2Param,
201
- ] => {
202
- if (isETHAddress(srcToken)) {
203
- switch (this.network) {
204
- case 1:
205
- return [
206
- this.wethGateway,
207
- WETH_GATEWAY[this.network],
208
- AaveV2PoolAndWethFunctions.depositETH,
209
- [this.augustusAddress, REF_CODE],
210
- ];
211
- case 137:
212
- return [
213
- this.wethGateway,
214
- WETH_GATEWAY[this.network],
215
- AaveV2PoolAndWethFunctions.depositETH,
216
- [aaveLendingPool[this.network], this.augustusAddress, REF_CODE],
217
- ];
218
- case 43114:
219
- return [
220
- this.wethGateway,
221
- WETH_GATEWAY[this.network],
222
- AaveV2PoolAndWethFunctions.depositETH,
223
- [aaveLendingPool[this.network], this.augustusAddress, REF_CODE],
224
- ];
225
- default:
226
- throw new Error(`Network ${this.network} not supported`);
227
- }
228
- }
229
-
230
- if (isETHAddress(destToken)) {
231
- switch (this.network) {
232
- case Network.MAINNET:
233
- return [
234
- this.wethGateway,
235
- WETH_GATEWAY[this.network],
236
- AaveV2PoolAndWethFunctions.withdrawETH,
237
- [amount, this.augustusAddress],
238
- ];
239
- case Network.POLYGON:
240
- return [
241
- this.wethGateway,
242
- WETH_GATEWAY[this.network],
243
- AaveV2PoolAndWethFunctions.withdrawETH,
244
- [aaveLendingPool[this.network], amount, this.augustusAddress],
245
- ];
246
- case Network.AVALANCHE:
247
- return [
248
- this.wethGateway,
249
- WETH_GATEWAY[this.network],
250
- AaveV2PoolAndWethFunctions.withdrawETH,
251
- [aaveLendingPool[this.network], amount, this.augustusAddress],
252
- ];
253
- default:
254
- throw new Error(`Network ${this.network} not supported`);
255
- }
256
- }
257
-
258
- if (data.fromAToken) {
259
- return [
260
- this.aavePool,
261
- aaveLendingPool[this.network],
262
- AaveV2PoolAndWethFunctions.withdraw,
263
- [destToken, amount, this.augustusAddress],
264
- ];
265
- }
266
-
267
- return [
268
- this.aavePool,
269
- aaveLendingPool[this.network],
270
- AaveV2PoolAndWethFunctions.deposit,
271
- [srcToken, amount, this.augustusAddress, REF_CODE],
272
- ];
273
- })();
274
-
275
- const swapData = Interface.encodeFunctionData(
276
- swapFunction,
277
- swapFunctionParams,
278
- );
279
-
280
- return this.buildSimpleParamWithoutWETHConversion(
281
- srcToken,
282
- amount,
283
- destToken,
284
- destAmount,
285
- swapData,
286
- swapCallee,
287
- );
288
- }
289
-
290
- getDexParam(
291
- srcToken: Address,
292
- destToken: Address,
293
- srcAmount: NumberAsString,
294
- destAmount: NumberAsString,
295
- recipient: Address,
296
- data: AaveV2Data,
297
- side: SwapSide,
298
- ): DexExchangeParam {
299
- const amount = side === SwapSide.SELL ? srcAmount : destAmount;
300
- const [Interface, swapCallee, swapFunction, swapFunctionParams] = ((): [
301
- Interface,
302
- Address,
303
- AaveV2PoolAndWethFunctions,
304
- AaveV2Param,
305
- ] => {
306
- if (isETHAddress(srcToken)) {
307
- switch (this.network) {
308
- case Network.MAINNET:
309
- return [
310
- this.wethGateway,
311
- WETH_GATEWAY[this.network],
312
- AaveV2PoolAndWethFunctions.depositETH,
313
- [recipient, REF_CODE],
314
- ];
315
- case Network.POLYGON:
316
- return [
317
- this.wethGateway,
318
- WETH_GATEWAY[this.network],
319
- AaveV2PoolAndWethFunctions.depositETH,
320
- [aaveLendingPool[this.network], recipient, REF_CODE],
321
- ];
322
- case Network.AVALANCHE:
323
- return [
324
- this.wethGateway,
325
- WETH_GATEWAY[this.network],
326
- AaveV2PoolAndWethFunctions.depositETH,
327
- [aaveLendingPool[this.network], recipient, REF_CODE],
328
- ];
329
- default:
330
- throw new Error(`Network ${this.network} not supported`);
331
- }
332
- }
333
-
334
- if (isETHAddress(destToken)) {
335
- switch (this.network) {
336
- case Network.MAINNET:
337
- return [
338
- this.wethGateway,
339
- WETH_GATEWAY[this.network],
340
- AaveV2PoolAndWethFunctions.withdrawETH,
341
- [amount, recipient],
342
- ];
343
- case Network.POLYGON:
344
- return [
345
- this.wethGateway,
346
- WETH_GATEWAY[this.network],
347
- AaveV2PoolAndWethFunctions.withdrawETH,
348
- [aaveLendingPool[this.network], amount, recipient],
349
- ];
350
- case Network.AVALANCHE:
351
- return [
352
- this.wethGateway,
353
- WETH_GATEWAY[this.network],
354
- AaveV2PoolAndWethFunctions.withdrawETH,
355
- [aaveLendingPool[this.network], amount, recipient],
356
- ];
357
- default:
358
- throw new Error(`Network ${this.network} not supported`);
359
- }
360
- }
361
-
362
- if (data.fromAToken) {
363
- return [
364
- this.aavePool,
365
- aaveLendingPool[this.network],
366
- AaveV2PoolAndWethFunctions.withdraw,
367
- [destToken, amount, recipient],
368
- ];
369
- }
370
-
371
- return [
372
- this.aavePool,
373
- aaveLendingPool[this.network],
374
- AaveV2PoolAndWethFunctions.deposit,
375
- [srcToken, amount, recipient, REF_CODE],
376
- ];
377
- })();
378
-
379
- const exchangeData = Interface.encodeFunctionData(
380
- swapFunction,
381
- swapFunctionParams,
382
- );
383
-
384
- return {
385
- needWrapNative: this.needWrapNative,
386
- dexFuncHasRecipient: true,
387
- exchangeData,
388
- targetExchange: swapCallee,
389
- returnAmountPos: undefined,
390
- };
391
- }
392
-
393
- // This is called once before getTopPoolsForToken is
394
- // called for multiple tokens. This can be helpful to
395
- // update common state required for calculating
396
- // getTopPoolsForToken. It is optional for a DEX
397
- updatePoolState(): Promise<void> {
398
- return Promise.resolve();
399
- }
400
-
401
- // Returns list of top pools based on liquidity. Max
402
- // limit number pools should be returned.
403
- async getTopPoolsForToken(
404
- tokenAddress: Address,
405
- limit: number,
406
- ): Promise<PoolLiquidity[]> {
407
- return [];
408
- }
409
- }
1
+ import { Interface } from '@ethersproject/abi';
2
+ import {
3
+ Token,
4
+ Address,
5
+ ExchangePrices,
6
+ PoolPrices,
7
+ AdapterExchangeParam,
8
+ SimpleExchangeParam,
9
+ PoolLiquidity,
10
+ Logger,
11
+ NumberAsString,
12
+ DexExchangeParam,
13
+ } from '../../types';
14
+ import { SwapSide, Network, NULL_ADDRESS } from '../../constants';
15
+ import * as CALLDATA_GAS_COST from '../../calldata-gas-cost';
16
+ import { isETHAddress, getDexKeysWithNetwork, getBigIntPow } from '../../utils';
17
+ import { AaveV2Data, AaveV2Param, AaveV2PoolAndWethFunctions } from './types';
18
+
19
+ import WETH_GATEWAY_ABI_MAINNET from '../../abi/aave-weth-gateway.json';
20
+ import WETH_GATEWAY_ABI_POLYGON from '../../abi/aave-weth-gateway-polygon.json';
21
+ import WETH_GATEWAY_ABI_AVALANCHE from '../../abi/aave-weth-gateway-avalanche.json';
22
+ import AAVE_LENDING_POOL_ABI_V2 from '../../abi/AaveV2_lending_pool.json';
23
+
24
+ import { IDex } from '../../dex/idex';
25
+ import { IDexHelper } from '../../dex-helper/idex-helper';
26
+
27
+ import { SimpleExchange } from '../simple-exchange';
28
+ import {
29
+ AaveV2Config,
30
+ Adapters,
31
+ WETH_GATEWAY,
32
+ aaveLendingPool,
33
+ } from './config';
34
+ import { isAaveV2Pair } from './tokens';
35
+
36
+ const WETH_GATEWAY_ABI: any = {
37
+ [Network.MAINNET]: WETH_GATEWAY_ABI_MAINNET,
38
+ [Network.POLYGON]: WETH_GATEWAY_ABI_POLYGON,
39
+ [Network.AVALANCHE]: WETH_GATEWAY_ABI_AVALANCHE,
40
+ };
41
+
42
+ const REF_CODE = 1;
43
+
44
+ const Aave2ETHGasCost = 246 * 100;
45
+ const Aave2LendingGasCost = 328 * 1000;
46
+
47
+ export class AaveV2
48
+ extends SimpleExchange
49
+ implements IDex<AaveV2Data, AaveV2Param>
50
+ {
51
+ readonly hasConstantPriceLargeAmounts = true;
52
+ readonly isFeeOnTransferSupported = false;
53
+
54
+ public static dexKeysWithNetwork: { key: string; networks: Network[] }[] =
55
+ getDexKeysWithNetwork(AaveV2Config);
56
+
57
+ logger: Logger;
58
+
59
+ private aavePool: Interface;
60
+ private wethGateway: Interface;
61
+ constructor(
62
+ protected network: Network,
63
+ dexKey: string,
64
+ protected dexHelper: IDexHelper,
65
+ ) {
66
+ super(dexHelper, dexKey);
67
+ this.logger = dexHelper.getLogger(dexKey);
68
+ this.wethGateway = new Interface(WETH_GATEWAY_ABI[network]);
69
+ this.aavePool = new Interface(AAVE_LENDING_POOL_ABI_V2);
70
+ }
71
+
72
+ // Initialize pricing is called once in the start of
73
+ // pricing service. It is intended to setup the integration
74
+ // for pricing requests. It is optional for a DEX to
75
+ async initializePricing(blockNumber: number) {}
76
+
77
+ // Returns the list of contract adapters (name and index)
78
+ // for a buy/sell. Return null if there are no adapters.
79
+ getAdapters(side: SwapSide): { name: string; index: number }[] | null {
80
+ return Adapters[this.network][side];
81
+ }
82
+
83
+ // Returns list of pool identifiers that can be used
84
+ // for a given swap. poolIdentifiers must be unique
85
+ // across DEXes. It is recommended to use
86
+ // ${dexKey}_${poolAddress} as a poolIdentifier
87
+ async getPoolIdentifiers(
88
+ srcToken: Token,
89
+ destToken: Token,
90
+ side: SwapSide,
91
+ blockNumber: number,
92
+ ): Promise<string[]> {
93
+ const aToken = isAaveV2Pair(
94
+ this.network,
95
+ this.dexHelper.config.wrapETH(srcToken),
96
+ this.dexHelper.config.wrapETH(destToken),
97
+ );
98
+ if (aToken === null) {
99
+ return [];
100
+ }
101
+ const tokenAddress = [
102
+ srcToken.address.toLowerCase(),
103
+ destToken.address.toLowerCase(),
104
+ ]
105
+ .sort((a, b) => (a > b ? 1 : -1))
106
+ .join('_');
107
+ return [`${this.dexKey}_${tokenAddress}`];
108
+ }
109
+
110
+ // Returns pool prices for amounts.
111
+ // If limitPools is defined only pools in limitPools
112
+ // should be used. If limitPools is undefined then
113
+ // any pools can be used.
114
+ async getPricesVolume(
115
+ srcToken: Token,
116
+ destToken: Token,
117
+ amounts: bigint[],
118
+ side: SwapSide,
119
+ blockNumber: number,
120
+ limitPools?: string[],
121
+ ): Promise<null | ExchangePrices<AaveV2Data>> {
122
+ const _src = this.dexHelper.config.wrapETH(srcToken);
123
+ const _dst = this.dexHelper.config.wrapETH(destToken);
124
+ const aToken = isAaveV2Pair(this.network, _src, _dst);
125
+ if (!aToken) {
126
+ return null;
127
+ }
128
+ const fromAToken = aToken == _src;
129
+ return [
130
+ {
131
+ prices: amounts,
132
+ unit: getBigIntPow(
133
+ (side === SwapSide.SELL ? destToken : srcToken).decimals,
134
+ ),
135
+ gasCost: isETHAddress(srcToken.address)
136
+ ? Aave2ETHGasCost
137
+ : Aave2LendingGasCost,
138
+ exchange: this.dexKey,
139
+ data: {
140
+ isV2: true,
141
+ fromAToken,
142
+ },
143
+ poolAddresses: [fromAToken ? srcToken.address : destToken.address],
144
+ },
145
+ ];
146
+ }
147
+
148
+ // Returns estimated gas cost of calldata for this DEX in multiSwap
149
+ getCalldataGasCost(poolPrices: PoolPrices<AaveV2Data>): number | number[] {
150
+ return (
151
+ CALLDATA_GAS_COST.DEX_OVERHEAD +
152
+ CALLDATA_GAS_COST.LENGTH_SMALL +
153
+ CALLDATA_GAS_COST.ADDRESS
154
+ );
155
+ }
156
+
157
+ // Encode params required by the exchange adapter
158
+ // Used for multiSwap, buy & megaSwap
159
+ getAdapterParam(
160
+ srcToken: string,
161
+ destToken: string,
162
+ srcAmount: string,
163
+ destAmount: string,
164
+ data: AaveV2Data,
165
+ side: SwapSide,
166
+ ): AdapterExchangeParam {
167
+ const aToken = data.fromAToken ? srcToken : destToken; // Warning
168
+ const payload = this.abiCoder.encodeParameter(
169
+ {
170
+ ParentStruct: {
171
+ aToken: 'address',
172
+ },
173
+ },
174
+ { aToken: aToken },
175
+ );
176
+
177
+ return {
178
+ // target exchange is not used by the contract
179
+ targetExchange: NULL_ADDRESS,
180
+ payload,
181
+ networkFee: '0',
182
+ };
183
+ }
184
+
185
+ // Encode call data used by simpleSwap like routers
186
+ // Used for simpleSwap & simpleBuy
187
+ async getSimpleParam(
188
+ srcToken: string,
189
+ destToken: string,
190
+ srcAmount: string,
191
+ destAmount: string,
192
+ data: AaveV2Data,
193
+ side: SwapSide,
194
+ ): Promise<SimpleExchangeParam> {
195
+ const amount = side === SwapSide.SELL ? srcAmount : destAmount;
196
+ const [Interface, swapCallee, swapFunction, swapFunctionParams] = ((): [
197
+ Interface,
198
+ Address,
199
+ AaveV2PoolAndWethFunctions,
200
+ AaveV2Param,
201
+ ] => {
202
+ if (isETHAddress(srcToken)) {
203
+ switch (this.network) {
204
+ case 1:
205
+ return [
206
+ this.wethGateway,
207
+ WETH_GATEWAY[this.network],
208
+ AaveV2PoolAndWethFunctions.depositETH,
209
+ [this.augustusAddress, REF_CODE],
210
+ ];
211
+ case 137:
212
+ return [
213
+ this.wethGateway,
214
+ WETH_GATEWAY[this.network],
215
+ AaveV2PoolAndWethFunctions.depositETH,
216
+ [aaveLendingPool[this.network], this.augustusAddress, REF_CODE],
217
+ ];
218
+ case 43114:
219
+ return [
220
+ this.wethGateway,
221
+ WETH_GATEWAY[this.network],
222
+ AaveV2PoolAndWethFunctions.depositETH,
223
+ [aaveLendingPool[this.network], this.augustusAddress, REF_CODE],
224
+ ];
225
+ default:
226
+ throw new Error(`Network ${this.network} not supported`);
227
+ }
228
+ }
229
+
230
+ if (isETHAddress(destToken)) {
231
+ switch (this.network) {
232
+ case Network.MAINNET:
233
+ return [
234
+ this.wethGateway,
235
+ WETH_GATEWAY[this.network],
236
+ AaveV2PoolAndWethFunctions.withdrawETH,
237
+ [amount, this.augustusAddress],
238
+ ];
239
+ case Network.POLYGON:
240
+ return [
241
+ this.wethGateway,
242
+ WETH_GATEWAY[this.network],
243
+ AaveV2PoolAndWethFunctions.withdrawETH,
244
+ [aaveLendingPool[this.network], amount, this.augustusAddress],
245
+ ];
246
+ case Network.AVALANCHE:
247
+ return [
248
+ this.wethGateway,
249
+ WETH_GATEWAY[this.network],
250
+ AaveV2PoolAndWethFunctions.withdrawETH,
251
+ [aaveLendingPool[this.network], amount, this.augustusAddress],
252
+ ];
253
+ default:
254
+ throw new Error(`Network ${this.network} not supported`);
255
+ }
256
+ }
257
+
258
+ if (data.fromAToken) {
259
+ return [
260
+ this.aavePool,
261
+ aaveLendingPool[this.network],
262
+ AaveV2PoolAndWethFunctions.withdraw,
263
+ [destToken, amount, this.augustusAddress],
264
+ ];
265
+ }
266
+
267
+ return [
268
+ this.aavePool,
269
+ aaveLendingPool[this.network],
270
+ AaveV2PoolAndWethFunctions.deposit,
271
+ [srcToken, amount, this.augustusAddress, REF_CODE],
272
+ ];
273
+ })();
274
+
275
+ const swapData = Interface.encodeFunctionData(
276
+ swapFunction,
277
+ swapFunctionParams,
278
+ );
279
+
280
+ return this.buildSimpleParamWithoutWETHConversion(
281
+ srcToken,
282
+ amount,
283
+ destToken,
284
+ destAmount,
285
+ swapData,
286
+ swapCallee,
287
+ );
288
+ }
289
+
290
+ getDexParam(
291
+ srcToken: Address,
292
+ destToken: Address,
293
+ srcAmount: NumberAsString,
294
+ destAmount: NumberAsString,
295
+ recipient: Address,
296
+ data: AaveV2Data,
297
+ side: SwapSide,
298
+ ): DexExchangeParam {
299
+ const amount = side === SwapSide.SELL ? srcAmount : destAmount;
300
+ const [Interface, swapCallee, swapFunction, swapFunctionParams] = ((): [
301
+ Interface,
302
+ Address,
303
+ AaveV2PoolAndWethFunctions,
304
+ AaveV2Param,
305
+ ] => {
306
+ if (isETHAddress(srcToken)) {
307
+ switch (this.network) {
308
+ case Network.MAINNET:
309
+ return [
310
+ this.wethGateway,
311
+ WETH_GATEWAY[this.network],
312
+ AaveV2PoolAndWethFunctions.depositETH,
313
+ [recipient, REF_CODE],
314
+ ];
315
+ case Network.POLYGON:
316
+ return [
317
+ this.wethGateway,
318
+ WETH_GATEWAY[this.network],
319
+ AaveV2PoolAndWethFunctions.depositETH,
320
+ [aaveLendingPool[this.network], recipient, REF_CODE],
321
+ ];
322
+ case Network.AVALANCHE:
323
+ return [
324
+ this.wethGateway,
325
+ WETH_GATEWAY[this.network],
326
+ AaveV2PoolAndWethFunctions.depositETH,
327
+ [aaveLendingPool[this.network], recipient, REF_CODE],
328
+ ];
329
+ default:
330
+ throw new Error(`Network ${this.network} not supported`);
331
+ }
332
+ }
333
+
334
+ if (isETHAddress(destToken)) {
335
+ switch (this.network) {
336
+ case Network.MAINNET:
337
+ return [
338
+ this.wethGateway,
339
+ WETH_GATEWAY[this.network],
340
+ AaveV2PoolAndWethFunctions.withdrawETH,
341
+ [amount, recipient],
342
+ ];
343
+ case Network.POLYGON:
344
+ return [
345
+ this.wethGateway,
346
+ WETH_GATEWAY[this.network],
347
+ AaveV2PoolAndWethFunctions.withdrawETH,
348
+ [aaveLendingPool[this.network], amount, recipient],
349
+ ];
350
+ case Network.AVALANCHE:
351
+ return [
352
+ this.wethGateway,
353
+ WETH_GATEWAY[this.network],
354
+ AaveV2PoolAndWethFunctions.withdrawETH,
355
+ [aaveLendingPool[this.network], amount, recipient],
356
+ ];
357
+ default:
358
+ throw new Error(`Network ${this.network} not supported`);
359
+ }
360
+ }
361
+
362
+ if (data.fromAToken) {
363
+ return [
364
+ this.aavePool,
365
+ aaveLendingPool[this.network],
366
+ AaveV2PoolAndWethFunctions.withdraw,
367
+ [destToken, amount, recipient],
368
+ ];
369
+ }
370
+
371
+ return [
372
+ this.aavePool,
373
+ aaveLendingPool[this.network],
374
+ AaveV2PoolAndWethFunctions.deposit,
375
+ [srcToken, amount, recipient, REF_CODE],
376
+ ];
377
+ })();
378
+
379
+ const exchangeData = Interface.encodeFunctionData(
380
+ swapFunction,
381
+ swapFunctionParams,
382
+ );
383
+
384
+ return {
385
+ needWrapNative: this.needWrapNative,
386
+ dexFuncHasRecipient: true,
387
+ exchangeData,
388
+ targetExchange: swapCallee,
389
+ returnAmountPos: undefined,
390
+ };
391
+ }
392
+
393
+ // This is called once before getTopPoolsForToken is
394
+ // called for multiple tokens. This can be helpful to
395
+ // update common state required for calculating
396
+ // getTopPoolsForToken. It is optional for a DEX
397
+ updatePoolState(): Promise<void> {
398
+ return Promise.resolve();
399
+ }
400
+
401
+ // Returns list of top pools based on liquidity. Max
402
+ // limit number pools should be returned.
403
+ async getTopPoolsForToken(
404
+ tokenAddress: Address,
405
+ limit: number,
406
+ ): Promise<PoolLiquidity[]> {
407
+ return [];
408
+ }
409
+ }