# OEV

Moonwell's OEV system captures value from liquidation MEV at the protocol level, splitting profits between liquidators and the protocol. Instead of block builders capturing all liquidation surplus, Moonwell delays Chainlink price updates by a short window and allows liquidators to access fresh prices early by routing liquidations through OEV wrapper contracts.

## How It Works

1. Each Chainlink price feed is wrapped in an OEV wrapper contract
2. The wrapper **delays price updates** by `maxRoundDelay` seconds (default: 10s)
3. Liquidators call `updatePriceEarlyAndLiquidate()` to access the fresh price and execute the liquidation in one transaction
4. Liquidation profits are split between the liquidator and the protocol based on `liquidatorFeeBps`
5. After the delay window expires, the fresh price becomes publicly available to everyone

There are two OEV wrapper variants - one for Moonwell Core markets (mToken-based) and one for Morpho Blue isolated markets.

## Configuration

These are the current deployed values. They are set at deployment and can be updated by the contract owner - do not hardcode them.

| Parameter          | Current Value | Description                                  |
| ------------------ | ------------- | -------------------------------------------- |
| `liquidatorFeeBps` | 4000          | Liquidator keeps 40% of profit               |
| `maxRoundDelay`    | 10            | Seconds before price becomes public          |
| `maxDecrements`    | 10            | Max previous rounds to search for valid data |

## Price Delay Mechanism

Both OEV wrappers implement the same delay logic in `latestRoundData()`:

```solidity
// Pseudo-code
if (roundId != cachedRoundId && block.timestamp < updatedAt + maxRoundDelay) {
    // Return PREVIOUS round data (delayed price)
    return getPreviousRoundData();
} else {
    // Return current round data (fresh price available to everyone)
    return currentRoundData;
}
```

After `maxRoundDelay` seconds, the fresh price becomes available to everyone. The OEV window is only valuable during this delay period.

{% content-ref url="oev/core-markets" %}
[core-markets](https://docs.moonwell.fi/moonwell/developers/protocol/oev/core-markets)
{% endcontent-ref %}

{% content-ref url="oev/morpho-markets" %}
[morpho-markets](https://docs.moonwell.fi/moonwell/developers/protocol/oev/morpho-markets)
{% endcontent-ref %}
