@ensuro/core 2.0.0-beta4 → 2.0.0-beta5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/build/contracts/AccessManager.sol/AccessManager.json +487 -0
  2. package/build/contracts/EToken.sol/EToken.json +4 -4
  3. package/build/contracts/LPManualWhitelist.sol/LPManualWhitelist.json +4 -4
  4. package/build/contracts/PolicyNFT.sol/PolicyNFT.json +2 -2
  5. package/build/contracts/PolicyPool.sol/PolicyPool.json +148 -15
  6. package/build/contracts/PolicyPoolComponent.sol/PolicyPoolComponent.json +2 -2
  7. package/build/contracts/PremiumsAccount.sol/PremiumsAccount.json +4 -4
  8. package/build/contracts/Reserve.sol/Reserve.json +2 -2
  9. package/build/contracts/RiskModule.sol/RiskModule.json +2 -2
  10. package/build/contracts/TrustfulRiskModule.sol/TrustfulRiskModule.json +4 -4
  11. package/build/contracts/interfaces/{IPolicyPoolConfig.sol/IPolicyPoolConfig.json → IAccessManager.sol/IAccessManager.json} +2 -67
  12. package/build/contracts/interfaces/IPolicyPool.sol/IPolicyPool.json +15 -2
  13. package/build/contracts/mocks/PolicyPoolComponentMock.sol/PolicyPoolComponentMock.json +2 -2
  14. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMock.json +19 -6
  15. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMockForward.json +6 -6
  16. package/contracts/AccessManager.sol +72 -0
  17. package/contracts/EToken.sol +6 -13
  18. package/contracts/PolicyNFT.sol +3 -3
  19. package/contracts/PolicyPool.sol +108 -43
  20. package/contracts/PolicyPoolComponent.sol +11 -11
  21. package/contracts/PremiumsAccount.sol +1 -1
  22. package/contracts/RiskModule.sol +4 -8
  23. package/contracts/interfaces/{IPolicyPoolConfig.sol → IAccessManager.sol} +2 -21
  24. package/contracts/interfaces/IPolicyPool.sol +10 -5
  25. package/contracts/mocks/PolicyPoolMock.sol +15 -13
  26. package/js/test-utils.js +177 -119
  27. package/package.json +1 -1
  28. package/build/contracts/FlightDelayRiskModule.sol/FlightDelayRiskModule.json +0 -890
  29. package/build/contracts/PolicyPoolConfig.sol/PolicyPoolConfig.json +0 -665
  30. package/build/contracts/dependencies/ChainlinkClientUpgradeable.sol/ChainlinkClientUpgradeable.json +0 -63
  31. package/build/contracts/mocks/LinkTokenMock.sol/LinkTokenMock.json +0 -370
  32. package/build/contracts/upgraded/IPolicyNFTv1.sol/IPolicyNFTv1.json +0 -322
  33. package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1.json +0 -584
  34. package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1_Upgrade.json +0 -615
  35. package/contracts/FlightDelayRiskModule.sol +0 -217
  36. package/contracts/PolicyPoolConfig.sol +0 -185
  37. package/contracts/dependencies/ChainlinkClientUpgradeable.sol +0 -371
  38. package/contracts/mocks/LinkTokenMock.sol +0 -30
  39. package/contracts/upgraded/IPolicyNFTv1.sol +0 -15
  40. package/contracts/upgraded/PolicyNFTv1.sol +0 -126
