@ensuro/core 2.0.0-beta1 → 2.0.0-beta11

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 (84) hide show
  1. package/README.md +36 -14
  2. package/build/contracts/AccessManager.sol/AccessManager.json +564 -0
  3. package/build/contracts/ERC4626AssetManager.sol/ERC4626AssetManager.json +238 -0
  4. package/build/contracts/EToken.sol/EToken.json +147 -46
  5. package/build/contracts/LPManualWhitelist.sol/LPManualWhitelist.json +23 -4
  6. package/build/contracts/LiquidityThresholdAssetManager.sol/LiquidityThresholdAssetManager.json +222 -0
  7. package/build/contracts/Policy.sol/Policy.json +2 -2
  8. package/build/contracts/PolicyPool.sol/PolicyPool.json +437 -61
  9. package/build/contracts/PolicyPoolComponent.sol/PolicyPoolComponent.json +21 -2
  10. package/build/contracts/PremiumsAccount.sol/PremiumsAccount.json +142 -16
  11. package/build/contracts/Reserve.sol/Reserve.json +85 -8
  12. package/build/contracts/RiskModule.sol/RiskModule.json +21 -2
  13. package/build/contracts/SignedQuoteRiskModule.sol/SignedQuoteRiskModule.json +979 -0
  14. package/build/contracts/TimeScaled.sol/TimeScaled.json +3 -17
  15. package/build/contracts/TrustfulRiskModule.sol/TrustfulRiskModule.json +24 -5
  16. package/build/contracts/{WadRayMath.sol → dependencies/WadRayMath.sol}/WadRayMath.json +3 -3
  17. package/build/contracts/interfaces/{IPolicyPoolConfig.sol/IPolicyPoolConfig.json → IAccessManager.sol/IAccessManager.json} +77 -59
  18. package/build/contracts/interfaces/IAssetManager.sol/IAssetManager.json +128 -0
  19. package/build/contracts/interfaces/IEToken.sol/IEToken.json +13 -0
  20. package/build/contracts/interfaces/IPolicyPool.sol/IPolicyPool.json +40 -16
  21. package/build/contracts/interfaces/IPolicyPoolComponent.sol/IPolicyPoolComponent.json +19 -0
  22. package/build/contracts/interfaces/IPremiumsAccount.sol/IPremiumsAccount.json +13 -0
  23. package/build/contracts/mocks/FixedRateVault.sol/FixedRateVault.json +736 -0
  24. package/build/contracts/mocks/ForwardProxy.sol/ForwardProxy.json +2 -2
  25. package/build/contracts/mocks/InterfaceIdCalculator.sol/InterfaceIdCalculator.json +180 -0
  26. package/build/contracts/mocks/PolicyHolderMock.sol/PolicyHolderMock.json +28 -2
  27. package/build/contracts/mocks/PolicyPoolComponentMock.sol/PolicyPoolComponentMock.json +21 -2
  28. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMock.json +118 -19
  29. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMockForward.json +6 -6
  30. package/build/contracts/mocks/RiskModuleMock.sol/RiskModuleMock.json +783 -0
  31. package/build/contracts/mocks/TestCurrency.sol/TestCurrency.json +2 -2
  32. package/build/contracts/mocks/TestNFT.sol/TestNFT.json +2 -2
  33. package/contracts/AccessManager.sol +132 -0
  34. package/contracts/ERC4626AssetManager.sol +55 -0
  35. package/contracts/EToken.sol +208 -102
  36. package/contracts/LPManualWhitelist.sol +19 -3
  37. package/contracts/LiquidityThresholdAssetManager.sol +175 -0
  38. package/contracts/Policy.sol +5 -3
  39. package/contracts/PolicyPool.sol +381 -116
  40. package/contracts/PolicyPoolComponent.sol +61 -44
  41. package/contracts/PremiumsAccount.sol +324 -86
  42. package/contracts/Reserve.sol +199 -4
  43. package/contracts/RiskModule.sol +62 -31
  44. package/contracts/SignedQuoteRiskModule.sol +248 -0
  45. package/contracts/TimeScaled.sol +19 -15
  46. package/contracts/TrustfulRiskModule.sol +69 -2
  47. package/contracts/dependencies/WadRayMath.sol +126 -0
  48. package/contracts/interfaces/IAccessManager.sol +148 -0
  49. package/contracts/interfaces/IAssetManager.sol +89 -0
  50. package/contracts/interfaces/IEToken.sol +25 -2
  51. package/contracts/interfaces/ILPWhitelist.sol +17 -0
  52. package/contracts/interfaces/IPolicyPool.sol +21 -14
  53. package/contracts/interfaces/IPolicyPoolComponent.sol +5 -1
  54. package/contracts/interfaces/IPremiumsAccount.sol +5 -0
  55. package/contracts/interfaces/IRiskModule.sol +67 -4
  56. package/contracts/mocks/FixedRateVault.sol +85 -0
  57. package/contracts/mocks/InterfaceIdCalculator.sol +32 -0
  58. package/contracts/mocks/PolicyHolderMock.sol +21 -0
  59. package/contracts/mocks/PolicyPoolComponentMock.sol +10 -0
  60. package/contracts/mocks/PolicyPoolMock.sol +41 -15
  61. package/contracts/mocks/RiskModuleMock.sol +75 -0
  62. package/js/deploy.js +704 -0
  63. package/js/test-utils.js +339 -159
  64. package/package.json +1 -1
  65. package/build/contracts/FlightDelayRiskModule.sol/FlightDelayRiskModule.json +0 -890
  66. package/build/contracts/PolicyNFT.sol/PolicyNFT.json +0 -583
  67. package/build/contracts/PolicyPoolConfig.sol/PolicyPoolConfig.json +0 -691
  68. package/build/contracts/dependencies/ChainlinkClientUpgradeable.sol/ChainlinkClientUpgradeable.json +0 -63
  69. package/build/contracts/interfaces/IPolicyNFT.sol/IPolicyNFT.json +0 -321
  70. package/build/contracts/mocks/LinkTokenMock.sol/LinkTokenMock.json +0 -370
  71. package/build/contracts/mocks/TestCurrencyUUPS.sol/TestCurrencyUUPS.json +0 -353
  72. package/build/contracts/upgraded/IPolicyNFTv1.sol/IPolicyNFTv1.json +0 -322
  73. package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1.json +0 -584
  74. package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1_Upgrade.json +0 -615
  75. package/contracts/FlightDelayRiskModule.sol +0 -217
  76. package/contracts/PolicyNFT.sol +0 -96
  77. package/contracts/PolicyPoolConfig.sol +0 -204
  78. package/contracts/WadRayMath.sol +0 -135
  79. package/contracts/dependencies/ChainlinkClientUpgradeable.sol +0 -371
  80. package/contracts/interfaces/IPolicyNFT.sol +0 -15
  81. package/contracts/interfaces/IPolicyPoolConfig.sol +0 -96
  82. package/contracts/mocks/LinkTokenMock.sol +0 -30
  83. package/contracts/upgraded/IPolicyNFTv1.sol +0 -15
  84. package/contracts/upgraded/PolicyNFTv1.sol +0 -126
