-
Notifications
You must be signed in to change notification settings - Fork 7
/
Exposure.sol
318 lines (299 loc) · 13.4 KB
/
Exposure.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
// SPDX-License-Identifier: AGPLv3
pragma solidity >=0.6.0 <0.7.0;
pragma experimental ABIEncoderV2;
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "../common/StructDefinitions.sol";
import "../common/Constants.sol";
import "../common/Controllable.sol";
import "../common/Whitelist.sol";
import "../interfaces/IERC20Detailed.sol";
import "../interfaces/ILifeGuard.sol";
import "../interfaces/IExposure.sol";
import "../interfaces/IVault.sol";
import "../interfaces/IBuoy.sol";
/// @notice Contract for calculating current protocol exposures on a stablecoin and
/// protocol level. This contract can be upgraded if the systems underlying protocols
/// or tokens have changed. Protocol exposure are calculated at a high level, as any
/// additional exposures from underlying protocol exposures should at most be equal to
/// the high level exposure.
/// For example: harvest finance stablecoin vaults (fTokens)
/// - High level exposure
/// - Harvest finance
/// - Low level exposures (from fToken investments):
/// - Compound
/// - Idle finance
/// Neither of these two low level exposures should matter as long as there arent
/// additional exposure to these protocol elsewhere. So by desing, the protocols
/// are given indexes based on the strategies in the stablecoin vaults, which need
/// to be symetrical for this to work - e.g. all vaults needs to have the same exposure
/// profile, and non of these exposure profiles can overlap. In the case where the
/// additional exposure needs to be taken into account (maker has USDC collateral,
/// Curve adds exposure to all stablecoins in a liquidity pool), they will be calculated
/// and added ontop of the base exposure from vaults and strategies.
///
/// --------------------------------------------------------
/// Current protocol setup:
/// --------------------------------------------------------
/// Stablecoins: DAI, USDC, USDT
/// LP tokens: 3Crv
/// Vaults: DAIVault, USDCVault, USDTVault, 3Crv vault
/// Strategy (exposures):
/// - Compound
/// - Idle finance
/// - Yearn Generic Lender:
/// - Cream
/// - CurveXpool:
/// - Curve3Pool
/// - CurveMetaPool
/// - Yearn
contract Exposure is Constants, Controllable, Whitelist, IExposure {
using SafeMath for uint256;
using SafeERC20 for IERC20;
uint256 public protocolCount;
uint256 public makerUSDCExposure;
event LogNewProtocolCount(uint256 count);
event LogNewMakerExposure(uint256 exposure);
/// @notice Add protocol for the exposure calculations
/// @dev Currently set to:
/// 1 - Harvest finance
/// 2 - Cream
/// Curve exposure is calculated separately as it has wider system impact
function setProtocolCount(uint256 _protocolCount) external onlyOwner {
protocolCount = _protocolCount;
emit LogNewProtocolCount(_protocolCount);
}
/// @notice Specify additional USDC exposure to Maker
/// @param _makerUSDCExposure Exposure amount to Maker
function setMakerUSDCExposure(uint256 _makerUSDCExposure) external onlyOwner {
makerUSDCExposure = _makerUSDCExposure;
emit LogNewMakerExposure(_makerUSDCExposure);
}
function getExactRiskExposure(SystemState calldata sysState)
external
view
override
returns (ExposureState memory expState)
{
expState = _calcRiskExposure(sysState, false);
ILifeGuard lifeguard = ILifeGuard(_controller().lifeGuard());
IBuoy buoy = IBuoy(_controller().buoy());
for (uint256 i = 0; i < N_COINS; i++) {
uint256 assets = lifeguard.assets(i);
uint256 assetsUsd = buoy.singleStableToUsd(assets, i);
expState.stablecoinExposure[i] = expState.stablecoinExposure[i].add(
assetsUsd.mul(PERCENTAGE_DECIMAL_FACTOR).div(sysState.totalCurrentAssetsUsd)
);
}
}
/// @notice Calculate stablecoin and protocol level risk exposure
/// @param sysState Struct holding info about systems current state
/// @dev This loops through all the vaults, checks the amount of assets in them
/// and their underlying strategies to understand stablecoin exposure
/// - Any assets invested in Curve or similar AMM will have additional stablecoin exposure.
/// The protocol exposure is calculated by assessing the amount of assets each
/// vault has invested in a strategy.
function calcRiskExposure(SystemState calldata sysState)
external
view
override
returns (ExposureState memory expState)
{
expState = _calcRiskExposure(sysState, true);
// Establish if any stablecoin/protocol is over exposed
(expState.stablecoinExposed, expState.protocolExposed) = isExposed(
sysState.rebalanceThreshold,
expState.stablecoinExposure,
expState.protocolExposure,
expState.curveExposure
);
}
/// @notice Do a rough USD dollar calculation by treating every stablecoin as
/// worth 1 USD and set all Decimals to 18
function getUnifiedAssets(address[N_COINS] calldata vaults)
public
view
override
returns (uint256 unifiedTotalAssets, uint256[N_COINS] memory unifiedAssets)
{
// unify all assets to 18 decimals, treat each stablecoin as being worth 1 USD
for (uint256 i = 0; i < N_COINS; i++) {
uint256 assets = IVault(vaults[i]).totalAssets();
unifiedAssets[i] = assets.mul(DEFAULT_DECIMALS_FACTOR).div(
uint256(10)**IERC20Detailed(IVault(vaults[i]).token()).decimals()
);
unifiedTotalAssets = unifiedTotalAssets.add(unifiedAssets[i]);
}
}
/// @notice Rough delta calculation - assumes each stablecoin is priced at 1 USD,
/// and looks at differences between current allocations and target allocations
/// @param targets Stable coin allocation targest
/// @param vaults Stablecoin vaults
/// @param withdrawUsd USD value of withdrawals
function calcRoughDelta(
uint256[N_COINS] calldata targets,
address[N_COINS] calldata vaults,
uint256 withdrawUsd
) external view override returns (uint256[N_COINS] memory delta) {
(uint256 totalAssets, uint256[N_COINS] memory vaultTotalAssets) = getUnifiedAssets(vaults);
require(totalAssets > withdrawUsd, "totalAssets < withdrawalUsd");
totalAssets = totalAssets.sub(withdrawUsd);
uint256 totalDelta;
for (uint256 i; i < N_COINS; i++) {
uint256 target = totalAssets.mul(targets[i]).div(PERCENTAGE_DECIMAL_FACTOR);
if (vaultTotalAssets[i] > target) {
delta[i] = vaultTotalAssets[i].sub(target);
totalDelta = totalDelta.add(delta[i]);
}
}
uint256 percent = PERCENTAGE_DECIMAL_FACTOR;
for (uint256 i; i < N_COINS - 1; i++) {
if (delta[i] > 0) {
delta[i] = delta[i].mul(PERCENTAGE_DECIMAL_FACTOR).div(totalDelta);
percent = percent.sub(delta[i]);
}
}
delta[N_COINS - 1] = percent;
return delta;
}
/// @notice Sort vaults by the delta of target asset - current asset,
/// only support 3 vaults now
/// @param bigFirst Return array order most exposed -> least exposed
/// @param unifiedTotalAssets Estimated system USD assets
/// @param unifiedAssets Estimated vault USD assets
/// @param targetPercents Vault target percent array
function sortVaultsByDelta(
bool bigFirst,
uint256 unifiedTotalAssets,
uint256[N_COINS] calldata unifiedAssets,
uint256[N_COINS] calldata targetPercents
) external pure override returns (uint256[N_COINS] memory vaultIndexes) {
uint256 maxIndex;
uint256 minIndex;
int256 maxDelta;
int256 minDelta;
for (uint256 i = 0; i < N_COINS; i++) {
// Get difference between vault current assets and vault target
int256 delta = int256(
unifiedAssets[i] - unifiedTotalAssets.mul(targetPercents[i]).div(PERCENTAGE_DECIMAL_FACTOR)
);
// Establish order
if (delta > maxDelta) {
maxDelta = delta;
maxIndex = i;
} else if (delta < minDelta) {
minDelta = delta;
minIndex = i;
}
}
if (bigFirst) {
vaultIndexes[0] = maxIndex;
vaultIndexes[2] = minIndex;
} else {
vaultIndexes[0] = minIndex;
vaultIndexes[2] = maxIndex;
}
vaultIndexes[1] = N_COINS - maxIndex - minIndex;
}
/// @notice Calculate what percentage of system total assets the assets in a strategy make up
/// @param vault Address of target vault that holds the strategy
/// @param index Index of strategy
/// @param vaultAssetsPercent Percentage of system assets
/// @param vaultAssets Total assets in vaults
function calculatePercentOfSystem(
address vault,
uint256 index,
uint256 vaultAssetsPercent,
uint256 vaultAssets
) private view returns (uint256 percentOfSystem) {
if (vaultAssets == 0) return 0;
uint256 strategyAssetsPercent = IVault(vault).getStrategyAssets(index).mul(PERCENTAGE_DECIMAL_FACTOR).div(
vaultAssets
);
percentOfSystem = vaultAssetsPercent.mul(strategyAssetsPercent).div(PERCENTAGE_DECIMAL_FACTOR);
}
/// @notice Calculate the net stablecoin exposure
/// @param directlyExposure Amount of stablecoin in vault+strategies
/// @param curveExposure Percent of assets in Curve
function calculateStableCoinExposure(uint256[N_COINS] memory directlyExposure, uint256 curveExposure)
private
view
returns (uint256[N_COINS] memory stableCoinExposure)
{
uint256 maker = directlyExposure[0].mul(makerUSDCExposure).div(PERCENTAGE_DECIMAL_FACTOR);
for (uint256 i = 0; i < N_COINS; i++) {
uint256 indirectExposure = curveExposure;
if (i == 1) {
indirectExposure = indirectExposure.add(maker);
}
stableCoinExposure[i] = directlyExposure[i].add(indirectExposure);
}
}
/// @notice Determine if an assets or protocol is overexposed
/// @param rebalanceThreshold Threshold for triggering a rebalance due to overexposure
/// @param stableCoinExposure Current stable coin exposures
/// @param protocolExposure Current prtocol exposures
/// @param curveExposure Current Curve exposure
function isExposed(
uint256 rebalanceThreshold,
uint256[N_COINS] memory stableCoinExposure,
uint256[] memory protocolExposure,
uint256 curveExposure
) private pure returns (bool stablecoinExposed, bool protocolExposed) {
for (uint256 i = 0; i < N_COINS; i++) {
if (stableCoinExposure[i] > rebalanceThreshold) {
stablecoinExposed = true;
break;
}
}
for (uint256 i = 0; i < protocolExposure.length; i++) {
if (protocolExposure[i] > rebalanceThreshold) {
protocolExposed = true;
break;
}
}
if (!protocolExposed && curveExposure > rebalanceThreshold) protocolExposed = true;
return (stablecoinExposed, protocolExposed);
}
function _calcRiskExposure(SystemState memory sysState, bool treatLifeguardAsCurve)
private
view
returns (ExposureState memory expState)
{
address[N_COINS] memory vaults = _controller().vaults();
uint256 pCount = protocolCount;
expState.protocolExposure = new uint256[](pCount);
if (sysState.totalCurrentAssetsUsd == 0) {
return expState;
}
// Stablecoin exposure
for (uint256 i = 0; i < N_COINS; i++) {
uint256 vaultAssetsPercent = sysState.vaultCurrentAssetsUsd[i].mul(PERCENTAGE_DECIMAL_FACTOR).div(
sysState.totalCurrentAssetsUsd
);
expState.stablecoinExposure[i] = vaultAssetsPercent;
// Protocol exposure
for (uint256 j = 0; j < pCount; j++) {
uint256 percentOfSystem = calculatePercentOfSystem(
vaults[i],
j,
vaultAssetsPercent,
sysState.vaultCurrentAssets[i]
);
expState.protocolExposure[j] = expState.protocolExposure[j].add(percentOfSystem);
}
}
if (treatLifeguardAsCurve) {
// Curve exposure is calculated by adding the Curve vaults total assets and any
// assets in the lifeguard which are poised to be invested into the Curve vault
expState.curveExposure = sysState.curveCurrentAssetsUsd.add(sysState.lifeguardCurrentAssetsUsd);
} else {
expState.curveExposure = sysState.curveCurrentAssetsUsd;
}
expState.curveExposure = expState.curveExposure.mul(PERCENTAGE_DECIMAL_FACTOR).div(
sysState.totalCurrentAssetsUsd
);
// Calculate stablecoin exposures
expState.stablecoinExposure = calculateStableCoinExposure(expState.stablecoinExposure, expState.curveExposure);
}
}