@ensuro/core 2.0.0-beta10 → 2.0.0-beta12

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 (51) hide show
  1. package/README.md +13 -0
  2. package/build/contracts/AccessManager.sol/AccessManager.json +7 -2
  3. package/build/contracts/ERC4626AssetManager.sol/ERC4626AssetManager.json +21 -2
  4. package/build/contracts/EToken.sol/EToken.json +21 -2
  5. package/build/contracts/LPManualWhitelist.sol/LPManualWhitelist.json +21 -2
  6. package/build/contracts/LiquidityThresholdAssetManager.sol/LiquidityThresholdAssetManager.json +19 -0
  7. package/build/contracts/Policy.sol/Policy.json +2 -2
  8. package/build/contracts/PolicyPool.sol/PolicyPool.json +21 -2
  9. package/build/contracts/PolicyPoolComponent.sol/PolicyPoolComponent.json +19 -0
  10. package/build/contracts/PremiumsAccount.sol/PremiumsAccount.json +21 -2
  11. package/build/contracts/Reserve.sol/Reserve.json +19 -0
  12. package/build/contracts/RiskModule.sol/RiskModule.json +19 -0
  13. package/build/contracts/SignedQuoteRiskModule.sol/SignedQuoteRiskModule.json +152 -2
  14. package/build/contracts/TimeScaled.sol/TimeScaled.json +2 -2
  15. package/build/contracts/TrustfulRiskModule.sol/TrustfulRiskModule.json +137 -2
  16. package/build/contracts/dependencies/WadRayMath.sol/WadRayMath.json +2 -2
  17. package/build/contracts/interfaces/IAssetManager.sol/IAssetManager.json +19 -0
  18. package/build/contracts/interfaces/IPolicyPool.sol/IPolicyPool.json +19 -0
  19. package/build/contracts/interfaces/IPolicyPoolComponent.sol/IPolicyPoolComponent.json +19 -0
  20. package/build/contracts/mocks/FixedRateVault.sol/FixedRateVault.json +2 -2
  21. package/build/contracts/mocks/ForwardProxy.sol/ForwardProxy.json +2 -2
  22. package/build/contracts/mocks/InterfaceIdCalculator.sol/InterfaceIdCalculator.json +180 -0
  23. package/build/contracts/mocks/PolicyHolderMock.sol/PolicyHolderMock.json +2 -2
  24. package/build/contracts/mocks/PolicyPoolComponentMock.sol/PolicyPoolComponentMock.json +21 -2
  25. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMock.json +21 -2
  26. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMockForward.json +2 -2
  27. package/build/contracts/mocks/RiskModuleMock.sol/RiskModuleMock.json +21 -2
  28. package/build/contracts/mocks/TestCurrency.sol/TestCurrency.json +2 -2
  29. package/build/contracts/mocks/TestNFT.sol/TestNFT.json +2 -2
  30. package/contracts/AccessManager.sol +30 -1
  31. package/contracts/ERC4626AssetManager.sol +3 -0
  32. package/contracts/EToken.sol +89 -19
  33. package/contracts/LPManualWhitelist.sol +18 -2
  34. package/contracts/LiquidityThresholdAssetManager.sol +17 -2
  35. package/contracts/Policy.sol +4 -2
  36. package/contracts/PolicyPool.sol +72 -13
  37. package/contracts/PolicyPoolComponent.sol +29 -3
  38. package/contracts/PremiumsAccount.sol +68 -23
  39. package/contracts/Reserve.sol +19 -3
  40. package/contracts/RiskModule.sol +46 -13
  41. package/contracts/SignedQuoteRiskModule.sol +120 -48
  42. package/contracts/TrustfulRiskModule.sol +59 -3
  43. package/contracts/interfaces/IAssetManager.sol +8 -3
  44. package/contracts/interfaces/IPolicyPool.sol +15 -4
  45. package/contracts/interfaces/IPolicyPoolComponent.sol +2 -1
  46. package/contracts/mocks/InterfaceIdCalculator.sol +32 -0
  47. package/contracts/mocks/PolicyPoolComponentMock.sol +10 -0
  48. package/contracts/mocks/PolicyPoolMock.sol +4 -0
  49. package/js/deploy.js +704 -0
  50. package/js/test-utils.js +139 -37
  51. package/package.json +1 -1
