-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV3Handler.sol
More file actions
364 lines (311 loc) · 11.7 KB
/
Copy pathV3Handler.sol
File metadata and controls
364 lines (311 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/access/AccessControl.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
// Uniswap V3 interfaces
interface ISwapRouter {
struct ExactInputSingleParams {
address tokenIn;
address tokenOut;
uint24 fee;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
uint160 sqrtPriceLimitX96;
}
struct ExactInputParams {
bytes path;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
}
function exactInputSingle(ExactInputSingleParams calldata params) external payable returns (uint256 amountOut);
function exactInput(ExactInputParams calldata params) external payable returns (uint256 amountOut);
function WETH9() external view returns (address);
}
interface IQuoterV2 {
struct QuoteExactInputSingleParams {
address tokenIn;
address tokenOut;
uint256 amountIn;
uint24 fee;
uint160 sqrtPriceLimitX96;
}
function quoteExactInputSingle(QuoteExactInputSingleParams memory params)
external
returns (
uint256 amountOut,
uint160 sqrtPriceX96After,
uint32 initializedTicksCrossed,
uint256 gasEstimate
);
}
/**
* @title V3Handler
* @notice Handles Uniswap V3 and PancakeSwap V3 interactions for the aggregator
* @dev Separate contract to keep main contracts under size limit
*/
contract V3Handler is AccessControl {
using SafeERC20 for IERC20;
bytes32 public constant EXECUTOR_ROLE = keccak256("EXECUTOR_ROLE");
struct V3Router {
address swapRouter;
address quoter;
bool isActive;
uint24[] supportedFees; // [100, 500, 3000, 10000] = 0.01%, 0.05%, 0.3%, 1%
}
// Gas estimates for different V3 operations
struct GasEstimates {
uint256 singleHopGas;
uint256 multiHopBaseGas;
uint256 perHopGas;
}
mapping(bytes32 => V3Router) public v3Routers;
bytes32[] public activeV3Routers;
GasEstimates public gasEstimates;
event V3SwapExecuted(
bytes32 indexed routerKey,
address indexed tokenIn,
address indexed tokenOut,
uint256 amountIn,
uint256 amountOut
);
constructor() {
_grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
_grantRole(EXECUTOR_ROLE, msg.sender);
// Initialize default gas estimates (can be updated by admin)
gasEstimates = GasEstimates({
singleHopGas: 180000,
multiHopBaseGas: 200000,
perHopGas: 50000
});
}
/**
* @notice Execute V3 swap with single hop
*/
function executeV3Swap(
bytes32 routerKey,
address tokenIn,
address tokenOut,
uint24 fee,
uint256 amountIn,
uint256 amountOutMinimum,
address recipient,
uint256 deadline
) external onlyRole(EXECUTOR_ROLE) returns (uint256 amountOut) {
V3Router memory router = v3Routers[routerKey];
require(router.isActive, "Router not active");
// Transfer tokens from caller
IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
// Approve router
SafeERC20.forceApprove(IERC20(tokenIn), router.swapRouter, amountIn);
// Prepare swap params
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
tokenIn: tokenIn,
tokenOut: tokenOut,
fee: fee,
recipient: recipient,
deadline: deadline,
amountIn: amountIn,
amountOutMinimum: amountOutMinimum,
sqrtPriceLimitX96: 0
});
// Execute swap
amountOut = ISwapRouter(router.swapRouter).exactInputSingle(params);
emit V3SwapExecuted(routerKey, tokenIn, tokenOut, amountIn, amountOut);
// Reset approval
SafeERC20.forceApprove(IERC20(tokenIn), router.swapRouter, 0);
}
/**
* @notice Execute V3 multi-hop swap
*/
function executeV3MultiHopSwap(
bytes32 routerKey,
bytes memory path, // Encoded path: token0, fee0, token1, fee1, token2...
uint256 amountIn,
uint256 amountOutMinimum,
address recipient,
uint256 deadline
) external onlyRole(EXECUTOR_ROLE) returns (uint256 amountOut) {
V3Router memory router = v3Routers[routerKey];
require(router.isActive, "Router not active");
// Decode first token from path for approval
address tokenIn;
assembly {
tokenIn := mload(add(path, 0x20))
}
// Transfer tokens from caller
IERC20(tokenIn).safeTransferFrom(msg.sender, address(this), amountIn);
// Approve router
SafeERC20.forceApprove(IERC20(tokenIn), router.swapRouter, amountIn);
// Prepare swap params
ISwapRouter.ExactInputParams memory params = ISwapRouter.ExactInputParams({
path: path,
recipient: recipient,
deadline: deadline,
amountIn: amountIn,
amountOutMinimum: amountOutMinimum
});
// Execute swap
amountOut = ISwapRouter(router.swapRouter).exactInput(params);
// Reset approval
SafeERC20.forceApprove(IERC20(tokenIn), router.swapRouter, 0);
return amountOut;
}
/**
* @notice Execute native ETH to token swap on V3
*/
function executeV3ETHSwap(
bytes32 routerKey,
address tokenOut,
uint24 fee,
uint256 amountOutMinimum,
address recipient,
uint256 deadline
) external payable onlyRole(EXECUTOR_ROLE) returns (uint256 amountOut) {
V3Router memory router = v3Routers[routerKey];
require(router.isActive, "Router not active");
address weth = ISwapRouter(router.swapRouter).WETH9();
// Prepare swap params
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({
tokenIn: weth,
tokenOut: tokenOut,
fee: fee,
recipient: recipient,
deadline: deadline,
amountIn: msg.value,
amountOutMinimum: amountOutMinimum,
sqrtPriceLimitX96: 0
});
// Execute swap with ETH
amountOut = ISwapRouter(router.swapRouter).exactInputSingle{value: msg.value}(params);
emit V3SwapExecuted(routerKey, weth, tokenOut, msg.value, amountOut);
}
/**
* @notice Get quote for V3 swap
*/
function getV3Quote(
bytes32 routerKey,
address tokenIn,
address tokenOut,
uint24 fee,
uint256 amountIn
) external returns (uint256 amountOut, uint256 gasEstimate) {
V3Router memory router = v3Routers[routerKey];
require(router.quoter != address(0), "Quoter not set");
IQuoterV2.QuoteExactInputSingleParams memory params = IQuoterV2.QuoteExactInputSingleParams({
tokenIn: tokenIn,
tokenOut: tokenOut,
amountIn: amountIn,
fee: fee,
sqrtPriceLimitX96: 0
});
(amountOut, , , gasEstimate) = IQuoterV2(router.quoter).quoteExactInputSingle(params);
}
/**
* @notice Find best fee tier for a V3 pair
*/
function findBestV3Fee(
bytes32 routerKey,
address tokenIn,
address tokenOut,
uint256 amountIn
) external returns (uint24 bestFee, uint256 bestOutput) {
V3Router memory router = v3Routers[routerKey];
require(router.quoter != address(0), "Quoter not set");
for (uint256 i = 0; i < router.supportedFees.length; i++) {
uint24 fee = router.supportedFees[i];
try IQuoterV2(router.quoter).quoteExactInputSingle(
IQuoterV2.QuoteExactInputSingleParams({
tokenIn: tokenIn,
tokenOut: tokenOut,
amountIn: amountIn,
fee: fee,
sqrtPriceLimitX96: 0
})
) returns (uint256 amountOut, uint160, uint32, uint256) {
if (amountOut > bestOutput) {
bestOutput = amountOut;
bestFee = fee;
}
} catch {
// Pool doesn't exist for this fee tier
continue;
}
}
}
/**
* @notice Get comprehensive V3 quote with gas estimation
*/
function getV3QuoteWithGas(
bytes32 routerKey,
address tokenIn,
address tokenOut,
uint256 amountIn
) external returns (
uint24 bestFee,
uint256 bestOutput,
uint256 gasEstimate
) {
(bestFee, bestOutput) = this.findBestV3Fee(routerKey, tokenIn, tokenOut, amountIn);
gasEstimate = gasEstimates.singleHopGas; // Use dynamic gas estimate
}
/**
* @notice Get gas estimate for multi-hop V3 swap
*/
function getV3MultiHopGasEstimate(uint256 hopCount) public view returns (uint256) {
return gasEstimates.multiHopBaseGas + (gasEstimates.perHopGas * hopCount);
}
// --- ADMIN FUNCTIONS ---
function addV3Router(
bytes32 key,
address swapRouter,
address quoter,
uint24[] memory supportedFees
) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(swapRouter != address(0), "Invalid router");
require(v3Routers[key].swapRouter == address(0), "Router exists");
v3Routers[key] = V3Router({
swapRouter: swapRouter,
quoter: quoter,
isActive: true,
supportedFees: supportedFees
});
activeV3Routers.push(key);
}
function updateV3Router(
bytes32 key,
bool isActive
) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(v3Routers[key].swapRouter != address(0), "Router not found");
v3Routers[key].isActive = isActive;
}
function grantExecutorRole(address executor) external onlyRole(DEFAULT_ADMIN_ROLE) {
grantRole(EXECUTOR_ROLE, executor);
}
function updateGasEstimates(
uint256 _singleHopGas,
uint256 _multiHopBaseGas,
uint256 _perHopGas
) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(_singleHopGas > 0 && _singleHopGas < 1000000, "Invalid single hop gas");
require(_multiHopBaseGas > 0 && _multiHopBaseGas < 2000000, "Invalid multi hop base gas");
require(_perHopGas > 0 && _perHopGas < 500000, "Invalid per hop gas");
gasEstimates = GasEstimates({
singleHopGas: _singleHopGas,
multiHopBaseGas: _multiHopBaseGas,
perHopGas: _perHopGas
});
}
// Emergency functions
function emergencyWithdrawToken(address token, uint256 amount) external onlyRole(DEFAULT_ADMIN_ROLE) {
IERC20(token).safeTransfer(msg.sender, amount);
}
function emergencyWithdrawETH() external onlyRole(DEFAULT_ADMIN_ROLE) {
payable(msg.sender).transfer(address(this).balance);
}
receive() external payable {}
}