@@ -3,19 +3,20 @@ pragma solidity ^0.8.0;
3
3
 
4
4
  import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
5
5
  import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
6
+ import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
6
7
  import {IPolicyPool} from "./interfaces/IPolicyPool.sol";
7
8
  import {IPolicyPoolComponent} from "./interfaces/IPolicyPoolComponent.sol";
8
- import {IPolicyPoolConfig} from "./interfaces/IPolicyPoolConfig.sol";
9
+ import {IAccessManager} from "./interfaces/IAccessManager.sol";
9
10
  import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
10
- import {WadRayMath} from "./WadRayMath.sol";
11
+ import {WadRayMath} from "./dependencies/WadRayMath.sol";
11
12
 
12
13
  /**
13
14
  * @title Base class for PolicyPool components
14
15
  * @dev This is the base class of all the components of the protocol that are linked to the PolicyPool and created
15
16
  * after it.
16
17
  * Holds the reference to _policyPool as immutable, also provides access to common admin roles:
17
- * - LEVEL1_ROLE: High impact changes like upgrades or other critical operations
18
- * - LEVEL2_ROLE: Mid-impact changes like adding new risk modules or changing some parameters
18
+ * - LEVEL1_ROLE: High impact changes like upgrades, adding or removing components or other critical operations
19
+ * - LEVEL2_ROLE: Mid-impact changes like changing some parameters
19
20
  * - LEVEL3_ROLE: Low-impact changes like changing some parameters up to given percentage (tweaks)
20
21
  * - GUARDIAN_ROLE: For emergency operations oriented to protect the protocol in case of attacks or hacking.
21
22
  *
@@ -42,59 +43,83 @@ abstract contract PolicyPoolComponent is
42
43
  uint40 internal _lastTweakTimestamp;
43
44
  uint56 internal _lastTweakActions; // bitwise map of applied actions
44
45
 
45
- event GovernanceAction(IPolicyPoolConfig.GovernanceActions indexed action, uint256 value);
46
- event ComponentChanged(IPolicyPoolConfig.GovernanceActions indexed action, address value);
46
+ event GovernanceAction(IAccessManager.GovernanceActions indexed action, uint256 value);
47
+ event ComponentChanged(IAccessManager.GovernanceActions indexed action, address value);
47
48
 
48
49
  modifier onlyPolicyPool() {
49
50
  require(_msgSender() == address(_policyPool), "The caller must be the PolicyPool");
50
51
  _;
51
52
  }
52
53
 
53
- modifier onlyPoolRole3(
54
- bytes32 role1,
55
- bytes32 role2,
56
- bytes32 role3
57
- ) {
58
- if (!hasPoolRole(role1)) {
59
- _policyPool.config().checkRole2(role2, role3, msg.sender);
60
- }
54
+ modifier onlyComponentRole(bytes32 role) {
55
+ _policyPool.access().checkComponentRole(address(this), role, _msgSender(), false);
61
56
  _;
62
57
  }
63
58
 
64
- modifier onlyPoolRole2(bytes32 role1, bytes32 role2) {
65
- _policyPool.config().checkRole2(role1, role2, msg.sender);
59
+ modifier onlyGlobalOrComponentRole(bytes32 role) {
60
+ _policyPool.access().checkComponentRole(address(this), role, _msgSender(), true);
66
61
  _;
67
62
  }
68
63
 
69
- modifier onlyPoolRole(bytes32 role) {
70
- _policyPool.config().checkRole(role, msg.sender);
64
+ modifier onlyGlobalOrComponentRole2(bytes32 role1, bytes32 role2) {
65
+ _policyPool.access().checkComponentRole2(address(this), role1, role2, _msgSender(), true);
71
66
  _;
72
67
  }
73
68
 
74
- modifier onlyComponentRole(bytes32 role) {
75
- _policyPool.config().checkComponentRole(address(this), role, msg.sender);
69
+ modifier onlyGlobalOrComponentRole3(
70
+ bytes32 role1,
71
+ bytes32 role2,
72
+ bytes32 role3
73
+ ) {
74
+ IAccessManager access = _policyPool.access();
75
+ if (!access.hasComponentRole(address(this), role1, _msgSender(), true)) {
76
+ _policyPool.access().checkComponentRole2(address(this), role2, role3, _msgSender(), true);
77
+ }
76
78
  _;
77
79
  }
78
80
 
79
81
  /// @custom:oz-upgrades-unsafe-allow constructor
80
82
  constructor(IPolicyPool policyPool_) {
83
+ require(
84
+ address(policyPool_) != address(0),
85
+ "PolicyPoolComponent: policyPool cannot be zero address"
86
+ );
87
+ _disableInitializers();
81
88
  _policyPool = policyPool_;
82
89
  }
83
90
 
84
91
  // solhint-disable-next-line func-name-mixedcase
85
- function __PolicyPoolComponent_init() internal initializer {
92
+ function __PolicyPoolComponent_init() internal onlyInitializing {
86
93
  __UUPSUpgradeable_init();
87
94
  __Pausable_init();
88
95
  }
89
96
 
90
- // solhint-disable-next-line no-empty-blocks
91
- function _authorizeUpgrade(address) internal override onlyPoolRole2(GUARDIAN_ROLE, LEVEL1_ROLE) {}
97
+ function _authorizeUpgrade(address newImpl)
98
+ internal
99
+ view
100
+ override
101
+ onlyGlobalOrComponentRole2(GUARDIAN_ROLE, LEVEL1_ROLE)
102
+ {
103
+ require(
104
+ IPolicyPoolComponent(newImpl).policyPool() == _policyPool,
105
+ "Can't upgrade changing the PolicyPool!"
106
+ );
107
+ }
92
108
 
93
- function pause() public onlyPoolRole(GUARDIAN_ROLE) {
109
+ /**
110
+ * @dev See {IERC165-supportsInterface}.
111
+ */
112
+ function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
113
+ return
114
+ interfaceId == type(IERC165).interfaceId ||
115
+ interfaceId == type(IPolicyPoolComponent).interfaceId;
116
+ }
117
+
118
+ function pause() public onlyGlobalOrComponentRole(GUARDIAN_ROLE) {
94
119
  _pause();
95
120
  }
