# RiskModule

Risk Module is an abstract contract that needs to be implemented with specific policies for the main responsibilities of the contract: pricing and resolution.

The contract also stores several parameters that are used for the creation and validation of the policies.

## **Roles**

The specific roles and functions of the contract are as follows:

<table><thead><tr><th>Role</th><th width="83" data-type="checkbox">Global*</th><th>Description</th><th>Methods Accessible</th></tr></thead><tbody><tr><td>LEVEL1_ROLE</td><td>true</td><td>High impact changes like upgrades or other critical operations.</td><td><ul><li><a href="#setting-parameters">setParam</a>: Set parameters.</li></ul></td></tr><tr><td>LEVEL2_ROLE</td><td>true</td><td>Mid-impact changes like changing some parameters.</td><td><ul><li><a href="#setting-parameters">setParam</a>: Set parameters.</li></ul></td></tr><tr><td>LEVEL3_ROLE</td><td>true</td><td>Low-impact changes like changing some parameters up to given percentage (tweaks).</td><td><ul><li><a href="https://app.gitbook.com/o/0kzqc3VraIOjgEzncVRI/s/pvASGnPdK0F6cKq9e2Lh/~/changes/73/smart-contracts/contracts/riskmodule#setting-parameters">setParam</a>: Set parameters.</li></ul></td></tr><tr><td>RM_PROVIDER</td><td>false</td><td>Manages the risk module.</td><td><ul><li>setWallet: Sets the risk module wallet.</li></ul></td></tr></tbody></table>

( \* ) Global means that the role can be delegated to a user at the protocol level (for all components) or only for a specific component. Non-global roles can only be granted for a specific component.

## Parameters

These parameters are managed and adjusted by the protocol governance. Some come from business decisions and others are quantitative analysis decisions.

| Field              | Type             | Description                                                                                                                                                                                              |
| ------------------ | ---------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| moc                | uint256 (wad)    | Margin of Conservatism. A factor used to scale the expected losses calculated as `payout * lossProb`                                                                                                     |
| jrCollRatio        | uint256 (wad)    | The collateralization ratio up to the junior eToken (see [Solvency Breakdown](https://docs.ensuro.co/ensuro-docs/policy#solvency-breakdown))                                                             |
| collRatio          | uint256 (wad)    | The overall collateralization ratio (see [Solvency Breakdown](https://docs.ensuro.co/ensuro-docs/policy#solvency-breakdown))                                                                             |
| ensuroPpFee        | uint256 (wad)    | Percentage applied to the pure premium used to compute the [Ensuro Commision](https://docs.ensuro.co/ensuro-docs/policy#ensuro-commission).                                                              |
| ensuroCocFee       | uint256 (wad)    | Percentage applied to the cost of capital used to compute the [Ensuro Commision](https://docs.ensuro.co/ensuro-docs/policy#ensuro-commission).                                                           |
| jrRoc              | uint256 (wad)    | Annualized return expected by Junior eToken liquidity providers for the capital exposed to these risks. See [more details](https://docs.ensuro.co/ensuro-docs/policy#junior-and-senior-cost-of-capital). |
| srRoc              | uint256 (wad)    | Annualized return expected by Senior eToken liquidity providers for the capital exposed to these risks. See [more details](https://docs.ensuro.co/ensuro-docs/policy#junior-and-senior-cost-of-capital). |
| maxPayoutPerPolicy | uint256 (amount) | The maximum payout that's allowed as payout for each individual policy of this risk module.                                                                                                              |
| exposureLimit      | uint256 (amount) | The limit on the cumulative exposure of all the active policies of this module.                                                                                                                          |
| maxDuration        | uint256(hours)   | The maximum duration of the policies (in hours).                                                                                                                                                         |

### Packed parameters

To save gas, the parameters are stored in a packed struct that fits in 256bits (a *word* in the EVM). This is not visible externally, but put's limits on the precision of the parameters. The *wad* ones, have 4 decimals as precision, so 1.12345 will be truncated to 1.1234. *maxPayoutPerPolicy* has a precision of two decimals, and *exposureLimit* 0 decimals.

## Internal methods

### \_newPolicy

```solidity
function _newPolicy(uint256 payout, uint256 premium, uint256 lossProb, uint40 expiration, address payer, address onBehalfOf, uint96 internalId) internal returns (struct Policy.PolicyData)
```

Called from child contracts to create policies (after they validated the pricing)

| Name       | Type    | Description                                                                       |
| ---------- | ------- | --------------------------------------------------------------------------------- |
| payout     | uint256 | The exposure (maximum payout) of the policy                                       |
| premium    | uint256 | The premium that will be paid by the policyHolder                                 |
| lossProb   | uint256 | The probability of having to pay the maximum payout (wad)                         |
| expiration | uint40  | The expiration of the policy (timestamp)                                          |
| payer      | address | The account that pays for the premium                                             |
| onBehalfOf | address | The policy holder                                                                 |
| internalId | uint96  | An id that's unique within this module and it will be used to identify the policy |

## External Methods

### releaseExposure

```solidity
function releaseExposure(uint256 payout) external
```

Called when a policy expires or is resolved to update the exposure.

Requirements:

* Must be called by `policyPool()`

| Name   | Type    | Description                                 |
| ------ | ------- | ------------------------------------------- |
| payout | uint256 | The exposure (maximum payout) of the policy |

### Setting parameters

Usually, **LEVEL1\_ROLE, LEVEL2\_ROLE** or **LEVEL3\_ROLE** is requested for performing operations that change numerical parameters of the components. Both global and component role are accepted. If the user has **LEVEL3\_ROLE** the changes might be restricted to small tweaks.
