forked from neptune-mutual-blue/protocol
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStrategyLibV1.sol
350 lines (294 loc) · 10.9 KB
/
StrategyLibV1.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
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
/* solhint-disable ordering */
pragma solidity ^0.8.0;
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "../interfaces/IStore.sol";
import "../interfaces/ILendingStrategy.sol";
import "./PriceLibV1.sol";
import "./ProtoUtilV1.sol";
import "./RegistryLibV1.sol";
library StrategyLibV1 {
using ProtoUtilV1 for IStore;
using StoreKeyUtil for IStore;
using RegistryLibV1 for IStore;
uint256 public constant DEFAULT_LENDING_PERIOD = 180 days;
uint256 public constant DEFAULT_WITHDRAWAL_WINDOW = 7 days;
event StrategyAdded(address indexed strategy);
event RiskPoolingPeriodSet(bytes32 indexed key, uint256 lendingPeriod, uint256 withdrawalWindow);
event MaxLendingRatioSet(uint256 ratio);
/**
* @dev Hash key of the "active strategy flag".
*
* Warning: this function does not validate the input arguments.
*
* @param strategyAddress Enter a strategy address
*
*/
function _getIsActiveStrategyKey(address strategyAddress) private pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE, strategyAddress));
}
/**
* @dev Hash key of the "disabled strategy flag".
*
* Warning: this function does not validate the input arguments.
*
* @param strategyAddress Enter a strategy address
*
*/
function _getIsDisabledStrategyKey(address strategyAddress) private pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED, strategyAddress));
}
/**
* @dev Disables a strategy
*
* @custom:suppress-address-trust-issue The address `toFind` can be trusted since we are not treating it like a contract.
*
*/
function disableStrategyInternal(IStore s, address toFind) external {
_disableStrategy(s, toFind);
s.setAddressArrayByKey(ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED, toFind);
}
/**
* @dev Deletes a strategy
*
* @custom:suppress-address-trust-issue The address `toFind` can be trusted since we are not treating it like a contract.
*
*/
function deleteStrategyInternal(IStore s, address toFind) external {
_deleteStrategy(s, toFind);
}
function addStrategiesInternal(IStore s, address[] calldata strategies) external {
for (uint256 i = 0; i < strategies.length; i++) {
address strategy = strategies[i];
_addStrategy(s, strategy);
}
}
function getRiskPoolingPeriodsInternal(IStore s, bytes32 coverKey) external view returns (uint256 lendingPeriod, uint256 withdrawalWindow) {
lendingPeriod = s.getUintByKey(getLendingPeriodKey(coverKey));
withdrawalWindow = s.getUintByKey(getWithdrawalWindowKey(coverKey));
if (lendingPeriod == 0) {
lendingPeriod = s.getUintByKey(getLendingPeriodKey(0));
withdrawalWindow = s.getUintByKey(getWithdrawalWindowKey(0));
}
lendingPeriod = lendingPeriod == 0 ? DEFAULT_LENDING_PERIOD : lendingPeriod;
withdrawalWindow = withdrawalWindow == 0 ? DEFAULT_WITHDRAWAL_WINDOW : withdrawalWindow;
}
function setRiskPoolingPeriodsInternal(
IStore s,
bytes32 coverKey,
uint256 lendingPeriod,
uint256 withdrawalWindow
) external {
s.setUintByKey(getLendingPeriodKey(coverKey), lendingPeriod);
s.setUintByKey(getWithdrawalWindowKey(coverKey), withdrawalWindow);
emit RiskPoolingPeriodSet(coverKey, lendingPeriod, withdrawalWindow);
}
/**
* @dev Hash key of the "lending period" for the given cover.
*
* Warning: this function does not validate the cover key supplied.
*
* @param coverKey Enter cover key
*
*/
function getLendingPeriodKey(bytes32 coverKey) public pure returns (bytes32) {
if (coverKey > 0) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_COVER_LIQUIDITY_LENDING_PERIOD, coverKey));
}
return ProtoUtilV1.NS_COVER_LIQUIDITY_LENDING_PERIOD;
}
function getMaxLendingRatioInternal(IStore s) external view returns (uint256) {
return s.getUintByKey(getMaxLendingRatioKey());
}
function setMaxLendingRatioInternal(IStore s, uint256 ratio) external {
s.setUintByKey(getMaxLendingRatioKey(), ratio);
emit MaxLendingRatioSet(ratio);
}
/**
* @dev Hash key of the "maximum lending ratio" for the given cover.
*/
function getMaxLendingRatioKey() public pure returns (bytes32) {
return ProtoUtilV1.NS_COVER_LIQUIDITY_MAX_LENDING_RATIO;
}
/**
* @dev Hash key of the "withdrawal window duration" for the given cover.
*
* Warning: this function does not validate the cover key supplied.
*
* @param coverKey Enter cover key
*
*/
function getWithdrawalWindowKey(bytes32 coverKey) public pure returns (bytes32) {
if (coverKey > 0) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_COVER_LIQUIDITY_WITHDRAWAL_WINDOW, coverKey));
}
return ProtoUtilV1.NS_COVER_LIQUIDITY_WITHDRAWAL_WINDOW;
}
function _addStrategy(IStore s, address deployedOn) private {
ILendingStrategy strategy = ILendingStrategy(deployedOn);
require(strategy.getWeight() <= ProtoUtilV1.MULTIPLIER, "Weight too much");
s.setBoolByKey(_getIsActiveStrategyKey(deployedOn), true);
s.setAddressArrayByKey(ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE, deployedOn);
emit StrategyAdded(deployedOn);
}
function _disableStrategy(IStore s, address toFind) private {
bytes32 key = ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE;
uint256 pos = s.getAddressArrayItemPosition(key, toFind);
require(pos > 0, "Invalid strategy");
s.deleteAddressArrayItem(key, toFind);
s.setBoolByKey(_getIsActiveStrategyKey(toFind), false);
s.setBoolByKey(_getIsDisabledStrategyKey(toFind), true);
}
function _deleteStrategy(IStore s, address toFind) private {
bytes32 key = ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED;
uint256 pos = s.getAddressArrayItemPosition(key, toFind);
require(pos > 0, "Invalid strategy");
s.deleteAddressArrayItem(key, toFind);
s.setBoolByKey(_getIsDisabledStrategyKey(toFind), false);
}
function getDisabledStrategiesInternal(IStore s) external view returns (address[] memory strategies) {
return s.getAddressArrayByKey(ProtoUtilV1.NS_LENDING_STRATEGY_DISABLED);
}
function getActiveStrategiesInternal(IStore s) external view returns (address[] memory strategies) {
return s.getAddressArrayByKey(ProtoUtilV1.NS_LENDING_STRATEGY_ACTIVE);
}
/**
* @dev Hash key of the "strategy outs" for the given cover and token.
*
* Warning: this function does not validate the cover key and token supplied.
*
* @param coverKey Enter cover key
* @param token Enter the token address
*
*/
function getStrategyOutKey(bytes32 coverKey, address token) public pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_STRATEGY_OUT, coverKey, token));
}
/**
* @dev Hash key of the "outs" to a specific strategy for the given cover and token.
*
* Warning: this function does not validate the cover key and token supplied.
*
* @param coverKey Enter cover key
* @param token Enter the token address
*
*/
function getSpecificStrategyOutKey(
bytes32 coverKey,
bytes32 strategyName,
address token
) public pure returns (bytes32) {
return keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_STRATEGY_OUT, coverKey, strategyName, token));
}
function getAmountInStrategies(
IStore s,
bytes32 coverKey,
address token
) public view returns (uint256) {
bytes32 k = getStrategyOutKey(coverKey, token);
return s.getUintByKey(k);
}
function getAmountInStrategy(
IStore s,
bytes32 coverKey,
bytes32 strategyName,
address token
) public view returns (uint256) {
bytes32 k = getSpecificStrategyOutKey(coverKey, strategyName, token);
return s.getUintByKey(k);
}
function preTransferToStrategyInternal(
IStore s,
IERC20 token,
bytes32 coverKey,
bytes32 strategyName,
uint256 amount
) external {
if (s.getStablecoin() != address(token)) {
return;
}
_addToStrategyOut(s, coverKey, address(token), amount);
_addToSpecificStrategyOut(s, coverKey, strategyName, address(token), amount);
}
function postReceiveFromStrategyInternal(
IStore s,
IERC20 token,
bytes32 coverKey,
bytes32 strategyName,
uint256 received
) external returns (uint256 income, uint256 loss) {
if (s.getStablecoin() != address(token)) {
return (income, loss);
}
uint256 amountInThisStrategy = getAmountInStrategy(s, coverKey, strategyName, address(token));
income = received > amountInThisStrategy ? received - amountInThisStrategy : 0;
loss = received < amountInThisStrategy ? amountInThisStrategy - received : 0;
_reduceStrategyOut(s, coverKey, address(token), amountInThisStrategy);
_clearSpecificStrategyOut(s, coverKey, strategyName, address(token));
_logIncomes(s, coverKey, strategyName, income, loss);
}
function _addToStrategyOut(
IStore s,
bytes32 coverKey,
address token,
uint256 amountToAdd
) private {
bytes32 k = getStrategyOutKey(coverKey, token);
s.addUintByKey(k, amountToAdd);
}
function _reduceStrategyOut(
IStore s,
bytes32 coverKey,
address token,
uint256 amount
) private {
bytes32 k = getStrategyOutKey(coverKey, token);
s.subtractUintByKey(k, amount);
}
function _addToSpecificStrategyOut(
IStore s,
bytes32 coverKey,
bytes32 strategyName,
address token,
uint256 amountToAdd
) private {
bytes32 k = getSpecificStrategyOutKey(coverKey, strategyName, token);
s.addUintByKey(k, amountToAdd);
}
function _clearSpecificStrategyOut(
IStore s,
bytes32 coverKey,
bytes32 strategyName,
address token
) private {
bytes32 k = getSpecificStrategyOutKey(coverKey, strategyName, token);
s.deleteUintByKey(k);
}
function _logIncomes(
IStore s,
bytes32 coverKey,
bytes32 strategyName,
uint256 income,
uint256 loss
) private {
// Overall Income
s.addUintByKey(ProtoUtilV1.NS_VAULT_LENDING_INCOMES, income);
// By Cover
s.addUintByKey(keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_LENDING_INCOMES, coverKey)), income);
// By Cover on This Strategy
s.addUintByKey(keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_LENDING_INCOMES, coverKey, strategyName)), income);
// Overall Loss
s.addUintByKey(ProtoUtilV1.NS_VAULT_LENDING_LOSSES, loss);
// By Cover
s.addUintByKey(keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_LENDING_LOSSES, coverKey)), loss);
// By Cover on This Strategy
s.addUintByKey(keccak256(abi.encodePacked(ProtoUtilV1.NS_VAULT_LENDING_LOSSES, coverKey, strategyName)), loss);
}
function getStablecoinOwnedByVaultInternal(IStore s, bytes32 coverKey) external view returns (uint256) {
address stablecoin = s.getStablecoin();
uint256 balance = IERC20(stablecoin).balanceOf(s.getVaultAddress(coverKey));
uint256 inStrategies = getAmountInStrategies(s, coverKey, stablecoin);
return balance + inStrategies;
}
}