# Mediation Fees¶

## Overview¶

Mediation fees are used to incentivize people to mediate payments. There are (currently) three components:

**Flat fees**: absolute fees paid per mediation.**Proportional fees**: fees that grows proportionally to the mediated amount.**Imbalance fees**: fees that can be both positive or negative, which are used incentivize mediations which put the channels into a state desired by the mediator.

Each of these fees is calculated by the mediator for both the incoming and outgoing channel. The sum of all fee components for both channels is the total mediation fee for a single mediator.

For an explanation why this fee system has been chosen, please consult the architectural decision record.

## Imbalance Fees¶

The imbalance fee is calculated from an Imbalance Penalty (IP) function. \(\mathit{IP}(\mathit{cap})\) describes how much a node is willing to pay to bring a channel from the capacity \(\mathit{cap}\) into its preferred capacity.

Mediators can choose arbitrary IP functions to describe which channel capacities are preferable for them and how important that is to them. If a node prefers to have a channel capacity of 5 while the total capacity of that channel is 10 (so that it could mediate up to 5 tokens in both directions) the IP function might look like

```
IP
^
|X X
|X X
| X X
| X X
| X X
| X X|
| X X |
| X X |dIP = IP(cap_after) - IP(cap_before)
| XX XX |
| XX XX----->
| XXX amount
+-----------+--+-----+--> Capacity
0 5 6 9
```

If the node currently has a capacity of 6 and is asked to mediate a payment of 3 tokens coming from this channel, it will get into the less desired position of 9 capacity. To compensate for this, it will demand an imbalance fee of \(i = \mathit{IP}(9) - \mathit{IP}(6)\). If the situation was reversed and the capacity would go from 9 to 6, the absolute value would be the same, but this time it would be negative and thus incentivize moving towards the preferred state. By viewing the channel balances in this way, the imbalance fee is a zero sum game in the long term. All tokens which are earned by going into a bad state will be spent for moving into a good state again, later.

The flexible shape of the IP function allows to encode many different mediator intentions. If I use a channel solely to pay (apart from mediation), having more free capacity is always desired and any capacity gained by mediating in the reverse direction is welcome. In that case, the IP function could look like

```
IP
^
|X
|X
| X
| X
| XX
| XX
| XX
| XX
| XX
| XXX
| XXX
+------------------------> Capacity
```

## Nomenclature¶

In this description a simple mediation node is assumed. The incoming channel (for the mediated payment) is also called **payer channel** and the outgoing channel is also called **payee channel**. This scenario is shown in the following graphic:

```
c
--> a --> (M) --> b -->
```

- \(a\) is the locked amount of the payer channel. This locked amount includes fees.
- \(b\) is the locked amount of the payee channel. This locked amount includes fees for further hops.
- \(c\) is a helper value for making the calculation of mediation fees simpler. It is not exposed to the user.

In the calculation the different fees are used.

- \(f\) is the flat fee
- \(i\) is the imbalance fee

## Converting per-hop proportional fees in per-channel proportional fees¶

User usually think about deducting mediation fees as an atomic action, so it’s
easier to let them define a *per-hop* proportional mediation fee (called
\(p\)). However, this setting then needs to be converted into a
*per-channel* proportional fee (called \(q\)).

## Fee calculation¶

There are two fundamental formula to relate \(a\), \(b\) and \(c\).

- \(a = c + aq + f + i(-a)\)
- \(c = b + bq + f + i(b)\)

The other fundamental relations are:

- \(a - {fee}_{in} = c\)
- \(c - {fee}_{out} = b\)

The imbalance fee \(i(x)\) is defined as follows, where \(t\) is the channel capacity, \(x\) is the transferred amount and \(\mathit{IP}(\mathit{capacity})\) is the imbalance penalty function.

In (1) we pass the negative amount to \(i\) because the incoming channel’s balance is decreased by the transfer, while it is increased for outgoing channel in (2) .

Note

These equations only have symbolic solutions when no imbalance fees are used. With imbalance fees only approximate solutions are presented below. This means that forward and backwards fee calculations can differ slightly.

### Forward calculation (as in the client)¶

For the fee calculation in the client, only \(a\) is known and it needs to calculate \(c\) and \(b.\).

From (1) follows:

From (2) follows:

This leads to

Here one can see that the calculation depends on both \(b\) and \(c\). This formula doesn’t have a symbolic solution for arbitrary functions \(i(x)\).

We approximate the solution by calculating \(b \approx b' = \frac{c - f}{1+q}\) and than use that to solve for \(b\) (which is the first iteration towards the solution which assumes \(i = 0\)):

### Backward calculation (as in the PFS)¶

In the case of fee calculation in the PFS, only \(b\) is known and it needs to calculate \(c\) and \(a\).

From (2) follows:

From (1) follows:

Here the same approximation approach is used for the imbalance fee. The approximation \(i(-a')\) with \(a' = \frac{c + f}{1+q}\) is used in the symbolic solution.

### Example¶

Let’s assume:

- \(f = 100\)
- \(q = 0.1\)
- \(c = 1200\)
- \(b = 1000\)

Now forward and backward calculation should let us recalculate \(b\) or \(c\).

**Client**

**PFS**