@ensuro/core 2.0.0-beta11 → 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.
@@ -22,6 +22,12 @@ contract SignedQuoteRiskModule is RiskModule {
22
22
  /// @custom:oz-upgrades-unsafe-allow state-variable-immutable
23
23
  bool internal immutable _creationIsOpen;
24
24
 
25
+ /**
26
+ * @dev Event emitted every time a new policy is created. It allows to link the policyData with a particular policy
27
+ *
28
+ * @param policyId The id of the policy
29
+ * @param policyData The value sent in `policyData` parameter that's the hash of the off-chain stored data.
30
+ */
25
31
  event NewSignedPolicy(uint256 indexed policyId, bytes32 policyData);
26
32
 
27
33
  /// @custom:oz-upgrades-unsafe-allow constructor
@@ -74,7 +80,7 @@ contract SignedQuoteRiskModule is RiskModule {
74
80
  uint40 quoteValidUntil,
75
81
  address payer,
76
82
  address onBehalfOf
77
- ) internal returns (uint256 policyId) {
83
+ ) internal returns (Policy.PolicyData memory createdPolicy) {
78
84
  if (!_creationIsOpen)
79
85
  _policyPool.access().checkComponentRole(
80
86
  address(this),
@@ -108,33 +114,92 @@ contract SignedQuoteRiskModule is RiskModule {
108
114
  _policyPool.access().checkComponentRole(address(this), PRICER_ROLE, signer, false);
109
115
  uint96 internalId = uint96(uint256(policyData) % 2**96);
110
116
 
111
- policyId = _newPolicy(payout, premium, lossProb, expiration, payer, onBehalfOf, internalId).id;
112
- emit NewSignedPolicy(policyId, policyData);
113
- return policyId;
117
+ createdPolicy = _newPolicy(
118
+ payout,
119
+ premium,
120
+ lossProb,
121
+ expiration,
122
+ payer,
123
+ onBehalfOf,
124
+ internalId
125
+ );
126
+ emit NewSignedPolicy(createdPolicy.id, policyData);
127
+ return createdPolicy;
128
+ }
129
+
130
+ /**
131
+  * @dev Creates a new Policy using a signed quote. The caller is the payer of the policy. Returns all the struct, not
132
+ * just the id.
133
+ *
134
+ * Same as {newPolicy} but returns all the PolicyData struct, usefull if the PolicyData needs to be stored on-chain.
135
+  *
136
+  * Requirements:
137
+  * - The caller approved the spending of the premium to the PolicyPool
138
+  * - The quote has been signed by an address with the component role PRICER_ROLE
139
+  *
140
+  * Emits:
141
+  * - {PolicyPool.NewPolicy}
142
+  * - {NewSignedPolicy}
143
+  *
144
+  * @param payout The exposure (maximum payout) of the policy
145
+  * @param premium The premium that will be paid by the payer
146
+  * @param lossProb The probability of having to pay the maximum payout (wad)
147
+  * @param expiration The expiration of the policy (timestamp)
148
+  * @param onBehalfOf The policy holder
149
+  * @param policyData A hash of the private details of the policy. The last 96 bits will be used as internalId
150
+  * @param quoteSignatureR The signature of the quote. R component (EIP-2098 signature)
151
+  * @param quoteSignatureVS The signature of the quote. VS component (EIP-2098 signature)
152
+  * @param quoteValidUntil The expiration of the quote
153
+  * @return createdPolicy Returns the created policy
154
+  */
155
+ function newPolicyFull(
156
+ uint256 payout,
157
+ uint256 premium,
158
+ uint256 lossProb,
159
+ uint40 expiration,
160
+ address onBehalfOf,
161
+ bytes32 policyData,
162
+ bytes32 quoteSignatureR,
163
+ bytes32 quoteSignatureVS,
164
+ uint40 quoteValidUntil
165
+ ) external returns (Policy.PolicyData memory createdPolicy) {
166
+ return
167
+ _newPolicySigned(
168
+ payout,
169
+ premium,
170
+ lossProb,
171
+ expiration,
172
+ policyData,
173
+ quoteSignatureR,
174
+ quoteSignatureVS,
175
+ quoteValidUntil,
176
+ _msgSender(),
177
+ onBehalfOf
178
+ );
114
179
  }
115
180
 
116
181
  /**
117
-  * @dev Creates a new Policy using a signed quote. The caller is the payer of the policy.
118
-  *
119
-  * Requirements:
120
-  * - The caller approved the spending of the premium to the PolicyPool
121
-  * - The quote has been signed by an address with the component role PRICER_ROLE
122
-  *
123
-  * Emits:
124
-  * - {PolicyPool.NewPolicy}
125
-  * - {NewSignedPolicy}
126
-  *
127
-  * @param payout The exposure (maximum payout) of the policy
128
-  * @param premium The premium that will be paid by the payer
129
-  * @param lossProb The probability of having to pay the maximum payout (wad)
130
-  * @param expiration The expiration of the policy (timestamp)
131
-  * @param onBehalfOf The policy holder
132
-  * @param policyData A hash of the private details of the policy. The last 96 bits will be used as internalId
133
-  * @param quoteSignatureR The signature of the "quote". R component (EIP-2098 signature)
134
-  * @param quoteSignatureVS The signature of the "quote". VS component (EIP-2098 signature)
135
-  * @param quoteValidUntil The expiration of the quote
136
-  * @return Returns the id of the created policy
137
-  */
182
+  * @dev Creates a new Policy using a signed quote. The caller is the payer of the policy.
183
+  *
184
+  * Requirements:
185
+  * - The caller approved the spending of the premium to the PolicyPool
186
+  * - The quote has been signed by an address with the component role PRICER_ROLE
187
+  *
188
+  * Emits:
189
+  * - {PolicyPool.NewPolicy}
190
+  * - {NewSignedPolicy}
191
+  *
192
+  * @param payout The exposure (maximum payout) of the policy
193
+  * @param premium The premium that will be paid by the payer
194
+  * @param lossProb The probability of having to pay the maximum payout (wad)
195
+  * @param expiration The expiration of the policy (timestamp)
196
+  * @param onBehalfOf The policy holder
197
+  * @param policyData A hash of the private details of the policy. The last 96 bits will be used as internalId
198
+  * @param quoteSignatureR The signature of the quote. R component (EIP-2098 signature)
199
+  * @param quoteSignatureVS The signature of the quote. VS component (EIP-2098 signature)
200
+  * @param quoteValidUntil The expiration of the quote
201
+  * @return Returns the id of the created policy
202
+  */
138
203
  function newPolicy(
139
204
  uint256 payout,
140
205
  uint256 premium,
@@ -158,31 +223,31 @@ contract SignedQuoteRiskModule is RiskModule {
158
223
  quoteValidUntil,
159
224
  _msgSender(),
160
225
  onBehalfOf
161
- );
226
+ ).id;
162
227
  }
163
228
 
164
229
  /**
165
-  * @dev Creates a new Policy using a signed quote. The payer is the policy holder
166
-  *
167
-  * Requirements:
168
-  * - currency().allowance(onBehalfOf, _msgSender()) > 0
169
-  * - The quote has been signed by an address with the component role PRICER_ROLE
170
-  *
171
-  * Emits:
172
-  * - {PolicyPool.NewPolicy}
173
-  * - {NewSignedPolicy}
174
-  *
175
-  * @param payout The exposure (maximum payout) of the policy
176
-  * @param premium The premium that will be paid by the payer
177
-  * @param lossProb The probability of having to pay the maximum payout (wad)
178
-  * @param expiration The expiration of the policy (timestamp)
179
-  * @param onBehalfOf The policy holder
180
-  * @param policyData A hash of the private details of the policy. The last 96 bits will be used as internalId
181
-  * @param quoteSignatureR The signature of the "quote". R component (EIP-2098 signature)
182
-  * @param quoteSignatureVS The signature of the "quote". VS component (EIP-2098 signature)
183
-  * @param quoteValidUntil The expiration of the quote
184
-  * @return Returns the id of the created policy
185
-  */
230
+  * @dev Creates a new Policy using a signed quote. The payer is the policy holder
231
+  *
232
+  * Requirements:
233
+  * - currency().allowance(onBehalfOf, _msgSender()) > 0
234
+  * - The quote has been signed by an address with the component role PRICER_ROLE
235
+  *
236
+  * Emits:
237
+  * - {PolicyPool.NewPolicy}
238
+  * - {NewSignedPolicy}
239
+  *
240
+  * @param payout The exposure (maximum payout) of the policy
241
+  * @param premium The premium that will be paid by the payer
242
+  * @param lossProb The probability of having to pay the maximum payout (wad)
243
+  * @param expiration The expiration of the policy (timestamp)
244
+  * @param onBehalfOf The policy holder
245
+  * @param policyData A hash of the private details of the policy. The last 96 bits will be used as internalId
246
+  * @param quoteSignatureR The signature of the quote. R component (EIP-2098 signature)
247
+  * @param quoteSignatureVS The signature of the quote. VS component (EIP-2098 signature)
248
+  * @param quoteValidUntil The expiration of the quote
249
+  * @return Returns the id of the created policy
250
+  */
186
251
  function newPolicyPaidByHolder(
187
252
  uint256 payout,
188
253
  uint256 premium,
@@ -220,7 +285,7 @@ contract SignedQuoteRiskModule is RiskModule {
220
285
  quoteValidUntil,
221
286
  onBehalfOf,
222
287
  onBehalfOf
223
- );
288
+ ).id;
224
289
  }
225
290
 
226
291
  function resolvePolicy(Policy.PolicyData calldata policy, uint256 payout)
@@ -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
  /**
@@ -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.
@@ -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
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@ensuro/core",
3
3
  "description": "Ensuro - Decentralized insurance protocol",
4
- "version": "2.0.0-beta11",
4
+ "version": "2.0.0-beta12",
5
5
  "files": [
6
6
  "**/*.sol",
7
7
  "/build/contracts/*.json",