-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUniV2Controller.sol
296 lines (245 loc) · 11 KB
/
UniV2Controller.sol
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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import {IController} from "../core/IController.sol";
import {IControllerFacade} from "../core/IControllerFacade.sol";
import {IUniV2Factory} from "./IUniV2Factory.sol";
/**
@title Uniswap V2 Controller
@notice Controller for uniswap v2 interaction
eth:0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D
*/
contract UniV2Controller is IController {
/* -------------------------------------------------------------------------- */
/* CONSTANT VARIABLES */
/* -------------------------------------------------------------------------- */
/// @notice swapExactTokensForTokens(uint256,uint256,address[],address,uint256) function signature
bytes4 constant SWAP_EXACT_TOKENS_FOR_TOKENS = 0x38ed1739;
/// @notice swapTokensForExactTokens(uint256,uint256,address[],address,uint256) function signature
bytes4 constant SWAP_TOKENS_FOR_EXACT_TOKENS = 0x8803dbee;
/// @notice swapExactETHForTokens(uint256,address[],address,uint256) function signature
bytes4 constant SWAP_EXACT_ETH_FOR_TOKENS = 0x7ff36ab5;
/// @notice swapTokensForExactETH(uint256,uint256,address[],address,uint256) function signature
bytes4 constant SWAP_TOKENS_FOR_EXACT_ETH = 0x4a25d94a;
/// @notice swapExactTokensForETH(uint256,uint256,address[],address,uint256) function signature
bytes4 constant SWAP_EXACT_TOKENS_FOR_ETH = 0x18cbafe5;
/// @notice swapETHForExactTokens(uint256,address[],address,uint256) function signature
bytes4 constant SWAP_ETH_FOR_EXACT_TOKENS = 0xfb3bdb41;
/// @notice addLiquidity(address,address,uint256,uint256,uint256,uint256,address,uint256) function signature
bytes4 constant ADD_LIQUIDITY = 0xe8e33700;
/// @notice removeLiquidity(address,address,uint256,uint256,uint256,address,uint256) function signature
bytes4 constant REMOVE_LIQUIDITY = 0xbaa2abde;
/// @notice addLiquidityETH(address,uint256,uint256,uint256,address,uint256) function signature
bytes4 constant ADD_LIQUIDITY_ETH = 0xf305d719;
/// @notice removeLiquidityETH(address,uint256,uint256,uint256,address,uint256) function signature
bytes4 constant REMOVE_LIQUIDITY_ETH = 0x02751cec;
/* -------------------------------------------------------------------------- */
/* PUBLIC FUNCTIONS */
/* -------------------------------------------------------------------------- */
/// @notice WETH address
address public immutable WETH;
/// @notice Uniswap v2 factory
IUniV2Factory public immutable UNIV2_FACTORY;
/// @notice IControllerFacade
IControllerFacade public immutable controller;
/* -------------------------------------------------------------------------- */
/* CONSTRUCTOR */
/* -------------------------------------------------------------------------- */
/**
@notice Contract constructor
@param _WETH WETH address
@param _uniV2Factory Uniswap V2 Factory address
@param _controller Controller Facade
*/
constructor(
address _WETH,
IUniV2Factory _uniV2Factory,
IControllerFacade _controller
) {
WETH = _WETH;
UNIV2_FACTORY = _uniV2Factory;
controller = _controller;
}
/* -------------------------------------------------------------------------- */
/* PUBLIC FUNCTIONS */
/* -------------------------------------------------------------------------- */
/// @inheritdoc IController
function canCall(address, bool, bytes calldata data)
external
view
returns (bool, address[] memory, address[] memory)
{
bytes4 sig = bytes4(data);
// Swap Functions
if (sig == SWAP_EXACT_TOKENS_FOR_TOKENS || sig == SWAP_TOKENS_FOR_EXACT_TOKENS)
return swapErc20ForErc20(data[4:]); // ERC20 -> ERC20
if (sig == SWAP_EXACT_ETH_FOR_TOKENS || sig == SWAP_ETH_FOR_EXACT_TOKENS)
return swapEthForErc20(data[4:]); // ETH -> ERC20
if (sig == SWAP_TOKENS_FOR_EXACT_ETH || sig == SWAP_EXACT_TOKENS_FOR_ETH)
return swapErc20ForEth(data[4:]); // ERC20 -> ETH
// LP Functions
if (sig == ADD_LIQUIDITY) return addLiquidity(data[4:]);
if (sig == REMOVE_LIQUIDITY) return removeLiquidity(data[4:]);
if (sig == ADD_LIQUIDITY_ETH) return addLiquidityEth(data[4:]);
if (sig == REMOVE_LIQUIDITY_ETH) return removeLiquidityEth(data[4:]);
return(false, new address[](0), new address[](0));
}
/* -------------------------------------------------------------------------- */
/* INTERNAL FUNCTIONS */
/* -------------------------------------------------------------------------- */
/**
@notice Evaluates whether liquidity can be added
@param data calldata for adding liquidity
@return canCall Specifies if the interaction is accepted
@return tokensIn List of tokens that the account will receive after the
interactions
@return tokensOut List of tokens that will be removed from the account
after the interaction
*/
function addLiquidity(bytes calldata data)
internal
view
returns (bool, address[] memory, address[] memory)
{
(address tokenA, address tokenB) = abi.decode(data, (address, address));
address[] memory tokensOut = new address[](2);
tokensOut[0] = tokenA;
tokensOut[1] = tokenB;
address[] memory tokensIn = new address[](1);
tokensIn[0] = UNIV2_FACTORY.getPair(tokenA, tokenB);
return(controller.isTokenAllowed(tokensIn[0]), tokensIn, tokensOut);
}
/**
@notice Evaluates whether liquidity can be added
@param data calldata for adding liquidity
@return canCall Specifies if the interaction is accepted
@return tokensIn List of tokens that the account will receive after the
interactions
@return tokensOut List of tokens that will be removed from the account
after the interaction
*/
function addLiquidityEth(bytes calldata data)
internal
view
returns (bool, address[] memory, address[] memory)
{
address token = abi.decode(data, (address));
address[] memory tokensOut = new address[](1);
tokensOut[0] = token;
address[] memory tokensIn = new address[](1);
tokensIn[0] = UNIV2_FACTORY.getPair(token, WETH);
return(controller.isTokenAllowed(tokensIn[0]), tokensIn, tokensOut);
}
/**
@notice Evaluates whether liquidity can be removed
@param data calldata for removing liquidity
@return canCall Specifies if the interaction is accepted
@return tokensIn List of tokens that the account will receive after the
interactions
@return tokensOut List of tokens that will be removed from the account
after the interaction
*/
function removeLiquidity(bytes calldata data)
internal
view
returns (bool, address[] memory, address[] memory)
{
(address tokenA, address tokenB) = abi.decode(data, (address, address));
address[] memory tokensIn = new address[](2);
tokensIn[0] = tokenA;
tokensIn[1] = tokenB;
address[] memory tokensOut = new address[](1);
tokensOut[0] = UNIV2_FACTORY.getPair(tokenA, tokenB);
return(true, tokensIn, tokensOut);
}
/**
@notice Evaluates whether liquidity can be removed
@param data calldata for removing liquidity
@return canCall Specifies if the interaction is accepted
@return tokensIn List of tokens that the account will receive after the
interactions
@return tokensOut List of tokens that will be removed from the account
after the interaction
*/
function removeLiquidityEth(bytes calldata data)
internal
view
returns (bool, address[] memory, address[] memory)
{
(address token) = abi.decode(data, (address));
address[] memory tokensIn = new address[](1);
tokensIn[0] = token;
address[] memory tokensOut = new address[](1);
tokensOut[0] = UNIV2_FACTORY.getPair(token, WETH);
return(true, tokensIn, tokensOut);
}
/**
@notice Evaluates whether swap can be performed
@param data calldata for swapping tokens
@return canCall Specifies if the interaction is accepted
@return tokensIn List of tokens that the account will receive after the
interactions
@return tokensOut List of tokens that will be removed from the account
after the interaction
*/
function swapErc20ForErc20(bytes calldata data)
internal
view
returns (bool, address[] memory, address[] memory)
{
(,, address[] memory path,,)
= abi.decode(data, (uint, uint, address[], address, uint));
address[] memory tokensOut = new address[](1);
tokensOut[0] = path[0];
address[] memory tokensIn = new address[](1);
tokensIn[0] = path[path.length - 1];
return(
controller.isTokenAllowed(tokensIn[0]),
tokensIn,
tokensOut
);
}
/**
@notice Evaluates whether swap can be performed
@param data calldata for swapping tokens
@return canCall Specifies if the interaction is accepted
@return tokensIn List of tokens that the account will receive after the
interactions
@return tokensOut List of tokens that will be removed from the account
after the interaction
*/
function swapEthForErc20(bytes calldata data)
internal
view
returns (bool, address[] memory, address[] memory)
{
(, address[] memory path,,)
= abi.decode(data, (uint, address[], address, uint));
address[] memory tokensIn = new address[](1);
tokensIn[0] = path[path.length - 1];
return (
controller.isTokenAllowed(tokensIn[0]),
tokensIn,
new address[](0)
);
}
/**
@notice Evaluates whether swap can be performed
@param data calldata for swapping tokens
@return canCall Specifies if the interaction is accepted
@return tokensIn List of tokens that the account will receive after the
interactions
@return tokensOut List of tokens that will be removed from the account
after the interaction
*/
function swapErc20ForEth(bytes calldata data)
internal
pure
returns (bool, address[] memory, address[] memory)
{
(,, address[] memory path)
= abi.decode(data, (uint, uint, address[]));
address[] memory tokensOut = new address[](1);
tokensOut[0] = path[0];
return (true, new address[](0), tokensOut);
}
}