-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseStrategyRedux.sol
532 lines (406 loc) · 14.9 KB
/
BaseStrategyRedux.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
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.0 <0.7.0;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
struct StrategyParams {
uint256 performanceFee;
uint256 activation;
uint256 debtRatio;
uint256 minDebtPerHarvest;
uint256 maxDebtPerHarvest;
uint256 lastReport;
uint256 totalDebt;
uint256 totalGain;
uint256 totalLoss;
}
interface VaultAPI is IERC20 {
function name() external view returns (string calldata);
function symbol() external view returns (string calldata);
function decimals() external view returns (uint256);
function apiVersion() external pure returns (string memory);
function permit(
address owner,
address spender,
uint256 amount,
uint256 expiry,
bytes calldata signature
) external returns (bool);
function deposit() external returns (uint256);
function deposit(uint256 amount) external returns (uint256);
function deposit(uint256 amount, address recipient)
external
returns (uint256);
function withdraw() external returns (uint256);
function withdraw(uint256 maxShares) external returns (uint256);
function withdraw(uint256 maxShares, address recipient)
external
returns (uint256);
function token() external view returns (address);
function strategies(address _strategy)
external
view
returns (StrategyParams memory);
function pricePerShare() external view returns (uint256);
function totalAssets() external view returns (uint256);
function depositLimit() external view returns (uint256);
function maxAvailableShares() external view returns (uint256);
function creditAvailable() external view returns (uint256);
function debtOutstanding() external view returns (uint256);
function expectedReturn() external view returns (uint256);
function report(
uint256 _gain,
uint256 _loss,
uint256 _debtPayment
) external returns (uint256);
function revokeStrategy() external;
function governance() external view returns (address);
function management() external view returns (address);
function guardian() external view returns (address);
}
interface StrategyAPI {
function name() external view returns (string memory);
function vault() external view returns (address);
function want() external view returns (address);
function apiVersion() external pure returns (string memory);
function keeper() external view returns (address);
function isActive() external view returns (bool);
function delegatedAssets() external view returns (uint256);
function estimatedTotalAssets() external view returns (uint256);
function tendTrigger(uint256 callCost) external view returns (bool);
function tend() external;
function harvestTrigger(uint256 callCost) external view returns (bool);
function harvest() external;
event Harvested(
uint256 profit,
uint256 loss,
uint256 debtPayment,
uint256 debtOutstanding
);
}
interface HealthCheck {
function check(
uint256 profit,
uint256 loss,
uint256 debtPayment,
uint256 debtOutstanding,
uint256 totalDebt
) external view returns (bool);
}
/**
* @title BaseStrategyRedux
* @author HeroBorg
* @notice
* This is an exact copy of BaseStrategy 0.4.3 from yearn, but i removed `ethToWant()` because we always override it and make it return 0
* Also, I've removed some require error messages
* This is a solution until we completely revamp the corestrat
*/
abstract contract BaseStrategyRedux {
using SafeMath for uint256;
using SafeERC20 for IERC20;
string public metadataURI;
bool public doHealthCheck;
address public healthCheck;
function apiVersion() public pure returns (string memory) {
return "0.4.3";
}
function name() external view virtual returns (string memory);
function delegatedAssets() external view virtual returns (uint256) {
return 0;
}
VaultAPI public vault;
address public strategist;
address public rewards;
address public keeper;
IERC20 public want;
event Harvested(
uint256 profit,
uint256 loss,
uint256 debtPayment,
uint256 debtOutstanding
);
event UpdatedStrategist(address newStrategist);
event UpdatedKeeper(address newKeeper);
event UpdatedRewards(address rewards);
event UpdatedMinReportDelay(uint256 delay);
event UpdatedMaxReportDelay(uint256 delay);
event UpdatedProfitFactor(uint256 profitFactor);
event UpdatedDebtThreshold(uint256 debtThreshold);
event EmergencyExitEnabled();
event UpdatedMetadataURI(string metadataURI);
uint256 public minReportDelay;
uint256 public maxReportDelay;
uint256 public profitFactor;
uint256 public debtThreshold;
bool public emergencyExit;
modifier onlyAuthorized() {
require(msg.sender == strategist || msg.sender == governance());
_;
}
modifier onlyEmergencyAuthorized() {
require(
msg.sender == strategist ||
msg.sender == governance() ||
msg.sender == vault.guardian() ||
msg.sender == vault.management()
);
_;
}
modifier onlyStrategist() {
require(msg.sender == strategist);
_;
}
modifier onlyGovernance() {
require(msg.sender == governance());
_;
}
modifier onlyKeepers() {
require(
msg.sender == keeper ||
msg.sender == strategist ||
msg.sender == governance() ||
msg.sender == vault.guardian() ||
msg.sender == vault.management()
);
_;
}
modifier onlyVaultManagers() {
require(msg.sender == vault.management() || msg.sender == governance());
_;
}
constructor(address _vault) public {
_initialize(_vault, msg.sender, msg.sender, msg.sender);
}
function _initialize(
address _vault,
address _strategist,
address _rewards,
address _keeper
) internal {
//Already initialized
require(address(want) == address(0));
vault = VaultAPI(_vault);
want = IERC20(vault.token());
want.safeApprove(_vault, uint256(-1));
strategist = _strategist;
rewards = _rewards;
keeper = _keeper;
minReportDelay = 0;
maxReportDelay = 86400;
profitFactor = 100;
debtThreshold = 0;
vault.approve(rewards, uint256(-1));
}
function setHealthCheck(address _healthCheck) external onlyVaultManagers {
healthCheck = _healthCheck;
}
function setDoHealthCheck(bool _doHealthCheck) external onlyVaultManagers {
doHealthCheck = _doHealthCheck;
}
function setStrategist(address _strategist) external onlyAuthorized {
require(_strategist != address(0));
strategist = _strategist;
emit UpdatedStrategist(_strategist);
}
function setKeeper(address _keeper) external onlyAuthorized {
require(_keeper != address(0));
keeper = _keeper;
emit UpdatedKeeper(_keeper);
}
function setRewards(address _rewards) external onlyStrategist {
require(_rewards != address(0));
vault.approve(rewards, 0);
rewards = _rewards;
vault.approve(rewards, uint256(-1));
emit UpdatedRewards(_rewards);
}
function setMinReportDelay(uint256 _delay) external onlyAuthorized {
minReportDelay = _delay;
emit UpdatedMinReportDelay(_delay);
}
function setMaxReportDelay(uint256 _delay) external onlyAuthorized {
maxReportDelay = _delay;
emit UpdatedMaxReportDelay(_delay);
}
function setProfitFactor(uint256 _profitFactor) external onlyAuthorized {
profitFactor = _profitFactor;
emit UpdatedProfitFactor(_profitFactor);
}
function setDebtThreshold(uint256 _debtThreshold) external onlyAuthorized {
debtThreshold = _debtThreshold;
emit UpdatedDebtThreshold(_debtThreshold);
}
function setMetadataURI(string calldata _metadataURI)
external
onlyAuthorized
{
metadataURI = _metadataURI;
emit UpdatedMetadataURI(_metadataURI);
}
function governance() internal view returns (address) {
return vault.governance();
}
// Removing ethToWant() because we always override it and put it to 0
//function ethToWant(uint256 _amtInWei) public view virtual returns (uint256);
function estimatedTotalAssets() public view virtual returns (uint256);
function isActive() public view returns (bool) {
return
vault.strategies(address(this)).debtRatio > 0 ||
estimatedTotalAssets() > 0;
}
function prepareReturn(uint256 _debtOutstanding)
internal
virtual
returns (
uint256 _profit,
uint256 _loss,
uint256 _debtPayment
);
function adjustPosition(uint256 _debtOutstanding) internal virtual;
function liquidatePosition(uint256 _amountNeeded)
internal
virtual
returns (uint256 _liquidatedAmount, uint256 _loss);
function liquidateAllPositions()
internal
virtual
returns (uint256 _amountFreed);
function tendTrigger(uint256 callCostInWei)
public
view
virtual
returns (bool)
{
return false;
}
function tend() external onlyKeepers {
adjustPosition(vault.debtOutstanding());
}
/// @notice the only change is that we set callCost to 0. The behaviour is exactly the same as before, since ethToWant always returned 0
function harvestTrigger(uint256 callCostInWei)
public
view
virtual
returns (bool)
{
//OLD VERSION
//uint256 callCost = ethToWant(callCostInWei);
//NEW VERSION
uint256 callCost = 0;
StrategyParams memory params = vault.strategies(address(this));
if (params.activation == 0) return false;
if (block.timestamp.sub(params.lastReport) < minReportDelay)
return false;
if (block.timestamp.sub(params.lastReport) >= maxReportDelay)
return true;
uint256 outstanding = vault.debtOutstanding();
if (outstanding > debtThreshold) return true;
uint256 total = estimatedTotalAssets();
if (total.add(debtThreshold) < params.totalDebt) return true;
uint256 profit = 0;
if (total > params.totalDebt) profit = total.sub(params.totalDebt);
uint256 credit = vault.creditAvailable();
return (profitFactor.mul(callCost) < credit.add(profit));
}
function harvest() external onlyKeepers {
uint256 profit = 0;
uint256 loss = 0;
uint256 debtOutstanding = vault.debtOutstanding();
uint256 debtPayment = 0;
if (emergencyExit) {
uint256 amountFreed = liquidateAllPositions();
if (amountFreed < debtOutstanding) {
loss = debtOutstanding.sub(amountFreed);
} else if (amountFreed > debtOutstanding) {
profit = amountFreed.sub(debtOutstanding);
}
debtPayment = debtOutstanding.sub(loss);
} else {
(profit, loss, debtPayment) = prepareReturn(debtOutstanding);
}
uint256 totalDebt = vault.strategies(address(this)).totalDebt;
debtOutstanding = vault.report(profit, loss, debtPayment);
adjustPosition(debtOutstanding);
if (doHealthCheck && healthCheck != address(0)) {
require(
HealthCheck(healthCheck).check(
profit,
loss,
debtPayment,
debtOutstanding,
totalDebt
),
"!h"
);
} else {
doHealthCheck = true;
}
emit Harvested(profit, loss, debtPayment, debtOutstanding);
}
function withdraw(uint256 _amountNeeded) external returns (uint256 _loss) {
require(msg.sender == address(vault));
uint256 amountFreed;
(amountFreed, _loss) = liquidatePosition(_amountNeeded);
want.safeTransfer(msg.sender, amountFreed);
}
function prepareMigration(address _newStrategy) internal virtual;
function migrate(address _newStrategy) external {
require(msg.sender == address(vault));
require(BaseStrategyRedux(_newStrategy).vault() == vault);
prepareMigration(_newStrategy);
want.safeTransfer(_newStrategy, want.balanceOf(address(this)));
}
function setEmergencyExit() external onlyEmergencyAuthorized {
emergencyExit = true;
vault.revokeStrategy();
emit EmergencyExitEnabled();
}
function protectedTokens() internal view virtual returns (address[] memory);
function sweep(address _token) external onlyGovernance {
require(_token != address(want));
require(_token != address(vault));
address[] memory _protectedTokens = protectedTokens();
for (uint256 i; i < _protectedTokens.length; i++)
require(_token != _protectedTokens[i]);
IERC20(_token).safeTransfer(
governance(),
IERC20(_token).balanceOf(address(this))
);
}
}
// NOTE: we do not use it, if you need it, just use the one from yearn
// abstract contract BaseStrategyInitializable is BaseStrategy {
// bool public isOriginal = true;
// event Cloned(address indexed clone);
// constructor(address _vault) public BaseStrategy(_vault) {}
// function initialize(
// address _vault,
// address _strategist,
// address _rewards,
// address _keeper
// ) external virtual {
// _initialize(_vault, _strategist, _rewards, _keeper);
// }
// function clone(address _vault) external returns (address) {
// require(isOriginal, "!clone");
// return this.clone(_vault, msg.sender, msg.sender, msg.sender);
// }
// function clone(
// address _vault,
// address _strategist,
// address _rewards,
// address _keeper
// ) external returns (address newStrategy) {
// bytes20 addressBytes = bytes20(address(this));
// assembly {
// 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)
// }
// BaseStrategyInitializable(newStrategy).initialize(_vault, _strategist, _rewards, _keeper);
// emit Cloned(newStrategy);
// }
// }