-
Notifications
You must be signed in to change notification settings - Fork 35
/
ValidationLibV1.sol
514 lines (444 loc) · 17.2 KB
/
ValidationLibV1.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
512
513
514
// 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 "openzeppelin-solidity/contracts/access/IAccessControl.sol";
import "../interfaces/IStore.sol";
import "../interfaces/IPausable.sol";
import "../interfaces/ICxToken.sol";
import "./GovernanceUtilV1.sol";
import "./AccessControlLibV1.sol";
library ValidationLibV1 {
using CoverUtilV1 for IStore;
using GovernanceUtilV1 for IStore;
using ProtoUtilV1 for IStore;
using RegistryLibV1 for IStore;
using StoreKeyUtil for IStore;
/**
* @dev Reverts if the protocol is paused
*/
function mustNotBePaused(IStore s) public view {
address protocol = s.getProtocolAddressInternal();
require(IPausable(protocol).paused() == false, "Protocol is paused");
}
/**
* @dev Reverts if the cover or any of the cover's product is not normal.
* @param coverKey Enter the cover key to check
*/
function mustEnsureAllProductsAreNormal(IStore s, bytes32 coverKey) external view {
require(s.getBoolByKeys(ProtoUtilV1.NS_COVER, coverKey), "Cover does not exist");
require(s.isCoverNormalInternal(coverKey) == true, "Status not normal");
}
/**
* @dev Reverts if the key does not resolve in a valid cover contract
* or if the cover is under governance.
* @param coverKey Enter the cover key to check
* @param productKey Enter the product key to check
*/
function mustHaveNormalProductStatus(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
require(s.getBoolByKeys(ProtoUtilV1.NS_COVER, coverKey), "Cover does not exist");
require(s.getProductStatusInternal(coverKey, productKey) == CoverUtilV1.ProductStatus.Normal, "Status not normal");
}
/**
* @dev Reverts if the key does not resolve in a valid cover contract.
* @param coverKey Enter the cover key to check
*/
function mustBeValidCoverKey(IStore s, bytes32 coverKey) external view {
require(s.getBoolByKeys(ProtoUtilV1.NS_COVER, coverKey), "Cover does not exist");
}
/**
* @dev Reverts if the cover does not support creating products.
* @param coverKey Enter the cover key to check
*/
function mustSupportProducts(IStore s, bytes32 coverKey) external view {
require(s.supportsProductsInternal(coverKey), "Does not have products");
}
/**
* @dev Reverts if the key does not resolve in a valid product of a cover contract.
* @param coverKey Enter the cover key to check
* @param productKey Enter the cover key to check
*/
function mustBeValidProduct(
IStore s,
bytes32 coverKey,
bytes32 productKey
) public view {
require(s.isValidProductInternal(coverKey, productKey), "Product does not exist");
}
/**
* @dev Reverts if the key resolves in an expired product.
* @param coverKey Enter the cover key to check
* @param productKey Enter the cover key to check
*/
function mustBeActiveProduct(
IStore s,
bytes32 coverKey,
bytes32 productKey
) public view {
require(s.isActiveProductInternal(coverKey, productKey), "Product retired or deleted");
}
/**
* @dev Reverts if the sender is not the cover owner
* @param coverKey Enter the cover key to check
* @param sender The `msg.sender` value
*/
function mustBeCoverOwner(
IStore s,
bytes32 coverKey,
address sender
) public view {
bool isCoverOwner = s.getCoverOwnerInternal(coverKey) == sender;
require(isCoverOwner, "Forbidden");
}
/**
* @dev Reverts if the sender is not the cover owner or the cover contract
* @param coverKey Enter the cover key to check
* @param sender The `msg.sender` value
*/
function mustBeCoverOwnerOrCoverContract(
IStore s,
bytes32 coverKey,
address sender
) external view {
bool isCoverOwner = s.getCoverOwnerInternal(coverKey) == sender;
bool isCoverContract = address(s.getCoverContract()) == sender;
require(isCoverOwner || isCoverContract, "Forbidden");
}
function senderMustBeCoverOwnerOrAdmin(IStore s, bytes32 coverKey) external view {
if (AccessControlLibV1.hasAccessInternal(s, AccessControlLibV1.NS_ROLES_ADMIN, msg.sender) == false) {
mustBeCoverOwner(s, coverKey, msg.sender);
}
}
function senderMustBePolicyContract(IStore s) external view {
s.senderMustBeExactContract(ProtoUtilV1.CNS_COVER_POLICY);
}
function senderMustBePolicyManagerContract(IStore s) external view {
s.senderMustBeExactContract(ProtoUtilV1.CNS_COVER_POLICY_MANAGER);
}
function senderMustBeCoverContract(IStore s) external view {
s.senderMustBeExactContract(ProtoUtilV1.CNS_COVER);
}
function senderMustBeVaultContract(IStore s, bytes32 coverKey) external view {
address vault = s.getVaultAddress(coverKey);
require(msg.sender == vault, "Forbidden");
}
function senderMustBeGovernanceContract(IStore s) external view {
s.senderMustBeExactContract(ProtoUtilV1.CNS_GOVERNANCE);
}
function senderMustBeClaimsProcessorContract(IStore s) external view {
s.senderMustBeExactContract(ProtoUtilV1.CNS_CLAIM_PROCESSOR);
}
function callerMustBeClaimsProcessorContract(IStore s, address caller) external view {
s.callerMustBeExactContract(ProtoUtilV1.CNS_CLAIM_PROCESSOR, caller);
}
function senderMustBeStrategyContract(IStore s) external view {
bool senderIsStrategyContract = s.getBoolByKey(_getIsActiveStrategyKey(msg.sender));
require(senderIsStrategyContract == true, "Not a strategy contract");
}
function callerMustBeStrategyContract(IStore s, address caller) public view {
bool isActive = s.getBoolByKey(_getIsActiveStrategyKey(caller));
bool wasDisabled = s.getBoolByKey(_getIsDisabledStrategyKey(caller));
require(isActive == true || wasDisabled == true, "Not a strategy contract");
}
function callerMustBeSpecificStrategyContract(
IStore s,
address caller,
bytes32 strategyName
) external view {
callerMustBeStrategyContract(s, caller);
require(IMember(caller).getName() == strategyName, "Access denied");
}
/**
* @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));
}
function senderMustBeProtocolMember(IStore s) external view {
require(s.isProtocolMemberInternal(msg.sender), "Forbidden");
}
function mustBeReporting(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
require(s.getProductStatusInternal(coverKey, productKey) == CoverUtilV1.ProductStatus.IncidentHappened, "Not reporting");
}
function mustBeDisputed(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
require(s.getProductStatusInternal(coverKey, productKey) == CoverUtilV1.ProductStatus.FalseReporting, "Not disputed");
}
function mustBeClaimable(
IStore s,
bytes32 coverKey,
bytes32 productKey
) public view {
require(s.getProductStatusInternal(coverKey, productKey) == CoverUtilV1.ProductStatus.Claimable, "Not claimable");
}
function mustBeClaimingOrDisputed(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
CoverUtilV1.ProductStatus status = s.getProductStatusInternal(coverKey, productKey);
bool claiming = status == CoverUtilV1.ProductStatus.Claimable;
bool falseReporting = status == CoverUtilV1.ProductStatus.FalseReporting;
require(claiming || falseReporting, "Not claimable nor disputed");
}
function mustBeReportingOrDisputed(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
CoverUtilV1.ProductStatus status = s.getProductStatusInternal(coverKey, productKey);
bool incidentHappened = status == CoverUtilV1.ProductStatus.IncidentHappened;
bool falseReporting = status == CoverUtilV1.ProductStatus.FalseReporting;
require(incidentHappened || falseReporting, "Not reported nor disputed");
}
function mustBeBeforeResolutionDeadline(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
uint256 deadline = s.getResolutionDeadlineInternal(coverKey, productKey);
if (deadline > 0) {
require(block.timestamp < deadline, "Emergency resolution deadline over"); // solhint-disable-line
}
}
function mustNotHaveResolutionDeadline(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
uint256 deadline = s.getResolutionDeadlineInternal(coverKey, productKey);
require(deadline == 0, "Resolution already has deadline");
}
function mustBeAfterResolutionDeadline(
IStore s,
bytes32 coverKey,
bytes32 productKey
) public view {
uint256 deadline = s.getResolutionDeadlineInternal(coverKey, productKey);
require(deadline > 0 && block.timestamp >= deadline, "Still unresolved"); // solhint-disable-line
}
function mustBeAfterFinalization(
IStore s,
bytes32 coverKey,
bytes32 productKey,
uint256 incidentDate
) public view {
require(s.getBoolByKey(GovernanceUtilV1.getHasFinalizedKeyInternal(coverKey, productKey, incidentDate)), "Incident not finalized");
}
function mustBeValidIncidentDate(
IStore s,
bytes32 coverKey,
bytes32 productKey,
uint256 incidentDate
) public view {
require(s.getActiveIncidentDateInternal(coverKey, productKey) == incidentDate, "Invalid incident date");
}
function mustHaveDispute(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
bool hasDispute = s.getBoolByKey(GovernanceUtilV1.getHasDisputeKeyInternal(coverKey, productKey));
require(hasDispute == true, "Not disputed");
}
function mustNotHaveDispute(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
bool hasDispute = s.getBoolByKey(GovernanceUtilV1.getHasDisputeKeyInternal(coverKey, productKey));
require(hasDispute == false, "Already disputed");
}
function mustBeDuringReportingPeriod(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
require(s.getResolutionTimestampInternal(coverKey, productKey) >= block.timestamp, "Reporting window closed"); // solhint-disable-line
}
function mustBeAfterReportingPeriod(
IStore s,
bytes32 coverKey,
bytes32 productKey
) public view {
require(block.timestamp > s.getResolutionTimestampInternal(coverKey, productKey), "Reporting still active"); // solhint-disable-line
}
function mustBeValidCxToken(
IStore s,
bytes32 coverKey,
bytes32 productKey,
address cxToken,
uint256 incidentDate
) public view {
require(s.getBoolByKeys(ProtoUtilV1.NS_COVER_CXTOKEN, cxToken) == true, "Unknown cxToken");
bytes32 COVER_KEY = ICxToken(cxToken).COVER_KEY(); // solhint-disable-line
bytes32 PRODUCT_KEY = ICxToken(cxToken).PRODUCT_KEY(); // solhint-disable-line
require(coverKey == COVER_KEY && productKey == PRODUCT_KEY, "Invalid cxToken");
uint256 expires = ICxToken(cxToken).expiresOn();
require(expires > incidentDate, "Invalid or expired cxToken");
}
function mustBeValidClaim(
IStore s,
address account,
bytes32 coverKey,
bytes32 productKey,
address cxToken,
uint256 incidentDate,
uint256 amount
) external view {
mustBeSupportedProductOrEmpty(s, coverKey, productKey);
mustBeValidCxToken(s, coverKey, productKey, cxToken, incidentDate);
mustBeClaimable(s, coverKey, productKey);
mustBeValidIncidentDate(s, coverKey, productKey, incidentDate);
mustBeDuringClaimPeriod(s, coverKey, productKey);
require(ICxToken(cxToken).getClaimablePolicyOf(account) >= amount, "Claim exceeds your coverage");
}
function mustNotHaveUnstaken(
IStore s,
address account,
bytes32 coverKey,
bytes32 productKey,
uint256 incidentDate
) public view {
uint256 withdrawal = s.getReportingUnstakenAmountInternal(account, coverKey, productKey, incidentDate);
require(withdrawal == 0, "Already unstaken");
}
/**
* @dev Validates your `unstakeWithoutClaim` arguments
*
* @custom:note This function is not intended be used and does not produce correct result
* before an incident is finalized. Please use `validateUnstakeWithClaim` if you are accessing
* this function after resolution and before finalization.
*/
function validateUnstakeWithoutClaim(
IStore s,
bytes32 coverKey,
bytes32 productKey,
uint256 incidentDate
) external view {
mustNotBePaused(s);
mustBeSupportedProductOrEmpty(s, coverKey, productKey);
mustNotHaveUnstaken(s, msg.sender, coverKey, productKey, incidentDate);
mustBeAfterFinalization(s, coverKey, productKey, incidentDate);
}
/**
* @dev Validates your `unstakeWithClaim` arguments
*
* @custom:note This function is only intended be used after resolution and before finalization.
* Please use `validateUnstakeWithoutClaim` if you are accessing
* this function after finalization.
*/
function validateUnstakeWithClaim(
IStore s,
bytes32 coverKey,
bytes32 productKey,
uint256 incidentDate
) external view {
mustNotBePaused(s);
mustBeSupportedProductOrEmpty(s, coverKey, productKey);
mustNotHaveUnstaken(s, msg.sender, coverKey, productKey, incidentDate);
// If this reporting gets finalized, incident date will become invalid
// meaning this execution will revert thereby restricting late comers
// to access this feature. But they can still access `unstake` feature
// to withdraw their stake.
mustBeValidIncidentDate(s, coverKey, productKey, incidentDate);
// Before the deadline, emergency resolution can still happen
// that may have an impact on the final decision. We, therefore, have to wait.
mustBeAfterResolutionDeadline(s, coverKey, productKey);
}
function mustBeDuringClaimPeriod(
IStore s,
bytes32 coverKey,
bytes32 productKey
) public view {
uint256 beginsFrom = s.getUintByKeys(ProtoUtilV1.NS_CLAIM_BEGIN_TS, coverKey, productKey);
uint256 expiresAt = s.getUintByKeys(ProtoUtilV1.NS_CLAIM_EXPIRY_TS, coverKey, productKey);
require(beginsFrom > 0, "Invalid claim begin date");
require(expiresAt > beginsFrom, "Invalid claim period");
require(block.timestamp >= beginsFrom, "Claim period hasn't begun"); // solhint-disable-line
require(block.timestamp <= expiresAt, "Claim period has expired"); // solhint-disable-line
}
function mustBeAfterClaimExpiry(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
require(block.timestamp > s.getUintByKeys(ProtoUtilV1.NS_CLAIM_EXPIRY_TS, coverKey, productKey), "Claim still active"); // solhint-disable-line
}
/**
* @dev Reverts if the sender is not whitelisted cover creator.
*/
function senderMustBeWhitelistedCoverCreator(IStore s) external view {
require(s.getAddressBooleanByKey(ProtoUtilV1.NS_COVER_CREATOR_WHITELIST, msg.sender), "Not whitelisted");
}
function senderMustBeWhitelistedIfRequired(
IStore s,
bytes32 coverKey,
bytes32 productKey,
address sender
) external view {
bool supportsProducts = s.supportsProductsInternal(coverKey);
bool required = supportsProducts ? s.checkIfProductRequiresWhitelistInternal(coverKey, productKey) : s.checkIfRequiresWhitelistInternal(coverKey);
if (required == false) {
return;
}
require(s.getAddressBooleanByKeys(ProtoUtilV1.NS_COVER_USER_WHITELIST, coverKey, productKey, sender), "You are not whitelisted");
}
function mustBeSupportedProductOrEmpty(
IStore s,
bytes32 coverKey,
bytes32 productKey
) public view {
bool hasProducts = s.supportsProductsInternal(coverKey);
hasProducts ? require(productKey > 0, "Specify a product") : require(productKey == 0, "Invalid product");
if (hasProducts) {
mustBeValidProduct(s, coverKey, productKey);
mustBeActiveProduct(s, coverKey, productKey);
}
}
function mustNotHavePolicyDisabled(
IStore s,
bytes32 coverKey,
bytes32 productKey
) external view {
require(!s.isPolicyDisabledInternal(coverKey, productKey), "Policy purchase disabled");
}
function mustMaintainStablecoinThreshold(IStore s, uint256 amount) external view {
uint256 stablecoinPrecision = s.getStablecoinPrecisionInternal();
require(amount >= ProtoUtilV1.MIN_LIQUIDITY * stablecoinPrecision, "Liquidity is below threshold");
require(amount <= ProtoUtilV1.MAX_LIQUIDITY * stablecoinPrecision, "Liquidity is above threshold");
}
function mustMaintainProposalThreshold(IStore s, uint256 amount) external view {
uint256 stablecoinPrecision = s.getStablecoinPrecisionInternal();
require(amount >= ProtoUtilV1.MIN_PROPOSAL_AMOUNT * stablecoinPrecision, "Proposal is below threshold");
require(amount <= ProtoUtilV1.MAX_PROPOSAL_AMOUNT * stablecoinPrecision, "Proposal is above threshold");
}
}