This repository has been archived by the owner on Nov 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 211
/
StrategyUSDT3pool.sol
293 lines (239 loc) · 11.1 KB
/
StrategyUSDT3pool.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
pragma solidity ^0.5.17;
import "@openzeppelinV2/contracts/token/ERC20/IERC20.sol";
import "@openzeppelinV2/contracts/math/SafeMath.sol";
import "@openzeppelinV2/contracts/utils/Address.sol";
import "@openzeppelinV2/contracts/token/ERC20/SafeERC20.sol";
import "../../interfaces/yearn/IController.sol";
import "../../interfaces/curve/Curve.sol";
interface yvERC20 {
function deposit(uint256) external;
function withdraw(uint256) external;
function earn() external;
function getPricePerFullShare() external view returns (uint256);
}
/*
A strategy must implement the following calls;
- deposit()
- withdraw(address) must exclude any tokens used in the yield - Controller role - withdraw should return to Controller
- withdraw(uint) - Controller | Vault role - withdraw should always return to vault
- withdrawAll() - Controller | Vault role - withdraw should always return to vault
- balanceOf()
Where possible, strategies must remain as immutable as possible, instead of updating variables, we update the contract by linking it in the controller
*/
contract StrategyUSDT3pool {
using SafeERC20 for IERC20;
using Address for address;
using SafeMath for uint256;
address public constant want = address(0xdAC17F958D2ee523a2206206994597C13D831ec7);
address public constant _3pool = address(0xbEbc44782C7dB0a1A60Cb6fe97d0b483032FF1C7);
address public constant _3crv = address(0x6c3F90f043a72FA612cbac8115EE7e52BDe6E490);
address public constant y3crv = address(0x9cA85572E6A3EbF24dEDd195623F188735A5179f);
address public governance;
address public controller;
address public strategist;
uint256 public constant DENOMINATOR = 10000;
uint256 public treasuryFee = 500;
uint256 public withdrawalFee = 0;
uint256 public strategistReward = 50;
uint256 public threshold = 6000;
uint256 public slip = 100;
uint256 public tank = 0;
uint256 public p = 0;
event Threshold(address indexed strategy);
modifier isAuthorized() {
require(msg.sender == governance || msg.sender == strategist || msg.sender == controller || msg.sender == address(this), "!authorized");
_;
}
constructor(address _controller) public {
governance = msg.sender;
strategist = msg.sender;
controller = _controller;
}
function getName() external pure returns (string memory) {
return "StrategyUSDT3pool";
}
function deposit() public isAuthorized {
rebalance();
uint256 _want = (IERC20(want).balanceOf(address(this))).sub(tank);
if (_want > 0) {
IERC20(want).safeApprove(_3pool, 0);
IERC20(want).safeApprove(_3pool, _want);
uint256 v = _want.mul(1e30).div(ICurveFi(_3pool).get_virtual_price());
ICurveFi(_3pool).add_liquidity([0, 0, _want], v.mul(DENOMINATOR.sub(slip)).div(DENOMINATOR));
}
uint256 _bal = IERC20(_3crv).balanceOf(address(this));
if (_bal > 0) {
IERC20(_3crv).safeApprove(y3crv, 0);
IERC20(_3crv).safeApprove(y3crv, _bal);
yvERC20(y3crv).deposit(_bal);
}
}
// Controller only function for creating additional rewards from dust
function withdraw(IERC20 _asset) external returns (uint256 balance) {
require(msg.sender == controller, "!controller");
require(want != address(_asset), "want");
require(_3crv != address(_asset), "3crv");
require(y3crv != address(_asset), "y3crv");
balance = _asset.balanceOf(address(this));
_asset.safeTransfer(controller, balance);
}
// Withdraw partial funds, normally used with a vault withdrawal
function withdraw(uint256 _amount) external {
require(msg.sender == controller, "!controller");
rebalance();
uint256 _balance = IERC20(want).balanceOf(address(this));
if (_balance < _amount) {
_amount = _withdrawSome(_amount.sub(_balance));
_amount = _amount.add(_balance);
tank = 0;
} else {
if (tank >= _amount) tank = tank.sub(_amount);
else tank = 0;
}
address _vault = IController(controller).vaults(address(want));
require(_vault != address(0), "!vault"); // additional protection so we don't burn the funds
uint256 _fee = _amount.mul(withdrawalFee).div(DENOMINATOR);
IERC20(want).safeTransfer(IController(controller).rewards(), _fee);
IERC20(want).safeTransfer(_vault, _amount.sub(_fee));
}
function _withdrawSome(uint256 _amount) internal returns (uint256) {
uint256 _amnt = _amount.mul(1e30).div(ICurveFi(_3pool).get_virtual_price());
uint256 _amt = _amnt.mul(1e18).div(yvERC20(y3crv).getPricePerFullShare());
uint256 _before = IERC20(_3crv).balanceOf(address(this));
yvERC20(y3crv).withdraw(_amt);
uint256 _after = IERC20(_3crv).balanceOf(address(this));
return _withdrawOne(_after.sub(_before));
}
function _withdrawOne(uint256 _amnt) internal returns (uint256) {
uint256 _before = IERC20(want).balanceOf(address(this));
IERC20(_3crv).safeApprove(_3pool, 0);
IERC20(_3crv).safeApprove(_3pool, _amnt);
ICurveFi(_3pool).remove_liquidity_one_coin(_amnt, 2, _amnt.mul(DENOMINATOR.sub(slip)).div(DENOMINATOR).div(1e12));
uint256 _after = IERC20(want).balanceOf(address(this));
return _after.sub(_before);
}
// Withdraw all funds, normally used when migrating strategies
function withdrawAll() external returns (uint256 balance) {
require(msg.sender == controller, "!controller");
_withdrawAll();
balance = IERC20(want).balanceOf(address(this));
address _vault = IController(controller).vaults(address(want));
require(_vault != address(0), "!vault"); // additional protection so we don't burn the funds
IERC20(want).safeTransfer(_vault, balance);
}
function _withdrawAll() internal {
uint256 _y3crv = IERC20(y3crv).balanceOf(address(this));
if (_y3crv > 0) {
yvERC20(y3crv).withdraw(_y3crv);
_withdrawOne(IERC20(_3crv).balanceOf(address(this)));
}
}
function balanceOfWant() public view returns (uint256) {
return IERC20(want).balanceOf(address(this));
}
function balanceOf3CRV() public view returns (uint256) {
return IERC20(_3crv).balanceOf(address(this));
}
function balanceOf3CRVinWant() public view returns (uint256) {
return balanceOf3CRV().mul(ICurveFi(_3pool).get_virtual_price()).div(1e30);
}
function balanceOfy3CRV() public view returns (uint256) {
return IERC20(y3crv).balanceOf(address(this));
}
function balanceOfy3CRVin3CRV() public view returns (uint256) {
return balanceOfy3CRV().mul(yvERC20(y3crv).getPricePerFullShare()).div(1e18);
}
function balanceOfy3CRVinWant() public view returns (uint256) {
return balanceOfy3CRVin3CRV().mul(ICurveFi(_3pool).get_virtual_price()).div(1e30);
}
function balanceOf() public view returns (uint256) {
return balanceOfWant().add(balanceOfy3CRVinWant());
}
function migrate(address _strategy) external {
require(msg.sender == governance, "!governance");
require(IController(controller).approvedStrategies(want, _strategy), "!stategyAllowed");
IERC20(y3crv).safeTransfer(_strategy, IERC20(y3crv).balanceOf(address(this)));
IERC20(_3crv).safeTransfer(_strategy, IERC20(_3crv).balanceOf(address(this)));
IERC20(want).safeTransfer(_strategy, IERC20(want).balanceOf(address(this)));
}
function forceD(uint256 _amount) external isAuthorized {
IERC20(want).safeApprove(_3pool, 0);
IERC20(want).safeApprove(_3pool, _amount);
uint256 v = _amount.mul(1e30).div(ICurveFi(_3pool).get_virtual_price());
ICurveFi(_3pool).add_liquidity([0, 0, _amount], v.mul(DENOMINATOR.sub(slip)).div(DENOMINATOR));
if (_amount < tank) tank = tank.sub(_amount);
else tank = 0;
uint256 _bal = IERC20(_3crv).balanceOf(address(this));
IERC20(_3crv).safeApprove(y3crv, 0);
IERC20(_3crv).safeApprove(y3crv, _bal);
yvERC20(y3crv).deposit(_bal);
}
function forceW(uint256 _amt) external isAuthorized {
uint256 _before = IERC20(_3crv).balanceOf(address(this));
yvERC20(y3crv).withdraw(_amt);
uint256 _after = IERC20(_3crv).balanceOf(address(this));
_amt = _after.sub(_before);
IERC20(_3crv).safeApprove(_3pool, 0);
IERC20(_3crv).safeApprove(_3pool, _amt);
_before = IERC20(want).balanceOf(address(this));
ICurveFi(_3pool).remove_liquidity_one_coin(_amt, 2, _amt.mul(DENOMINATOR.sub(slip)).div(DENOMINATOR).div(1e12));
_after = IERC20(want).balanceOf(address(this));
tank = tank.add(_after.sub(_before));
}
function drip() public isAuthorized {
uint256 _p = yvERC20(y3crv).getPricePerFullShare();
_p = _p.mul(ICurveFi(_3pool).get_virtual_price()).div(1e18);
require(_p >= p, "backward");
uint256 _r = (_p.sub(p)).mul(balanceOfy3CRV()).div(1e18);
uint256 _s = _r.mul(strategistReward).div(DENOMINATOR);
IERC20(y3crv).safeTransfer(strategist, _s.mul(1e18).div(_p));
uint256 _t = _r.mul(treasuryFee).div(DENOMINATOR);
IERC20(y3crv).safeTransfer(IController(controller).rewards(), _t.mul(1e18).div(_p));
p = _p;
}
function tick() public view returns (uint256 _t, uint256 _c) {
_t = ICurveFi(_3pool).balances(2).mul(threshold).div(DENOMINATOR);
_c = balanceOfy3CRVinWant();
}
function rebalance() public isAuthorized {
drip();
(uint256 _t, uint256 _c) = tick();
if (_c > _t) {
_withdrawSome(_c.sub(_t));
tank = IERC20(want).balanceOf(address(this));
emit Threshold(address(this));
}
}
function setGovernance(address _governance) external {
require(msg.sender == governance, "!governance");
governance = _governance;
}
function setIController(address _controller) external {
require(msg.sender == governance, "!governance");
controller = _controller;
}
function setStrategist(address _strategist) external {
require(msg.sender == governance || msg.sender == strategist, "!gs");
strategist = _strategist;
}
function setWithdrawalFee(uint256 _withdrawalFee) external {
require(msg.sender == governance, "!governance");
withdrawalFee = _withdrawalFee;
}
function setTreasuryFee(uint256 _treasuryFee) external {
require(msg.sender == governance, "!governance");
treasuryFee = _treasuryFee;
}
function setStrategistReward(uint256 _strategistReward) external {
require(msg.sender == governance, "!governance");
strategistReward = _strategistReward;
}
function setThreshold(uint256 _threshold) external {
require(msg.sender == strategist || msg.sender == governance, "!sg");
threshold = _threshold;
}
function setSlip(uint256 _slip) external {
require(msg.sender == strategist || msg.sender == governance, "!sg");
slip = _slip;
}
}