@ensuro/core 1.2.7 → 2.0.0-beta3

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 (91) hide show
  1. package/README.md +50 -17
  2. package/build/contracts/EToken.sol/EToken.json +144 -173
  3. package/build/contracts/FlightDelayRiskModule.sol/FlightDelayRiskModule.json +101 -323
  4. package/build/contracts/LPManualWhitelist.sol/LPManualWhitelist.json +15 -2
  5. package/build/contracts/Policy.sol/Policy.json +2 -2
  6. package/build/contracts/PolicyNFT.sol/PolicyNFT.json +16 -3
  7. package/build/contracts/PolicyPool.sol/PolicyPool.json +105 -351
  8. package/build/contracts/PolicyPoolComponent.sol/PolicyPoolComponent.json +13 -0
  9. package/build/contracts/PolicyPoolConfig.sol/PolicyPoolConfig.json +77 -98
  10. package/build/contracts/PremiumsAccount.sol/PremiumsAccount.json +761 -0
  11. package/build/contracts/{BaseAssetManager.sol/BaseAssetManager.json → Reserve.sol/Reserve.json} +15 -202
  12. package/build/contracts/RiskModule.sol/RiskModule.json +83 -310
  13. package/build/contracts/TimeScaled.sol/TimeScaled.json +24 -0
  14. package/build/contracts/TrustfulRiskModule.sol/TrustfulRiskModule.json +131 -333
  15. package/build/contracts/dependencies/ChainlinkClientUpgradeable.sol/ChainlinkClientUpgradeable.json +15 -2
  16. package/build/contracts/dependencies/ERC1967Proxy.sol/ERC1967Proxy.json +64 -0
  17. package/build/{interfaces/IExchange.sol/IExchange.json → contracts/dependencies/IERC20Metadata.sol/IERC20Metadata.json} +83 -79
  18. package/build/{interfaces → contracts/interfaces}/IEToken.sol/IEToken.json +32 -97
  19. package/build/{interfaces → contracts/interfaces}/ILPWhitelist.sol/ILPWhitelist.json +1 -1
  20. package/build/{interfaces → contracts/interfaces}/IPolicyHolder.sol/IPolicyHolder.json +1 -1
  21. package/build/{interfaces → contracts/interfaces}/IPolicyNFT.sol/IPolicyNFT.json +1 -1
  22. package/build/{interfaces → contracts/interfaces}/IPolicyPool.sol/IPolicyPool.json +121 -108
  23. package/build/{interfaces → contracts/interfaces}/IPolicyPoolComponent.sol/IPolicyPoolComponent.json +1 -1
  24. package/build/{interfaces → contracts/interfaces}/IPolicyPoolConfig.sol/IPolicyPoolConfig.json +20 -49
  25. package/build/contracts/interfaces/IPremiumsAccount.sol/IPremiumsAccount.json +302 -0
  26. package/build/{interfaces → contracts/interfaces}/IRiskModule.sol/IRiskModule.json +58 -34
  27. package/build/contracts/mocks/ForwardProxy.sol/ForwardProxy.json +43 -0
  28. package/build/{interfaces/IInsolvencyHook.sol/IInsolvencyHook.json → contracts/mocks/IMintableERC20.sol/IMintableERC20.json} +13 -8
  29. package/build/contracts/mocks/LinkTokenMock.sol/LinkTokenMock.json +370 -0
  30. package/build/contracts/mocks/PolicyHolderMock.sol/PolicyHolderMock.json +202 -0
  31. package/build/contracts/mocks/PolicyPoolComponentMock.sol/PolicyPoolComponentMock.json +35 -0
  32. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMock.json +603 -0
  33. package/build/contracts/mocks/PolicyPoolMock.sol/PolicyPoolMockForward.json +92 -0
  34. package/build/contracts/mocks/TestCurrency.sol/TestCurrency.json +343 -0
  35. package/build/contracts/mocks/TestNFT.sol/TestNFT.json +388 -0
  36. package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1.json +16 -3
  37. package/build/contracts/upgraded/PolicyNFTv1.sol/PolicyNFTv1_Upgrade.json +16 -3
  38. package/contracts/EToken.sol +210 -267
  39. package/contracts/FlightDelayRiskModule.sol +30 -21
  40. package/contracts/LPManualWhitelist.sol +4 -4
  41. package/contracts/Policy.sol +48 -44
  42. package/contracts/PolicyNFT.sol +2 -2
  43. package/contracts/PolicyPool.sol +114 -467
  44. package/contracts/PolicyPoolComponent.sol +8 -3
  45. package/contracts/PolicyPoolConfig.sol +29 -91
  46. package/contracts/PremiumsAccount.sol +274 -0
  47. package/contracts/Reserve.sol +34 -0
  48. package/contracts/RiskModule.sol +216 -164
  49. package/contracts/TimeScaled.sol +134 -0
  50. package/contracts/TrustfulRiskModule.sol +23 -21
  51. package/contracts/dependencies/ERC1967Proxy.sol +19 -0
  52. package/contracts/dependencies/IERC20Metadata.sol +19 -0
  53. package/contracts/interfaces/IEToken.sol +172 -0
  54. package/{interfaces → contracts/interfaces}/ILPWhitelist.sol +0 -0
  55. package/{interfaces → contracts/interfaces}/IPolicyHolder.sol +0 -0
  56. package/{interfaces → contracts/interfaces}/IPolicyNFT.sol +0 -0
  57. package/contracts/interfaces/IPolicyPool.sol +138 -0
  58. package/{interfaces → contracts/interfaces}/IPolicyPoolComponent.sol +0 -0
  59. package/{interfaces → contracts/interfaces}/IPolicyPoolConfig.sol +29 -22
  60. package/contracts/interfaces/IPremiumsAccount.sol +71 -0
  61. package/contracts/interfaces/IRiskModule.sol +60 -0
  62. package/contracts/mocks/ForwardProxy.sol +66 -0
  63. package/contracts/mocks/IMintableERC20.sol +13 -0
  64. package/contracts/mocks/LinkTokenMock.sol +30 -0
  65. package/contracts/mocks/PolicyHolderMock.sol +68 -0
  66. package/contracts/mocks/PolicyPoolComponentMock.sol +17 -0
  67. package/contracts/mocks/PolicyPoolMock.sol +122 -0
  68. package/contracts/mocks/TestCurrency.sol +35 -0
  69. package/contracts/mocks/TestNFT.sol +23 -0
  70. package/contracts/upgraded/PolicyNFTv1.sol +1 -1
  71. package/js/test-utils.js +278 -0
  72. package/package.json +2 -2
  73. package/build/contracts/AaveAssetManager.sol/AaveAssetManager.json +0 -742
  74. package/build/contracts/DataTypes.sol/DataTypes.json +0 -10
  75. package/build/contracts/Exchange.sol/Exchange.json +0 -591
  76. package/build/contracts/extras/IPriceRiskModule.sol/IPriceRiskModule.json +0 -253
  77. package/build/contracts/extras/PriceRiskModule.sol/PriceRiskModule.json +0 -1055
  78. package/build/interfaces/IAssetManager.sol/IAssetManager.json +0 -31
  79. package/build/interfaces/IExchange.sol/IPriceOracle.json +0 -30
  80. package/contracts/AaveAssetManager.sol +0 -298
  81. package/contracts/BaseAssetManager.sol +0 -301
  82. package/contracts/DataTypes.sol +0 -287
  83. package/contracts/Exchange.sol +0 -189
  84. package/contracts/extras/IPriceRiskModule.sol +0 -41
  85. package/contracts/extras/PriceRiskModule.sol +0 -243
  86. package/interfaces/IAssetManager.sol +0 -23
  87. package/interfaces/IEToken.sol +0 -48
  88. package/interfaces/IExchange.sol +0 -59
  89. package/interfaces/IInsolvencyHook.sol +0 -27
  90. package/interfaces/IPolicyPool.sol +0 -44
  91. package/interfaces/IRiskModule.sol +0 -29
