-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathVaultDelegateBase.sol
More file actions
429 lines (400 loc) · 15.4 KB
/
VaultDelegateBase.sol
File metadata and controls
429 lines (400 loc) · 15.4 KB
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
// Neptune Mutual Protocol (https://neptunemutual.com)
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.0;
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";
import "../../interfaces/IVaultDelegate.sol";
import "../../libraries/VaultLibV1.sol";
import "../Recoverable.sol";
/**
* Important: This contract is not intended to be accessed
* by anyone/anything except individual vault contracts.
*
* @title Vault Delegate Base Contract
*
*
* @dev The vault delegate base contract includes pre and post hooks.
* The hooks are accessible only to vault contracts.
*
*/
abstract contract VaultDelegateBase is IVaultDelegate, Recoverable {
using ProtoUtilV1 for IStore;
using RoutineInvokerLibV1 for IStore;
using StrategyLibV1 for IStore;
using ValidationLibV1 for IStore;
using VaultLibV1 for IStore;
/**
* @dev Constructs this contract
*
* @param store Provide the store contract instance
*/
constructor(IStore store) Recoverable(store) {} // solhint-disable-line
/**
* @dev This hook runs before `transferGovernance` implementation on vault(s).
*
* @custom:suppress-acl This function is only callable by the claims processor contract through the vault contract
* @custom:note Please note the following:
*
* - Governance transfers are allowed via claims processor contract only.
* - This function's caller must be the vault of the specified coverKey.
*
* @param caller Enter your msg.sender value.
* @param coverKey Provide your vault's cover key.
*
* @return stablecoin Returns address of the protocol stablecoin if the hook validation passes.
*
*/
function preTransferGovernance(
address caller,
bytes32 coverKey,
address, /*to*/
uint256 /*amount*/
) external override nonReentrant returns (address stablecoin) {
// @suppress-zero-value-check This function does not transfer any values
s.mustNotBePaused();
s.mustBeProtocolMember(caller);
s.mustBeProtocolMember(msg.sender);
s.senderMustBeVaultContract(coverKey);
s.callerMustBeClaimsProcessorContract(caller);
stablecoin = s.getStablecoinAddressInternal();
}
/**
* @dev This hook runs after `transferGovernance` implementation on vault(s)
* and performs cleanup and/or validation if needed.
*
* @custom:suppress-acl This function is only callable by the claims processor contract through the vault contract
* @custom:note do not update state and liquidity since `transferGovernance` is an internal contract-only function
* @custom:suppress-reentrancy The `postTransferGovernance` hook is executed under the same context of `preTransferGovernance`.
*
* @param caller Enter your msg.sender value.
* @param coverKey Provide your vault's cover key.
*
*/
function postTransferGovernance(
address caller,
bytes32 coverKey,
address, /*to*/
uint256 /*amount*/
) external view override {
s.mustNotBePaused();
s.mustBeProtocolMember(caller);
s.mustBeProtocolMember(msg.sender);
s.senderMustBeVaultContract(coverKey);
s.callerMustBeClaimsProcessorContract(caller);
}
/**
* @dev This hook runs before `transferToStrategy` implementation on vault(s)
*
* @custom:suppress-acl This function is only callable by a strategy contract through vault contract
* @custom:note Please note the following:
*
* - Transfers are allowed to exact strategy contracts only
* where the strategy can perform lending.
*
* @param caller Enter your msg.sender value
* @param token Provide the ERC20 token you'd like to transfer to the given strategy
* @param coverKey Provide your vault's cover key
* @param strategyName Enter the strategy name
* @param amount Enter the amount to transfer
*
*/
function preTransferToStrategy(
address caller,
IERC20 token,
bytes32 coverKey,
bytes32 strategyName,
uint256 amount
) external override nonReentrant {
// @suppress-zero-value-check Checked
s.mustNotBePaused();
s.mustBeProtocolMember(caller);
s.mustBeProtocolMember(msg.sender);
s.senderMustBeVaultContract(coverKey);
s.callerMustBeSpecificStrategyContract(caller, strategyName);
s.preTransferToStrategyInternal(token, coverKey, strategyName, amount);
}
/**
* @dev This hook runs after `transferToStrategy` implementation on vault(s)
* and performs cleanup and/or validation if needed.
*
* @custom:suppress-acl This function is only callable by a strategy contract through vault contract
* @custom:suppress-reentrancy Not required. The `postTransferToStrategy` hook is executed under the same context of `preTransferToStrategy`.
* @custom:note Do not update state and liquidity since `transferToStrategy` itself is a part of the state update
*
* @param caller Enter your msg.sender value
* @param coverKey Enter the coverKey
* @param strategyName Enter the strategy name
*
*/
function postTransferToStrategy(
address caller,
IERC20, /*token*/
bytes32 coverKey,
bytes32 strategyName,
uint256 /*amount*/
) external view override {
s.mustNotBePaused();
s.mustBeProtocolMember(caller);
s.mustBeProtocolMember(msg.sender);
s.senderMustBeVaultContract(coverKey);
s.callerMustBeSpecificStrategyContract(caller, strategyName);
}
/**
* @dev This hook runs before `receiveFromStrategy` implementation on vault(s)
*
* @custom:note Please note the following:
*
* - Access is allowed to exact strategy contracts only
* - The caller must be the strategy contract
* - msg.sender must be the correct vault contract
*
* @param caller Enter your msg.sender value
* @param coverKey Provide your vault's cover key
* @param strategyName Enter the strategy name
*
*/
function preReceiveFromStrategy(
address caller,
IERC20, /*token*/
bytes32 coverKey,
bytes32 strategyName,
uint256 /*amount*/
) external override nonReentrant {
// @suppress-zero-value-check This function does not transfer any tokens
s.mustNotBePaused();
s.mustBeProtocolMember(caller);
s.mustBeProtocolMember(msg.sender);
s.senderMustBeVaultContract(coverKey);
s.callerMustBeSpecificStrategyContract(caller, strategyName);
}
/**
* @dev This hook runs after `receiveFromStrategy` implementation on vault(s)
* and performs cleanup and/or validation if needed.
*
* @custom:note Do not update state and liquidity since `receiveFromStrategy` itself is a part of the state update
* @custom:suppress-reentrancy Not required. The `postReceiveFromStrategy` hook is executed under the same context of `preReceiveFromStrategy`.
*
* @param caller Enter your msg.sender value
* @param token Enter the token your vault received from strategy
* @param coverKey Enter the coverKey
* @param strategyName Enter the strategy name
* @param amount Enter the amount received
*
*/
function postReceiveFromStrategy(
address caller,
IERC20 token,
bytes32 coverKey,
bytes32 strategyName,
uint256 amount
) external override returns (uint256 income, uint256 loss) {
// @suppress-zero-value-check This call does not perform any transfers
s.mustNotBePaused();
s.mustBeProtocolMember(caller);
s.mustBeProtocolMember(msg.sender);
s.senderMustBeVaultContract(coverKey);
s.callerMustBeSpecificStrategyContract(caller, strategyName);
(income, loss) = s.postReceiveFromStrategyInternal(token, coverKey, strategyName, amount);
}
/**
* @dev This hook runs before `addLiquidity` implementation on vault(s)
*
* @custom:suppress-acl No need to define ACL as this function is only accessible to associated vault contract of the coverKey
* @custom:note Please note the following:
*
* - msg.sender must be correct vault contract
*
* @param coverKey Enter the cover key
* @param amount Enter the amount of liquidity token to supply.
* @param npmStakeToAdd Enter the amount of NPM token to stake.
*
*/
function preAddLiquidity(
address caller,
bytes32 coverKey,
uint256 amount,
uint256 npmStakeToAdd
) external override nonReentrant returns (uint256 podsToMint, uint256 previousNpmStake) {
// @suppress-zero-value-check This call does not transfer any tokens
s.mustNotBePaused();
s.mustBeProtocolMember(msg.sender);
s.senderMustBeVaultContract(coverKey);
s.mustEnsureAllProductsAreNormal(coverKey);
ValidationLibV1.mustMaintainStablecoinThreshold(s, amount);
GovernanceUtilV1.mustNotExceedNpmThreshold(npmStakeToAdd);
address pod = msg.sender;
(podsToMint, previousNpmStake) = s.preAddLiquidityInternal(coverKey, pod, caller, amount, npmStakeToAdd);
}
/**
* @dev This hook runs after `addLiquidity` implementation on vault(s)
* and performs cleanup and/or validation if needed.
*
* @custom:suppress-acl No need to define ACL as this function is only accessible to associated vault contract of the coverKey
* @custom:suppress-reentrancy Not required. The `postAddLiquidity` hook is executed under the same context of `preAddLiquidity`.
*
* @param coverKey Enter the coverKey
*
*/
function postAddLiquidity(
address, /*caller*/
bytes32 coverKey,
uint256, /*amount*/
uint256 /*npmStakeToAdd*/
) external override {
// @suppress-zero-value-check This function does not transfer any tokens
s.mustNotBePaused();
s.mustBeProtocolMember(msg.sender);
s.senderMustBeVaultContract(coverKey);
s.mustEnsureAllProductsAreNormal(coverKey);
s.updateStateAndLiquidityInternal(coverKey);
}
/**
* @dev This implemention enables liquidity manages to
* accrue interests on a vault before withdrawals are allowed.
*
* @custom:suppress-acl This function is only accessible to the vault contract
* @custom:note Please note the following:
*
* - Caller must be a liquidity manager
* - msg.sender must the correct vault contract
*
* @param caller Enter your msg.sender value
* @param coverKey Provide your vault's cover key
*
*/
function accrueInterestImplementation(address caller, bytes32 coverKey) external override {
s.mustNotBePaused();
s.senderMustBeVaultContract(coverKey);
AccessControlLibV1.callerMustBeLiquidityManager(s, caller);
s.accrueInterestInternal(coverKey);
}
/**
* @dev This hook runs before `removeLiquidity` implementation on vault(s)
*
* @custom:suppress-acl No need to define ACL as this function is only accessible to associated vault contract of the coverKey
* @custom:note Please note the following:
*
* - msg.sender must be the correct vault contract
* - Must have at couple of block height offset following a deposit.
* - Must be done during withdrawal period
* - Must have no balance in strategies
* - Cover status should be normal
* - Interest should already be accrued
*
* @param caller Enter your msg.sender value
* @param coverKey Enter the cover key
* @param podsToRedeem Enter the amount of pods to redeem
* @param npmStakeToRemove Enter the amount of NPM stake to remove.
* @param exit If this is set to true, LPs can remove their entire NPM stake during a withdrawal period. No restriction.
*
*/
function preRemoveLiquidity(
address caller,
bytes32 coverKey,
uint256 podsToRedeem,
uint256 npmStakeToRemove,
bool exit
) external override nonReentrant returns (address stablecoin, uint256 stablecoinToRelease) {
// @suppress-zero-value-check This call does not transfer any tokens
s.mustNotBePaused();
s.mustBeProtocolMember(msg.sender);
s.senderMustBeVaultContract(coverKey);
s.mustMaintainBlockHeightOffset(coverKey);
s.mustEnsureAllProductsAreNormal(coverKey);
s.mustBeDuringWithdrawalPeriod(coverKey);
s.mustHaveNoBalanceInStrategies(coverKey, stablecoin);
s.mustBeAccrued(coverKey);
address pod = msg.sender; // The sender is vault contract
return s.preRemoveLiquidityInternal(coverKey, pod, caller, podsToRedeem, npmStakeToRemove, exit);
}
/**
* @dev This hook runs after `removeLiquidity` implementation on vault(s)
* and performs cleanup and/or validation if needed.
*
* @custom:suppress-acl No need to define ACL as this function is only accessible to associated vault contract of the coverKey
* @custom:suppress-reentrancy Not required. The `postRemoveLiquidity` hook is executed under the same context as `preRemoveLiquidity`.
*
* @param coverKey Enter the coverKey
*
*/
function postRemoveLiquidity(
address, /*caller*/
bytes32 coverKey,
uint256, /*podsToRedeem*/
uint256, /*npmStakeToRemove*/
bool /*exit*/
) external override {
// @suppress-zero-value-check The uint values are not used and therefore not checked
s.mustNotBePaused();
s.mustBeProtocolMember(msg.sender);
s.senderMustBeVaultContract(coverKey);
s.updateStateAndLiquidityInternal(coverKey);
}
/**
* @dev Calculates the amount of PODs to mint for the given amount of stablecoin
*
* @param coverKey Enter the cover for which you want to calculate PODs
* @param stablecoinIn Enter the amount in the stablecoin units
*
* @return Returns the units of PODs to be minted if this stablecoin liquidity was supplied.
* Be warned that this value may change based on the cover vault's usage.
*
*/
function calculatePodsImplementation(bytes32 coverKey, uint256 stablecoinIn) external view override returns (uint256) {
s.senderMustBeVaultContract(coverKey);
address pod = msg.sender;
return s.calculatePodsInternal(coverKey, pod, stablecoinIn);
}
/**
* @dev Calculates the amount of stablecoin units to receive for the given amount of PODs to redeem
*
* @param coverKey Enter the cover for which you want to calculate PODs
* @param podsToBurn Enter the amount in the POD units to redeem
*
* @return Returns the units of stablecoins to redeem if the specified PODs were burned.
* Be warned that this value may change based on the cover's vault usage.
*
*/
function calculateLiquidityImplementation(bytes32 coverKey, uint256 podsToBurn) external view override returns (uint256) {
s.senderMustBeVaultContract(coverKey);
address pod = msg.sender;
return s.calculateLiquidityInternal(coverKey, pod, podsToBurn);
}
/**
* @dev Returns the stablecoin balance of this vault
* This also includes amounts lent out in lending strategies by this vault
*
* Warning: this function does not validate the cover key supplied.
*
* @param coverKey Enter the cover for which you want to get the stablecoin balance
*/
function getStablecoinBalanceOfImplementation(bytes32 coverKey) external view override returns (uint256) {
s.senderMustBeVaultContract(coverKey);
return s.getStablecoinOwnedByVaultInternal(coverKey);
}
/**
* @dev Gets information of a given vault by the cover key
*
* Warning: this function does not validate the cover key and account supplied.
*
* @param coverKey Specify cover key to obtain the info of
* @param you The address for which the info will be customized
*
*/
function getInfoImplementation(bytes32 coverKey, address you) external view override returns (IVault.VaultInfoType memory) {
s.senderMustBeVaultContract(coverKey);
address pod = msg.sender;
return s.getInfoInternal(coverKey, pod, you);
}
/**
* @dev Version number of this contract
*/
function version() external pure override returns (bytes32) {
return "v0.1";
}
/**
* @dev Name of this contract
*/
function getName() external pure override returns (bytes32) {
return ProtoUtilV1.CNAME_VAULT_DELEGATE;
}
}