@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,440 +1,440 @@
1
- import { Interface, JsonFragment } from '@ethersproject/abi';
2
- import { SwapSide } from '../../constants';
3
- import {
4
- AdapterExchangeParam,
5
- Address,
6
- DexExchangeParam,
7
- ExchangeTxInfo,
8
- NumberAsString,
9
- PreprocessTransactionOptions,
10
- SimpleExchangeParam,
11
- Token,
12
- TxInfo,
13
- } from '../../types';
14
- import { IDexTxBuilder } from '../idex';
15
- import {
16
- getLocalDeadlineAsFriendlyPlaceholder,
17
- SimpleExchange,
18
- } from '../simple-exchange';
19
- import GenericFactoryZapABI from '../../abi/curve-v2/GenericFactoryZap.json';
20
- import DirectSwapABI from '../../abi/DirectSwap.json';
21
- import CurveV2ABI from '../../abi/CurveV2.json';
22
- import { IDexHelper } from '../../dex-helper';
23
- import { assert } from 'ts-essentials';
24
- import { Logger } from 'log4js';
25
- import { OptimalSwapExchange } from '@paraswap/core';
26
- import { isETHAddress, uuidToBytes16 } from '../../utils';
27
- import { DIRECT_METHOD_NAME_V6 } from './constants';
28
- import {
29
- CurveV2DirectSwap,
30
- CurveV2DirectSwapParam,
31
- CurveV2SwapType,
32
- } from './types';
33
- import { packCurveData } from '../../lib/curve/encoder';
34
- import { hexConcat, hexZeroPad, hexlify } from 'ethers/lib/utils';
35
-
36
- const DIRECT_METHOD_NAME = 'directCurveV2Swap';
37
-
38
- type CurveV2Data = {
39
- i: number;
40
- j: number;
41
- exchange: string;
42
- originalPoolAddress: Address;
43
- swapType: CurveV2SwapType;
44
- isApproved?: boolean;
45
- };
46
-
47
- type CurveV2Param = [
48
- i: NumberAsString,
49
- j: NumberAsString,
50
- dx: NumberAsString,
51
- min_dy: NumberAsString,
52
- ethDeposit?: boolean,
53
- ];
54
-
55
- type CurveV2ParamsForGenericFactoryZap = [
56
- _pool: Address,
57
- i: NumberAsString,
58
- j: NumberAsString,
59
- dx: NumberAsString,
60
- min_dy: NumberAsString,
61
- ];
62
-
63
- export type DirectCurveV2Param = [
64
- fromToken: Address,
65
- toToken: Address,
66
- exchange: Address,
67
- poolAddress: Address,
68
- fromAmount: NumberAsString,
69
- toAmount: NumberAsString,
70
- expectedAmount: NumberAsString,
71
- feePercent: NumberAsString,
72
- i: NumberAsString,
73
- j: NumberAsString,
74
- partner: Address,
75
- isApproved: boolean,
76
- swapType: CurveV2SwapType,
77
- beneficiary: Address,
78
- needWrapNative: boolean,
79
- permit: string,
80
- uuid: string,
81
- ];
82
-
83
- enum CurveV2SwapFunctions {
84
- exchange = 'exchange(uint256 i, uint256 j, uint256 dx, uint256 minDy)',
85
- exchange_underlying = 'exchange_underlying(uint256 i, uint256 j, uint256 dx, uint256 minDy)',
86
- exchange_in_generic_factory_zap = 'exchange(address _pool, uint256 i, uint256 j, uint256 _dx, uint256 _min_dy)',
87
- }
88
-
89
- export class CurveV2
90
- extends SimpleExchange
91
- implements IDexTxBuilder<CurveV2Data, DirectCurveV2Param | CurveV2DirectSwap>
92
- {
93
- static dexKeys = ['curvev2'];
94
- exchangeRouterInterface: Interface;
95
- genericFactoryZapIface: Interface;
96
- minConversionRate = '1';
97
- // TODO: it's wrong, cause some pools can have eth and others can have weth
98
- needWrapNative = true;
99
- logger: Logger;
100
-
101
- readonly directSwapIface = new Interface(DirectSwapABI);
102
-
103
- constructor(readonly dexHelper: IDexHelper) {
104
- super(dexHelper, 'curvev2');
105
- this.exchangeRouterInterface = new Interface(CurveV2ABI as JsonFragment[]);
106
- this.genericFactoryZapIface = new Interface(GenericFactoryZapABI);
107
- this.logger = dexHelper.getLogger(
108
- `CurveV2_${dexHelper.config.data.network}`,
109
- );
110
- }
111
-
112
- getAdapterParam(
113
- _0: string,
114
- _1: string,
115
- _2: string,
116
- _3: string,
117
- data: CurveV2Data,
118
- side: SwapSide,
119
- ): AdapterExchangeParam {
120
- if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
121
-
122
- const { i, j, originalPoolAddress, swapType } = data;
123
- const payload = this.abiCoder.encodeParameter(
124
- {
125
- ParentStruct: {
126
- i: 'uint256',
127
- j: 'uint256',
128
- originalPoolAddress: 'address',
129
- swapType: 'uint8',
130
- },
131
- },
132
- {
133
- i,
134
- j,
135
- originalPoolAddress,
136
- swapType,
137
- },
138
- );
139
-
140
- return {
141
- targetExchange: data.exchange,
142
- payload,
143
- networkFee: '0',
144
- };
145
- }
146
-
147
- getTokenFromAddress(address: Address): Token {
148
- // In this Dex decimals are not used
149
- return { address, decimals: 0 };
150
- }
151
-
152
- async preProcessTransaction(
153
- optimalSwapExchange: OptimalSwapExchange<CurveV2Data>,
154
- srcToken: Token,
155
- _0: Token,
156
- _1: SwapSide,
157
- options: PreprocessTransactionOptions,
158
- ): Promise<[OptimalSwapExchange<CurveV2Data>, ExchangeTxInfo]> {
159
- if (!options.isDirectMethod) {
160
- return [
161
- optimalSwapExchange,
162
- {
163
- deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
164
- },
165
- ];
166
- }
167
-
168
- assert(
169
- optimalSwapExchange.data !== undefined,
170
- `preProcessTransaction: data field is missing`,
171
- );
172
-
173
- let isApproved: boolean | undefined;
174
-
175
- try {
176
- isApproved = await this.dexHelper.augustusApprovals.hasApproval(
177
- options.executionContractAddress,
178
- this.dexHelper.config.wrapETH(srcToken).address,
179
- optimalSwapExchange.data.exchange,
180
- );
181
- } catch (e) {
182
- this.logger.error(
183
- `preProcessTransaction failed to retrieve allowance info: `,
184
- e,
185
- );
186
- }
187
-
188
- return [
189
- {
190
- ...optimalSwapExchange,
191
- data: {
192
- ...optimalSwapExchange.data,
193
- isApproved,
194
- },
195
- },
196
- {
197
- deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
198
- },
199
- ];
200
- }
201
-
202
- getDirectParam(
203
- srcToken: Address,
204
- destToken: Address,
205
- srcAmount: NumberAsString,
206
- destAmount: NumberAsString,
207
- expectedAmount: NumberAsString,
208
- data: CurveV2Data,
209
- side: SwapSide,
210
- permit: string,
211
- uuid: string,
212
- feePercent: NumberAsString,
213
- _0: NumberAsString,
214
- partner: string,
215
- beneficiary: string,
216
- contractMethod?: string,
217
- ): TxInfo<DirectCurveV2Param> {
218
- if (contractMethod !== DIRECT_METHOD_NAME) {
219
- throw new Error(`Invalid contract method ${contractMethod}`);
220
- }
221
- assert(side === SwapSide.SELL, 'Buy not supported');
222
-
223
- let isApproved: boolean = !!data.isApproved;
224
- if (data.isApproved === undefined) {
225
- this.logger.warn(`isApproved is undefined, defaulting to false`);
226
- }
227
-
228
- const swapParams: DirectCurveV2Param = [
229
- srcToken,
230
- destToken,
231
- data.exchange,
232
- data.originalPoolAddress,
233
- srcAmount,
234
- destAmount,
235
- expectedAmount,
236
- feePercent,
237
- data.i.toString(),
238
- data.j.toString(),
239
- partner,
240
- isApproved,
241
- data.swapType,
242
- beneficiary,
243
- // For now we always wrap native. We don't support non native trade
244
- true,
245
- permit,
246
- uuidToBytes16(uuid),
247
- ];
248
-
249
- const encoder = (...params: DirectCurveV2Param) => {
250
- return this.directSwapIface.encodeFunctionData(DIRECT_METHOD_NAME, [
251
- params,
252
- ]);
253
- };
254
-
255
- return {
256
- params: swapParams,
257
- encoder,
258
- networkFee: '0',
259
- };
260
- }
261
-
262
- getDirectParamV6(
263
- srcToken: Address,
264
- destToken: Address,
265
- fromAmount: NumberAsString,
266
- toAmount: NumberAsString,
267
- quotedAmount: NumberAsString,
268
- data: CurveV2Data,
269
- side: SwapSide,
270
- permit: string,
271
- uuid: string,
272
- partnerAndFee: string,
273
- beneficiary: string,
274
- blockNumber: number,
275
- contractMethod?: string,
276
- ) {
277
- if (contractMethod !== DIRECT_METHOD_NAME_V6) {
278
- throw new Error(`Invalid contract method ${contractMethod}`);
279
- }
280
-
281
- assert(side === SwapSide.SELL, 'Buy not supported');
282
-
283
- const metadata = hexConcat([
284
- hexZeroPad(uuidToBytes16(uuid), 16),
285
- hexZeroPad(hexlify(blockNumber), 16),
286
- ]);
287
-
288
- const swapParams: CurveV2DirectSwapParam = [
289
- packCurveData(
290
- data.exchange,
291
- !data.isApproved, // approve flag, if not approved then set to true
292
- isETHAddress(destToken) ? 2 : isETHAddress(srcToken) ? 1 : 0,
293
- data.swapType,
294
- ).toString(),
295
- data.i,
296
- data.j,
297
- data.originalPoolAddress,
298
- srcToken,
299
- destToken,
300
- fromAmount,
301
- toAmount,
302
- quotedAmount,
303
- metadata,
304
- beneficiary,
305
- ];
306
-
307
- const encodeParams: CurveV2DirectSwap = [swapParams, partnerAndFee, permit];
308
-
309
- const encoder = (...params: CurveV2DirectSwap) => {
310
- return this.augustusV6Interface.encodeFunctionData(
311
- DIRECT_METHOD_NAME_V6,
312
- [...params],
313
- );
314
- };
315
-
316
- return {
317
- params: encodeParams,
318
- encoder,
319
- networkFee: '0',
320
- };
321
- }
322
-
323
- static getDirectFunctionNameV6(): string[] {
324
- return [DIRECT_METHOD_NAME_V6];
325
- }
326
-
327
- static getDirectFunctionName(): string[] {
328
- return [DIRECT_METHOD_NAME];
329
- }
330
-
331
- async getSimpleParam(
332
- srcToken: string,
333
- destToken: string,
334
- srcAmount: string,
335
- destAmount: string,
336
- data: CurveV2Data,
337
- side: SwapSide,
338
- ): Promise<SimpleExchangeParam> {
339
- if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
340
-
341
- const { exchange, i, j, originalPoolAddress, swapType } = data;
342
-
343
- const args: CurveV2Param | CurveV2ParamsForGenericFactoryZap =
344
- swapType === CurveV2SwapType.EXCHANGE_GENERIC_FACTORY_ZAP
345
- ? [
346
- originalPoolAddress,
347
- i.toString(),
348
- j.toString(),
349
- srcAmount,
350
- this.minConversionRate,
351
- ]
352
- : [i.toString(), j.toString(), srcAmount, this.minConversionRate];
353
-
354
- let swapMethod: string;
355
- switch (swapType) {
356
- case CurveV2SwapType.EXCHANGE:
357
- swapMethod = CurveV2SwapFunctions.exchange;
358
- break;
359
- case CurveV2SwapType.EXCHANGE_UNDERLYING:
360
- swapMethod = CurveV2SwapFunctions.exchange_underlying;
361
- break;
362
- case CurveV2SwapType.EXCHANGE_GENERIC_FACTORY_ZAP:
363
- swapMethod = CurveV2SwapFunctions.exchange_in_generic_factory_zap;
364
- break;
365
- default:
366
- throw new Error(
367
- `getSimpleParam: not all cases covered for CurveV2SwapTypes`,
368
- );
369
- }
370
-
371
- const swapData =
372
- swapMethod === CurveV2SwapFunctions.exchange_in_generic_factory_zap
373
- ? this.genericFactoryZapIface.encodeFunctionData(swapMethod, args)
374
- : this.exchangeRouterInterface.encodeFunctionData(swapMethod, args);
375
-
376
- return this.buildSimpleParamWithoutWETHConversion(
377
- srcToken,
378
- srcAmount,
379
- destToken,
380
- destAmount,
381
- swapData,
382
- exchange,
383
- );
384
- }
385
-
386
- getDexParam(
387
- srcToken: Address,
388
- destToken: Address,
389
- srcAmount: NumberAsString,
390
- destAmount: NumberAsString,
391
- recipient: Address,
392
- data: CurveV2Data,
393
- side: SwapSide,
394
- ): DexExchangeParam {
395
- if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
396
-
397
- const { exchange, i, j, originalPoolAddress, swapType } = data;
398
-
399
- const args: CurveV2Param | CurveV2ParamsForGenericFactoryZap =
400
- swapType === CurveV2SwapType.EXCHANGE_GENERIC_FACTORY_ZAP
401
- ? [
402
- originalPoolAddress,
403
- i.toString(),
404
- j.toString(),
405
- srcAmount,
406
- this.minConversionRate,
407
- ]
408
- : [i.toString(), j.toString(), srcAmount, this.minConversionRate];
409
-
410
- let swapMethod: string;
411
- switch (swapType) {
412
- case CurveV2SwapType.EXCHANGE:
413
- swapMethod = CurveV2SwapFunctions.exchange;
414
- break;
415
- case CurveV2SwapType.EXCHANGE_UNDERLYING:
416
- swapMethod = CurveV2SwapFunctions.exchange_underlying;
417
- break;
418
- case CurveV2SwapType.EXCHANGE_GENERIC_FACTORY_ZAP:
419
- swapMethod = CurveV2SwapFunctions.exchange_in_generic_factory_zap;
420
- break;
421
- default:
422
- throw new Error(
423
- `getSimpleParam: not all cases covered for CurveV2SwapTypes`,
424
- );
425
- }
426
-
427
- const swapData =
428
- swapMethod === CurveV2SwapFunctions.exchange_in_generic_factory_zap
429
- ? this.genericFactoryZapIface.encodeFunctionData(swapMethod, args)
430
- : this.exchangeRouterInterface.encodeFunctionData(swapMethod, args);
431
-
432
- return {
433
- needWrapNative: this.needWrapNative,
434
- dexFuncHasRecipient: false,
435
- exchangeData: swapData,
436
- targetExchange: exchange,
437
- returnAmountPos: undefined,
438
- };
439
- }
440
- }
1
+ import { Interface, JsonFragment } from '@ethersproject/abi';
2
+ import { SwapSide } from '../../constants';
3
+ import {
4
+ AdapterExchangeParam,
5
+ Address,
6
+ DexExchangeParam,
7
+ ExchangeTxInfo,
8
+ NumberAsString,
9
+ PreprocessTransactionOptions,
10
+ SimpleExchangeParam,
11
+ Token,
12
+ TxInfo,
13
+ } from '../../types';
14
+ import { IDexTxBuilder } from '../idex';
15
+ import {
16
+ getLocalDeadlineAsFriendlyPlaceholder,
17
+ SimpleExchange,
18
+ } from '../simple-exchange';
19
+ import GenericFactoryZapABI from '../../abi/curve-v2/GenericFactoryZap.json';
20
+ import DirectSwapABI from '../../abi/DirectSwap.json';
21
+ import CurveV2ABI from '../../abi/CurveV2.json';
22
+ import { IDexHelper } from '../../dex-helper';
23
+ import { assert } from 'ts-essentials';
24
+ import { Logger } from 'log4js';
25
+ import { OptimalSwapExchange } from '@paraswap/core';
26
+ import { isETHAddress, uuidToBytes16 } from '../../utils';
27
+ import { DIRECT_METHOD_NAME_V6 } from './constants';
28
+ import {
29
+ CurveV2DirectSwap,
30
+ CurveV2DirectSwapParam,
31
+ CurveV2SwapType,
32
+ } from './types';
33
+ import { packCurveData } from '../../lib/curve/encoder';
34
+ import { hexConcat, hexZeroPad, hexlify } from 'ethers/lib/utils';
35
+
36
+ const DIRECT_METHOD_NAME = 'directCurveV2Swap';
37
+
38
+ type CurveV2Data = {
39
+ i: number;
40
+ j: number;
41
+ exchange: string;
42
+ originalPoolAddress: Address;
43
+ swapType: CurveV2SwapType;
44
+ isApproved?: boolean;
45
+ };
46
+
47
+ type CurveV2Param = [
48
+ i: NumberAsString,
49
+ j: NumberAsString,
50
+ dx: NumberAsString,
51
+ min_dy: NumberAsString,
52
+ ethDeposit?: boolean,
53
+ ];
54
+
55
+ type CurveV2ParamsForGenericFactoryZap = [
56
+ _pool: Address,
57
+ i: NumberAsString,
58
+ j: NumberAsString,
59
+ dx: NumberAsString,
60
+ min_dy: NumberAsString,
61
+ ];
62
+
63
+ export type DirectCurveV2Param = [
64
+ fromToken: Address,
65
+ toToken: Address,
66
+ exchange: Address,
67
+ poolAddress: Address,
68
+ fromAmount: NumberAsString,
69
+ toAmount: NumberAsString,
70
+ expectedAmount: NumberAsString,
71
+ feePercent: NumberAsString,
72
+ i: NumberAsString,
73
+ j: NumberAsString,
74
+ partner: Address,
75
+ isApproved: boolean,
76
+ swapType: CurveV2SwapType,
77
+ beneficiary: Address,
78
+ needWrapNative: boolean,
79
+ permit: string,
80
+ uuid: string,
81
+ ];
82
+
83
+ enum CurveV2SwapFunctions {
84
+ exchange = 'exchange(uint256 i, uint256 j, uint256 dx, uint256 minDy)',
85
+ exchange_underlying = 'exchange_underlying(uint256 i, uint256 j, uint256 dx, uint256 minDy)',
86
+ exchange_in_generic_factory_zap = 'exchange(address _pool, uint256 i, uint256 j, uint256 _dx, uint256 _min_dy)',
87
+ }
88
+
89
+ export class CurveV2
90
+ extends SimpleExchange
91
+ implements IDexTxBuilder<CurveV2Data, DirectCurveV2Param | CurveV2DirectSwap>
92
+ {
93
+ static dexKeys = ['curvev2'];
94
+ exchangeRouterInterface: Interface;
95
+ genericFactoryZapIface: Interface;
96
+ minConversionRate = '1';
97
+ // TODO: it's wrong, cause some pools can have eth and others can have weth
98
+ needWrapNative = true;
99
+ logger: Logger;
100
+
101
+ readonly directSwapIface = new Interface(DirectSwapABI);
102
+
103
+ constructor(readonly dexHelper: IDexHelper) {
104
+ super(dexHelper, 'curvev2');
105
+ this.exchangeRouterInterface = new Interface(CurveV2ABI as JsonFragment[]);
106
+ this.genericFactoryZapIface = new Interface(GenericFactoryZapABI);
107
+ this.logger = dexHelper.getLogger(
108
+ `CurveV2_${dexHelper.config.data.network}`,
109
+ );
110
+ }
111
+
112
+ getAdapterParam(
113
+ _0: string,
114
+ _1: string,
115
+ _2: string,
116
+ _3: string,
117
+ data: CurveV2Data,
118
+ side: SwapSide,
119
+ ): AdapterExchangeParam {
120
+ if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
121
+
122
+ const { i, j, originalPoolAddress, swapType } = data;
123
+ const payload = this.abiCoder.encodeParameter(
124
+ {
125
+ ParentStruct: {
126
+ i: 'uint256',
127
+ j: 'uint256',
128
+ originalPoolAddress: 'address',
129
+ swapType: 'uint8',
130
+ },
131
+ },
132
+ {
133
+ i,
134
+ j,
135
+ originalPoolAddress,
136
+ swapType,
137
+ },
138
+ );
139
+
140
+ return {
141
+ targetExchange: data.exchange,
142
+ payload,
143
+ networkFee: '0',
144
+ };
145
+ }
146
+
147
+ getTokenFromAddress(address: Address): Token {
148
+ // In this Dex decimals are not used
149
+ return { address, decimals: 0 };
150
+ }
151
+
152
+ async preProcessTransaction(
153
+ optimalSwapExchange: OptimalSwapExchange<CurveV2Data>,
154
+ srcToken: Token,
155
+ _0: Token,
156
+ _1: SwapSide,
157
+ options: PreprocessTransactionOptions,
158
+ ): Promise<[OptimalSwapExchange<CurveV2Data>, ExchangeTxInfo]> {
159
+ if (!options.isDirectMethod) {
160
+ return [
161
+ optimalSwapExchange,
162
+ {
163
+ deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
164
+ },
165
+ ];
166
+ }
167
+
168
+ assert(
169
+ optimalSwapExchange.data !== undefined,
170
+ `preProcessTransaction: data field is missing`,
171
+ );
172
+
173
+ let isApproved: boolean | undefined;
174
+
175
+ try {
176
+ isApproved = await this.dexHelper.augustusApprovals.hasApproval(
177
+ options.executionContractAddress,
178
+ this.dexHelper.config.wrapETH(srcToken).address,
179
+ optimalSwapExchange.data.exchange,
180
+ );
181
+ } catch (e) {
182
+ this.logger.error(
183
+ `preProcessTransaction failed to retrieve allowance info: `,
184
+ e,
185
+ );
186
+ }
187
+
188
+ return [
189
+ {
190
+ ...optimalSwapExchange,
191
+ data: {
192
+ ...optimalSwapExchange.data,
193
+ isApproved,
194
+ },
195
+ },
196
+ {
197
+ deadline: BigInt(getLocalDeadlineAsFriendlyPlaceholder()),
198
+ },
199
+ ];
200
+ }
201
+
202
+ getDirectParam(
203
+ srcToken: Address,
204
+ destToken: Address,
205
+ srcAmount: NumberAsString,
206
+ destAmount: NumberAsString,
207
+ expectedAmount: NumberAsString,
208
+ data: CurveV2Data,
209
+ side: SwapSide,
210
+ permit: string,
211
+ uuid: string,
212
+ feePercent: NumberAsString,
213
+ _0: NumberAsString,
214
+ partner: string,
215
+ beneficiary: string,
216
+ contractMethod?: string,
217
+ ): TxInfo<DirectCurveV2Param> {
218
+ if (contractMethod !== DIRECT_METHOD_NAME) {
219
+ throw new Error(`Invalid contract method ${contractMethod}`);
220
+ }
221
+ assert(side === SwapSide.SELL, 'Buy not supported');
222
+
223
+ let isApproved: boolean = !!data.isApproved;
224
+ if (data.isApproved === undefined) {
225
+ this.logger.warn(`isApproved is undefined, defaulting to false`);
226
+ }
227
+
228
+ const swapParams: DirectCurveV2Param = [
229
+ srcToken,
230
+ destToken,
231
+ data.exchange,
232
+ data.originalPoolAddress,
233
+ srcAmount,
234
+ destAmount,
235
+ expectedAmount,
236
+ feePercent,
237
+ data.i.toString(),
238
+ data.j.toString(),
239
+ partner,
240
+ isApproved,
241
+ data.swapType,
242
+ beneficiary,
243
+ // For now we always wrap native. We don't support non native trade
244
+ true,
245
+ permit,
246
+ uuidToBytes16(uuid),
247
+ ];
248
+
249
+ const encoder = (...params: DirectCurveV2Param) => {
250
+ return this.directSwapIface.encodeFunctionData(DIRECT_METHOD_NAME, [
251
+ params,
252
+ ]);
253
+ };
254
+
255
+ return {
256
+ params: swapParams,
257
+ encoder,
258
+ networkFee: '0',
259
+ };
260
+ }
261
+
262
+ getDirectParamV6(
263
+ srcToken: Address,
264
+ destToken: Address,
265
+ fromAmount: NumberAsString,
266
+ toAmount: NumberAsString,
267
+ quotedAmount: NumberAsString,
268
+ data: CurveV2Data,
269
+ side: SwapSide,
270
+ permit: string,
271
+ uuid: string,
272
+ partnerAndFee: string,
273
+ beneficiary: string,
274
+ blockNumber: number,
275
+ contractMethod?: string,
276
+ ) {
277
+ if (contractMethod !== DIRECT_METHOD_NAME_V6) {
278
+ throw new Error(`Invalid contract method ${contractMethod}`);
279
+ }
280
+
281
+ assert(side === SwapSide.SELL, 'Buy not supported');
282
+
283
+ const metadata = hexConcat([
284
+ hexZeroPad(uuidToBytes16(uuid), 16),
285
+ hexZeroPad(hexlify(blockNumber), 16),
286
+ ]);
287
+
288
+ const swapParams: CurveV2DirectSwapParam = [
289
+ packCurveData(
290
+ data.exchange,
291
+ !data.isApproved, // approve flag, if not approved then set to true
292
+ isETHAddress(destToken) ? 2 : isETHAddress(srcToken) ? 1 : 0,
293
+ data.swapType,
294
+ ).toString(),
295
+ data.i,
296
+ data.j,
297
+ data.originalPoolAddress,
298
+ srcToken,
299
+ destToken,
300
+ fromAmount,
301
+ toAmount,
302
+ quotedAmount,
303
+ metadata,
304
+ beneficiary,
305
+ ];
306
+
307
+ const encodeParams: CurveV2DirectSwap = [swapParams, partnerAndFee, permit];
308
+
309
+ const encoder = (...params: CurveV2DirectSwap) => {
310
+ return this.augustusV6Interface.encodeFunctionData(
311
+ DIRECT_METHOD_NAME_V6,
312
+ [...params],
313
+ );
314
+ };
315
+
316
+ return {
317
+ params: encodeParams,
318
+ encoder,
319
+ networkFee: '0',
320
+ };
321
+ }
322
+
323
+ static getDirectFunctionNameV6(): string[] {
324
+ return [DIRECT_METHOD_NAME_V6];
325
+ }
326
+
327
+ static getDirectFunctionName(): string[] {
328
+ return [DIRECT_METHOD_NAME];
329
+ }
330
+
331
+ async getSimpleParam(
332
+ srcToken: string,
333
+ destToken: string,
334
+ srcAmount: string,
335
+ destAmount: string,
336
+ data: CurveV2Data,
337
+ side: SwapSide,
338
+ ): Promise<SimpleExchangeParam> {
339
+ if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
340
+
341
+ const { exchange, i, j, originalPoolAddress, swapType } = data;
342
+
343
+ const args: CurveV2Param | CurveV2ParamsForGenericFactoryZap =
344
+ swapType === CurveV2SwapType.EXCHANGE_GENERIC_FACTORY_ZAP
345
+ ? [
346
+ originalPoolAddress,
347
+ i.toString(),
348
+ j.toString(),
349
+ srcAmount,
350
+ this.minConversionRate,
351
+ ]
352
+ : [i.toString(), j.toString(), srcAmount, this.minConversionRate];
353
+
354
+ let swapMethod: string;
355
+ switch (swapType) {
356
+ case CurveV2SwapType.EXCHANGE:
357
+ swapMethod = CurveV2SwapFunctions.exchange;
358
+ break;
359
+ case CurveV2SwapType.EXCHANGE_UNDERLYING:
360
+ swapMethod = CurveV2SwapFunctions.exchange_underlying;
361
+ break;
362
+ case CurveV2SwapType.EXCHANGE_GENERIC_FACTORY_ZAP:
363
+ swapMethod = CurveV2SwapFunctions.exchange_in_generic_factory_zap;
364
+ break;
365
+ default:
366
+ throw new Error(
367
+ `getSimpleParam: not all cases covered for CurveV2SwapTypes`,
368
+ );
369
+ }
370
+
371
+ const swapData =
372
+ swapMethod === CurveV2SwapFunctions.exchange_in_generic_factory_zap
373
+ ? this.genericFactoryZapIface.encodeFunctionData(swapMethod, args)
374
+ : this.exchangeRouterInterface.encodeFunctionData(swapMethod, args);
375
+
376
+ return this.buildSimpleParamWithoutWETHConversion(
377
+ srcToken,
378
+ srcAmount,
379
+ destToken,
380
+ destAmount,
381
+ swapData,
382
+ exchange,
383
+ );
384
+ }
385
+
386
+ getDexParam(
387
+ srcToken: Address,
388
+ destToken: Address,
389
+ srcAmount: NumberAsString,
390
+ destAmount: NumberAsString,
391
+ recipient: Address,
392
+ data: CurveV2Data,
393
+ side: SwapSide,
394
+ ): DexExchangeParam {
395
+ if (side === SwapSide.BUY) throw new Error(`Buy not supported`);
396
+
397
+ const { exchange, i, j, originalPoolAddress, swapType } = data;
398
+
399
+ const args: CurveV2Param | CurveV2ParamsForGenericFactoryZap =
400
+ swapType === CurveV2SwapType.EXCHANGE_GENERIC_FACTORY_ZAP
401
+ ? [
402
+ originalPoolAddress,
403
+ i.toString(),
404
+ j.toString(),
405
+ srcAmount,
406
+ this.minConversionRate,
407
+ ]
408
+ : [i.toString(), j.toString(), srcAmount, this.minConversionRate];
409
+
410
+ let swapMethod: string;
411
+ switch (swapType) {
412
+ case CurveV2SwapType.EXCHANGE:
413
+ swapMethod = CurveV2SwapFunctions.exchange;
414
+ break;
415
+ case CurveV2SwapType.EXCHANGE_UNDERLYING:
416
+ swapMethod = CurveV2SwapFunctions.exchange_underlying;
417
+ break;
418
+ case CurveV2SwapType.EXCHANGE_GENERIC_FACTORY_ZAP:
419
+ swapMethod = CurveV2SwapFunctions.exchange_in_generic_factory_zap;
420
+ break;
421
+ default:
422
+ throw new Error(
423
+ `getSimpleParam: not all cases covered for CurveV2SwapTypes`,
424
+ );
425
+ }
426
+
427
+ const swapData =
428
+ swapMethod === CurveV2SwapFunctions.exchange_in_generic_factory_zap
429
+ ? this.genericFactoryZapIface.encodeFunctionData(swapMethod, args)
430
+ : this.exchangeRouterInterface.encodeFunctionData(swapMethod, args);
431
+
432
+ return {
433
+ needWrapNative: this.needWrapNative,
434
+ dexFuncHasRecipient: false,
435
+ exchangeData: swapData,
436
+ targetExchange: exchange,
437
+ returnAmountPos: undefined,
438
+ };
439
+ }
440
+ }