-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathUniV3LiquidityAmo.sol
380 lines (320 loc) · 10.9 KB
/
UniV3LiquidityAmo.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
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.19;
import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { AccessControl } from "@openzeppelin/contracts/access/AccessControl.sol";
import { ERC721Holder } from "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol";
import { SafeMath } from "@openzeppelin/contracts/utils/math/SafeMath.sol";
import { TransferHelper } from "../libraries/TransferHelper.sol";
import { IRdpxV2Core } from "../core/IRdpxV2Core.sol";
// Uniswamp V3
import "../uniswap_V3/IUniswapV3Factory.sol";
import "../uniswap_V3/libraries/TickMath.sol";
import "../uniswap_V3/libraries/LiquidityAmounts.sol";
import "../uniswap_V3/periphery/interfaces/INonfungiblePositionManager.sol";
import "../uniswap_V3/IUniswapV3Pool.sol";
import "../uniswap_V3/ISwapRouter.sol";
// Interfaces
import { IERC20WithBurn } from "../interfaces/IERC20WithBurn.sol";
abstract contract OracleLike {
function read() external view virtual returns (uint);
function uniswapPool() external view virtual returns (address);
}
contract UniV3LiquidityAMO is AccessControl, ERC721Holder {
using SafeMath for uint256;
using SafeERC20 for IERC20WithBurn;
/* ========== STATE VARIABLES ========== */
// Uniswap v3
IUniswapV3Factory public univ3_factory;
INonfungiblePositionManager public univ3_positions;
ISwapRouter public univ3_router;
// Details about the AMO's uniswap positions
struct Position {
uint256 token_id;
address collateral_address;
uint128 liquidity; // the liquidity of the position
int24 tickLower; // the tick range of the position
int24 tickUpper;
uint24 fee_tier;
}
// Add liquidity param
struct AddLiquidityParams {
address _tokenA;
address _tokenB;
int24 _tickLower;
int24 _tickUpper;
uint24 _fee;
uint256 _amount0Desired;
uint256 _amount1Desired;
uint256 _amount0Min;
uint256 _amount1Min;
}
// Array of all Uni v3 NFT positions held by the AMO
Position[] public positions_array;
// Map token_id to Position
mapping(uint256 => Position) public positions_mapping;
// Rdpx address
address public rdpx;
// RdpxV2Core address
address public rdpxV2Core;
/* ========== CONSTRUCTOR ========== */
constructor(address _rdpx, address _rdpxV2Core) {
rdpx = _rdpx;
rdpxV2Core = _rdpxV2Core;
_setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
univ3_factory = IUniswapV3Factory(
0x1F98431c8aD98523631AE4a59f267346ea31F984
);
univ3_positions = INonfungiblePositionManager(
0xC36442b4a4522E871399CD717aBDD847Ab11FE88
);
univ3_router = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564);
}
/* ========== VIEWS ========== */
// Returns this contract's liquidity in a specific [Rdpx]-[collateral] uni v3 pool
function liquidityInPool(
address _collateral_address,
int24 _tickLower,
int24 _tickUpper,
uint24 _fee
) public view returns (uint128) {
IUniswapV3Pool get_pool = IUniswapV3Pool(
univ3_factory.getPool(address(rdpx), _collateral_address, _fee)
);
// goes into the pool's positions mapping, and grabs this address's liquidity
(uint128 liquidity, , , , ) = get_pool.positions(
keccak256(abi.encodePacked(address(this), _tickLower, _tickUpper))
);
return liquidity;
}
// Only counts non-withdrawn positions
function numPositions() public view returns (uint256) {
return positions_array.length;
}
/* ========== RESTRICTED FUNCTIONS, BUT CUSTODIAN CAN CALL ========== */
// Iterate through all positions and collect fees accumulated
function collectFees() external onlyRole(DEFAULT_ADMIN_ROLE) {
for (uint i = 0; i < positions_array.length; i++) {
Position memory current_position = positions_array[i];
INonfungiblePositionManager.CollectParams
memory collect_params = INonfungiblePositionManager.CollectParams(
current_position.token_id,
rdpxV2Core,
type(uint128).max,
type(uint128).max
);
// Send to custodian address
univ3_positions.collect(collect_params);
}
}
/* ---------------------------------------------------- */
/* ---------------------- Uni v3 ---------------------- */
/* ---------------------------------------------------- */
function approveTarget(
address _target,
address _token,
uint256 _amount,
bool use_safe_approve
) public onlyRole(DEFAULT_ADMIN_ROLE) {
if (use_safe_approve) {
// safeApprove needed for USDT and others for the first approval
// You need to approve 0 every time beforehand for USDT: it resets
TransferHelper.safeApprove(_token, _target, _amount);
} else {
IERC20WithBurn(_token).approve(_target, _amount);
}
}
// IUniswapV3Pool public current_uni_pool; // only used for mint callback; is set and accessed during execution of addLiquidity()
function addLiquidity(
AddLiquidityParams memory params
) public onlyRole(DEFAULT_ADMIN_ROLE) {
IERC20WithBurn(params._tokenA).transferFrom(
rdpxV2Core,
address(this),
params._amount0Desired
);
IERC20WithBurn(params._tokenB).transferFrom(
rdpxV2Core,
address(this),
params._amount1Desired
);
IERC20WithBurn(params._tokenA).approve(
address(univ3_positions),
params._amount0Desired
);
IERC20WithBurn(params._tokenB).approve(
address(univ3_positions),
params._amount1Desired
);
INonfungiblePositionManager.MintParams
memory mintParams = INonfungiblePositionManager.MintParams(
params._tokenA,
params._tokenB,
params._fee,
params._tickLower,
params._tickUpper,
params._amount0Desired,
params._amount1Desired,
params._amount0Min,
params._amount1Min,
address(this),
type(uint256).max
);
(uint256 tokenId, uint128 amountLiquidity, , ) = univ3_positions.mint(
mintParams
);
Position memory pos = Position(
tokenId,
params._tokenA == address(rdpx) ? params._tokenB : params._tokenA,
amountLiquidity,
params._tickLower,
params._tickUpper,
params._fee
);
positions_array.push(pos);
positions_mapping[tokenId] = pos;
// send tokens to rdpxV2Core
_sendTokensToRdpxV2Core(params._tokenA, params._tokenB);
}
function removeLiquidity(
uint256 positionIndex,
uint256 minAmount0,
uint256 minAmount1
) public onlyRole(DEFAULT_ADMIN_ROLE) {
Position memory pos = positions_array[positionIndex];
INonfungiblePositionManager.CollectParams
memory collect_params = INonfungiblePositionManager.CollectParams(
pos.token_id,
rdpxV2Core,
type(uint128).max,
type(uint128).max
);
(
,
,
address tokenA,
address tokenB,
,
,
,
uint128 liquidity,
,
,
,
) = univ3_positions.positions(pos.token_id);
// remove liquidity
INonfungiblePositionManager.DecreaseLiquidityParams
memory decreaseLiquidityParams = INonfungiblePositionManager
.DecreaseLiquidityParams(
pos.token_id,
liquidity,
minAmount0,
minAmount1,
block.timestamp
);
univ3_positions.decreaseLiquidity(decreaseLiquidityParams);
univ3_positions.collect(collect_params);
univ3_positions.burn(pos.token_id);
positions_array[positionIndex] = positions_array[
positions_array.length - 1
];
positions_array.pop();
delete positions_mapping[pos.token_id];
// send tokens to rdpxV2Core
_sendTokensToRdpxV2Core(tokenA, tokenB);
emit log(positions_array.length);
emit log(positions_mapping[pos.token_id].token_id);
}
// Swap tokenA into tokenB using univ3_router.ExactInputSingle()
// Uni V3 only
function swap(
address _tokenA,
address _tokenB,
uint24 _fee_tier,
uint256 _amountAtoB,
uint256 _amountOutMinimum,
uint160 _sqrtPriceLimitX96
) public onlyRole(DEFAULT_ADMIN_ROLE) returns (uint256) {
// transfer token from rdpx v2 core
IERC20WithBurn(_tokenA).transferFrom(
rdpxV2Core,
address(this),
_amountAtoB
);
ISwapRouter.ExactInputSingleParams memory swap_params = ISwapRouter
.ExactInputSingleParams(
_tokenA,
_tokenB,
_fee_tier,
address(this),
2105300114, // Expiration: a long time from now
_amountAtoB,
_amountOutMinimum,
_sqrtPriceLimitX96
);
// Approval
TransferHelper.safeApprove(_tokenA, address(univ3_router), _amountAtoB);
uint256 amountOut = univ3_router.exactInputSingle(swap_params);
_sendTokensToRdpxV2Core(_tokenA, _tokenB);
return amountOut;
}
/* ========== OWNER / GOVERNANCE FUNCTIONS ONLY ========== */
// Only owner or timelock can call, to limit risk
function recoverERC20(
address tokenAddress,
uint256 tokenAmount
) external onlyRole(DEFAULT_ADMIN_ROLE) {
// Can only be triggered by owner or governance, not custodian
// Tokens are sent to the custodian, as a sort of safeguard
TransferHelper.safeTransfer(tokenAddress, rdpxV2Core, tokenAmount);
emit RecoveredERC20(tokenAddress, tokenAmount);
}
function recoverERC721(
address tokenAddress,
uint256 token_id
) external onlyRole(DEFAULT_ADMIN_ROLE) {
// Only the owner address can ever receive the recovery withdrawal
// INonfungiblePositionManager inherits IERC721 so the latter does not need to be imported
INonfungiblePositionManager(tokenAddress).safeTransferFrom(
address(this),
rdpxV2Core,
token_id
);
emit RecoveredERC721(tokenAddress, token_id);
}
// Generic proxy
function execute(
address _to,
uint256 _value,
bytes calldata _data
) external onlyRole(DEFAULT_ADMIN_ROLE) returns (bool, bytes memory) {
(bool success, bytes memory result) = _to.call{ value: _value }(_data);
return (success, result);
}
/* ========== INTERNAL ========== */
/**
* @dev sends token A and B to the rdpxV2Core
*/
function _sendTokensToRdpxV2Core(address tokenA, address tokenB) internal {
uint256 tokenABalance = IERC20WithBurn(tokenA).balanceOf(address(this));
uint256 tokenBBalance = IERC20WithBurn(tokenB).balanceOf(address(this));
// transfer token A and B from this contract to the rdpxV2Core
IERC20WithBurn(tokenA).safeTransfer(rdpxV2Core, tokenABalance);
IERC20WithBurn(tokenB).safeTransfer(rdpxV2Core, tokenBBalance);
// sync token balances
IRdpxV2Core(rdpxV2Core).sync();
emit LogAssetsTransfered(tokenABalance, tokenBBalance, tokenA, tokenB);
}
/* ========== EVENTS ========== */
event RecoveredERC20(address token, uint256 amount);
event RecoveredERC721(address token, uint256 id);
event LogAssetsTransfered(
uint256 tokenAAmount,
uint256 tokenBAmount,
address tokenAAddress,
address tokenBAddress
);
/*
** burn tokenAmount from the recipient and send tokens to the receipient
*/
event log(uint);
}