@@ -79,7 +79,57 @@ contract TrustfulRiskModule is RiskModule {
79
79
  address onBehalfOf,
80
80
  uint96 internalId
81
81
  ) external onlyComponentRole(PRICER_ROLE) returns (uint256) {
82
- address payer = onBehalfOf;
82
+ return
83
+ _newPolicy(
84
+ payout,
85
+ premium,
86
+ lossProb,
87
+ expiration,
88
+ _getPayer(onBehalfOf, premium),
89
+ onBehalfOf,
90
+ internalId
91
+ ).id;
92
+ }
93
+
94
+ /**
95
+ * @dev Creates a new Policy. Version that returns all the PolicyData struct, not just the id.
96
+ *
97
+ * Requirements:
98
+ * - The caller has been granted componentRole(PRICER_ROLE)
99
+ *
100
+ * Emits:
101
+ * - {PolicyPool.NewPolicy}
102
+ *
103
+ * @param payout The exposure (maximum payout) of the policy
104
+ * @param premium The premium that will be paid by the policyHolder
105
+ * @param lossProb The probability of having to pay the maximum payout (wad)
106
+ * @param expiration The expiration of the policy (timestamp)
107
+ * @param onBehalfOf The policy holder
108
+ * @param internalId An id that's unique within this module and it will be used to identify the policy
109
+ * @return createdPolicy Returns the id of the created policy
110
+ */
111
+ function newPolicyFull(
112
+ uint256 payout,
113
+ uint256 premium,
114
+ uint256 lossProb,
115
+ uint40 expiration,
116
+ address onBehalfOf,
117
+ uint96 internalId
118
+ ) external onlyComponentRole(PRICER_ROLE) returns (Policy.PolicyData memory createdPolicy) {
119
+ return
120
+ _newPolicy(
121
+ payout,
122
+ premium,
123
+ lossProb,
124
+ expiration,
125
+ _getPayer(onBehalfOf, premium),
126
+ onBehalfOf,
127
+ internalId
128
+ );
129
+ }
130
+
131
+ function _getPayer(address onBehalfOf, uint256 premium) internal returns (address payer) {
132
+ payer = onBehalfOf;
83
133
  if (payer != _msgSender() && _policyPool.currency().allowance(payer, _msgSender()) < premium)
84
134
  /**
85
135
  * The standard is the payer should be the _msgSender() but usually, in this type of module,
@@ -93,8 +143,7 @@ contract TrustfulRiskModule is RiskModule {
93
143
  * premium even for multiple policies.
94
144
  */
95
145
  payer = _msgSender();
96
-
97
- return _newPolicy(payout, premium, lossProb, expiration, payer, onBehalfOf, internalId).id;
146
+ return payer;
98
147
  }
99
148
 
100
149
  /**
@@ -140,4 +189,11 @@ contract TrustfulRiskModule is RiskModule {
140
189
  {
141
190
  _policyPool.resolvePolicyFullPayout(policy, customerWon);
142
191
  }
192
+
193
+ /**
194
+ * @dev This empty reserved space is put in place to allow future versions to add new
195
+ * variables without shifting down storage in the inheritance chain.
196
+ * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
197
+ */
198
+ uint256[50] private __gap;
143
199
  }
@@ -1,14 +1,19 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.0;
3
3
 
