> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nilemarkets.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Fee Distribution

> Fees split 70% to LP pool / 30% to treasury — trading fee (0.05% of notional), liquidation penalty (0.3%), oracle fee (flat per price read), all snapshotted at position open time

The Open Nile Protocol collects fees on trading activity and distributes them between the protocol treasury and liquidity providers. All fee parameters are configurable by the admin and snapshotted at position open time.

## Fee Types

| Fee                     | Formula                                             | When Collected                                 |
| ----------------------- | --------------------------------------------------- | ---------------------------------------------- |
| **Trading Fee**         | `notional * snapshotTradingFeeBps / 10,000`         | Open, increase, settlement, close, reduce      |
| **Liquidation Penalty** | `notional * snapshotLiquidationPenaltyBps / 10,000` | Liquidation only (added on top of trading fee) |
| **Oracle Fee**          | Flat `snapshotOracleFee` per price read             | Every forward price lookup                     |

<Note>
  All fee rates are **snapshotted at position open time**. If the admin changes fee configuration after a position is opened, the change only affects newly opened positions. Existing positions retain the rates they were opened with, ensuring traders know their costs upfront.
</Note>

## Distribution Split

Collected fees are split between two destinations using `FeeLib.distributeFee()`:

| Destination  | Default Share   | Description                                                 |
| ------------ | --------------- | ----------------------------------------------------------- |
| **Treasury** | 30% (3,000 bps) | Protocol revenue, used for operations and development       |
| **Pool**     | 70% (7,000 bps) | LP compensation, distributed via vault share price increase |

The pool's share flows directly into the ERC-4626 vault's total assets, increasing the share price for all liquidity providers proportionally to their holdings. LPs do not need to claim fees -- they are automatically reflected in the value of their vault shares.

<Warning>
  Fee destination shares must sum to exactly 10,000 basis points (100%). This is enforced by `Config.setFeeDestinations()` and is protocol invariant #9. Any configuration that does not sum to 10,000 will revert.
</Warning>

## Fee Capping

Fees are always capped at the margin available after PnL settlement. The protocol never reverts due to insufficient funds for fees -- it collects what is available.

<AccordionGroup>
  <Accordion title="After Profit">
    The full margin at risk is available for fee collection:

    ```
    actualFee = min(fee, marginAtRisk)
    ```
  </Accordion>

  <Accordion title="After Loss">
    Loss is deducted from margin first, then fees are taken from the remainder:

    ```
    marginAfterLoss = marginAtRisk - cappedLoss
    actualFee = min(fee, marginAfterLoss)
    ```
  </Accordion>

  <Accordion title="Zero PnL">
    The full margin at risk is available for fee collection:

    ```
    actualFee = min(fee, marginAtRisk)
    ```
  </Accordion>
</AccordionGroup>

<Info>
  If a position's margin is insufficient to cover the full fee after PnL settlement, the protocol collects a partial fee rather than reverting. This ensures settlements and liquidations always complete, even in bad debt scenarios.
</Info>

## Fee Collection Methods

Fees are collected through different mechanisms depending on the context:

| Context                                                     | Collection Method             | Source                                         |
| ----------------------------------------------------------- | ----------------------------- | ---------------------------------------------- |
| **Position open**                                           | `collectFee`                  | Free collateral (available balance)            |
| **Position increase**                                       | `collectFee`                  | Free collateral (available balance)            |
| **Settlement** (maturity, liquidation, early close, reduce) | Deducted in `_settlePosition` | Locked margin (`imLocked`)                     |
| **Oracle fee**                                              | `collectFeeFromCollateral`    | Total collateral (can draw from locked margin) |

<Note>
  The oracle fee uses `collectFeeFromCollateral`, which can draw from the account's total collateral balance including locked margin. This is necessary because oracle fees are incurred during price lookups that may happen before any position-specific margin is available.
</Note>

## Example Fee Calculation

Consider a trader opening a 10,000 USDC notional LONG position with the following snapshotted parameters:

| Parameter      | Value            |
| -------------- | ---------------- |
| Trading Fee    | 5 bps (0.05%)    |
| Oracle Fee     | 0.10 USDC (flat) |
| Treasury Share | 30% (3,000 bps)  |
| Pool Share     | 70% (7,000 bps)  |

**Fees at open:**

* Trading fee: `10,000 * 5 / 10,000 = 5 USDC`
* Oracle fee: `0.10 USDC`
* Total: `5.10 USDC`

**Distribution:**

* Treasury receives: `5 * 0.30 = 1.50 USDC` (trading fee share) + oracle fee handling
* Pool receives: `5 * 0.70 = 3.50 USDC` (trading fee share), increasing vault share price

**At settlement (assuming 5 bps trading fee again):**

* Another `5 USDC` trading fee is collected from locked margin
* Split the same way: `1.50 USDC` to treasury, `3.50 USDC` to pool