96
121
 
97
- function unpause() public onlyPoolRole2(GUARDIAN_ROLE, LEVEL1_ROLE) {
122
+ function unpause() public onlyGlobalOrComponentRole2(GUARDIAN_ROLE, LEVEL1_ROLE) {
98
123
  _unpause();
99
124
  }
100
125
 
@@ -107,22 +132,7 @@ abstract contract PolicyPoolComponent is
107
132
  }
108
133
 
109
134
  function hasPoolRole(bytes32 role) internal view returns (bool) {
110
- return _policyPool.config().hasRole(role, msg.sender);
111
- }
112
-
113
- function _isTweakRay(
114
- uint256 oldValue,
115
- uint256 newValue,
116
- uint256 maxTweak
117
- ) internal pure returns (bool) {
118
- if (oldValue == newValue) return true;
119
- if (oldValue == 0) return maxTweak >= WadRayMath.RAY;
120
- if (newValue == 0) return false;
121
- if (oldValue < newValue) {
122
- return (newValue.rayDiv(oldValue) - WadRayMath.RAY) <= maxTweak;
123
- } else {
124
- return (WadRayMath.RAY - newValue.rayDiv(oldValue)) <= maxTweak;
125
- }
135
+ return _policyPool.access().hasComponentRole(address(this), role, _msgSender(), true);
126
136
  }