@@ -1,217 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- pragma solidity ^0.8.0;
3
-
4
- import {IPolicyPool} from "./interfaces/IPolicyPool.sol";
5
- import {IPremiumsAccount} from "./interfaces/IPremiumsAccount.sol";
6
- import {Policy} from "./Policy.sol";
7
- import {RiskModule} from "./RiskModule.sol";
8
- import {Chainlink} from "@chainlink/contracts/src/v0.8/Chainlink.sol";
9
- import {ChainlinkClientUpgradeable} from "./dependencies/ChainlinkClientUpgradeable.sol";
10
-
11
- /**
12
- * @title Flight Delay Risk Module
13
- * @dev Risk Module that resolves policy based in actualarrivaldate of flight
14
- * @custom:security-contact security@ensuro.co
15
- * @author Ensuro
16
- */
17
- contract FlightDelayRiskModule is RiskModule, ChainlinkClientUpgradeable {
18
- using Chainlink for Chainlink.Request;
19
-
20
- bytes32 public constant PRICER_ROLE = keccak256("PRICER_ROLE");
21
- bytes32 public constant ORACLE_ADMIN_ROLE = keccak256("ORACLE_ADMIN_ROLE");
22
- // Multiplier to calculate expiration = expectedArrival + tolerance + delayTime * DELAY_EXPIRATION_TIMES
23
- uint40 public constant DELAY_EXPIRATION_TIMES = 5;
24
-
25
- struct PolicyData {
26
- Policy.PolicyData ensuroPolicy;
27
- string flight;
28
- uint40 departure;
29
- uint40 expectedArrival;
30
- uint40 tolerance;
31
- }
32
-
33
- struct OracleParams {
34
- address oracle;
35
- uint96 delayTime;
36
- uint256 fee;
37
- bytes16 dataJobId;
38
- bytes16 sleepJobId;
39
- }
40
-
41
- OracleParams internal _oracleParams;
42
-
43
- mapping(bytes32 => uint256) internal _pendingQueries;
44
- mapping(uint256 => PolicyData) internal _policies;
45
-
46
- /// @custom:oz-upgrades-unsafe-allow constructor
47
- // solhint-disable-next-line no-empty-blocks
48
- constructor(IPolicyPool policyPool_, IPremiumsAccount premiumsAccount_)
49
- RiskModule(policyPool_, premiumsAccount_)
50
- {} // solhint-disable-line no-empty-blocks
51
-
52
- /**
53
- * @dev Initializes the RiskModule
54
- * @param name_ Name of the Risk Module
55
- * @param collRatio_ Collateralization ratio to compute solvency requirement as % of payout (in ray)
56
- * @param ensuroPpFee_ % of pure premium that will go for Ensuro treasury (in ray)
57
- * @param srRoc_ return on capital paid to Senior LPs (annualized percentage - in ray)
58
- * @param maxPayoutPerPolicy_ Maximum payout per policy (in wad)
59
- * @param exposureLimit_ Max exposure (sum of payouts) to be allocated to this module (in wad)
60
- * @param wallet_ Address of the RiskModule provider
61
- * @param linkToken_ Address of ChainLink LINK token
62
- * @param oracleParams_ Parameters of the Oracle
63
- */
64
- function initialize(
65
- string memory name_,
66
- uint256 collRatio_,
67
- uint256 ensuroPpFee_,
68
- uint256 srRoc_,
69
- uint256 maxPayoutPerPolicy_,
70
- uint256 exposureLimit_,
71
- address wallet_,
72
- address linkToken_,
73
- OracleParams memory oracleParams_
74
- ) public initializer {
75
- __RiskModule_init(
76
- name_,
77
- collRatio_,
78
- ensuroPpFee_,
79
- srRoc_,
80
- maxPayoutPerPolicy_,
81
- exposureLimit_,
82
- wallet_
83
- );
84
- __ChainlinkClient_init();
85
- __FlightDelayRiskModule_init_unchained(linkToken_, oracleParams_);
86
- }
87
-
88
- // solhint-disable-next-line func-name-mixedcase
89
- function __FlightDelayRiskModule_init_unchained(
90
- address linkToken_,
91
- OracleParams memory oracleParams_
92
- ) internal initializer {
93
- setChainlinkToken(linkToken_);
94
- _oracleParams = oracleParams_;
95
- }
96
-
97
- function setOracleParams(OracleParams memory newParams)
98
- external
99
- onlyComponentRole(ORACLE_ADMIN_ROLE)
100
- {
101
- _oracleParams = newParams;
102
- }
103
-
104
- function oracleParams() external view returns (OracleParams memory) {
105
- return _oracleParams;
106
- }
107
-
108
- /**
109
- * @dev Creates a new policy
110
- * @param flight Flight Number as String (ex: NAX105)
111
- * @param departure Departure in epoch seconds (ex: 1631817600)
112
- * @param expectedArrival Expected arrival in epoch seconds (ex: 1631824800)
113
- * @param tolerance In seconds, the tolerance margin after expectedArrival before trigger the policy
114
- * @param payout Payout for customer in case policy is triggered
115
- * @param premium Premium the customer pays
116
- * @param lossProb Probability of policy being triggered
117
- * @param customer Customer address (to take premium from and send payout)
118
- */
119
- function newPolicy(
120
- string memory flight,
121
- uint40 departure,
122
- uint40 expectedArrival,
123
- uint40 tolerance,
124
- uint256 payout,
125
- uint256 premium,
126
- uint256 lossProb,
127
- address customer,
128
- uint96 internalId
129
- ) external onlyComponentRole(PRICER_ROLE) returns (uint256) {
130
- require(expectedArrival > block.timestamp, "expectedArrival can't be in the past");
131
- require(departure != 0 && expectedArrival > departure, "expectedArrival <= departure!");
132
- uint40 expiration = expectedArrival +
133
- tolerance +
134
- uint40(_oracleParams.delayTime) *
135
- DELAY_EXPIRATION_TIMES;
136
- Policy.PolicyData memory ensuroPolicy = _newPolicy(
137
- payout,
138
- premium,
139
- lossProb,
140
- expiration,
141
- customer,
142
- internalId
143
- );
144
- PolicyData storage policy = _policies[ensuroPolicy.id];
145
- policy.ensuroPolicy = ensuroPolicy;
146
- policy.flight = flight;
147
- policy.departure = departure;
148
- policy.expectedArrival = expectedArrival;
149
- policy.tolerance = tolerance;
150
-
151
- uint256 until = expectedArrival + tolerance + uint256(_oracleParams.delayTime);
152
- _chainlinkRequest(ensuroPolicy.id, policy, until);
153
- return ensuroPolicy.id;
154
- }
155
-
156
- function _chainlinkRequest(
157
- uint256 policyId,
158
- PolicyData storage policy,
159
- uint256 until
160
- ) internal {
161
- // request takes a JobID, a callback address, and callback function as input
162
- Chainlink.Request memory req = buildChainlinkRequest(
163
- until == 0 ? _oracleParams.dataJobId : _oracleParams.sleepJobId,
164
- address(this),
165
- this.fulfill.selector
166
- );
167
- req.add("flight", policy.flight);
168
- req.add("endpoint", "actualarrivaldate");
169
- req.addUint("departure", policy.departure);
170
- if (until > 0) {
171
- req.addUint("until", until);
172
- }
173
-
174
- // Sends the request with the amount of payment specified to the oracle
175
- // (results will arrive with the callback = later)
176
- bytes32 queryId = sendChainlinkRequestTo(_oracleParams.oracle, req, _oracleParams.fee);
177
- _pendingQueries[queryId] = policyId;
178
- }
179
-
180
- /**
181
- * @dev Forces the resolution of the policy (without waiting Chainlink scheduled on creation)
182
- * @param policyId The id of the policy previously created (in newPolicy)
183
- */
184
- function resolvePolicy(uint256 policyId)
185
- external
186
- onlyComponentRole(PRICER_ROLE)
187
- returns (uint256)
188
- {
189
- PolicyData storage policy = _policies[policyId];
190
- require(policy.expectedArrival != 0, "Policy not found!");
191
- _chainlinkRequest(policyId, policy, 0);
192
- return policyId;
193
- }
194
-
195
- function fulfill(bytes32 queryId, int256 actualArrivalDate)
196
- public
197
- recordChainlinkFulfillment(queryId)
198
- {
199
- uint256 policyId = _pendingQueries[queryId];
200
- require(policyId != 0, "queryId not found!");
201
- PolicyData storage policy = _policies[policyId];
202
-
203
- if (actualArrivalDate == 0) {
204
- if (block.timestamp > (policy.expectedArrival + policy.tolerance)) {
205
- // Treat as arrived after tolerance
206
- actualArrivalDate = int256(uint256((policy.expectedArrival + policy.tolerance) + 1));
207
- } else {
208
- // Not arrived yet
209
- return;
210
- }
211
- }
212
- bool customerWon = (actualArrivalDate <= 0 || // cancelled
213
- uint256(actualArrivalDate) > uint256(policy.expectedArrival + policy.tolerance)); // arrived after tolerance
214
-
215
- _policyPool.resolvePolicyFullPayout(policy.ensuroPolicy, customerWon);
216
- }
217
- }
@@ -1,185 +0,0 @@
1
- // SPDX-License-Identifier: MIT
2
- pragma solidity ^0.8.0;
3
-
4
- import {AccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
5
- import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
6
- import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
7
- import {IPolicyPoolConfig} from "./interfaces/IPolicyPoolConfig.sol";
8
- import {IPolicyPool} from "./interfaces/IPolicyPool.sol";
9
- import {IRiskModule} from "./interfaces/IRiskModule.sol";
10
- import {IPolicyPoolComponent} from "./interfaces/IPolicyPoolComponent.sol";
11
- import {WadRayMath} from "./WadRayMath.sol";
12
-
13
- /**
14
- * @title PolicyPoolConfig - Protocol access roles and other settings/components
15
- * @dev Contract that holds the access roles for PolicyPool and other components of the protocol.
16
- * @custom:security-contact security@ensuro.co
17
- * @author Ensuro
18
- */
19
- contract PolicyPoolConfig is
20
- Initializable,
21
- AccessControlUpgradeable,
22
- UUPSUpgradeable,
23
- IPolicyPoolConfig
24
- {
25
- using WadRayMath for uint256;
26
-
27
- // Core governance roles
28
- bytes32 public constant GUARDIAN_ROLE = keccak256("GUARDIAN_ROLE");
29
- bytes32 public constant LEVEL1_ROLE = keccak256("LEVEL1_ROLE");
30
- bytes32 public constant LEVEL2_ROLE = keccak256("LEVEL2_ROLE");
31
- bytes32 public constant LEVEL3_ROLE = keccak256("LEVEL3_ROLE");
32
-
33
- uint256 public constant L2_RM_LIMIT = 5e16; // 5% in WAD
34
-
35
- address internal _treasury; // address of Ensuro treasury
36
- IPolicyPool internal _policyPool;
37
-
38
- mapping(IRiskModule => RiskModuleStatus) private _riskModules;
39
-
40
- event ComponentChanged(IPolicyPoolConfig.GovernanceActions indexed action, address value);
41
-
42
- modifier onlyRole2(bytes32 role1, bytes32 role2) {
43
- if (!hasRole(role1, _msgSender())) _checkRole(role2, _msgSender());
44
- _;
45
- }
46
-
47
- modifier onlyRole3(
48
- bytes32 role1,
49
- bytes32 role2,
50
- bytes32 role3
51
- ) {
52
- if (!hasRole(role1, _msgSender()) && !hasRole(role2, _msgSender())) {
53
- _checkRole(role3, _msgSender());
54
- }
55
- _;
56
- }
57
-
58
- function initialize(IPolicyPool policyPool_, address treasury_) public initializer {
59
- __AccessControl_init();
60
- __UUPSUpgradeable_init();
61
- __PolicyPoolConfig_init_unchained(policyPool_, treasury_);
62
- }
63
-
64
- // solhint-disable-next-line func-name-mixedcase
65
- function __PolicyPoolConfig_init_unchained(IPolicyPool policyPool_, address treasury_)
66
- internal
67
- initializer
68
- {
69
- _setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
70
- _policyPool = policyPool_;
71
- _treasury = treasury_;
72
- }
73
-
74
- /**
75
- * @dev This function can be called only once in contract's lifetime. It links the PolicyPoolConfig with the
76
- * PolicyPool contract. It's called in PolicyPool initialization.
77
- */
78
- function connect() external override {
79
- require(
80
- address(_policyPool) == address(0) || address(_policyPool) == _msgSender(),
81
- "PolicyPool already connected"
82
- );
83
- _policyPool = IPolicyPool(_msgSender());
84
- // Not possible to do this validation because connect is called in _policyPool initialize :'(
85
- // require(_policyPool.config() == this, "PolicyPool not connected to this config");
86
- }
87
-
88
- function policyPool() external view returns (IPolicyPool) {
89
- return _policyPool;
90
- }
91
-
92
- // solhint-disable-next-line no-empty-blocks
93
- function _authorizeUpgrade(address) internal override onlyRole2(GUARDIAN_ROLE, LEVEL1_ROLE) {}
94
-
95
- function getComponentRole(address component, bytes32 role) public pure returns (bytes32) {
96
- return bytes32(bytes20(component)) ^ role;
97
- }
98
-
99
- function checkComponentRole(
100
- address component,
101
- bytes32 role,
102
- address account
103
- ) external view override {
104
- _checkRole(getComponentRole(component, role), account);
105
- }
106
-
107
- function grantComponentRole(
108
- address component,
109
- bytes32 role,
110
- address account
111
- ) external onlyRole(getRoleAdmin(getComponentRole(component, role))) {
112
- _grantRole(getComponentRole(component, role), account);
113
- }
114
-
115
- function checkRole(bytes32 role, address account) external view override {
116
- _checkRole(role, account);
117
- }
118
-
119
- function checkRole2(
120
- bytes32 role1,
121
- bytes32 role2,
122
- address account
123
- ) external view override {
124
- if (!hasRole(role1, account)) _checkRole(role2, account);
125
- }
126
-
127
- function setTreasury(address treasury_) external onlyRole(LEVEL1_ROLE) {
128
- _treasury = treasury_;
129
- emit ComponentChanged(GovernanceActions.setTreasury, _treasury);
130
- }
131
-
132
- function treasury() external view override returns (address) {
133
- return _treasury;
134
- }
135
-
136
- function addRiskModule(IRiskModule riskModule) external onlyRole(LEVEL1_ROLE) {
137
- require(
138
- _riskModules[riskModule] == RiskModuleStatus.inactive,
139
- "Risk Module already in the pool"
140
- );
141
- require(address(riskModule) != address(0), "riskModule can't be zero");
142
- require(
143
- IPolicyPoolComponent(address(riskModule)).policyPool() == _policyPool,
144
- "RiskModule not linked to this pool"
145
- );
146
- _riskModules[riskModule] = RiskModuleStatus.active;
147
- emit RiskModuleStatusChanged(riskModule, RiskModuleStatus.active);
148
- }
149
-
150
- function removeRiskModule(IRiskModule riskModule) external onlyRole(LEVEL2_ROLE) {
151
- require(_riskModules[riskModule] != RiskModuleStatus.inactive, "Risk Module not found");
152
- require(riskModule.activeExposure() == 0, "Can't remove a module with active policies");
153
- delete _riskModules[riskModule];
154
- emit RiskModuleStatusChanged(riskModule, RiskModuleStatus.inactive);
155
- }
156
-
157
- function changeRiskModuleStatus(IRiskModule riskModule, RiskModuleStatus newStatus)
158
- external
159
- onlyRole2(GUARDIAN_ROLE, LEVEL1_ROLE)
160
- {
161
- require(_riskModules[riskModule] != RiskModuleStatus.inactive, "Risk Module not found");
162
- require(
163
- newStatus != RiskModuleStatus.suspended || hasRole(GUARDIAN_ROLE, msg.sender),
164
- "Only GUARDIAN can suspend modules"
165
- );
166
- // To activate LEVEL1 required or LEVEL2 if <5% of total liquidity
167
- require(hasRole(LEVEL1_ROLE, msg.sender), "Only LEVEL1 can activate modules");
168
- // Anyone (LEVEL1, GUARDIAN) can deprecate
169
- _riskModules[riskModule] = newStatus;
170
- emit RiskModuleStatusChanged(riskModule, newStatus);
171
- }
172
-
173
- function checkAcceptsNewPolicy(IRiskModule riskModule) external view override {
174
- RiskModuleStatus rmStatus = _riskModules[riskModule];
175
- require(rmStatus == RiskModuleStatus.active, "RM module not found or not active");
176
- }
177
-
178
- function checkAcceptsResolvePolicy(IRiskModule riskModule) external view override {
179
- RiskModuleStatus rmStatus = _riskModules[riskModule];
180
- require(
181
- rmStatus == RiskModuleStatus.active || rmStatus == RiskModuleStatus.deprecated,
182
- "Module must be active or deprecated to process resolutions"
183
- );
184
- }
185
- }