generated from yearn/brownie-strategy-mix
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Strategy.sol
346 lines (285 loc) · 9.8 KB
/
Strategy.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
// SPDX-License-Identifier: AGPL-3.0
// Feel free to change the license, but this is what we use
// Feel free to change this version of Solidity. We support >=0.6.0 <0.7.0;
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
interface ChefLike {
function deposit(uint256 _pid, uint256 _amount) external;
function withdraw(uint256 _pid, uint256 _amount) external;
function emergencyWithdraw(uint256 _pid) external;
function poolInfo(uint256 _pid)
external
view
returns (
address,
uint256,
uint256,
uint256
);
function userInfo(uint256 _pid, address user)
external
view
returns (uint256, uint256);
}
// These are the core Yearn libraries
import "@yearnvaults/contracts/BaseStrategy.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/math/Math.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "./interfaces/UniswapInterfaces/IUniswapV2Router02.sol";
// Import interfaces for many popular DeFi projects, or add your own!
//import "../interfaces/<protocol>/<Interface>.sol";
contract Strategy is BaseStrategy {
using SafeERC20 for IERC20;
using Address for address;
using SafeMath for uint256;
address public masterchef;
address public reward;
address private constant uniswapRouter =
address(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
address private constant sushiswapRouter =
address(0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F);
address private constant weth =
address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
address public router;
uint256 public pid;
address[] public path;
event Cloned(address indexed clone);
constructor(
address _vault,
address _masterchef,
address _reward,
address _router,
uint256 _pid
) public BaseStrategy(_vault) {
_initializeStrat(_masterchef, _reward, _router, _pid);
}
function initialize(
address _vault,
address _strategist,
address _rewards,
address _keeper,
address _masterchef,
address _reward,
address _router,
uint256 _pid
) external {
//note: initialise can only be called once. in _initialize in BaseStrategy we have: require(address(want) == address(0), "Strategy already initialized");
_initialize(_vault, _strategist, _rewards, _keeper);
_initializeStrat(_masterchef, _reward, _router, _pid);
}
function _initializeStrat(
address _masterchef,
address _reward,
address _router,
uint256 _pid
) internal {
require(
router == address(0),
"Masterchef Strategy already initialized"
);
require(
_router == uniswapRouter || _router == sushiswapRouter,
"incorrect router"
);
// You can set these parameters on deployment to whatever you want
maxReportDelay = 6300;
profitFactor = 1500;
debtThreshold = 1_000_000 * 1e18;
masterchef = _masterchef;
reward = _reward;
router = _router;
pid = _pid;
(address poolToken, , , ) = ChefLike(masterchef).poolInfo(pid);
require(poolToken == address(want), "wrong pid");
want.safeApprove(_masterchef, uint256(-1));
IERC20(reward).safeApprove(router, uint256(-1));
}
function cloneStrategy(
address _vault,
address _masterchef,
address _reward,
address _router,
uint256 _pid
) external returns (address newStrategy) {
newStrategy = this.cloneStrategy(
_vault,
msg.sender,
msg.sender,
msg.sender,
_masterchef,
_reward,
_router,
_pid
);
}
function cloneStrategy(
address _vault,
address _strategist,
address _rewards,
address _keeper,
address _masterchef,
address _reward,
address _router,
uint256 _pid
) external returns (address newStrategy) {
// Copied from https://github.com/optionality/clone-factory/blob/master/contracts/CloneFactory.sol
bytes20 addressBytes = bytes20(address(this));
assembly {
// EIP-1167 bytecode
let clone_code := mload(0x40)
mstore(
clone_code,
0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000
)
mstore(add(clone_code, 0x14), addressBytes)
mstore(
add(clone_code, 0x28),
0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000
)
newStrategy := create(0, clone_code, 0x37)
}
Strategy(newStrategy).initialize(
_vault,
_strategist,
_rewards,
_keeper,
_masterchef,
_reward,
_router,
_pid
);
emit Cloned(newStrategy);
}
function setRouter(address _router)
public
onlyAuthorized
{
require(
_router == uniswapRouter || _router == sushiswapRouter,
"incorrect router"
);
router = _router;
IERC20(reward).safeApprove(router, 0);
IERC20(reward).safeApprove(router, uint256(-1));
}
function setPath(address[] calldata _path)
public
onlyGovernance
{
path = _path;
}
// ******** OVERRIDE THESE METHODS FROM BASE CONTRACT ************
function name() external view override returns (string memory) {
return "StrategyMasterchefGeneric";
}
function estimatedTotalAssets() public view override returns (uint256) {
(uint256 deposited, ) =
ChefLike(masterchef).userInfo(pid, address(this));
return want.balanceOf(address(this)).add(deposited);
}
function prepareReturn(uint256 _debtOutstanding)
internal
override
returns (
uint256 _profit,
uint256 _loss,
uint256 _debtPayment
)
{
ChefLike(masterchef).deposit(pid, 0);
_sell();
uint256 assets = estimatedTotalAssets();
uint256 wantBal = want.balanceOf(address(this));
uint256 debt = vault.strategies(address(this)).totalDebt;
if (assets > debt) {
_debtPayment = _debtOutstanding;
_profit = assets - debt;
uint256 amountToFree = _profit.add(_debtPayment);
if (amountToFree > 0 && wantBal < amountToFree) {
liquidatePosition(amountToFree);
uint256 newLoose = want.balanceOf(address(this));
//if we dont have enough money adjust _debtOutstanding and only change profit if needed
if (newLoose < amountToFree) {
if (_profit > newLoose) {
_profit = newLoose;
_debtPayment = 0;
} else {
_debtPayment = Math.min(
newLoose - _profit,
_debtPayment
);
}
}
}
} else {
//serious loss should never happen but if it does lets record it accurately
_loss = debt - assets;
}
}
function adjustPosition(uint256 _debtOutstanding) internal override {
if (emergencyExit) {
return;
}
uint256 wantBalance = want.balanceOf(address(this));
ChefLike(masterchef).deposit(pid, wantBalance);
}
function liquidatePosition(uint256 _amountNeeded)
internal
override
returns (uint256 _liquidatedAmount, uint256 _loss)
{
uint256 totalAssets = want.balanceOf(address(this));
if (_amountNeeded > totalAssets) {
uint256 amountToFree = _amountNeeded.sub(totalAssets);
(uint256 deposited, ) =
ChefLike(masterchef).userInfo(pid, address(this));
if (deposited < amountToFree) {
amountToFree = deposited;
}
if (deposited > 0) {
ChefLike(masterchef).withdraw(pid, amountToFree);
}
_liquidatedAmount = want.balanceOf(address(this));
} else {
_liquidatedAmount = _amountNeeded;
}
}
// NOTE: Can override `tendTrigger` and `harvestTrigger` if necessary
function prepareMigration(address _newStrategy) internal override {
liquidatePosition(uint256(-1)); //withdraw all. does not matter if we ask for too much
_sell();
}
function emergencyWithdrawal(uint256 _pid) external onlyGovernance{
ChefLike(masterchef).emergencyWithdraw(_pid);
}
//sell all function
function _sell() internal {
uint256 rewardBal = IERC20(reward).balanceOf(address(this));
if( rewardBal == 0){
return;
}
if(path.length == 0){
address[] memory tpath;
if(address(want) != weth){
tpath = new address[](3);
tpath[2] = address(want);
}else{
tpath = new address[](2);
}
tpath[0] = address(reward);
tpath[1] = weth;
IUniswapV2Router02(router).swapExactTokensForTokens(rewardBal, uint256(0), tpath, address(this), now);
}else{
IUniswapV2Router02(router).swapExactTokensForTokens(rewardBal, uint256(0), path, address(this), now);
}
}
function protectedTokens()
internal
view
override
returns (address[] memory)
{}
}