generated from yearn/brownie-strategy-mix
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStrategy.sol
187 lines (149 loc) · 6.08 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
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
import {BaseStrategy,StrategyParams} from "@yearnvaults/contracts/BaseStrategy.sol";
import {IERC20, Address} from "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/math/Math.sol";
interface ISushiBar is IERC20 {
function enter(uint _amount) external;
function leave(uint _share) external;
}
interface IBaseFee {
function isCurrentBaseFeeAcceptable() external view returns (bool);
}
contract Strategy is BaseStrategy {
using Address for address;
ISushiBar public constant xSushi = ISushiBar(0x8798249c2E607446EfB7Ad49eC89dD1865Ff4272);
//Big number to guarantee sushiPerXSushi to account for all decimals without rounding:
uint256 internal constant AVOID_ROUNDING_DECIMALS = 1e27;
bool internal forceHarvestTriggerOnce;
uint256 public creditThreshold = 5e5 * 1e18;
constructor(address _vault) public BaseStrategy(_vault) {
maxReportDelay = 35 days;
want.safeApprove(address(xSushi), type(uint256).max);
}
// ******** OVERRIDE METHODS FROM BASE CONTRACT ************
function name() external view override returns (string memory) {
return "Strategy-xSushi-Staker";
}
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;
}
// 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;
}
StrategyParams memory params = vault.strategies(address(this));
// harvest once we reach our maxDelay if our gas price is okay
if (block.timestamp.sub(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;
}
function estimatedTotalAssets() public view override returns (uint256) {
return balanceOfXSushi().mul(sushiPerXSushi()).div(AVOID_ROUNDING_DECIMALS).add(balanceOfWant());
}
function prepareReturn(uint256 _debtOutstanding)
internal
override
returns (
uint256 _profit,
uint256 _loss,
uint256 _debtPayment
)
{
uint256 totalDebt = vault.strategies(address(this)).totalDebt;
uint256 totalAssetsAfterProfit = estimatedTotalAssets();
_profit = totalAssetsAfterProfit > totalDebt
? totalAssetsAfterProfit.sub(totalDebt)
: 0;
uint256 _amountFreed;
(_amountFreed, _loss) = liquidatePosition(_debtOutstanding.add(_profit));
_debtPayment = Math.min(_debtOutstanding, _amountFreed);
//Net profit and loss calculation
if (_loss > _profit) {
_loss = _loss.sub(_profit);
_profit = 0;
} else {
_profit = _profit.sub(_loss);
_loss = 0;
}
// we're done harvesting, so reset our trigger if we used it
forceHarvestTriggerOnce = false;
}
function adjustPosition(uint256 _debtOutstanding) internal override {
xSushi.enter(balanceOfWant());
}
function liquidatePosition(uint256 _amountNeeded)
internal
override
returns (uint256 _liquidatedAmount, uint256 _loss)
{
uint256 wantBalance = balanceOfWant();
if (wantBalance < _amountNeeded){
//Unstake xSushi amount in want corresponding to _amountNeeded (or total balance of xSushi (unlikely))
xSushi.leave(Math.min(balanceOfXSushi(), _amountNeeded.sub(wantBalance).mul(AVOID_ROUNDING_DECIMALS).div(sushiPerXSushi())));
_liquidatedAmount = Math.min(balanceOfWant(), _amountNeeded);
_loss = _amountNeeded > _liquidatedAmount ? _amountNeeded.sub(_liquidatedAmount) : 0;
} else {
_liquidatedAmount = _amountNeeded;
}
}
function liquidateAllPositions() internal override returns (uint256) {
xSushi.leave(balanceOfXSushi());
return balanceOfWant();
}
function prepareMigration(address _newStrategy) internal override {
uint256 xSushiBalance = balanceOfXSushi();
xSushi.transfer(_newStrategy, xSushiBalance);
}
function protectedTokens() internal view override returns (address[] memory){}
function ethToWant(uint _amtInWei) public view override returns (uint){return _amtInWei;}
// check if the current baseFee is below our external target
function isBaseFeeAcceptable() internal view returns (bool) {
return IBaseFee(0xb5e1CAcB567d98faaDB60a1fD4820720141f064F).isCurrentBaseFeeAcceptable();
}
/////////////////// GETTERS:
function balanceOfWant() public view returns (uint256){
return want.balanceOf(address(this));
}
function balanceOfXSushi() public view returns (uint256){
return xSushi.balanceOf(address(this));
}
function sushiPerXSushi() public view returns (uint256){
return want.balanceOf(address(xSushi)).mul(AVOID_ROUNDING_DECIMALS).div(xSushi.totalSupply());
}
/////////////////// Manual harvest through keepers:
function setForceHarvestTriggerOnce(bool _forceHarvestTriggerOnce)
external
onlyVaultManagers
{
forceHarvestTriggerOnce = _forceHarvestTriggerOnce;
}
function setCreditThreshold(uint256 _creditThreshold)
external
onlyVaultManagers
{
creditThreshold = _creditThreshold;
}
////////////////// EMERGENCY UNSTAKE:
function emergencyUnstakeXSushi(uint256 _amount) external onlyEmergencyAuthorized {
xSushi.leave(_amount);
}
}