@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,328 +1,328 @@
1
- import { AbiEncoder } from '@0x/utils';
2
- import { Interface } from '@ethersproject/abi';
3
-
4
- import IParaswapAbi from '../../abi/IParaswap.json';
5
- import ZRX_V2_ABI from '../../abi/zrx.v2.json';
6
- import ZRX_V3_ABI from '../../abi/zrx.v3.json';
7
- import ZRX_V4_ABI from '../../abi/zrx.v4.json';
8
-
9
- import { SwapSide } from '../../constants';
10
- import { SimpleExchange } from '../simple-exchange';
11
- import { ZeroXOrder } from './order';
12
-
13
- import type { IDexTxBuilder } from '../idex';
14
- import type {
15
- AdapterExchangeParam,
16
- Address,
17
- DexExchangeParam,
18
- NumberAsString,
19
- SimpleExchangeParam,
20
- TxInfo,
21
- } from '../../types';
22
- import {
23
- ZeroXFunctions,
24
- type ZeroXData,
25
- type ZeroXParam,
26
- type ZeroXSignedOrderV2,
27
- type ZeroXSignedOrderV4,
28
- } from './types';
29
- import { IDexHelper } from '../../dex-helper';
30
- import { ZRX_EXCHANGE, ZRX_EXCHANGE_ERC20PROXY } from './config';
31
-
32
- const ZRX_ABI: any = {
33
- 2: ZRX_V2_ABI,
34
- 3: ZRX_V3_ABI,
35
- 4: ZRX_V4_ABI,
36
- };
37
-
38
- export class ZeroX
39
- extends SimpleExchange
40
- implements IDexTxBuilder<ZeroXData, ZeroXParam>
41
- {
42
- static dexKeys = ['zerox'];
43
- routerInterface: Interface;
44
- needWrapNative = true;
45
-
46
- constructor(dexHelper: IDexHelper) {
47
- super(dexHelper, 'zerox');
48
- this.routerInterface = new Interface(IParaswapAbi);
49
- }
50
-
51
- private getExchange(data: ZeroXData) {
52
- return ZRX_EXCHANGE[this.network][data.version];
53
- }
54
-
55
- protected buildSimpleSwapData(data: ZeroXData, srcAmount: NumberAsString) {
56
- const zrxABI = ZRX_ABI[data.version];
57
- const signature = data.order.signature;
58
- const order = ZeroXOrder.formatOrders(data.order, data.version);
59
-
60
- const methodAbi = zrxABI.find(
61
- (m: any) =>
62
- m.name ===
63
- (data.version === 4 ? 'fillRfqOrder' : 'marketSellOrdersNoThrow'),
64
- );
65
-
66
- const abiEncoder = new AbiEncoder.Method(methodAbi);
67
- // TODO: fillLimitOrder only accepts one order, find something that can accept multiple orders
68
- return abiEncoder.encode(
69
- data.version === 4
70
- ? [order, signature, srcAmount]
71
- : [[order], srcAmount, [signature]],
72
- );
73
- }
74
-
75
- protected buildPayload(data: ZeroXData) {
76
- const payload =
77
- data.version === 4
78
- ? this.abiCoder.encodeParameter(
79
- {
80
- ParentStruct: {
81
- order: {
82
- makerToken: 'address',
83
- takerToken: 'address',
84
- makerAmount: 'uint128',
85
- takerAmount: 'uint128',
86
- maker: 'address',
87
- taker: 'address',
88
- txOrigin: 'address',
89
- pool: 'bytes32',
90
- expiry: 'uint64',
91
- salt: 'uint256',
92
- },
93
- signature: {
94
- signatureType: 'uint8',
95
- v: 'uint8',
96
- r: 'bytes32',
97
- s: 'bytes32',
98
- },
99
- },
100
- },
101
- {
102
- order: ZeroXOrder.formatOrders(data.order, 4),
103
- signature: data.order.signature,
104
- },
105
- )
106
- : this.abiCoder.encodeParameter(
107
- {
108
- ParentStruct: {
109
- 'orders[]': {
110
- makerAddress: 'address', // Address that created the order.
111
- takerAddress: 'address', // Address that is allowed to fill the order. If set to 0, any address is allowed to fill the order.
112
- feeRecipientAddress: 'address', // Address that will receive fees when order is filled.
113
- senderAddress: 'address', // Address that is allowed to call Exchange contract methods that affect this order. If set to 0, any address is allowed to call these methods.
114
- makerAssetAmount: 'uint256', // Amount of makerAsset being offered by maker. Must be greater than 0.
115
- takerAssetAmount: 'uint256', // Amount of takerAsset being bid on by maker. Must be greater than 0.
116
- makerFee: 'uint256', // Fee paid to feeRecipient by maker when order is filled.
117
- takerFee: 'uint256', // Fee paid to feeRecipient by taker when order is filled.
118
- expirationTimeSeconds: 'uint256', // Timestamp in seconds at which order expires.
119
- salt: 'uint256', // Arbitrary number to facilitate uniqueness of the order's hash.
120
- makerAssetData: 'bytes', // Encoded data that can be decoded by a specified proxy contract when transferring makerAsset. The leading bytes4 references the id of the asset proxy.
121
- takerAssetData: 'bytes',
122
- },
123
- signatures: 'bytes[]',
124
- },
125
- },
126
- {
127
- orders: [ZeroXOrder.formatOrders(data.order, 2)],
128
- signatures: [data.order.signature],
129
- },
130
- );
131
- return payload;
132
- }
133
-
134
- getAdapterParam(
135
- srcToken: Address,
136
- destToken: Address,
137
- srcAmount: NumberAsString,
138
- toAmount: NumberAsString, // required for buy case
139
- data: ZeroXData,
140
- side: SwapSide,
141
- ): AdapterExchangeParam {
142
- return {
143
- targetExchange: this.getExchange(data),
144
- payload: this.buildPayload(data),
145
- networkFee: '0',
146
- };
147
- }
148
-
149
- async getSimpleParam(
150
- src: Address,
151
- dest: Address,
152
- srcAmount: NumberAsString,
153
- destAmount: NumberAsString,
154
- data: ZeroXData,
155
- side: SwapSide,
156
- ): Promise<SimpleExchangeParam> {
157
- if (side === SwapSide.BUY) {
158
- // Adjust the srcAmount according to the exact order price (rounding up)
159
- if (data.version === 4) {
160
- const order = data.order as ZeroXSignedOrderV4;
161
- if (BigInt(destAmount) > BigInt(order.makerAmount)) {
162
- throw new Error(
163
- `ZeroX destAmount ${destAmount} > makerAmount ${order.makerAmount}`,
164
- );
165
- }
166
- const calc =
167
- (BigInt(destAmount) * BigInt(order.takerAmount) +
168
- BigInt(order.makerAmount) -
169
- 1n) /
170
- BigInt(order.makerAmount);
171
- if (calc > BigInt(srcAmount)) {
172
- throw new Error(`ZeroX calc ${calc} > srcAmount ${srcAmount}`);
173
- }
174
- srcAmount = calc.toString();
175
- } else {
176
- const order = data.order as ZeroXSignedOrderV2;
177
- if (BigInt(destAmount) > BigInt(order.makerAssetAmount)) {
178
- throw new Error(
179
- `ZeroX destAmount ${destAmount} > makerAmount ${order.makerAssetAmount}`,
180
- );
181
- }
182
- const calc =
183
- (BigInt(destAmount) * BigInt(order.takerAssetAmount) +
184
- BigInt(order.makerAssetAmount) -
185
- 1n) /
186
- BigInt(order.makerAssetAmount);
187
- if (calc > BigInt(srcAmount)) {
188
- throw new Error(`ZeroX calc ${calc} > srcAmount ${srcAmount}`);
189
- }
190
- srcAmount = calc.toString();
191
- }
192
- }
193
- const swapData = this.buildSimpleSwapData(data, srcAmount);
194
- const networkFees = '0';
195
-
196
- return this.buildSimpleParamWithoutWETHConversion(
197
- src,
198
- srcAmount,
199
- dest,
200
- destAmount,
201
- swapData,
202
- this.getExchange(data),
203
- ZRX_EXCHANGE_ERC20PROXY[this.network][data.version],
204
- networkFees,
205
- );
206
- }
207
-
208
- getDexParam(
209
- _srcToken: Address,
210
- _destToken: Address,
211
- srcAmount: NumberAsString,
212
- destAmount: NumberAsString,
213
- _recipient: Address,
214
- data: ZeroXData,
215
- side: SwapSide,
216
- ): DexExchangeParam {
217
- if (side === SwapSide.BUY) {
218
- // Adjust the srcAmount according to the exact order price (rounding up)
219
- if (data.version === 4) {
220
- const order = data.order as ZeroXSignedOrderV4;
221
- if (BigInt(destAmount) > BigInt(order.makerAmount)) {
222
- throw new Error(
223
- `ZeroX destAmount ${destAmount} > makerAmount ${order.makerAmount}`,
224
- );
225
- }
226
- const calc =
227
- (BigInt(destAmount) * BigInt(order.takerAmount) +
228
- BigInt(order.makerAmount) -
229
- 1n) /
230
- BigInt(order.makerAmount);
231
- if (calc > BigInt(srcAmount)) {
232
- throw new Error(`ZeroX calc ${calc} > srcAmount ${srcAmount}`);
233
- }
234
- srcAmount = calc.toString();
235
- } else {
236
- const order = data.order as ZeroXSignedOrderV2;
237
- if (BigInt(destAmount) > BigInt(order.makerAssetAmount)) {
238
- throw new Error(
239
- `ZeroX destAmount ${destAmount} > makerAmount ${order.makerAssetAmount}`,
240
- );
241
- }
242
- const calc =
243
- (BigInt(destAmount) * BigInt(order.takerAssetAmount) +
244
- BigInt(order.makerAssetAmount) -
245
- 1n) /
246
- BigInt(order.makerAssetAmount);
247
- if (calc > BigInt(srcAmount)) {
248
- throw new Error(`ZeroX calc ${calc} > srcAmount ${srcAmount}`);
249
- }
250
- srcAmount = calc.toString();
251
- }
252
- }
253
- const swapData = this.buildSimpleSwapData(data, srcAmount);
254
-
255
- return {
256
- needWrapNative: this.needWrapNative,
257
- dexFuncHasRecipient: false,
258
- exchangeData: swapData,
259
- targetExchange: this.getExchange(data),
260
- returnAmountPos: undefined,
261
- };
262
- }
263
-
264
- getDirectParam(
265
- srcToken: Address,
266
- destToken: Address,
267
- srcAmount: NumberAsString,
268
- destAmount: NumberAsString,
269
- expectedAmount: NumberAsString,
270
- data: ZeroXData,
271
- side: SwapSide,
272
- permit: string,
273
- uuid: string,
274
- feePercent: NumberAsString,
275
- deadline: NumberAsString,
276
- partner: string,
277
- beneficiary: string,
278
- contractMethod?: string,
279
- ): TxInfo<ZeroXParam> {
280
- const usePermit = permit !== '0x';
281
- const encoder = (...params: ZeroXParam) => {
282
- switch (data.version) {
283
- case 2:
284
- return this.routerInterface.encodeFunctionData(
285
- usePermit
286
- ? ZeroXFunctions.swapOnZeroXv2WithPermit
287
- : ZeroXFunctions.swapOnZeroXv2,
288
- params,
289
- );
290
- case 4:
291
- return this.routerInterface.encodeFunctionData(
292
- usePermit
293
- ? ZeroXFunctions.swapOnZeroXv4WithPermit
294
- : ZeroXFunctions.swapOnZeroXv4,
295
- params,
296
- );
297
- default:
298
- throw new Error(`ZeroX version ${data.version} is not supported!`);
299
- }
300
- };
301
- return {
302
- params: usePermit
303
- ? [
304
- srcToken,
305
- destToken,
306
- srcAmount,
307
- destAmount,
308
- this.getExchange(data),
309
- this.buildPayload(data),
310
- permit,
311
- ]
312
- : [
313
- srcToken,
314
- destToken,
315
- srcAmount,
316
- destAmount,
317
- this.getExchange(data),
318
- this.buildPayload(data),
319
- ],
320
- encoder,
321
- networkFee: '0',
322
- };
323
- }
324
-
325
- static getDirectFunctionName(): string[] {
326
- return [ZeroXFunctions.swapOnZeroXv2, ZeroXFunctions.swapOnZeroXv4];
327
- }
328
- }
1
+ import { AbiEncoder } from '@0x/utils';
2
+ import { Interface } from '@ethersproject/abi';
3
+
4
+ import IParaswapAbi from '../../abi/IParaswap.json';
5
+ import ZRX_V2_ABI from '../../abi/zrx.v2.json';
6
+ import ZRX_V3_ABI from '../../abi/zrx.v3.json';
7
+ import ZRX_V4_ABI from '../../abi/zrx.v4.json';
8
+
9
+ import { SwapSide } from '../../constants';
10
+ import { SimpleExchange } from '../simple-exchange';
11
+ import { ZeroXOrder } from './order';
12
+
13
+ import type { IDexTxBuilder } from '../idex';
14
+ import type {
15
+ AdapterExchangeParam,
16
+ Address,
17
+ DexExchangeParam,
18
+ NumberAsString,
19
+ SimpleExchangeParam,
20
+ TxInfo,
21
+ } from '../../types';
22
+ import {
23
+ ZeroXFunctions,
24
+ type ZeroXData,
25
+ type ZeroXParam,
26
+ type ZeroXSignedOrderV2,
27
+ type ZeroXSignedOrderV4,
28
+ } from './types';
29
+ import { IDexHelper } from '../../dex-helper';
30
+ import { ZRX_EXCHANGE, ZRX_EXCHANGE_ERC20PROXY } from './config';
31
+
32
+ const ZRX_ABI: any = {
33
+ 2: ZRX_V2_ABI,
34
+ 3: ZRX_V3_ABI,
35
+ 4: ZRX_V4_ABI,
36
+ };
37
+
38
+ export class ZeroX
39
+ extends SimpleExchange
40
+ implements IDexTxBuilder<ZeroXData, ZeroXParam>
41
+ {
42
+ static dexKeys = ['zerox'];
43
+ routerInterface: Interface;
44
+ needWrapNative = true;
45
+
46
+ constructor(dexHelper: IDexHelper) {
47
+ super(dexHelper, 'zerox');
48
+ this.routerInterface = new Interface(IParaswapAbi);
49
+ }
50
+
51
+ private getExchange(data: ZeroXData) {
52
+ return ZRX_EXCHANGE[this.network][data.version];
53
+ }
54
+
55
+ protected buildSimpleSwapData(data: ZeroXData, srcAmount: NumberAsString) {
56
+ const zrxABI = ZRX_ABI[data.version];
57
+ const signature = data.order.signature;
58
+ const order = ZeroXOrder.formatOrders(data.order, data.version);
59
+
60
+ const methodAbi = zrxABI.find(
61
+ (m: any) =>
62
+ m.name ===
63
+ (data.version === 4 ? 'fillRfqOrder' : 'marketSellOrdersNoThrow'),
64
+ );
65
+
66
+ const abiEncoder = new AbiEncoder.Method(methodAbi);
67
+ // TODO: fillLimitOrder only accepts one order, find something that can accept multiple orders
68
+ return abiEncoder.encode(
69
+ data.version === 4
70
+ ? [order, signature, srcAmount]
71
+ : [[order], srcAmount, [signature]],
72
+ );
73
+ }
74
+
75
+ protected buildPayload(data: ZeroXData) {
76
+ const payload =
77
+ data.version === 4
78
+ ? this.abiCoder.encodeParameter(
79
+ {
80
+ ParentStruct: {
81
+ order: {
82
+ makerToken: 'address',
83
+ takerToken: 'address',
84
+ makerAmount: 'uint128',
85
+ takerAmount: 'uint128',
86
+ maker: 'address',
87
+ taker: 'address',
88
+ txOrigin: 'address',
89
+ pool: 'bytes32',
90
+ expiry: 'uint64',
91
+ salt: 'uint256',
92
+ },
93
+ signature: {
94
+ signatureType: 'uint8',
95
+ v: 'uint8',
96
+ r: 'bytes32',
97
+ s: 'bytes32',
98
+ },
99
+ },
100
+ },
101
+ {
102
+ order: ZeroXOrder.formatOrders(data.order, 4),
103
+ signature: data.order.signature,
104
+ },
105
+ )
106
+ : this.abiCoder.encodeParameter(
107
+ {
108
+ ParentStruct: {
109
+ 'orders[]': {
110
+ makerAddress: 'address', // Address that created the order.
111
+ takerAddress: 'address', // Address that is allowed to fill the order. If set to 0, any address is allowed to fill the order.
112
+ feeRecipientAddress: 'address', // Address that will receive fees when order is filled.
113
+ senderAddress: 'address', // Address that is allowed to call Exchange contract methods that affect this order. If set to 0, any address is allowed to call these methods.
114
+ makerAssetAmount: 'uint256', // Amount of makerAsset being offered by maker. Must be greater than 0.
115
+ takerAssetAmount: 'uint256', // Amount of takerAsset being bid on by maker. Must be greater than 0.
116
+ makerFee: 'uint256', // Fee paid to feeRecipient by maker when order is filled.
117
+ takerFee: 'uint256', // Fee paid to feeRecipient by taker when order is filled.
118
+ expirationTimeSeconds: 'uint256', // Timestamp in seconds at which order expires.
119
+ salt: 'uint256', // Arbitrary number to facilitate uniqueness of the order's hash.
120
+ makerAssetData: 'bytes', // Encoded data that can be decoded by a specified proxy contract when transferring makerAsset. The leading bytes4 references the id of the asset proxy.
121
+ takerAssetData: 'bytes',
122
+ },
123
+ signatures: 'bytes[]',
124
+ },
125
+ },
126
+ {
127
+ orders: [ZeroXOrder.formatOrders(data.order, 2)],
128
+ signatures: [data.order.signature],
129
+ },
130
+ );
131
+ return payload;
132
+ }
133
+
134
+ getAdapterParam(
135
+ srcToken: Address,
136
+ destToken: Address,
137
+ srcAmount: NumberAsString,
138
+ toAmount: NumberAsString, // required for buy case
139
+ data: ZeroXData,
140
+ side: SwapSide,
141
+ ): AdapterExchangeParam {
142
+ return {
143
+ targetExchange: this.getExchange(data),
144
+ payload: this.buildPayload(data),
145
+ networkFee: '0',
146
+ };
147
+ }
148
+
149
+ async getSimpleParam(
150
+ src: Address,
151
+ dest: Address,
152
+ srcAmount: NumberAsString,
153
+ destAmount: NumberAsString,
154
+ data: ZeroXData,
155
+ side: SwapSide,
156
+ ): Promise<SimpleExchangeParam> {
157
+ if (side === SwapSide.BUY) {
158
+ // Adjust the srcAmount according to the exact order price (rounding up)
159
+ if (data.version === 4) {
160
+ const order = data.order as ZeroXSignedOrderV4;
161
+ if (BigInt(destAmount) > BigInt(order.makerAmount)) {
162
+ throw new Error(
163
+ `ZeroX destAmount ${destAmount} > makerAmount ${order.makerAmount}`,
164
+ );
165
+ }
166
+ const calc =
167
+ (BigInt(destAmount) * BigInt(order.takerAmount) +
168
+ BigInt(order.makerAmount) -
169
+ 1n) /
170
+ BigInt(order.makerAmount);
171
+ if (calc > BigInt(srcAmount)) {
172
+ throw new Error(`ZeroX calc ${calc} > srcAmount ${srcAmount}`);
173
+ }
174
+ srcAmount = calc.toString();
175
+ } else {
176
+ const order = data.order as ZeroXSignedOrderV2;
177
+ if (BigInt(destAmount) > BigInt(order.makerAssetAmount)) {
178
+ throw new Error(
179
+ `ZeroX destAmount ${destAmount} > makerAmount ${order.makerAssetAmount}`,
180
+ );
181
+ }
182
+ const calc =
183
+ (BigInt(destAmount) * BigInt(order.takerAssetAmount) +
184
+ BigInt(order.makerAssetAmount) -
185
+ 1n) /
186
+ BigInt(order.makerAssetAmount);
187
+ if (calc > BigInt(srcAmount)) {
188
+ throw new Error(`ZeroX calc ${calc} > srcAmount ${srcAmount}`);
189
+ }
190
+ srcAmount = calc.toString();
191
+ }
192
+ }
193
+ const swapData = this.buildSimpleSwapData(data, srcAmount);
194
+ const networkFees = '0';
195
+
196
+ return this.buildSimpleParamWithoutWETHConversion(
197
+ src,
198
+ srcAmount,
199
+ dest,
200
+ destAmount,
201
+ swapData,
202
+ this.getExchange(data),
203
+ ZRX_EXCHANGE_ERC20PROXY[this.network][data.version],
204
+ networkFees,
205
+ );
206
+ }
207
+
208
+ getDexParam(
209
+ _srcToken: Address,
210
+ _destToken: Address,
211
+ srcAmount: NumberAsString,
212
+ destAmount: NumberAsString,
213
+ _recipient: Address,
214
+ data: ZeroXData,
215
+ side: SwapSide,
216
+ ): DexExchangeParam {
217
+ if (side === SwapSide.BUY) {
218
+ // Adjust the srcAmount according to the exact order price (rounding up)
219
+ if (data.version === 4) {
220
+ const order = data.order as ZeroXSignedOrderV4;
221
+ if (BigInt(destAmount) > BigInt(order.makerAmount)) {
222
+ throw new Error(
223
+ `ZeroX destAmount ${destAmount} > makerAmount ${order.makerAmount}`,
224
+ );
225
+ }
226
+ const calc =
227
+ (BigInt(destAmount) * BigInt(order.takerAmount) +
228
+ BigInt(order.makerAmount) -
229
+ 1n) /
230
+ BigInt(order.makerAmount);
231
+ if (calc > BigInt(srcAmount)) {
232
+ throw new Error(`ZeroX calc ${calc} > srcAmount ${srcAmount}`);
233
+ }
234
+ srcAmount = calc.toString();
235
+ } else {
236
+ const order = data.order as ZeroXSignedOrderV2;
237
+ if (BigInt(destAmount) > BigInt(order.makerAssetAmount)) {
238
+ throw new Error(
239
+ `ZeroX destAmount ${destAmount} > makerAmount ${order.makerAssetAmount}`,
240
+ );
241
+ }
242
+ const calc =
243
+ (BigInt(destAmount) * BigInt(order.takerAssetAmount) +
244
+ BigInt(order.makerAssetAmount) -
245
+ 1n) /
246
+ BigInt(order.makerAssetAmount);
247
+ if (calc > BigInt(srcAmount)) {
248
+ throw new Error(`ZeroX calc ${calc} > srcAmount ${srcAmount}`);
249
+ }
250
+ srcAmount = calc.toString();
251
+ }
252
+ }
253
+ const swapData = this.buildSimpleSwapData(data, srcAmount);
254
+
255
+ return {
256
+ needWrapNative: this.needWrapNative,
257
+ dexFuncHasRecipient: false,
258
+ exchangeData: swapData,
259
+ targetExchange: this.getExchange(data),
260
+ returnAmountPos: undefined,
261
+ };
262
+ }
263
+
264
+ getDirectParam(
265
+ srcToken: Address,
266
+ destToken: Address,
267
+ srcAmount: NumberAsString,
268
+ destAmount: NumberAsString,
269
+ expectedAmount: NumberAsString,
270
+ data: ZeroXData,
271
+ side: SwapSide,
272
+ permit: string,
273
+ uuid: string,
274
+ feePercent: NumberAsString,
275
+ deadline: NumberAsString,
276
+ partner: string,
277
+ beneficiary: string,
278
+ contractMethod?: string,
279
+ ): TxInfo<ZeroXParam> {
280
+ const usePermit = permit !== '0x';
281
+ const encoder = (...params: ZeroXParam) => {
282
+ switch (data.version) {
283
+ case 2:
284
+ return this.routerInterface.encodeFunctionData(
285
+ usePermit
286
+ ? ZeroXFunctions.swapOnZeroXv2WithPermit
287
+ : ZeroXFunctions.swapOnZeroXv2,
288
+ params,
289
+ );
290
+ case 4:
291
+ return this.routerInterface.encodeFunctionData(
292
+ usePermit
293
+ ? ZeroXFunctions.swapOnZeroXv4WithPermit
294
+ : ZeroXFunctions.swapOnZeroXv4,
295
+ params,
296
+ );
297
+ default:
298
+ throw new Error(`ZeroX version ${data.version} is not supported!`);
299
+ }
300
+ };
301
+ return {
302
+ params: usePermit
303
+ ? [
304
+ srcToken,
305
+ destToken,
306
+ srcAmount,
307
+ destAmount,
308
+ this.getExchange(data),
309
+ this.buildPayload(data),
310
+ permit,
311
+ ]
312
+ : [
313
+ srcToken,
314
+ destToken,
315
+ srcAmount,
316
+ destAmount,
317
+ this.getExchange(data),
318
+ this.buildPayload(data),
319
+ ],
320
+ encoder,
321
+ networkFee: '0',
322
+ };
323
+ }
324
+
325
+ static getDirectFunctionName(): string[] {
326
+ return [ZeroXFunctions.swapOnZeroXv2, ZeroXFunctions.swapOnZeroXv4];
327
+ }
328
+ }