@ensuro/core 2.1.0 → 2.2.0-beta1

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.
@@ -144,28 +144,32 @@ abstract contract RiskModule is IRiskModule, PolicyPoolComponent {
144
144
  }
145
145
 
146
146
  // runs validation on RiskModule parameters
147
- function _validateParameters() internal view override {
148
- require(_params.jrCollRatio <= HUNDRED_PERCENT, "Validation: jrCollRatio must be <=1");
149
- require(
150
- _params.collRatio <= HUNDRED_PERCENT && _params.collRatio > 0,
151
- "Validation: collRatio must be <=1"
152
- );
153
- require(_params.collRatio >= _params.jrCollRatio, "Validation: collRatio >= jrCollRatio");
154
- require(_params.moc <= MAX_MOC && _params.moc >= MIN_MOC, "Validation: moc must be [0.5, 4]");
155
- require(_params.ensuroPpFee <= HUNDRED_PERCENT, "Validation: ensuroPpFee must be <= 1");
156
- require(_params.ensuroCocFee <= HUNDRED_PERCENT, "Validation: ensuroCocFee must be <= 1");
157
- require(_params.srRoc <= HUNDRED_PERCENT, "Validation: srRoc must be <= 1 (100%)");
158
- require(_params.jrRoc <= HUNDRED_PERCENT, "Validation: jrRoc must be <= 1 (100%)");
147
+ function _validateParameters() internal view virtual override {
159
148
  // _maxPayoutPerPolicy no limits
160
149
  require(
161
150
  exposureLimit() >= _activeExposure,
162
151
  "Validation: exposureLimit can't be less than actual activeExposure"
163
152
  );
153
+ require(_wallet != address(0), "Validation: Wallet can't be zero address");
154
+ _validatePackedParams(_params);
155
+ }
156
+
157
+ function _validatePackedParams(PackedParams storage params_) internal view {
158
+ require(params_.jrCollRatio <= HUNDRED_PERCENT, "Validation: jrCollRatio must be <=1");
164
159
  require(
165
- _params.exposureLimit > 0 && _params.maxPayoutPerPolicy > 0,
160
+ params_.collRatio <= HUNDRED_PERCENT && params_.collRatio > 0,
161
+ "Validation: collRatio must be <=1"
162
+ );
163
+ require(params_.collRatio >= params_.jrCollRatio, "Validation: collRatio >= jrCollRatio");
164
+ require(params_.moc <= MAX_MOC && params_.moc >= MIN_MOC, "Validation: moc must be [0.5, 4]");
165
+ require(params_.ensuroPpFee <= HUNDRED_PERCENT, "Validation: ensuroPpFee must be <= 1");
166
+ require(params_.ensuroCocFee <= HUNDRED_PERCENT, "Validation: ensuroCocFee must be <= 1");
167
+ require(params_.srRoc <= HUNDRED_PERCENT, "Validation: srRoc must be <= 1 (100%)");
168
+ require(params_.jrRoc <= HUNDRED_PERCENT, "Validation: jrRoc must be <= 1 (100%)");
169
+ require(
170
+ params_.exposureLimit > 0 && params_.maxPayoutPerPolicy > 0,
166
171
  "Exposure and MaxPayout must be >0"
167
172
  );
168
- require(_wallet != address(0), "Validation: Wallet can't be zero address");
169
173
  }
170
174
 
171
175
  function name() public view override returns (string memory) {
@@ -273,16 +277,20 @@ abstract contract RiskModule is IRiskModule, PolicyPoolComponent {
273
277
  );
274
278
  }
275
279
 
276
- function params() public view override returns (Params memory ret) {
280
+ function params() public view virtual override returns (Params memory ret) {
281
+ return _unpackParams(_params);
282
+ }
283
+
284
+ function _unpackParams(PackedParams storage params_) internal view returns (Params memory ret) {
277
285
  return
278
286
  Params({
279
- moc: _4toWad(_params.moc),
280
- jrCollRatio: _4toWad(_params.jrCollRatio),
281
- collRatio: _4toWad(_params.collRatio),
282
- ensuroPpFee: _4toWad(_params.ensuroPpFee),
283
- ensuroCocFee: _4toWad(_params.ensuroCocFee),
284
- jrRoc: _4toWad(_params.jrRoc),
285
- srRoc: _4toWad(_params.srRoc)
287
+ moc: _4toWad(params_.moc),
288
+ jrCollRatio: _4toWad(params_.jrCollRatio),
289
+ collRatio: _4toWad(params_.collRatio),
290
+ ensuroPpFee: _4toWad(params_.ensuroPpFee),
291
+ ensuroCocFee: _4toWad(params_.ensuroCocFee),
292
+ jrRoc: _4toWad(params_.jrRoc),
293
+ srRoc: _4toWad(params_.srRoc)
286
294
  });
287
295
  }
288
296
 
@@ -296,8 +304,16 @@ abstract contract RiskModule is IRiskModule, PolicyPoolComponent {
296
304
  uint256 payout,
297
305
  uint256 lossProb,
298
306
  uint40 expiration
299
- ) public view returns (uint256) {
300
- Params memory p = params();
307
+ ) public view virtual returns (uint256) {
308
+ return _getMinimumPremium(payout, lossProb, expiration, params());
309
+ }
310
+
311
+ function _getMinimumPremium(
312
+ uint256 payout,
313
+ uint256 lossProb,
314
+ uint40 expiration,
315
+ Params memory p
316
+ ) internal view returns (uint256) {
301
317
  uint256 purePremium = payout.wadMul(lossProb.wadMul(p.moc));
302
318
  uint256 jrScr = payout.wadMul(p.jrCollRatio);
303
319
  if (jrScr > purePremium) {
@@ -342,6 +358,66 @@ abstract contract RiskModule is IRiskModule, PolicyPoolComponent {
342
358
  address onBehalfOf,
343
359
  uint96 internalId
344
360
  ) internal whenNotPaused returns (Policy.PolicyData memory) {
361
+ return
362
+ _newPolicyWithParams(
363
+ payout,
364
+ premium,
365
+ lossProb,
366
+ expiration,
367
+ payer,
368
+ onBehalfOf,
369
+ internalId,
370
+ params()
371
+ );
372
+ }
373
+
374
+ /**
375
+ * @dev Called from child contracts to create policies (after they validated the pricing), receives custom params
376
+ *
377
+ * @param payout The exposure (maximum payout) of the policy
378
+ * @param premium The premium that will be paid by the policyHolder
379
+ * @param lossProb The probability of having to pay the maximum payout (wad)
380
+ * @param payer The account that pays for the premium
381
+ * @param expiration The expiration of the policy (timestamp)
382
+ * @param onBehalfOf The policy holder
383
+ * @param internalId An id that's unique within this module and it will be used to identify the policy
384
+ */
385
+ function _newPolicyCustomParams(
386
+ uint256 payout,
387
+ uint256 premium,
388
+ uint256 lossProb,
389
+ uint40 expiration,
390
+ address payer,
391
+ address onBehalfOf,
392
+ uint96 internalId,
393
+ Params memory params_
394
+ ) internal whenNotPaused returns (Policy.PolicyData memory) {
395
+ return
396
+ _newPolicyWithParams(
397
+ payout,
398
+ premium,
399
+ lossProb,
400
+ expiration,
401
+ payer,
402
+ onBehalfOf,
403
+ internalId,
404
+ params_
405
+ );
406
+ }
407
+
408
+ /**
409
+ * @dev Internal method without whenNotPaused, MUST be called from other function that has this modifier
410
+ */
411
+ function _newPolicyWithParams(
412
+ uint256 payout,
413
+ uint256 premium,
414
+ uint256 lossProb,
415
+ uint40 expiration,
416
+ address payer,
417
+ address onBehalfOf,
418
+ uint96 internalId,
419
+ Params memory params_
420
+ ) internal returns (Policy.PolicyData memory) {
345
421
  if (premium == type(uint256).max) {
346
422
  premium = getMinimumPremium(payout, lossProb, expiration);
347
423
  }
@@ -361,7 +437,7 @@ abstract contract RiskModule is IRiskModule, PolicyPoolComponent {
361
437
  require(payout <= maxPayoutPerPolicy(), "RiskModule: Payout is more than maximum per policy");
362
438
  Policy.PolicyData memory policy = Policy.initialize(
363
439
  this,
364
- params(),
440
+ params_,
365
441
  premium,
366
442
  payout,
367
443
  lossProb,
package/js/test-utils.js CHANGED
@@ -354,9 +354,11 @@ const _R = amountFunction(27);
354
354
  * getComponentRole("0xc6e7DF5E7b4f2A278906862b61205850344D4e7d", "ORACLE_ADMIN_ROLE")
355
355
  * // "0x05e01b185238b49f750d03d945e38a7f6c3be8b54de0ee42d481eb7814f0d3a8"
356
356
  */
357
- function getComponentRole(componentAddress, roleName) {
357
+ function getComponentRole(componentAddress, role) {
358
+ if (!role.startsWith("0x")) role = getRole(role);
359
+
358
360
  // 32 byte array
359
- const bytesRole = hre.ethers.utils.arrayify(getRole(roleName));
361
+ const bytesRole = hre.ethers.utils.arrayify(role);
360
362
 
361
363
  // 20 byte array
362
364
  const bytesAddress = hre.ethers.utils.arrayify(componentAddress);
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.1.0",
4
+ "version": "2.2.0-beta1",
5
5
  "files": [
6
6
  "**/*.sol",
7
7
  "/build/contracts/*.json",