127
137
 
128
138
  function _isTweakWad(
@@ -144,7 +154,7 @@ abstract contract PolicyPoolComponent is
144
154
  function _validateParameters() internal view virtual {} // Must be reimplemented with specific validations
145
155
 
146
156
  function _parameterChanged(
147
- IPolicyPoolConfig.GovernanceActions action,
157
+ IAccessManager.GovernanceActions action,
148
158
  uint256 value,
149
159
  bool tweak
150
160
  ) internal {
@@ -153,7 +163,7 @@ abstract contract PolicyPoolComponent is
153
163
  emit GovernanceAction(action, value);
154
164
  }
155
165
 
156
- function _componentChanged(IPolicyPoolConfig.GovernanceActions action, address value) internal {
166
+ function _componentChanged(IAccessManager.GovernanceActions action, address value) internal {
157
167
  _validateParameters();
158
168
  emit ComponentChanged(action, value);
159
169
  }
@@ -162,7 +172,7 @@ abstract contract PolicyPoolComponent is
162
172
  return (_lastTweakTimestamp, _lastTweakActions);
163
173
  }
164
174
 
165
- function _registerTweak(IPolicyPoolConfig.GovernanceActions action) internal {
175
+ function _registerTweak(IAccessManager.GovernanceActions action) internal {
166
176
  uint56 actionBitMap = uint56(1 << (uint8(action) - 1));
167
177
  if ((uint40(block.timestamp) - _lastTweakTimestamp) > TWEAK_EXPIRATION) {
168
178
  _lastTweakTimestamp = uint40(block.timestamp);
@@ -176,4 +186,11 @@ abstract contract PolicyPoolComponent is
176
186
  }
177
187
  }
178
188
  }
189
+
190
+ /**
191
+ * @dev This empty reserved space is put in place to allow future versions to add new
192
+ * variables without shifting down storage in the inheritance chain.
193
+ * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
194
+ */
195
+ uint256[49] private __gap;
179
196
  }