# PriceRiskModule

[PriceRiskModule](https://github.com/ensuro/price-risk-module) is a Risk Module that triggers the payout if the price of an asset is lower or higher than a set trigger price. It's not part of the core Ensuro repository but an extension with its [own repository](https://github.com/ensuro/price-risk-module).

The loss probability is determined by a cumulative distribution function $$f(currentPrice, targetPrice, duration)$$, which is the probability that the price will reach the target within that duration.

The function is pre-computed offchain and loaded into the contract as an array of histograms, one for each duration in hours.

## **Roles**

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

<table><thead><tr><th>Role</th><th width="100" data-type="checkbox">Global*</th><th>Description</th><th>Methds Accessible</th></tr></thead><tbody><tr><td>PRICER_ROLE</td><td>false</td><td>Sets the pricing.</td><td><ul><li>setCDF: Sets the probability distribution for a given policy duration.</li></ul></td></tr><tr><td>ORACLE_ADMIN_ROLE</td><td>false</td><td>Sets the pricing oracle.</td><td><ul><li>setMinDuration: Sets the minimum duration before a policy can be activated.</li><li>setOracle: Changes the pricing oracle used by the risk module.</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

| Field                      | Type                            | Description                                                                                                                                                                                                                        |
| -------------------------- | ------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| assetOracle                | address (AggregatorV3Interface) | Address of the Chainlink price feed oracle for the asset. Immutable                                                                                                                                                                |
| referenceOracle (optional) | address (AggregatorV3Interface) | Address of the Chainlink price feed oracle for the reference asset. If address(0), the price will be computed in the oracle denomination. If non zero, it has to be in the same denomination as the *assetOracle* param. Immutable |
| slotSize                   | uint256 (wad)                   | There's a fixed number of slots for the histograms (40) but the slot size can be set at contract initialization to accommodate different requirements.                                                                             |
| oracleTolerance            | uint40 (seconds)                | This is the maximum accepted age of the price data provided by the oracles.                                                                                                                                                        |
| minDuration                | uint40 (seconds)                | The minimum time that must elapse before a policy can be triggered, since the policy creation.                                                                                                                                     |

{% hint style="info" %}
**Important Notice**: the contract has no way of verifying that both oracles have the same denomination, users will have to make sure of that when deploying. A deployment audit by a reputable third party is highly recommended.
{% endhint %}

## External Methods

### newPolicy

```solidity
function newPolicy(uint256 triggerPrice, bool lower, uint256 payout, uint40 expiration) external returns (uint256)
```

| Name         | Type    | Description                                                                                        |
| ------------ | ------- | -------------------------------------------------------------------------------------------------- |
| triggerPrice | uint256 | Price of the asset that will trigger the policy (expressed in the reference currency)              |
| lower        | bool    | <p>If true -> triggers if the price is lower</p><p>If false -> triggers if the price is higher</p> |
| payout       | uint256 | Expressed in policyPool.currency()                                                                 |
| expiration   | uint40  | The policy expiration timestamp                                                                    |
| onBehalfOf   | address | The address that will own the new policy                                                           |

| Name     | Type    | Description   |
| -------- | ------- | ------------- |
| policyId | uint256 | The policy id |

### triggerPolicy

```solidity
function triggerPolicy(uint256 policyId) external
```

Triggers the policy if the `triggerPrice` has been reached, provided that the policy is active and `minDuration` has elapsed.

### pricePolicy

```solidity
function pricePolicy(uint256 triggerPrice, bool lower, uint256 payout, uint40 expiration) external view returns (uint256 premium, uint256 lossProb)
```

Returns the premium and lossProb of the policy

| Name         | Type    | Description                                                                            |
| ------------ | ------- | -------------------------------------------------------------------------------------- |
| triggerPrice | uint256 | Price of the asset that will trigger the policy (expressed in the reference currency)  |
| lower        | bool    | If true -> triggers if the price is lower, If false -> triggers if the price is higher |
| payout       | uint256 | Expressed in policyPool.currency()                                                     |
| expiration   | uint40  | Expiration of the policy                                                               |

| Name     | Type    | Description                              |
| -------- | ------- | ---------------------------------------- |
| premium  | uint256 | Premium that needs to be paid            |
| lossProb | uint256 | Probability of paying the maximum payout |

### setCDF

```solidity
function setCDF(int40 duration, uint256[30] cdf) external
```

Sets the probability distribution for a given duration

| Name     | Type         | Description                                                                                                                        |
| -------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------- |
| duration | int40        | Duration of the policy in hours (simetric rounding) positive if probability of lower price negative if probability of higher price |
| cdf      | uint256\[30] | Array where cdf\[i] = prob of price lower/higher than i% of current price                                                          |

### setMinDuration

<pre class="language-solidity"><code class="lang-solidity"><strong>function setMinDuration(uint40 minDuration)
</strong></code></pre>

Sets the minimum duration before a policy can be triggered.

Receives the new minimum duration in seconds.

### getCDF

```solidity
function getCDF(int40 duration) external view returns (uint256[30])
```

Returns the probability distribution for a given duration

### referenceOracle

```solidity
function referenceOracle() external view override returns (AggregatorV3Interface)
```

Returns the address for the reference oracle.

### assetOracle

```solidity
function assetOracle() external view override returns (AggregatorV3Interface)
```

Returns the address for the asset oracle.

### minDuration

```solidity
function minDuration() external view override returns (uint40)
```

Returns the minimal duration for policies.

### oracleTolerance

```
function oracleTolerance() external view override returns (uint40)
```

Returns the oracle tolerance.
