-
Notifications
You must be signed in to change notification settings - Fork 0
/
UniswapManager.sol
680 lines (615 loc) · 23.1 KB
/
UniswapManager.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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.6.12;
pragma experimental ABIEncoderV2;
import {
SafeERC20,
SafeMath,
IERC20,
Address
} from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "./interfaces/uniswap.sol";
import "./interfaces/IERC721.sol";
import {IStrategyInsurance} from "./StrategyInsurance.sol";
import "./libraries/PoolVariables.sol";
import "./libraries/PoolAddress.sol";
import "./interfaces/ISwapRouter.sol";
import "./interfaces/IUniswapV3Pool.sol";
import "./interfaces/IUniswapV3PositionsNFT.sol";
import "./libraries/OracleLibrary.sol";
import "./libraries/SafeUint128.sol";
contract UniswapManager {
using SafeERC20 for IERC20;
using Address for address;
using SafeMath for uint256;
using SafeMath for uint128;
using SafeMath for uint8;
using PoolVariables for IUniswapV3Pool;
event InitialDeposited(uint256 tokenId);
event Deposited(
uint256 tokenId,
uint256 token0Balance,
uint256 token1Balance,
uint256 refund0,
uint256 refund1
);
event Withdraw(uint256 tokenId, uint256 liquidity);
event Destroy(uint256 tokenId, uint256 liquidity);
event Rebalanced(uint256 tokenId, int24 _tickLower, int24 _tickUpper);
event ExecutionResult(bool success, bytes result);
IUniswapV3PositionsNFT public nftManager =
IUniswapV3PositionsNFT(0xC36442b4a4522E871399CD717aBDD847Ab11FE88);
struct positionInfo {
IUniswapV3Pool pool;
int24 tick_lower;
int24 tick_upper;
uint24 twapTime;
int24 tickRangeMultiplier;
address owner;
}
uint256[] tokenIds;
mapping(uint256 => positionInfo) positions;
address constant uniswapV3Factory =
0x1F98431c8aD98523631AE4a59f267346ea31F984;
IERC721 constant nft = IERC721(0xC36442b4a4522E871399CD717aBDD847Ab11FE88);
address public admin;
address public newAdmin = address(0);
ISwapRouter router;
constructor(address _router, address _admin) public {
router = ISwapRouter(_router);
admin = _admin;
}
function determineTicks(
IUniswapV3Pool _pool,
uint24 _twapTime,
int24 tickRangeMultiplier
) public view returns (int24, int24) {
int24 tickSpacing = _pool.tickSpacing();
int24 baseThreshold = tickSpacing * tickRangeMultiplier;
if (_twapTime > 0) {
uint32[] memory _observeTime = new uint32[](2);
_observeTime[0] = _twapTime;
_observeTime[1] = 0;
(int56[] memory _cumulativeTicks, ) = _pool.observe(_observeTime);
int56 _averageTick =
(_cumulativeTicks[1] - _cumulativeTicks[0]) / _twapTime;
return
PoolVariables.baseTicks(
int24(_averageTick),
baseThreshold,
tickSpacing
);
} else {
(, int24 tick, , , , , ) = _pool.slot0();
return PoolVariables.baseTicks(tick, baseThreshold, tickSpacing);
}
}
function getLiquidity(uint256 _tokenId)
public
view
returns (uint128 _liquidity)
{
(, , , , , , , _liquidity, , , , ) = nftManager.positions(_tokenId);
return _liquidity;
}
function getLpReserves(uint256 _tokenId)
external
view
returns (uint256 _token0, uint256 _token1)
{
uint128 _liquidity = uint128(getLiquidity(_tokenId)); //TODO: Is this cast safe?
positionInfo memory pos = positions[_tokenId];
(uint160 sqrtPriceX96, , , , , , ) = pos.pool.slot0();
uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(pos.tick_lower);
uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(pos.tick_upper);
return
LiquidityAmounts.getAmountsForLiquidity(
sqrtPriceX96,
sqrtRatioAX96,
sqrtRatioBX96,
_liquidity
);
}
function getCurrentTick(uint256 _tokenId)
external
view
returns (int24 tick)
{
positionInfo memory pos = positions[_tokenId];
(uint160 sqrtPriceX96, int24 currentTick, , , , , ) = pos.pool.slot0();
return currentTick;
}
function getLowerTick(uint256 _tokenId) external view returns (int24 tick) {
positionInfo memory pos = positions[_tokenId];
return pos.tick_lower;
}
function getUpperTick(uint256 _tokenId) external view returns (int24 tick) {
positionInfo memory pos = positions[_tokenId];
return pos.tick_upper;
}
function isUnbalanced(uint256 _tokenId)
external
view
returns (bool _result)
{
// TODO: which implementation works in practice?
/*
(uint256 _token0, uint256 _token1) = getLpReserves(_tokenId);
if((_token0 < 10000) || (_token1 < 10000)){
return true;
}
return false;
*/
positionInfo memory pos = positions[_tokenId];
// Slot0 Has the current price.
(uint160 sqrtPriceX96, , , , , , ) = pos.pool.slot0();
uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(pos.tick_lower);
uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(pos.tick_upper);
if ((sqrtPriceX96 < sqrtRatioAX96) || (sqrtPriceX96 > sqrtRatioBX96)) {
return true;
}
return false;
}
function getPriceAtTick(int24 tick) public view returns (uint256) {
uint160 sqrtRatio = TickMath.getSqrtRatioAtTick(tick);
uint256 priceX96 = getPriceX96FromSqrtPriceX96(sqrtRatio);
return FullMath.mulDiv(priceX96, 1e18, FixedPoint96.Q96);
}
function getSqrtTwapX96(IUniswapV3Pool uniswapV3Pool, uint32 twapInterval)
public
view
returns (uint160 sqrtPriceX96)
{
if (twapInterval == 0) {
// return the current price if twapInterval == 0
(sqrtPriceX96, , , , , , ) = uniswapV3Pool.slot0();
} else {
uint32[] memory secondsAgos = new uint32[](2);
secondsAgos[0] = twapInterval; // from (before)
secondsAgos[1] = 0; // to (now)
(int56[] memory tickCumulatives, ) =
uniswapV3Pool.observe(secondsAgos);
// tick(imprecise as it's an integer) to price
sqrtPriceX96 = TickMath.getSqrtRatioAtTick(
int24((tickCumulatives[1] - tickCumulatives[0]) / twapInterval)
);
}
}
function getPriceX96FromSqrtPriceX96(uint160 sqrtPriceX96)
public
pure
returns (uint256 priceX96)
{
return FullMath.mulDiv(sqrtPriceX96, sqrtPriceX96, FixedPoint96.Q96);
}
//TODO: Consider if this function could take tokenId instead of pool
function getTwapPrice(IUniswapV3Pool _pool, uint32 _time)
public
view
returns (uint256)
{
uint160 sqrtPriceX96 = getSqrtTwapX96(_pool, _time); // TODO: maybe use global twap time, recommended value is 60
//return getPriceX96FromSqrtPriceX96(sqrtPriceX96);
uint256 priceX96 = getPriceX96FromSqrtPriceX96(sqrtPriceX96);
//return priceX96.mul(1e18).div(FixedPoint96.Q96)
return FullMath.mulDiv(priceX96, 1e18, FixedPoint96.Q96);
}
// Attempt to take input tokens and balance them for the desired pool and range so they may be deposited.
function _balanceProportion(
IUniswapV3Pool _pool,
int24 _tickLower,
int24 _tickUpper
) internal returns (uint256 _amount0, uint256 _amount1) {
PoolVariables.Info memory _cache;
_cache.amount0Desired = IERC20(_pool.token0()).balanceOf(address(this));
_cache.amount1Desired = IERC20(_pool.token1()).balanceOf(address(this));
(uint160 sqrtPriceX96, , , , , , ) = _pool.slot0();
uint160 sqrtRatioAX96 = TickMath.getSqrtRatioAtTick(_tickLower);
uint160 sqrtRatioBX96 = TickMath.getSqrtRatioAtTick(_tickUpper);
_cache.liquidity = uint128(
LiquidityAmounts
.getLiquidityForAmount0(
sqrtRatioAX96,
sqrtRatioBX96,
_cache
.amount0Desired
)
.add(
LiquidityAmounts.getLiquidityForAmount1(
sqrtRatioAX96,
sqrtRatioBX96,
_cache.amount1Desired
)
)
);
(_cache.amount0, _cache.amount1) = LiquidityAmounts
.getAmountsForLiquidity(
sqrtPriceX96,
sqrtRatioAX96,
sqrtRatioBX96,
_cache.liquidity
);
//Determine Trade Direction
bool _zeroForOne =
_cache.amount0Desired > _cache.amount0 ? true : false;
//Determine Amount to swap
uint256 _amountSpecified =
_zeroForOne
? (_cache.amount0Desired.sub(_cache.amount0))
: (_cache.amount1Desired.sub(_cache.amount1));
if (_amountSpecified > 0) {
//Determine Token to swap
address _inputToken = _zeroForOne ? _pool.token0() : _pool.token1();
IERC20(_inputToken).safeApprove(address(router), 0);
IERC20(_inputToken).safeApprove(address(router), _amountSpecified);
//Swap the token imbalanced
router.exactInputSingle(
ISwapRouter.ExactInputSingleParams({
tokenIn: _inputToken,
tokenOut: _zeroForOne ? _pool.token1() : _pool.token0(),
fee: _pool.fee(),
recipient: address(this),
amountIn: _amountSpecified,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0
})
);
}
_amount0 = IERC20(_pool.token0()).balanceOf(address(this));
_amount1 = IERC20(_pool.token1()).balanceOf(address(this));
}
function getPool(
address _token0,
address _token1,
uint24 _fee
) public view returns (IUniswapV3Pool) {
return
IUniswapV3Pool(
PoolAddress.computeAddress(
uniswapV3Factory,
PoolAddress.getPoolKey(
address(_token0),
address(_token1),
_fee
)
)
);
}
// TWAP time interval to observe price range.
function setTwapTime(uint256 _tokenId, uint24 _twapTime) external {
positionInfo memory pos = positions[_tokenId];
require(pos.owner == msg.sender, "Not Owner");
positions[_tokenId] = positionInfo(
pos.pool,
pos.tick_lower,
pos.tick_upper,
_twapTime,
pos.tickRangeMultiplier,
pos.owner
);
}
// Used to establish how many multiples of the observed price range we want to use as our target.
function setTickRangeMultiplier(
uint256 _tokenId,
int24 _tickRangeMultiplier
) external {
positionInfo memory pos = positions[_tokenId];
require(pos.owner == msg.sender, "Not Owner");
positions[_tokenId] = positionInfo(
pos.pool,
pos.tick_lower,
pos.tick_upper,
pos.twapTime,
_tickRangeMultiplier,
pos.owner
);
}
// This function must be called to get a token ID for deposit().
struct positionParameters {
address _token0;
address _token1;
uint256 _amount0;
uint256 _amount1;
uint24 _fee;
uint24 _twapTime;
int24 _tickRangeMultiplier;
bool _balance;
}
function newPosition(positionParameters calldata _params)
external
returns (
uint256 _tokenId,
uint256 _refund0,
uint256 _refund1
)
{
require(_params._token0 != address(0), "Invalid Token0");
require(_params._token1 != address(0), "Invalid Token1");
require(_params._fee > 0, "Invalid Fee");
require(
_params._tickRangeMultiplier > 0,
"Invalid Tick Range Multiplier"
);
if (_params._amount0 > 0)
SafeERC20.safeTransferFrom(
IERC20(_params._token0),
msg.sender,
address(this),
_params._amount0
);
if (_params._amount1 > 0)
SafeERC20.safeTransferFrom(
IERC20(_params._token1),
msg.sender,
address(this),
_params._amount1
);
IUniswapV3Pool pool =
IUniswapV3Pool(
PoolAddress.computeAddress(
uniswapV3Factory,
PoolAddress.getPoolKey(
_params._token0,
_params._token1,
_params._fee
)
)
);
(int24 tickLower, int24 tickUpper) =
determineTicks(
pool,
_params._twapTime,
_params._tickRangeMultiplier
);
uint256 amount0Bal = _params._amount0;
uint256 amount1Bal = _params._amount1;
if (_params._balance) {
(amount0Bal, amount1Bal) = _balanceProportion(
pool,
tickLower,
tickUpper
);
}
IERC20(_params._token0).safeApprove(address(nftManager), uint256(0));
IERC20(_params._token1).safeApprove(address(nftManager), uint256(0));
IERC20(_params._token0).safeApprove(address(nftManager), uint256(-1));
IERC20(_params._token1).safeApprove(address(nftManager), uint256(-1));
(_tokenId, , , ) = nftManager.mint(
IUniswapV3PositionsNFT.MintParams({
token0: _params._token0,
token1: _params._token1,
fee: _params._fee,
tickLower: tickLower,
tickUpper: tickUpper,
amount0Desired: amount0Bal,
amount1Desired: amount1Bal,
amount0Min: 0,
amount1Min: 0,
recipient: address(this),
deadline: block.timestamp + 300
})
);
require(_tokenId > 0, "Invalid token ID");
_refund0 = IERC20(pool.token0()).balanceOf(address(this));
_refund1 = IERC20(pool.token1()).balanceOf(address(this));
if (_refund0 > 0) {
SafeERC20.safeTransfer(IERC20(pool.token0()), msg.sender, _refund0);
}
if (_refund1 > 0) {
SafeERC20.safeTransfer(IERC20(pool.token1()), msg.sender, _refund1);
}
positions[_tokenId] = positionInfo(
pool,
tickLower,
tickUpper,
_params._twapTime,
_params._tickRangeMultiplier,
msg.sender
);
emit InitialDeposited(_tokenId);
emit Deposited(_tokenId, amount0Bal, amount1Bal, _refund0, _refund1);
//return _tokenId;
}
// Deposit funds into uniswap position. Tokens will be balanced regardless of what is deposited.
function deposit(
uint256 _tokenId,
uint256 _amount0,
uint256 _amount1,
bool _balance
) external returns (uint256 _refund0, uint256 _refund1) {
positionInfo memory pos = positions[_tokenId];
require(pos.owner == msg.sender, "Not Owner");
if (_amount0 > 0)
SafeERC20.safeTransferFrom(
IERC20(pos.pool.token0()),
msg.sender,
address(this),
_amount0
);
if (_amount1 > 0)
SafeERC20.safeTransferFrom(
IERC20(pos.pool.token1()),
msg.sender,
address(this),
_amount1
);
uint256 token0Bal = _amount0;
uint256 token1Bal = _amount1;
if (_balance) {
(token0Bal, token1Bal) = _balanceProportion(
pos.pool,
pos.tick_lower,
pos.tick_upper
);
}
//uint256 token0Bal = IERC20(pos.pool.token0()).balanceOf(address(this));
//uint256 token1Bal = IERC20(pos.pool.token1()).balanceOf(address(this));
if (token0Bal > 0 && token1Bal > 0) {
// If the pool is out of range, it might be possible to single deposit.
nftManager.increaseLiquidity(
IUniswapV3PositionsNFT.IncreaseLiquidityParams({
tokenId: _tokenId,
amount0Desired: token0Bal,
amount1Desired: token1Bal,
amount0Min: 0,
amount1Min: 0,
deadline: block.timestamp + 300
})
);
IUniswapV3Pool pool = IUniswapV3Pool(pos.pool);
_refund0 = IERC20(pool.token0()).balanceOf(address(this));
_refund1 = IERC20(pool.token1()).balanceOf(address(this));
if (_refund0 > 0) {
SafeERC20.safeTransfer(
IERC20(pool.token0()),
msg.sender,
_refund0
);
}
if (_refund1 > 0) {
SafeERC20.safeTransfer(
IERC20(pool.token1()),
msg.sender,
_refund1
);
}
emit Deposited(_tokenId, token0Bal, token1Bal, _refund0, _refund1);
}
}
/*
function _deposit(uint256 _tokenId) internal returns (uint256 _refund0, uint256 _refund1) {
}
*/
// This should destroy old position NFT and create a new one with a new range.
function rebalance(uint256 _currentId) external returns (uint256 _tokenId) {
positionInfo memory pos = positions[_currentId];
require(pos.owner == msg.sender, "Not Owner");
_destroyPosition(_currentId);
(int24 tickLower, int24 tickUpper) =
determineTicks(pos.pool, pos.twapTime, pos.tickRangeMultiplier);
(uint256 amount0Desired, uint256 amount1Desired) =
_balanceProportion(pos.pool, tickLower, tickUpper);
(_tokenId, , , ) = nftManager.mint(
IUniswapV3PositionsNFT.MintParams({
token0: pos.pool.token0(),
token1: pos.pool.token1(),
fee: pos.pool.fee(),
tickLower: tickLower,
tickUpper: tickUpper,
amount0Desired: amount0Desired,
amount1Desired: amount1Desired,
amount0Min: 0,
amount1Min: 0,
recipient: address(this),
deadline: block.timestamp + 300
})
);
require(_tokenId > 0, "Invalid token ID");
//Record updated information.
delete positions[_currentId];
positions[_tokenId] = positionInfo(
pos.pool,
tickLower,
tickUpper,
pos.twapTime,
pos.tickRangeMultiplier,
pos.owner
);
emit Rebalanced(_tokenId, tickLower, tickUpper);
}
// Position owner may withdraw all funds from any position at any time. Position will not be destroyed.
function withdraw(uint256 _tokenId, uint128 _liquidity) external {
positionInfo memory pos = positions[_tokenId];
require(pos.owner == msg.sender, "Not Owner");
_withdraw(_tokenId, _liquidity);
_sweepTokens(_tokenId);
emit Withdraw(_tokenId, _liquidity);
}
function _withdraw(uint256 _tokenId, uint128 _liquidity) internal {
(uint256 liqAmt0, uint256 liqAmt1) =
nftManager.decreaseLiquidity(
IUniswapV3PositionsNFT.DecreaseLiquidityParams({
tokenId: _tokenId,
liquidity: _liquidity,
amount0Min: 0,
amount1Min: 0,
deadline: block.timestamp + 300
})
);
// This has to be done after DecreaseLiquidity to collect the tokens we
// decreased and the fees at the same time.
nftManager.collect(
IUniswapV3PositionsNFT.CollectParams({
tokenId: _tokenId,
recipient: address(this),
amount0Max: type(uint128).max,
amount1Max: type(uint128).max
})
);
positionInfo memory pos = positions[_tokenId];
nftManager.sweepToken(pos.pool.token0(), 0, address(this));
nftManager.sweepToken(pos.pool.token1(), 0, address(this));
}
function destroyPosition(uint256 _tokenId) public {
positionInfo memory pos = positions[_tokenId];
require(pos.owner == msg.sender, "Not Owner");
_destroyPosition(_tokenId);
_sweepTokens(_tokenId);
}
// Send user the contract balance
function _sweepTokens(uint256 _tokenId) internal {
positionInfo memory pos = positions[_tokenId];
IERC20 token0 = IERC20(pos.pool.token0());
IERC20 token1 = IERC20(pos.pool.token1());
if (token0.balanceOf(address(this)) > 0) {
token0.safeTransfer(pos.owner, token0.balanceOf(address(this)));
}
if (token1.balanceOf(address(this)) > 0) {
token1.safeTransfer(pos.owner, token1.balanceOf(address(this)));
}
}
function _destroyPosition(uint256 _tokenId) internal {
(, , , , , , , uint128 liquidity, , , , ) =
nftManager.positions(_tokenId);
_withdraw(_tokenId, liquidity);
nftManager.burn(_tokenId);
emit Destroy(_tokenId, liquidity);
}
function collectPositionFees(uint256 _tokenId) public {
positionInfo memory pos = positions[_tokenId];
require(pos.owner == msg.sender, "Not Owner");
nftManager.collect(
IUniswapV3PositionsNFT.CollectParams({
tokenId: _tokenId,
recipient: pos.owner,
amount0Max: type(uint128).max,
amount1Max: type(uint128).max
})
);
}
// For emergencies only! This will brick the strategies using the position!
function sweepNFT(address _to, uint256 _tokenId) external {
positionInfo memory pos = positions[_tokenId];
require(pos.owner == msg.sender || admin == msg.sender, "Not Owner");
nft.safeTransferFrom(address(this), _to, _tokenId);
}
// This has been put into the contract in the unlikely event of a breaking change in uniswap.
// To be used if sweepNFT doesnt do the trick.
function exec(address _target, bytes memory _data) external {
require(admin == msg.sender, "Not Administrator");
// Make the function call
(bool success, bytes memory result) = _target.call(_data);
// success is false if the call reverts, true otherwise
require(success, "Call failed");
// result contains whatever has returned the function
emit ExecutionResult(success, result);
}
function changeAdmin(address _admin) external {
require(admin == msg.sender, "Not Administrator");
newAdmin = _admin;
}
function acceptAdmin() external {
require(newAdmin == msg.sender, "Not new Administrator");
admin = newAdmin;
newAdmin = address(0);
}
}