@@ -1,7 +1,8 @@
1
1
  // SPDX-License-Identifier: MIT
2
2
  pragma solidity ^0.8.0;
3
3
 
4
- import {IPolicyPool} from "../interfaces/IPolicyPool.sol";
4
+ import {IPolicyPool} from "./interfaces/IPolicyPool.sol";
5
+ import {IPremiumsAccount} from "./interfaces/IPremiumsAccount.sol";
5
6
  import {Policy} from "./Policy.sol";
6
7
  import {RiskModule} from "./RiskModule.sol";
7
8
  import {Chainlink} from "@chainlink/contracts/src/v0.8/Chainlink.sol";
@@ -44,39 +45,40 @@ contract FlightDelayRiskModule is RiskModule, ChainlinkClientUpgradeable {
44
45
 
45
46
  /// @custom:oz-upgrades-unsafe-allow constructor
46
47
  // solhint-disable-next-line no-empty-blocks
47
- constructor(IPolicyPool policyPool_) RiskModule(policyPool_) {}
48
+ constructor(IPolicyPool policyPool_, IPremiumsAccount premiumsAccount_)
49
+ RiskModule(policyPool_, premiumsAccount_)
50
+ {} // solhint-disable-line no-empty-blocks
48
51
 
49
52
  /**
50
53
  * @dev Initializes the RiskModule
51
54
  * @param name_ Name of the Risk Module
52
- * @param scrPercentage_ Solvency Capital Requirement percentage, to calculate
53
- capital requirement as % of (payout - premium) (in ray)
54
- * @param ensuroFee_ % of premium that will go for Ensuro treasury (in ray)
55
- * @param scrInterestRate_ cost of capital (in ray)
56
- * @param maxScrPerPolicy_ Max SCR to be allocated to this module (in wad)
57
- * @param scrLimit_ Max SCR to be allocated to this module (in wad)
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)
58
60
  * @param wallet_ Address of the RiskModule provider
59
61
  * @param linkToken_ Address of ChainLink LINK token
60
62
  * @param oracleParams_ Parameters of the Oracle
61
63
  */
62
64
  function initialize(
63
65
  string memory name_,
64
- uint256 scrPercentage_,
65
- uint256 ensuroFee_,
66
- uint256 scrInterestRate_,
67
- uint256 maxScrPerPolicy_,
68
- uint256 scrLimit_,
66
+ uint256 collRatio_,
67
+ uint256 ensuroPpFee_,
68
+ uint256 srRoc_,
69
+ uint256 maxPayoutPerPolicy_,
70
+ uint256 exposureLimit_,
69
71
  address wallet_,
70
72
  address linkToken_,
71
73
  OracleParams memory oracleParams_
72
74
  ) public initializer {
73
75
  __RiskModule_init(
74
76
  name_,
75
- scrPercentage_,
76
- ensuroFee_,
77
- scrInterestRate_,
78
- maxScrPerPolicy_,
79
- scrLimit_,
77
+ collRatio_,
78
+ ensuroPpFee_,
79
+ srRoc_,
80
+ maxPayoutPerPolicy_,
81
+ exposureLimit_,
80
82
  wallet_
81
83
  );
82
84
  __ChainlinkClient_init();
@@ -92,7 +94,10 @@ contract FlightDelayRiskModule is RiskModule, ChainlinkClientUpgradeable {
92
94
  _oracleParams = oracleParams_;
93
95
  }
94
96
 
95
- function setOracleParams(OracleParams memory newParams) external onlyRole(ORACLE_ADMIN_ROLE) {
97
+ function setOracleParams(OracleParams memory newParams)
98
+ external
99
+ onlyComponentRole(ORACLE_ADMIN_ROLE)
100
+ {
96
101
  _oracleParams = newParams;
97
102
  }
98
103
 
@@ -121,7 +126,7 @@ contract FlightDelayRiskModule is RiskModule, ChainlinkClientUpgradeable {
121
126
  uint256 lossProb,
122
127
  address customer,
123
128
  uint96 internalId
124
- ) external onlyRole(PRICER_ROLE) returns (uint256) {
129
+ ) external onlyComponentRole(PRICER_ROLE) returns (uint256) {
125
130
  require(expectedArrival > block.timestamp, "expectedArrival can't be in the past");
126
131
  require(departure != 0 && expectedArrival > departure, "expectedArrival <= departure!");
127
132
  uint40 expiration = expectedArrival +
@@ -176,7 +181,11 @@ contract FlightDelayRiskModule is RiskModule, ChainlinkClientUpgradeable {
176
181
  * @dev Forces the resolution of the policy (without waiting Chainlink scheduled on creation)
177
182
  * @param policyId The id of the policy previously created (in newPolicy)
178
183
  */
179
- function resolvePolicy(uint256 policyId) external onlyRole(PRICER_ROLE) returns (uint256) {
184
+ function resolvePolicy(uint256 policyId)
185
+ external
186
+ onlyComponentRole(PRICER_ROLE)
187
+ returns (uint256)
188
+ {
180
189
  PolicyData storage policy = _policies[policyId];
181
190
  require(policy.expectedArrival != 0, "Policy not found!");
182
191
  _chainlinkRequest(policyId, policy, 0);
@@ -1,10 +1,10 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.0;
3
3
 
4
- import {IPolicyPool} from "../interfaces/IPolicyPool.sol";
4
+ import {IPolicyPool} from "./interfaces/IPolicyPool.sol";
5
5
  import {PolicyPoolComponent} from "./PolicyPoolComponent.sol";
6
- import {ILPWhitelist} from "../interfaces/ILPWhitelist.sol";
7
- import {IEToken} from "../interfaces/IEToken.sol";
6
+ import {ILPWhitelist} from "./interfaces/ILPWhitelist.sol";
7
+ import {IEToken} from "./interfaces/IEToken.sol";
8
8
 
9
9
  /**
10
10
  * @title Manual Whitelisting contract
@@ -32,7 +32,7 @@ contract LPManualWhitelist is ILPWhitelist, PolicyPoolComponent {
32
32
 
33
33
  function whitelistAddress(address provider, bool whitelisted)
34
34
  external
35
- onlyPoolRole(LP_WHITELIST_ROLE)
35
+ onlyComponentRole(LP_WHITELIST_ROLE)
36
36
  {
37
37
  _whitelisted[provider] = whitelisted;
38
38
  emit LPWhitelisted(provider, whitelisted);
@@ -1,7 +1,7 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.0;
3
3
  import {WadRayMath} from "./WadRayMath.sol";
4
- import {IRiskModule} from "../interfaces/IRiskModule.sol";
4
+ import {IRiskModule} from "./interfaces/IRiskModule.sol";
5
5
 
6
6
  /**
7
7
  * @title Policy library
@@ -13,33 +13,30 @@ import {IRiskModule} from "../interfaces/IRiskModule.sol";
13
13
  library Policy {
14
14
  using WadRayMath for uint256;
15
15
 
16
- uint256 internal constant SECONDS_IN_YEAR = 31536000e18; /* 365 * 24 * 3600 * 10e18 */
17
- uint256 internal constant SECONDS_IN_YEAR_RAY = 31536000e27; /* 365 * 24 * 3600 * 10e27 */
16
+ uint256 internal constant SECONDS_PER_YEAR = 365 days;
18
17
 
19
18
  // Active Policies
20
19
  struct PolicyData {
21
20
  uint256 id;
22
21
  uint256 payout;
23
22
  uint256 premium;
24
- uint256 scr;
25
- uint256 lossProb; // original loss probability (in ray)
23
+ uint256 jrScr;
24
+ uint256 srScr;
25
+ uint256 lossProb; // original loss probability (in wad)
26
26
  uint256 purePremium; // share of the premium that covers expected losses
27
27
  // equal to payout * lossProb * riskModule.moc
28
- uint256 premiumForEnsuro; // share of the premium that goes for Ensuro (if policy won)
29
- uint256 premiumForRm; // share of the premium that goes for the RM (if policy won)
30
- uint256 premiumForLps; // share of the premium that goes to the liquidity providers (won or not)
28
+ uint256 ensuroCommission; // share of the premium that goes for Ensuro
29
+ uint256 partnerCommission; // share of the premium that goes for the RM
30
+ uint256 jrCoc; // share of the premium that goes to junior liquidity providers (won or not)
31
+ uint256 srCoc; // share of the premium that goes to senior liquidity providers (won or not)
31
32
  IRiskModule riskModule;
32
33
  uint40 start;
33
34
  uint40 expiration;
34
35
  }
35
36
 
36
- /// #if_succeeds {:msg "premium preserved"} premium == (newPolicy.premium);
37
- /// #if_succeeds
38
- /// {:msg "premium distributed"}
39
- /// premium == (newPolicy.purePremium + newPolicy.premiumForLps +
40
- /// newPolicy.premiumForRm + newPolicy.premiumForEnsuro);
41
37
  function initialize(
42
38
  IRiskModule riskModule,
39
+ IRiskModule.Params memory rmParams,
43
40
  uint256 premium,
44
41
  uint256 payout,
45
42
  uint256 lossProb,
@@ -47,55 +44,62 @@ library Policy {
47
44
  ) internal view returns (PolicyData memory newPolicy) {
48
45
  require(premium <= payout, "Premium cannot be more than payout");
49
46
  PolicyData memory policy;
47
+
50
48
  policy.riskModule = riskModule;
51
49
  policy.premium = premium;
52
50
  policy.payout = payout;
53
51
  policy.lossProb = lossProb;
54
- policy.purePremium = payout.wadToRay().rayMul(lossProb.rayMul(riskModule.moc())).rayToWad();
55
- policy.scr = payout.wadMul(riskModule.scrPercentage().rayToWad()) - policy.purePremium;
56
- require(policy.scr != 0, "SCR can't be zero");
57
52
  policy.start = uint40(block.timestamp);
58
53
  policy.expiration = expiration;
59
- policy.premiumForLps = policy.scr.wadMul(
60
- (
61
- (riskModule.scrInterestRate() * (policy.expiration - policy.start)).rayDiv(
62
- SECONDS_IN_YEAR_RAY
63
- )
64
- ).rayToWad()
54
+ policy.purePremium = payout.wadMul(lossProb.wadMul(rmParams.moc));
55
+ // Calculate Junior and Senior SCR
56
+ policy.jrScr = payout.wadMul(rmParams.jrCollRatio);
57
+ if (policy.jrScr > policy.purePremium) {
58
+ policy.jrScr -= policy.purePremium;
59
+ } else {
60
+ policy.jrScr = 0;
61
+ }
62
+ policy.srScr = payout.wadMul(rmParams.collRatio);
63
+ if (policy.srScr > (policy.purePremium + policy.jrScr)) {
64
+ policy.srScr -= policy.purePremium + policy.jrScr;
65
+ } else {
66
+ policy.srScr = 0;
67
+ }
68
+ // Calculate CoCs
69
+ policy.jrCoc = policy.jrScr.wadMul(
70
+ (rmParams.jrRoc * (policy.expiration - policy.start)) / SECONDS_PER_YEAR
65
71
  );
66
- policy.premiumForEnsuro = (policy.purePremium + policy.premiumForLps).wadMul(
67
- riskModule.ensuroFee().rayToWad()
72
+ policy.srCoc = policy.srScr.wadMul(
73
+ (rmParams.srRoc * (policy.expiration - policy.start)) / SECONDS_PER_YEAR
68
74
  );
75
+ uint256 coc = policy.jrCoc + policy.srCoc;
76
+ policy.ensuroCommission =
77
+ policy.purePremium.wadMul(rmParams.ensuroPpFee) +
78
+ coc.wadMul(rmParams.ensuroCocFee);
69
79
  require(
70
- policy.purePremium + policy.premiumForEnsuro + policy.premiumForLps <= premium,
80
+ (policy.purePremium + policy.ensuroCommission + coc) <= premium,
71
81
  "Premium less than minimum"
72
82
  );
73
- policy.premiumForRm =
74
- premium -
75
- policy.purePremium -
76
- policy.premiumForLps -
77
- policy.premiumForEnsuro;
83
+ policy.partnerCommission = premium - policy.purePremium - coc - policy.ensuroCommission;
78
84
  return policy;
79
85
  }
80
86
 
81
- function interestRate(PolicyData memory policy) internal pure returns (uint256) {
87
+ function jrInterestRate(PolicyData memory policy) internal pure returns (uint256) {
82
88
  return
83
- policy
84
- .premiumForLps
85
- .wadMul(SECONDS_IN_YEAR)
86
- .wadDiv((policy.expiration - policy.start) * policy.scr)
87
- .wadToRay();
89
+ ((policy.jrCoc * SECONDS_PER_YEAR) / (policy.expiration - policy.start)).wadDiv(policy.jrScr);
90
+ }
91
+
92
+ function jrAccruedInterest(PolicyData memory policy) internal view returns (uint256) {
93
+ return (policy.jrCoc * (block.timestamp - policy.start)) / (policy.expiration - policy.start);
88
94
  }
89
95
 
90
- function accruedInterest(PolicyData memory policy) internal view returns (uint256) {
91
- uint256 secs = block.timestamp - policy.start;
96
+ function srInterestRate(PolicyData memory policy) internal pure returns (uint256) {
92
97
  return
93
- policy
94
- .scr
95
- .wadToRay()
96
- .rayMul(secs * interestRate(policy))
97
- .rayDiv(SECONDS_IN_YEAR_RAY)
98
- .rayToWad();
98
+ ((policy.srCoc * SECONDS_PER_YEAR) / (policy.expiration - policy.start)).wadDiv(policy.srScr);
99
+ }
100
+
101
+ function srAccruedInterest(PolicyData memory policy) internal view returns (uint256) {
102
+ return (policy.srCoc * (block.timestamp - policy.start)) / (policy.expiration - policy.start);
99
103
  }
100
104
 
101
105
  function hash(PolicyData memory policy) internal pure returns (bytes32) {
@@ -4,9 +4,9 @@ pragma solidity ^0.8.2;
4
4
  import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
5
5
  import {ERC721Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
6
6
  import {PausableUpgradeable} from "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
7
- import {IPolicyPool} from "../interfaces/IPolicyPool.sol";
7
+ import {IPolicyPool} from "./interfaces/IPolicyPool.sol";
8
8
  import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
9
- import {IPolicyNFT} from "../interfaces/IPolicyNFT.sol";
9
+ import {IPolicyNFT} from "./interfaces/IPolicyNFT.sol";
10
10
 
11
11
  /**
12
12
  * @title PolicyNFT - NFT that keeps track of issued policies and its owners