generated from yearn/brownie-strategy-mix
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathStrategy.sol
511 lines (422 loc) · 16.5 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
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
// SPDX-License-Identifier: MIT
pragma experimental ABIEncoderV2;
pragma solidity ^0.8.12;
import "@openzeppelin/contracts/utils/math/Math.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {
BaseStrategy,
StrategyParams
} from "@yearnvaults/contracts/BaseStrategy.sol";
import {IERC20Metadata} from "@yearnvaults/contracts/yToken.sol";
import "./interfaces/Angle/IStableMaster.sol";
import "./interfaces/Angle/IAngleGauge.sol";
import "./interfaces/Yearn/ITradeFactory.sol";
import "./interfaces/Uniswap/IUniV2.sol";
interface IBaseFee {
function isCurrentBaseFeeAcceptable() external view returns (bool);
}
contract Strategy is BaseStrategy {
using SafeERC20 for IERC20;
using Address for address;
event Cloned(address indexed clone);
bool public isOriginal = true;
IERC20 public constant angleToken = IERC20(0x31429d1856aD1377A8A0079410B297e1a9e214c2);
IStableMaster public constant angleStableMaster = IStableMaster(0x5adDc89785D75C86aB939E9e15bfBBb7Fc086A87);
uint256 public constant MAX_BPS = 10000;
// variable for determining how much governance token to hold for voting rights
uint256 public percentKeep;
IERC20 public sanToken;
IAngleGauge public sanTokenGauge;
address public constant treasury = 0x93A62dA5a14C80f265DAbC077fCEE437B1a0Efde; // To change this, migrate
address public constant unirouter = 0xd9e1cE17f2641f24aE83637ab66a2cca9C378B9F; // SushiSwap
address public constant usdt = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
address public constant weth = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address public poolManager;
address public tradeFactory = address(0);
// keeper stuff
uint256 public harvestProfitMin; // minimum size in USD (6 decimals) that we want to harvest
uint256 public harvestProfitMax; // maximum size in USD (6 decimals) that we want to harvest
uint256 public creditThreshold; // amount of credit in underlying tokens that will automatically trigger a harvest
bool internal forceHarvestTriggerOnce; // only set this to true when we want to trigger our keepers to harvest for us
constructor(
address _vault,
address _sanToken,
address _sanTokenGauge,
address _poolManager
) public BaseStrategy(_vault) {
// Constructor should initialize local variables
_initializeStrategy(
_sanToken,
_sanTokenGauge,
_poolManager
);
}
// Cloning & initialization code adapted from https://github.com/yearn/yearn-vaults/blob/43a0673ab89742388369bc0c9d1f321aa7ea73f6/contracts/BaseStrategy.sol#L866
function _initializeStrategy(
address _sanToken,
address _sanTokenGauge,
address _poolManager
) internal {
sanToken = IERC20(_sanToken);
sanTokenGauge = IAngleGauge(_sanTokenGauge);
poolManager = _poolManager;
percentKeep = 1000;
healthCheck = 0xDDCea799fF1699e98EDF118e0629A974Df7DF012;
doHealthCheck = true;
maxReportDelay = 21 days; // 21 days in seconds, if we hit this then harvestTrigger = True
harvestProfitMin = 2_000e6;
harvestProfitMax = 10_000e6;
creditThreshold = 1e6 * 1e18;
IERC20(want).safeApprove(address(angleStableMaster), type(uint256).max);
IERC20(sanToken).safeApprove(_sanTokenGauge, type(uint256).max);
}
function initialize(
address _vault,
address _strategist,
address _rewards,
address _keeper,
address _sanToken,
address _sanTokenGauge,
address _poolManager
) external {
_initialize(_vault, _strategist, _rewards, _keeper);
_initializeStrategy(
_sanToken,
_sanTokenGauge,
_poolManager
);
}
function cloneAngle(
address _vault,
address _strategist,
address _rewards,
address _keeper,
address _sanToken,
address _sanTokenGauge,
address _poolManager
) external returns (address newStrategy) {
require(isOriginal, "!clone");
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,
_sanToken,
_sanTokenGauge,
_poolManager
);
emit Cloned(newStrategy);
}
function name() external view override returns (string memory) {
return
string(
abi.encodePacked(
"StrategyAngle",
IERC20Metadata(address(want)).symbol()
)
);
}
// returns sum of all assets, realized and unrealized
function estimatedTotalAssets() public view override returns (uint256) {
return balanceOfWant() + valueOfStakedSanToken() + valueOfSanToken();
}
function prepareReturn(uint256 _debtOutstanding)
internal
override
returns (
uint256 _profit,
uint256 _loss,
uint256 _debtPayment
)
{
// Run initial profit + loss calculations.
uint256 _totalAssets = estimatedTotalAssets();
uint256 _totalDebt = vault.strategies(address(this)).totalDebt;
if (_totalAssets >= _totalDebt) {
// Implicitly, _profit & _loss are 0 before we change them.
_profit = _totalAssets - _totalDebt;
} else {
_loss = _totalDebt - _totalAssets;
}
// Free up _debtOutstanding + our profit, and make any necessary adjustments to the accounting.
(uint256 _amountFreed, uint256 _liquidationLoss) =
liquidatePosition(_debtOutstanding + _profit);
_loss = _loss + _liquidationLoss;
_debtPayment = Math.min(_debtOutstanding, _amountFreed);
if (_loss > _profit) {
_loss = _loss - _profit;
_profit = 0;
} else {
_profit = _profit - _loss;
_loss = 0;
}
}
// Deposit value & stake
function adjustPosition(uint256 _debtOutstanding) internal override {
if (emergencyExit) {
return;
}
// Claim rewards here so that we can chain tend() -> yswap sell -> harvest() in a single transaction
sanTokenGauge.claim_rewards();
uint256 _tokensAvailable = balanceOfAngleToken();
if (_tokensAvailable > 0) {
uint256 _tokensToGov =
(_tokensAvailable * percentKeep) / MAX_BPS;
if (_tokensToGov > 0) {
angleToken.transfer(treasury, _tokensToGov);
}
}
uint256 _balanceOfWant = balanceOfWant();
// do not invest if we have more debt than want
if (_debtOutstanding > _balanceOfWant) {
return;
}
// Invest the rest of the want
uint256 _wantAvailable = _balanceOfWant - _debtOutstanding;
if (_wantAvailable > 0) {
// deposit for sanToken
depositToStableMaster(_wantAvailable);
}
// Stake any san tokens, whether they originated through the above deposit or some other means (e.g. migration)
uint256 _sanTokenBalance = balanceOfSanToken();
if (_sanTokenBalance > 0) {
sanTokenGauge.deposit(_sanTokenBalance);
}
}
function liquidateAllPositions()
internal
override
returns (uint256 _amountFreed)
{
(_amountFreed, ) = liquidatePosition(estimatedTotalAssets());
}
function liquidatePosition(uint256 _amountNeeded)
internal
override
returns (uint256 _liquidatedAmount, uint256 _loss)
{
// NOTE: Maintain invariant `_liquidatedAmount + _loss <= _amountNeeded`
_amountNeeded = Math.min(_amountNeeded, estimatedTotalAssets()); // This makes it safe to request to liquidate more than we have
uint256 _balanceOfWant = balanceOfWant();
if (_balanceOfWant < _amountNeeded) {
// We need to withdraw to get back more want
_withdrawSome(_amountNeeded - _balanceOfWant);
// reload balance of want after side effect
_balanceOfWant = balanceOfWant();
}
if (_balanceOfWant >= _amountNeeded) {
_liquidatedAmount = _amountNeeded;
} else {
_liquidatedAmount = _balanceOfWant;
_loss = _amountNeeded - _balanceOfWant;
}
}
// withdraw some want from Angle
function _withdrawSome(uint256 _amount) internal {
uint256 _amountInSanToken = wantToSanToken(_amount);
uint256 _sanTokenBalance = balanceOfSanToken();
if (_amountInSanToken > _sanTokenBalance) {
sanTokenGauge.withdraw(
Math.min(_amountInSanToken - _sanTokenBalance, balanceOfStakedSanToken())
);
}
withdrawFromStableMaster(Math.min(_amountInSanToken, balanceOfSanToken()));
}
// can be used in conjunction with migration if this function is still working
function claimRewards() external onlyVaultManagers {
sanTokenGauge.claim_rewards();
}
// transfers all tokens to new strategy
function prepareMigration(address _newStrategy) internal override {
// want is transferred by the base contract's migrate function
sanTokenGauge.withdraw(balanceOfStakedSanToken());
IERC20(sanToken).safeTransfer(_newStrategy, balanceOfSanToken());
IERC20(angleToken).transfer(_newStrategy, balanceOfAngleToken());
}
function protectedTokens()
internal
view
override
returns (address[] memory)
{}
function ethToWant(uint256 _amtInWei)
public
view
override
returns (uint256)
{
return _amtInWei;
}
/* ========== KEEP3RS ========== */
// use this to determine when to harvest
function harvestTrigger(uint256 callCostinEth)
public
view
override
returns (bool)
{
// Should not trigger if strategy is not active (no assets and no debtRatio). This means we don't need to adjust keeper job.
if (!isActive()) {
return false;
}
// harvest if we have a profit to claim at our upper limit without considering gas price
uint256 claimableProfit = claimableProfitInUsdt();
if (claimableProfit > harvestProfitMax) {
return true;
}
// check if the base fee gas price is higher than we allow. if it is, block harvests.
if (!isBaseFeeAcceptable()) {
return false;
}
// trigger if we want to manually harvest, but only if our gas price is acceptable
if (forceHarvestTriggerOnce) {
return true;
}
// harvest if we have a sufficient profit to claim, but only if our gas price is acceptable
if (claimableProfit > harvestProfitMin) {
return true;
}
StrategyParams memory params = vault.strategies(address(this));
// harvest no matter what once we reach our maxDelay
if (block.timestamp - params.lastReport > maxReportDelay) {
return true;
}
// harvest our credit if it's above our threshold
if (vault.creditAvailable() > creditThreshold) {
return true;
}
// otherwise, we don't harvest
return false;
}
/// @notice The value in dollars that our claimable rewards are worth (in USDT, 6 decimals).
function claimableProfitInUsdt() public view returns (uint256) {
address[] memory path = new address[](3);
path[0] = address(angleToken);
path[1] = weth;
path[2] = address(usdt);
uint256 _claimableRewards = sanTokenGauge.claimable_reward(address(this), address(angleToken));
if (_claimableRewards < 1e18) { // Dust check
return 0;
}
uint256[] memory amounts = IUniV2(unirouter).getAmountsOut(
_claimableRewards,
path
);
return amounts[amounts.length - 1];
}
// check if the current baseFee is below our external target
function isBaseFeeAcceptable() internal view returns (bool) {
return
IBaseFee(0xb5e1CAcB567d98faaDB60a1fD4820720141f064F)
.isCurrentBaseFeeAcceptable();
}
// ---------------------- SETTERS -----------------------
// This allows us to manually harvest with our keeper as needed
function setForceHarvestTriggerOnce(bool _forceHarvestTriggerOnce)
external
onlyVaultManagers
{
forceHarvestTriggerOnce = _forceHarvestTriggerOnce;
}
// Min profit to start checking for harvests if gas is good, max will harvest no matter gas (both in USDT, 6 decimals). Credit threshold is in want token, and will trigger a harvest if credit is large enough. check earmark to look at convex's booster.
function setHarvestTriggerParams(
uint256 _harvestProfitMin,
uint256 _harvestProfitMax,
uint256 _creditThreshold
) external onlyVaultManagers {
harvestProfitMin = _harvestProfitMin;
harvestProfitMax = _harvestProfitMax;
creditThreshold = _creditThreshold;
}
function setKeepInBips(uint256 _percentKeep) external onlyVaultManagers {
require(
_percentKeep <= MAX_BPS,
"_percentKeep can't be larger than 10,000"
);
percentKeep = _percentKeep;
}
// ----------------- SUPPORT & UTILITY FUNCTIONS ----------
function balanceOfWant() public view returns (uint256) {
return want.balanceOf(address(this));
}
function balanceOfStakedSanToken() public view returns (uint256) {
return IERC20(address(sanTokenGauge)).balanceOf(address(this));
}
function balanceOfSanToken() public view returns (uint256) {
return sanToken.balanceOf(address(this));
}
function balanceOfAngleToken() public view returns (uint256) {
return angleToken.balanceOf(address(this));
}
function valueOfSanToken() public view returns (uint256) {
return sanTokenToWant(balanceOfSanToken());
}
function valueOfStakedSanToken() public view returns (uint256) {
return sanTokenToWant(balanceOfStakedSanToken());
}
function sanTokenToWant(uint256 _sanTokenAmount)
public
view
returns (uint256)
{
return (_sanTokenAmount * getSanRate()) / 1e18;
}
function wantToSanToken(uint256 _wantAmount) public view returns (uint256) {
return ((_wantAmount * 1e18) / getSanRate()) + 1;
}
// Get rate of conversion between sanTokens and want
function getSanRate() public view returns (uint256) {
(, , , , , uint256 _sanRate, , , ) =
IStableMaster(angleStableMaster).collateralMap(poolManager);
return _sanRate;
}
function depositToStableMaster(uint256 _amount) internal {
IStableMaster(angleStableMaster).deposit(
_amount,
address(this),
poolManager
);
}
function withdrawFromStableMaster(uint256 _amountInSanToken) internal {
IStableMaster(angleStableMaster).withdraw(
_amountInSanToken,
address(this),
address(this),
poolManager
);
}
// ---------------------- YSWAPS FUNCTIONS ----------------------
function setTradeFactory(address _tradeFactory) external onlyGovernance {
if (tradeFactory != address(0)) {
_removeTradeFactoryPermissions();
}
angleToken.safeApprove(_tradeFactory, type(uint256).max);
ITradeFactory tf = ITradeFactory(_tradeFactory);
tf.enable(address(angleToken), address(want));
tradeFactory = _tradeFactory;
}
function removeTradeFactoryPermissions() external onlyEmergencyAuthorized {
_removeTradeFactoryPermissions();
}
function _removeTradeFactoryPermissions() internal {
angleToken.safeApprove(tradeFactory, 0);
tradeFactory = address(0);
}
}