@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
@@ -0,0 +1,148 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {IAccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
5
+
6
+ /**
7
+ * @title IAccessManager - Interface for the contract that handles roles for the PolicyPool and components
8
+ * @dev Interface for the contract that handles roles for the PolicyPool and components
9
+ * @author Ensuro
10
+ */
11
+ interface IAccessManager is IAccessControlUpgradeable {
12
+ /**
13
+ * @dev Enum with the different governance actions supported in the protocol.
14
+ * It's good to keep actions of the same component consecutive, parts of the code relay on that,
15
+ * so we put some fillers in case new actions are added.
16
+ */
17
+ enum GovernanceActions {
18
+ none,
19
+ setTreasury, // Changes PolicyPool treasury address
20
+ setAssetManager, // Change in the asset manager strategy of a reserve
21
+ setAssetManagerForced, // Change in the asset manager strategy of a reserve, forced (deinvest failed)
22
+ ppFiller1, // Reserve space for future PolicyPool or AccessManager actions
23
+ ppFiller2, // Reserve space for future PolicyPool or AccessManager actions
24
+ ppFiller3, // Reserve space for future PolicyPool or AccessManager actions
25
+ ppFiller4, // Reserve space for future PolicyPool or AccessManager actions
26
+ // RiskModule Governance Actions
27
+ setMoc,
28
+ setJrCollRatio,
29
+ setCollRatio,
30
+ setEnsuroPpFee,
31
+ setEnsuroCocFee,
32
+ setJrRoc,
33
+ setSrRoc,
34
+ setMaxPayoutPerPolicy,
35
+ setExposureLimit,
36
+ setMaxDuration,
37
+ setWallet,
38
+ rmFiller1, // Reserve space for future RM actions
39
+ rmFiller2, // Reserve space for future RM actions
40
+ rmFiller3, // Reserve space for future RM actions
41
+ rmFiller4, // Reserve space for future RM actions
42
+ // EToken Governance Actions
43
+ setLPWhitelist, // Changes EToken Liquidity Providers Whitelist
44
+ setLiquidityRequirement,
45
+ setMinUtilizationRate,
46
+ setMaxUtilizationRate,
47
+ setInternalLoanInterestRate,
48
+ etkFiller1, // Reserve space for future EToken actions
49
+ etkFiller2, // Reserve space for future EToken actions
50
+ etkFiller3, // Reserve space for future EToken actions
51
+ etkFiller4, // Reserve space for future EToken actions
52
+ // PremiumsAccount Governance Actions
53
+ setDeficitRatio,
54
+ setDeficitRatioWithAdjustment,
55
+ paFiller1,
56
+ paFiller2,
57
+ paFiller3,
58
+ paFiller4,
59
+ // AssetManager Governance Actions
60
+ setLiquidityMin,
61
+ setLiquidityMiddle,
62
+ setLiquidityMax,
63
+ amFiller1, // Reserve space for future Asset Manager actions
64
+ amFiller2, // Reserve space for future Asset Manager actions
65
+ amFiller3, // Reserve space for future Asset Manager actions
66
+ amFiller4, // Reserve space for future Asset Manager actions
67
+ last
68
+ }
69
+
70
+ /**
71
+ * @dev Gets a role identifier mixing the hash of the global role and the address of the component
72
+ *
73
+ * @param component The component where this role will apply
74
+ * @param role A role such as `keccak256("LEVEL1_ROLE")` that's global
75
+ * @return A new role, mixing (XOR) the component address and the role.
76
+ */
77
+ function getComponentRole(address component, bytes32 role) external view returns (bytes32);
78
+
79
+ /**
80
+ * @dev Tells if a user has been granted a given role for a component
81
+ *
82
+ * @param component The component where this role will apply
83
+ * @param role A role such as `keccak256("LEVEL1_ROLE")` that's global
84
+ * @param account The user address for who we want to verify the permission
85
+ * @param alsoGlobal If true, it will return if the users has either the component role, or the role itself.
86
+ * If false, only the component role is accepted
87
+ * @return Whether the user has or not any of the roles
88
+ */
89
+ function hasComponentRole(
90
+ address component,
91
+ bytes32 role,
92
+ address account,
93
+ bool alsoGlobal
94
+ ) external view returns (bool);
95
+
96
+ /**
97
+ * @dev Checks if a user has been granted a given role and reverts if it doesn't
98
+ *
99
+ * @param role A role such as `keccak256("LEVEL1_ROLE")` that's global
100
+ * @param account The user address for who we want to verify the permission
101
+ */
102
+ function checkRole(bytes32 role, address account) external view;
103
+
104
+ /**
105
+ * @dev Checks if a user has been granted any of the two roles specified and reverts if it doesn't
106
+ *
107
+ * @param role1 A role such as `keccak256("LEVEL1_ROLE")` that's global
108
+ * @param role2 Another role such as `keccak256("GUARDIAN_ROLE")` that's global
109
+ * @param account The user address for who we want to verify the permission
110
+ */
111
+ function checkRole2(
112
+ bytes32 role1,
113
+ bytes32 role2,
114
+ address account
115
+ ) external view;
116
+
117
+ /**
118
+ * @dev Checks if a user has been granted a given component role and reverts if it doesn't
119
+ *
120
+ * @param role A role such as `keccak256("LEVEL1_ROLE")` that's global
121
+ * @param account The user address for who we want to verify the permission
122
+ * @param alsoGlobal If true, it will accept not only the component role, but also the (global) `role` itself.
123
+ * If false, only the component role is accepted
124
+ */
125
+ function checkComponentRole(
126
+ address component,
127
+ bytes32 role,
128
+ address account,
129
+ bool alsoGlobal
130
+ ) external view;
131
+
132
+ /**
133
+ * @dev Checks if a user has been granted any of the two component roles specified and reverts if it doesn't
134
+ *
135
+ * @param role1 A role such as `keccak256("LEVEL1_ROLE")` that's global
136
+ * @param role2 Another role such as `keccak256("GUARDIAN_ROLE")` that's global
137
+ * @param account The user address for who we want to verify the permission
138
+ * @param alsoGlobal If true, it will accept not only the component roles, but also the global ones.
139
+ * If false, only the component roles are accepted
140
+ */
141
+ function checkComponentRole2(
142
+ address component,
143
+ bytes32 role1,
144
+ bytes32 role2,
145
+ address account,
146
+ bool alsoGlobal
147
+ ) external view;
148
+ }
@@ -0,0 +1,89 @@
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
+
6
+ /**
7
+ * @title IAssetManager - Interface of the asset management strategy that's plugged into the reserves
8
+ * @dev The asset manager is a contract that's plugged and called with `delegatecall` (operates in the context of the
9
+ * reserve - see {Reserve}). The asset manager contract applies a strategy to invest the reserve funds and
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
14
+ * @author Ensuro
15
+ */
16
+ interface IAssetManager is IERC165 {
17
+ /**
18
+ * @dev Event emitted when funds are removed from Reserve liquidity and invested in the investment strategy,
19
+ * @param amount The amount invested
20
+ */
21
+ event MoneyInvested(uint256 amount);
22
+
23
+ /**
24
+ * @dev Event emitted when funds are deinvested from the investment strategy and returned to the reserve as liquid
25
+ * funds.
26
+ *
27
+ * @param amount The amount de-invested
28
+ */
29
+ event MoneyDeinvested(uint256 amount);
30
+
31
+ /**
32
+ * @dev Event emitted when investment yields are accounted in the reserve
33
+ *
34
+ * @param earnings The amount of earnings generated since last record. It's positive in the case of earnings or
35
+ * negative when there are losses.
36
+ */
37
+ event EarningsRecorded(int256 earnings);
38
+
39
+ /**
40
+ * @dev Function called when an asset manager is plugged into a reserve. Useful for initialization tasks
41
+ */
42
+ function connect() external;
43
+
44
+ /**
45
+ * @dev Gives the opportunity to the asset manager to rebalance the funds between those that are kept liquid in the
46
+ * reserve balance and those that are invested. Called with delegatecall by the reserve from the external function
47
+ * rebalance (see {Reserve-rebalance}).
48
+ *
49
+ * Events:
50
+ * - Emits {MoneyInvested} or {MoneyDeinvested}
51
+ */
52
+ function rebalance() external;
53
+
54
+ /**
55
+ * @dev Gives the opportunity to the asset manager to rebalance the funds between those that are kept liquid in the
56
+ * reserve balance and those that are invested. Called with delegatecall by the reserve from the external function
57
+ * rebalance (see {Reserve-rebalance}).
58
+ *
59
+ * Events:
60
+ * - Emits {MoneyInvested} or {MoneyDeinvested}
61
+ */
62
+ function recordEarnings() external returns (int256);
63
+
64
+ /**
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`)
67
+ *
68
+ * Events:
69
+ * - Emits {MoneyDeinvested} with the amount transferred to the liquid balance.
70
+ *
71
+ * @param paymentAmount The total amount of the payment that needs to be made. If this function is called, it's
72
+ * because paymentAmount > balanceOf(reserve). The minimum amount that needs to be transferred to the reserve is
73
+ * `paymentAmount - balanceOf(reserve)`, but it can transfer more.
74
+ * @return Returns the actual amount transferred
75
+ */
76
+ function refillWallet(uint256 paymentAmount) external returns (uint256);
77
+
78
+ /**
79
+ * @dev Deinvests all the funds transfer all the assets to the liquid balance. Called from the reserve when the asset
80
+ * manager is unplugged.
81
+ *
82
+ * Events:
83
+ * - Emits {MoneyDeinvested} with the amount transferred to the liquid balance.
84
+ * - Emits {EarningsRecorded} with the amount of earnings since earnings were recorded last time.
85
+ *
86
+ * @return Returns the earnings or losses (negative) since last time earnings were recorded.
87
+ */
88
+ function deinvestAll() external returns (int256);
89
+ }
@@ -9,6 +9,16 @@ import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
9
9
  * @author Ensuro
10
10
  */
11
11
  interface IEToken is IERC20 {
12
+ /**
13
+ * @dev Enum of the different parameters that are configurable in an EToken.
14
+ */
15
+ enum Parameter {
16
+ liquidityRequirement,
17
+ minUtilizationRate,
18
+ maxUtilizationRate,
19
+ internalLoanInterestRate
20
+ }
21
+
12
22
  /**
13
23
  * @dev Event emitted when part of the funds of the eToken are locked as solvency capital.
14
24
  * @param interestRate The annualized interestRate paid for the capital (wad)
@@ -77,7 +87,7 @@ interface IEToken is IERC20 {
77
87
  *
78
88
  * @param provider The address of the liquidity provider
79
89
  * @param amount The amount deposited.
80
- * @return The actual balance of the provider (TODO)
90
+ * @return The actual balance of the provider
81
91
  */
82
92
  function deposit(address provider, uint256 amount) external returns (uint256);
83
93
 
@@ -110,12 +120,25 @@ interface IEToken is IERC20 {
110
120
  * - Must be called by `policyPool()`
111
121
  *
112
122
  * Events:
113
- * - Emits {PoolBorrowerAdded}
123
+ * - Emits {InternalBorrowerAdded}
114
124
  *
115
125
  * @param borrower The address of the _borrower_, a PremiumsAccount that has this eToken as senior or junior eToken.
116
126
  */
117
127
  function addBorrower(address borrower) external;
118
128
 
129
+ /**
130
+ * @dev Removes an authorized _borrower_ to the eToken. The _borrower_ can't no longer lock funds or take loans.
131
+ *
132
+ * Requirements:
133
+ * - Must be called by `policyPool()`
134
+ *
135
+ * Events:
136
+ * - Emits {InternalBorrowerRemoved} with the defaulted debt
137
+ *
138
+ * @param borrower The address of the _borrower_, a PremiumsAccount that has this eToken as senior or junior eToken.
139
+ */
140
+ function removeBorrower(address borrower) external;
141
+
119
142
  /**
120
143
  * @dev Lends `amount` to the borrower (msg.sender), transferring the money to `receiver`. This reduces the
121
144
  * `totalSupply()` of the eToken, and stores a debt that will be repaid (hopefully) with `repayLoan`.
@@ -8,12 +8,29 @@ import {IEToken} from "./IEToken.sol";
8
8
  * @author Ensuro
9
9
  */
10
10
  interface ILPWhitelist {
11
+ /**
12
+ * @dev Indicates whether or not a liquidity provider can do a deposit in an eToken.
13
+ *
14
+ * @param etoken The eToken (see {EToken}) where the provider wants to deposit money.
15
+ * @param provider The address of the liquidity provider (user) that wants to deposit
16
+ * @param amount The amount of the deposit
17
+ * @return true if `provider` deposit is accepted, false if not
18
+ */
11
19
  function acceptsDeposit(
12
20
  IEToken etoken,
13
21
  address provider,
14
22
  uint256 amount
15
23
  ) external view returns (bool);
16
24
 
25
+ /**
26
+ * @dev Indicates whether or not the eTokens can be transferred from `providerFrom` to `providerTo`
27
+ *
28
+ * @param etoken The eToken (see {EToken}) that the LPs have the intention to transfer.
29
+ * @param providerFrom The current owner of the tokens
30
+ * @param providerTo The destination of the tokens if the transfer is accepted
31
+ * @param amount The amount of tokens to be transferred
32
+ * @return true if the transfer operation is accepted, false if not.
33
+ */
17
34
  function acceptsTransfer(
18
35
  IEToken etoken,
19
36
  address providerFrom,
@@ -4,7 +4,7 @@ pragma solidity ^0.8.0;
4
4
  import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
5
5
  import {Policy} from "../Policy.sol";
6
6
  import {IEToken} from "./IEToken.sol";
7
- import {IPolicyPoolConfig} from "./IPolicyPoolConfig.sol";
7
+ import {IAccessManager} from "./IAccessManager.sol";
8
8
 
9
9
  interface IPolicyPool {
10
10
  /**
@@ -14,40 +14,40 @@ interface IPolicyPool {
14
14
  function currency() external view returns (IERC20Metadata);
15
15
 
16
16
  /**
17
- * @dev Reference to the {PolicyPoolConfig} contract, this contract manages the access controls and installed risk
18
- * modules.
19
- * @return The address of the PolicyPoolConfig contract
17
+ * @dev Reference to the {AccessManager} contract, this contract manages the access controls.
18
+ * @return The address of the AccessManager contract
20
19
  */
21
- function config() external view returns (IPolicyPoolConfig);
20
+ function access() external view returns (IAccessManager);
22
21
 
23
22
  /**
24
- * @dev Reference to the {PolicyNFT} contract. This contract it's an ERC721 Token that handles the ownership of the
25
- * policies.
26
- * @return The address of the PolicyNFT contract
23
+ * @dev Address of the treasury, that receives protocol fees.
24
+ * @return The address of the treasury
27
25
  */
28
- function policyNFT() external view returns (address);
26
+ function treasury() external view returns (address);
29
27
 
30
28
  /**
31
29
  * @dev Creates a new Policy. Must be called from an active RiskModule
32
30
  *
33
31
  * Requirements:
34
32
  * - `msg.sender` must be an active RiskModule
35
- * - `customer` approved the spending of `currency()` for at least `policy.premium`
33
+ * - `caller` approved the spending of `currency()` for at least `policy.premium`
36
34
  * - `internalId` must be unique within `policy.riskModule` and not used before
37
35
  *
38
36
  * Events:
39
37
  * - {PolicyPool-NewPolicy}: with all the details about the policy
40
- * - {ERC20-Transfer}: does several transfers from customer address to the different receivers of the premium
38
+ * - {ERC20-Transfer}: does several transfers from caller address to the different receivers of the premium
41
39
  * (see Premium Split in the docs)
42
40
  *
43
41
  * @param policy A policy created with {Policy-initialize}
44
- * @param customer The address of the policy holder and the payer of the premiums
42
+ * @param caller The pricer that's creating the policy and will pay for the premium
43
+ * @param policyHolder The address of the policy holder and the payer of the premiums
45
44
  * @param internalId A unique id within the RiskModule, that will be used to compute the policy id
46
45
  * @return The policy id, identifying the NFT and the policy
47
46
  */
48
47
  function newPolicy(
49
48
  Policy.PolicyData memory policy,
50
- address customer,
49
+ address caller,
50
+ address policyHolder,
51
51
  uint96 internalId
52
52
  ) external returns (uint256);
53
53
 
@@ -100,6 +100,13 @@ interface IPolicyPool {
100
100
  */
101
101
  function expirePolicy(Policy.PolicyData calldata policy) external;
102
102
 
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.
107
+ */
108
+ function isActive(uint256 policyId) external view returns (bool);
109
+
103
110
  /**
104
111
  * @dev Deposits liquidity into an eToken. Forwards the call to {EToken-deposit}, after transferring the funds.
105
112
  * The user will receive etokens for the same amount deposited.
@@ -130,7 +137,7 @@ interface IPolicyPool {
130
137
  *
131
138
  * @param eToken The address of the eToken from where the user wants to withdraw liquidity
132
139
  * @param amount The amount to withdraw. If equal to type(uint256).max, means full withdrawal.
133
- * If the balance is not enought or can't be withdrawn (locked as SCR), it withdraws
140
+ * If the balance is not enough or can't be withdrawn (locked as SCR), it withdraws
134
141
  * as much as it can, but doesn't fails.
135
142
  * @return Returns the actual amount withdrawn.
136
143
  */
@@ -2,12 +2,16 @@
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 {
13
+ /**
14
+ * @dev Returns the address of the PolicyPool (see {PolicyPool}) where this component belongs.
15
+ */
12
16
  function policyPool() external view returns (IPolicyPool);
13
17
  }
@@ -68,4 +68,9 @@ interface IPremiumsAccount {
68
68
  * @dev The junior eToken, the primary source of solvency, used if the premiums account is exhausted.
69
69
  */
70
70
  function juniorEtk() external view returns (IEToken);
71
+
72
+ /**
73
+ * @dev The total amount of premiums hold by this PremiumsAccount
74
+ */
75
+ function purePremiums() external view returns (uint256);
71
76
  }
@@ -9,6 +9,9 @@ import {IPremiumsAccount} from "./IPremiumsAccount.sol";
9
9
  * @author Ensuro
10
10
  */
11
11
  interface IRiskModule {
12
+ /**
13
+ * @dev Enum with the different parameters of the risk module, used in {RiskModule-setParam}.
14
+ */
12
15
  enum Parameter {
13
16
  moc,
14
17
  jrCollRatio,
@@ -22,28 +25,85 @@ interface IRiskModule {
22
25
  maxDuration
23
26
  }
24
27
 
28
+ /**
29
+ * Struct of the parameters of the risk module that are used to calculate the different Policy fields (see
30
+ * {Policy-PolicyData}.
31
+ */
25
32
  struct Params {
33
+ /**
34
+ * @dev MoC (Margin of Conservativism) is a factor that multiplies the lossProb to increase or decrease the pure
35
+ * premium.
36
+ */
26
37
  uint256 moc;
38
+ /**
39
+ * @dev Junior Collateralization Ratio is the percentage of policy exposure (payout) that will be covered with the
40
+ * purePremium and the Junior EToken
41
+ */
27
42
  uint256 jrCollRatio;
43
+ /**
44
+ * @dev Collateralization Ratio is the percentage of policy exposure (payout) that will be covered by the
45
+ * purePremium and the Junior and Senior EToken. Usually is calculated as the relation between VAR99.5% and VAR100
46
+ * (full collateralization).
47
+ */
28
48
  uint256 collRatio;
49
+ /**
50
+ * @dev Ensuro PurePremium Fee is the percentage that will be multiplied by the pure premium to obtain the part of
51
+ * the Ensuro Fee that's proportional to the pure premium.
52
+ */
29
53
  uint256 ensuroPpFee;
54
+ /**
55
+ * @dev Ensuro Cost of Capital Fee is the percentage that will be multiplied by the cost of capital (CoC) to
56
+ * obtain the part of the Ensuro Fee that's proportional to the CoC.
57
+ */
30
58
  uint256 ensuroCocFee;
59
+ /**
60
+ * @dev Junior Return on Capital is the annualized interest rate that's charged for the capital locked in the Junior
61
+ * EToken.
62
+ */
31
63
  uint256 jrRoc;
64
+ /**
65
+ * @dev Senior Return on Capital is the annualized interest rate that's charged for the capital locked in the Senior
66
+ * EToken.
67
+ */
32
68
  uint256 srRoc;
33
69
  }
34
70
 
71
+ /**
72
+ * @dev A readable name of this risk module. Never changes.
73
+ */
35
74
  function name() external view returns (string memory);
36
75
 
76
+ /**
77
+ * @dev Returns different parameters of the risk module (see {Params})
78
+ */
37
79
  function params() external view returns (Params memory);
38
80
 
39
- function maxPayoutPerPolicy() external view returns (uint256);
40
-
41
- function exposureLimit() external view returns (uint256);
42
-
81
+ /**
82
+ * @dev Returns the maximum duration (in hours) of the policies of this risk module.
83
+ * The `expiration` of the policies has to be `<= (block.timestamp + 3600 * maxDuration())`
84
+ */
43
85
  function maxDuration() external view returns (uint256);
44
86
 
87
+ /**
88
+ * @dev Returns the maximum payout accepted for new policies.
89
+ */
90
+ function maxPayoutPerPolicy() external view returns (uint256);
91
+
92
+ /**
93
+ * @dev Returns sum of the (maximum) payout of the active policies of this risk module, i.e. the maximum possible
94
+ * amount of money that's exposed for this risk module.
95
+ */
45
96
  function activeExposure() external view returns (uint256);
46
97
 
98
+ /**
99
+ * @dev Returns maximum exposure (sum of the (maximum) payout of the active policies) of this risk module.
100
+ * `activeExposure() <= exposureLimit()` always
101
+ */
102
+ function exposureLimit() external view returns (uint256);
103
+
104
+ /**
105
+ * @dev Returns the address of the partner that receives the partnerCommission
106
+ */
47
107
  function wallet() external view returns (address);
48
108
 
49
109
  /**
@@ -56,5 +116,8 @@ interface IRiskModule {
56
116
  */
57
117
  function releaseExposure(uint256 payout) external;
58
118
 
119
+ /**
120
+ * @dev Returns the {PremiumsAccount} where the premiums of this risk module are collected. Never changes.
121
+ */
59
122
  function premiumsAccount() external view returns (IPremiumsAccount);
60
123
  }
@@ -0,0 +1,85 @@
1
+ //SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
5
+ import {ERC4626} from "@openzeppelin/contracts/token/ERC20/extensions/ERC4626.sol";
6
+ import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
7
+ import {IMintableERC20} from "./IMintableERC20.sol";
8
+ import {TimeScaled} from "../TimeScaled.sol";
9
+
10
+ contract FixedRateVault is ERC4626 {
11
+ using TimeScaled for TimeScaled.ScaledAmount;
12
+
13
+ uint256 internal _interestRate;
14
+ TimeScaled.ScaledAmount internal _totalAssets;
15
+ bool internal _broken;
16
+
17
+ constructor(
18
+ string memory name_,
19
+ string memory symbol_,
20
+ IERC20Metadata asset_,
21
+ uint256 interestRate_
22
+ ) ERC20(name_, symbol_) ERC4626(asset_) {
23
+ _interestRate = interestRate_;
24
+ _totalAssets.init();
25
+ }
26
+
27
+ /** @dev See {IERC4262-totalAssets}. */
28
+ function totalAssets() public view virtual override returns (uint256) {
29
+ require(!_broken, "Vault is broken");
30
+ return _totalAssets.getScaledAmount(_interestRate);
31
+ }
32
+
33
+ /**
34
+ * @dev Deposit/mint common workflow.
35
+ */
36
+ function _deposit(
37
+ address caller,
38
+ address receiver,
39
+ uint256 assets,
40
+ uint256 shares
41
+ ) internal virtual override {
42
+ require(!_broken, "Vault is broken");
43
+ _totalAssets.add(assets, _interestRate);
44
+ super._deposit(caller, receiver, assets, shares);
45
+ }
46
+
47
+ /**
48
+ * @dev Withdraw/redeem common workflow.
49
+ */
50
+ function _withdraw(
51
+ address caller,
52
+ address receiver,
53
+ address owner,
54
+ uint256 assets,
55
+ uint256 shares
56
+ ) internal virtual override {
57
+ require(!_broken, "Vault is broken");
58
+ _totalAssets.sub(assets, _interestRate);
59
+ uint256 balance = IERC20Metadata(asset()).balanceOf(address(this));
60
+ if (balance < assets) {
61
+ // Here comes the magic! Free money!
62
+ IMintableERC20(asset()).mint(address(this), assets - balance);
63
+ }
64
+ super._withdraw(caller, receiver, owner, assets, shares);
65
+ }
66
+
67
+ /*
68
+ * @dev Adds or remove assets not generated by deposits/withdraw - For testing discrete earnings/losses
69
+ */
70
+ function discreteEarning(int256 assets) external {
71
+ if (assets > 0) {
72
+ _totalAssets.add(uint256(assets), _interestRate);
73
+ } else {
74
+ _totalAssets.sub(uint256(-assets), _interestRate);
75
+ }
76
+ }
77
+
78
+ function setBroken(bool broken_) external {
79
+ _broken = broken_;
80
+ }
81
+
82
+ function broken() external view returns (bool) {
83
+ return _broken;
84
+ }
85
+ }
@@ -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
+ }