@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,175 @@
1
+ // SPDX-License-Identifier: Apache-2.0
2
+ pragma solidity ^0.8.0;
3
+
4
+ import {SafeCast} from "@openzeppelin/contracts/utils/math/SafeCast.sol";
5
+ import {IERC20Metadata} from "@openzeppelin/contracts/token/ERC20/extensions/IERC20Metadata.sol";
6
+ import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
7
+ import {IPolicyPoolComponent} from "./interfaces/IPolicyPoolComponent.sol";
8
+ import {IAssetManager} from "./interfaces/IAssetManager.sol";
9
+ import {IAccessManager} from "./interfaces/IAccessManager.sol";
10
+ import {Math} from "@openzeppelin/contracts/utils/math/Math.sol";
11
+
12
+ /**
13
+ * @title Base class for asset management strategies that use thresholds for cash and investment balance
14
+ * @dev Base class for asset management strategies that use thresholds for cash and investment balance.
15
+ * The specific asset management strategy needs to be implemented by child contracts.
16
+ * Settings liquidityMin, liquidityMiddle, liquidityMax are the thresholds used to define how much liquidity
17
+ * to keep in the PolicyPool and when to invest/deinvest. Every invest/deinvest operation tries to leave the
18
+ * cash at liquidityMiddle.
19
+ * @custom:security-contact security@ensuro.co
20
+ * @author Ensuro
21
+ *
22
+ * @notice This contracts uses Diamond Storage and should not define state variables outside of that. See the diamondStorage method for more details.
23
+ */
24
+ abstract contract LiquidityThresholdAssetManager is IAssetManager {
25
+ using SafeCast for uint256;
26
+
27
+ IERC20Metadata internal immutable _asset;
28
+ event GovernanceAction(IAccessManager.GovernanceActions indexed action, uint256 value);
29
+
30
+ struct DiamondStorage {
31
+ uint32 liquidityMin; // stored with 0 decimals
32
+ uint32 liquidityMiddle; // stored with 0 decimals
33
+ uint32 liquidityMax; // stored with 0 decimals
34
+ uint128 lastInvestmentValue;
35
+ }
36
+
37
+ modifier validateParamsAfterChange() {
38
+ _;
39
+ _validateParameters();
40
+ }
41
+
42
+ constructor(IERC20Metadata asset_) {
43
+ require(
44
+ address(asset_) != address(0),
45
+ "LiquidityThresholdAssetManager: asset cannot be zero address"
46
+ );
47
+ _asset = asset_;
48
+ }
49
+
50
+ function diamondStorage() internal pure returns (DiamondStorage storage ds) {
51
+ // Inspired from https://eips.ethereum.org/EIPS/eip-2535#facets-state-variables-and-diamond-storage
52
+ // Set the position of our struct in contract storage
53
+ bytes32 storagePosition = keccak256("co.ensuro.LiquidityThresholdAssetManager");
54
+ // solhint-disable-next-line no-inline-assembly
55
+ assembly {
56
+ ds.slot := storagePosition
57
+ }
58
+ }
59
+
60
+ function _validateParameters() internal view {
61
+ DiamondStorage storage ds = diamondStorage();
62
+
63
+ require(
64
+ ds.liquidityMin <= ds.liquidityMiddle && ds.liquidityMiddle <= ds.liquidityMax,
65
+ "Validation: Liquidity limits are invalid"
66
+ );
67
+ }
68
+
69
+ function connect() public virtual override {
70
+ require(
71
+ IPolicyPoolComponent(address(this)).policyPool().currency() == _asset,
72
+ "Asset mismatch"
73
+ );
74
+ }
75
+
76
+ function recordEarnings() external virtual override returns (int256) {
77
+ uint256 investmentValue = getInvestmentValue();
78
+ DiamondStorage storage ds = diamondStorage();
79
+ int256 earnings = int256(investmentValue) - int256(uint256(ds.lastInvestmentValue));
80
+ ds.lastInvestmentValue = investmentValue.toUint128();
81
+ emit EarningsRecorded(earnings);
82
+ return earnings;
83
+ }
84
+
85
+ /**
86
+ * @dev Returns the current value of the investment portfolio
87
+ */
88
+ function getInvestmentValue() public view virtual returns (uint256);
89
+
90
+ /**
91
+ * @dev Rebalances cash between PolicyPool wallet and
92
+ */
93
+ function rebalance() external virtual override {
94
+ uint256 cash = _asset.balanceOf(address(this));
95
+ if (cash > liquidityMax()) {
96
+ _invest(cash - liquidityMiddle());
97
+ } else if (cash < liquidityMin()) {
98
+ uint256 deinvestAmount = Math.min(getInvestmentValue(), liquidityMiddle() - cash);
99
+ if (deinvestAmount > 0) {
100
+ _deinvest(deinvestAmount);
101
+ }
102
+ }
103
+ }
104
+
105
+ /**
106
+ * @dev This is called from PolicyPool when doesn't have enough money for payment.
107
+ * After the call, there should be enough money in PolicyPool.currency().balanceOf(this) to
108
+ * do the payment
109
+ * @param paymentAmount The amount of the payment
110
+ */
111
+ function refillWallet(uint256 paymentAmount) external override returns (uint256 deinvest) {
112
+ uint256 cash = _asset.balanceOf(address(this));
113
+ require(cash < paymentAmount, "No need to refill the wallet for this payment");
114
+ uint256 investmentValue = getInvestmentValue();
115
+ // try to leave the pool balance at liquidity_middle after the payment
116
+ deinvest = paymentAmount + liquidityMiddle() - cash;
117
+ if (deinvest > investmentValue) deinvest = investmentValue;
118
+ _deinvest(deinvest);
119
+ return deinvest;
120
+ }
121
+
122
+ function _invest(uint256 amount) internal virtual {
123
+ DiamondStorage storage ds = diamondStorage();
124
+ ds.lastInvestmentValue += amount.toUint128();
125
+ emit MoneyInvested(amount);
126
+ // must be reimplemented do the actual cash movement
127
+ }
128
+
129
+ function _deinvest(uint256 amount) internal virtual {
130
+ DiamondStorage storage ds = diamondStorage();
131
+ ds.lastInvestmentValue -= Math.min(uint256(ds.lastInvestmentValue), amount).toUint128();
132
+ emit MoneyDeinvested(amount);
133
+ // must be reimplemented do the actual cash movement
134
+ }
135
+
136
+ function liquidityMin() public view returns (uint256) {
137
+ return diamondStorage().liquidityMin * 10**_asset.decimals();
138
+ }
139
+
140
+ function liquidityMiddle() public view returns (uint256) {
141
+ return diamondStorage().liquidityMiddle * 10**_asset.decimals();
142
+ }
143
+
144
+ function liquidityMax() public view returns (uint256) {
145
+ return diamondStorage().liquidityMax * 10**_asset.decimals();
146
+ }
147
+
148
+ function setLiquidityThresholds(
149
+ uint256 min,
150
+ uint256 middle,
151
+ uint256 max
152
+ ) external validateParamsAfterChange {
153
+ DiamondStorage storage ds = diamondStorage();
154
+ if (min != type(uint256).max) {
155
+ ds.liquidityMin = (min / 10**_asset.decimals()).toUint32();
156
+ emit GovernanceAction(IAccessManager.GovernanceActions.setLiquidityMin, min);
157
+ }
158
+ if (middle != type(uint256).max) {
159
+ ds.liquidityMiddle = (middle / 10**_asset.decimals()).toUint32();
160
+ emit GovernanceAction(IAccessManager.GovernanceActions.setLiquidityMiddle, middle);
161
+ }
162
+ if (max != type(uint256).max) {
163
+ ds.liquidityMax = (max / 10**_asset.decimals()).toUint32();
164
+ emit GovernanceAction(IAccessManager.GovernanceActions.setLiquidityMax, max);
165
+ }
166
+ }
167
+
168
+ /**
169
+ * @dev See {IERC165-supportsInterface}.
170
+ */
171
+ function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
172
+ return
173
+ interfaceId == type(IERC165).interfaceId || interfaceId == type(IAssetManager).interfaceId;
174
+ }
175
+ }
@@ -1,6 +1,6 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  pragma solidity ^0.8.0;
3
- import {WadRayMath} from "./WadRayMath.sol";
3
+ import {WadRayMath} from "./dependencies/WadRayMath.sol";
4
4
  import {IRiskModule} from "./interfaces/IRiskModule.sol";
5
5
 
6
6
  /**
@@ -102,7 +102,9 @@ library Policy {
102
102
  return (policy.srCoc * (block.timestamp - policy.start)) / (policy.expiration - policy.start);
103
103
  }
104
104
 
105
- function hash(PolicyData memory policy) internal pure returns (bytes32) {
106
- return keccak256(abi.encode(policy));
105
+ function hash(PolicyData memory policy) internal pure returns (bytes32 retHash) {
106
+ retHash = keccak256(abi.encode(policy));
107
+ require(retHash != bytes32(0), "Policy: hash cannot be 0");
108
+ return retHash;
107
109
  }
108
110
  }