4
+ import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
5
+
4
6
  /**
5
7
  * @title IAssetManager - Interface of the asset management strategy that's plugged into the reserves
6
8
  * @dev The asset manager is a contract that's plugged and called with `delegatecall` (operates in the context of the
7
9
  * reserve - see {Reserve}). The asset manager contract applies a strategy to invest the reserve funds and
8
10
  * get additional yields.
11
+ *
12
+ * All implementations of this contract should use the Diamond Storage pattern to avoid overwriting the calling contract's state.
13
+ * See https://eips.ethereum.org/EIPS/eip-2535#storage
9
14
  * @author Ensuro
10
15
  */
11
- interface IAssetManager {
16
+ interface IAssetManager is IERC165 {
12
17
  /**
13
18
  * @dev Event emitted when funds are removed from Reserve liquidity and invested in the investment strategy,
14
19
  * @param amount The amount invested
@@ -57,8 +62,8 @@ interface IAssetManager {
57
62
  function recordEarnings() external returns (int256);
58
63
 
59
64
  /**
60
- * @dev Refills the reserve balance with enought money to do a payment. Called from the reserve when a payment needs
61
- * to be made and there's no enought liquid balance (`currency().balanceOf(reserve) < paymentAmount`)
65
+ * @dev Refills the reserve balance with enough money to do a payment. Called from the reserve when a payment needs
66
+ * to be made and there's no enough liquid balance (`currency().balanceOf(reserve) < paymentAmount`)
62
67
  *
63
68
  * Events:
64
69
  * - Emits {MoneyDeinvested} with the amount transferred to the liquid balance.
@@ -101,12 +101,23 @@ interface IPolicyPool {
101
101
  function expirePolicy(Policy.PolicyData calldata policy) external;
102
102
 
103
103
  /**
104
- * @dev Returns whether or not a policy is active
105
- * @param policyId The id of the policy we are querying
106
- * @return Returns true if a policy with that id was created and wasn't yet resolved or expired, or false otherwise.
104
+ * @dev Returns whether a policy is active, i.e., it's still in the PolicyPool, not yet resolved or expired.
105
+ * Be aware that a policy might be active but the `block.timestamp` might be after the expiration date, so it
106
+ * can't be triggered with a payout.
107
+ *
108
+ * @param policyId The id of the policy queried
109
+ * @return Whether the policy is active or not
107
110
  */
108
111
  function isActive(uint256 policyId) external view returns (bool);
109
112
 
113
+ /**
114
+ * @dev Returns the stored hash of the policy. It's `bytes32(0)` is the policy isn't active.
115
+ *
116
+ * @param policyId The id of the policy queried
117
+ * @return Returns the hash of a given policy id
118
+ */
119
+ function getPolicyHash(uint256 policyId) external view returns (bytes32);
120
+
110
121
  /**
111
122
  * @dev Deposits liquidity into an eToken. Forwards the call to {EToken-deposit}, after transferring the funds.
112
123
  * The user will receive etokens for the same amount deposited.
@@ -137,7 +148,7 @@ interface IPolicyPool {
137
148
  *
138
149
  * @param eToken The address of the eToken from where the user wants to withdraw liquidity
139
150
  * @param amount The amount to withdraw. If equal to type(uint256).max, means full withdrawal.
140
- * If the balance is not enought or can't be withdrawn (locked as SCR), it withdraws
151
+ * If the balance is not enough or can't be withdrawn (locked as SCR), it withdraws
141
152
  * as much as it can, but doesn't fails.
142
153
  * @return Returns the actual amount withdrawn.
143
154
  */
@@ -2,13 +2,14 @@
2
2
  pragma solidity ^0.8.0;
3
3
 
4
4
  import {IPolicyPool} from "./IPolicyPool.sol";
5
+ import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
5
6
 
6
7
  /**
7
8
  * @title IPolicyPoolComponent interface
8
9
  * @dev Interface for Contracts linked (owned) by a PolicyPool. Useful to avoid cyclic dependencies
9
10
  * @author Ensuro
10
11
  */
11
- interface IPolicyPoolComponent {
12
+ interface IPolicyPoolComponent is IERC165 {
12
13
  /**
13
14
  * @dev Returns the address of the PolicyPool (see {PolicyPool}) where this component belongs.
14
15
  */
@@ -0,0 +1,32 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
5
+ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
6
+ import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
7
+ import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
8
+ import {IAccessControl} from "@openzeppelin/contracts/access/IAccessControl.sol";
9
+ import {IPolicyPool} from "../interfaces/IPolicyPool.sol";
10
+ import {IPolicyPoolComponent} from "../interfaces/IPolicyPoolComponent.sol";
11
+ import {IEToken} from "../interfaces/IEToken.sol";
12
+ import {IRiskModule} from "../interfaces/IRiskModule.sol";
13
+ import {IPremiumsAccount} from "../interfaces/IPremiumsAccount.sol";
14
+ import {ILPWhitelist} from "../interfaces/ILPWhitelist.sol";
15
+ import {IAccessManager} from "../interfaces/IAccessManager.sol";
16
+ import {IAssetManager} from "../interfaces/IAssetManager.sol";
17
+
18
+ contract InterfaceIdCalculator {
19
+ bytes4 public constant IERC165_INTERFACEID = type(IERC165).interfaceId;
20
+ bytes4 public constant IERC20_INTERFACEID = type(IERC20).interfaceId;
21
+ bytes4 public constant IERC20METADATA_INTERFACEID = type(IERC20Metadata).interfaceId;
22
+ bytes4 public constant IERC721_INTERFACEID = type(IERC721).interfaceId;
23
+ bytes4 public constant IACCESSCONTROL_INTERFACEID = type(IAccessControl).interfaceId;
24
+ bytes4 public constant IPOLICYPOOL_INTERFACEID = type(IPolicyPool).interfaceId;
25
+ bytes4 public constant IPOLICYPOOLCOMPONENT_INTERFACEID = type(IPolicyPoolComponent).interfaceId;
26
+ bytes4 public constant IETOKEN_INTERFACEID = type(IEToken).interfaceId;
27
+ bytes4 public constant IRISKMODULE_INTERFACEID = type(IRiskModule).interfaceId;
28
+ bytes4 public constant IPREMIUMSACCOUNT_INTERFACEID = type(IPremiumsAccount).interfaceId;
29
+ bytes4 public constant ILPWHITELIST_INTERFACEID = type(ILPWhitelist).interfaceId;
30
+ bytes4 public constant IACCESSMANAGER_INTERFACEID = type(IAccessManager).interfaceId;
31
+ bytes4 public constant IASSETMANAGER_INTERFACEID = type(IAssetManager).interfaceId;
32
+ }
@@ -1,6 +1,7 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.0;
3
3
 
4
+ import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
4
5
  import {IPolicyPool} from "../interfaces/IPolicyPool.sol";
5
6
  import {IPolicyPoolComponent} from "../interfaces/IPolicyPoolComponent.sol";
6
7
 
@@ -14,4 +15,13 @@ contract PolicyPoolComponentMock is IPolicyPoolComponent {
14
15
  function policyPool() external view override returns (IPolicyPool) {
15
16
  return _policyPool;
16
17
  }
18
+
19
+ /**
20
+ * @dev See {IERC165-supportsInterface}.
21
+ */
22
+ function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
23
+ return
24
+ interfaceId == type(IERC165).interfaceId ||
25
+ interfaceId == type(IPolicyPoolComponent).interfaceId;
26
+ }
17
27
  }
@@ -85,6 +85,10 @@ contract PolicyPoolMock is IPolicyPool {
85
85
  return policyHashes[policyId] != bytes32(0);
86
86
  }
87
87
 
88
+ function getPolicyHash(uint256 policyId) external view override returns (bytes32) {
89
+ return policyHashes[policyId];
90
+ }
91
+
88
92
  function deposit(IEToken, uint256) external pure override {
89
93
  revert("Not Implemented deposit");
90
94
  }