<p>To answer your question two things have to be known:</p>

<ol>
<li>Routing fees are always charged from the recipient to the sender as the fee rate of later packages impacts earlier routing decisions. </li>
<li>Nodes get paid a routing fee to forward a package on an outgoing channel. </li>
</ol>

<p>The first remark is not of importance for your case as the path only includes 1 routing node. The second remark means that only the fee police for channel <code>c2</code> are relevant. Node <code>A</code> does not charge itself a routing fee on channel <code>c1</code> to forward to <code>B</code> and node <code>B</code> would only charge a fee on channel <code>c1</code> if a paymant was to be forwarded to node <code>A</code></p>

<p>So let us look at the payment amount and the policy of channel <code>c2</code>
the policy says: </p>

<pre><code> "fee_base_msat": "10000",
 "fee_rate_milli_msat": "100",
</code></pre>

<p>where the <code>payment_amount</code> is <code>4999</code> that means that you have to send an onion to B that has the following amount of msat</p>

<p><code>payment_amount * 1000 + fee_base_msat + payment_amount * fee_rate_milli_msat / 1'000</code></p>

<p>pluging in your numbers this means: </p>

<p><code>4999000 + 10000 + 4999 * 100 / 1000 = 5009000 + int(499.9) = 5009000 + 499 = 5009499</code></p>

<p>That is exactly the amount your node sends on its way to complete the onion. Also note that if your node would have been forwarding that payment from anouther node <code>E</code> the other node would have to use that result as the <code>payment_amount</code> with the above formula and your channel police from <code>c1</code> to forward the payment. That is what I meant in the first remark that the amounts are calculated from the receiver to the sender. </p>

CC BY-SA 4.0
2019-11-13T14:17:37.420Z
Thank you for answering, However it seems that the lnd client rounds payments, if I enter `4999999` its rounded to `4999000` surely it should not do this? Edit: `queryroutes` takes Satoshis, not mSat, so the rounding will always occur regardless.
Oonah
CC BY-SA 4.0
2019-11-13T16:11:30.523Z
One issue I found now, that i cannot seem to explain is, in one case `n1_policy` is `'fee_base_msat': '489', 'fee_rate_milli_msat': '1000',` whilst `n2_policy` is `'fee_base_msat': '1000',  'fee_rate_milli_msat': '1',`. In this case `n1` received the payment, why would `lnd` choose `n1` policy instead of `n2` ?
Oonah
CC BY-SA 4.0
2019-11-13T13:56:46.800Z
2019-11-13T13:56:46.800Z
Rene Pickhardt
<p>I am testing my own routing implementation and for some weird reason a handful of channels reject my payment stating <code>InsufficientFee</code>. </p>

<p>The scenario is <code>A -c1-&gt; B -c2-&gt; C</code> whereby Node <code>A</code> is sending a payment to <code>C</code> via <code>B</code> where the channels used are denoted as <code>c1</code> and <code>c2</code>.</p>

<p>For <code>c1</code> the node fee policies are</p>

<pre><code>    "node1_policy": {
        "time_lock_delta": 40,
        "min_htlc": "1",
        "fee_base_msat": "1000",
        "fee_rate_milli_msat": "1",
        "max_htlc_msat": "16777215000",
    },
    "node2_policy": {
        "time_lock_delta": 144,
        "min_htlc": "1000",
        "fee_base_msat": "1000",
        "fee_rate_milli_msat": "100",
        "max_htlc_msat": "16777215000",
    },
</code></pre>

<p>For <code>c2</code> the node fee policies are</p>

<pre><code>    "node1_policy": null,
    "node2_policy": {
        "time_lock_delta": 144,
        "min_htlc": "1000",
        "fee_base_msat": "10000",
        "fee_rate_milli_msat": "100",
        "max_htlc_msat": "10000000000",

    }
</code></pre>

<p>For an amount of 4999000 mSat <code>queryroutes</code> uses this route and calculates the fees as </p>

<pre><code>{'amt_to_forward': '4999',
                       'amt_to_forward_msat': '4999000',
                       'chan_capacity': '16777215',
                       'chan_id': 'c1',
                       'fee': '10',
                       'fee_msat': '10499',},
                      {'amt_to_forward': '4999',
                       'amt_to_forward_msat': '4999000',
                       'chan_capacity': '10000000',
                       'chan_id': 'c2',
                       'fee': '0',
                       'fee_msat': '0',}
             'total_amt': '5009',
             'total_amt_msat': '5009499',
             'total_fees': '10',
             'total_fees_msat': '10499',}
</code></pre>

<p>Could someone please explain in step-by-step detail how the <code>lnd</code> client gets to this value? </p>

<lightning-network><lightning-network-daemon><lightning-routing>
How does the 'queryroutes' function in lightning's LND calculate the route fees?
Oonah
# How does the 'queryroutes' function in lightning's LND calculate the route fees?

(Asked by: username_0 on 2019-11-12)

I am testing my own routing implementation and for some weird reason a handful of channels reject my payment stating `InsufficientFee`.

The scenario is `A -c1-> B -c2-> C` whereby Node `A` is sending a payment to `C` via `B` where the channels used are denoted as `c1` and `c2`.

For `c1` the node fee policies are

```
    "node1_policy": {
        "time_lock_delta": 40,
        "min_htlc": "1",
        "fee_base_msat": "1000",
        "fee_rate_milli_msat": "1",
        "max_htlc_msat": "16777215000",
    },
    "node2_policy": {
        "time_lock_delta": 144,
        "min_htlc": "1000",
        "fee_base_msat": "1000",
        "fee_rate_milli_msat": "100",
        "max_htlc_msat": "16777215000",
    },

```

For `c2` the node fee policies are

```
    "node1_policy": null,
    "node2_policy": {
        "time_lock_delta": 144,
        "min_htlc": "1000",
        "fee_base_msat": "10000",
        "fee_rate_milli_msat": "100",
        "max_htlc_msat": "10000000000",

    }

```

For an amount of 4999000 mSat `queryroutes` uses this route and calculates the fees as

```
{'amt_to_forward': '4999',
                       'amt_to_forward_msat': '4999000',
                       'chan_capacity': '16777215',
                       'chan_id': 'c1',
                       'fee': '10',
                       'fee_msat': '10499',},
                      {'amt_to_forward': '4999',
                       'amt_to_forward_msat': '4999000',
                       'chan_capacity': '10000000',
                       'chan_id': 'c2',
                       'fee': '0',
                       'fee_msat': '0',}
             'total_amt': '5009',
             'total_amt_msat': '5009499',
             'total_fees': '10',
             'total_fees_msat': '10499',}

```

Could someone please explain in step-by-step detail how the `lnd` client gets to this value?

**Tags:** lightning-network, lightning-network-daemon, lightning-routing


---

## 1 answers

### Answer by username_1 on 2019-11-13. Score: 2 | Accepted answer

To answer your question two things have to be known:

1. Routing fees are always charged from the recipient to the sender as the fee rate of later packages impacts earlier routing decisions.
2. Nodes get paid a routing fee to forward a package on an outgoing channel.

The first remark is not of importance for your case as the path only includes 1 routing node. The second remark means that only the fee police for channel `c2` are relevant. Node `A` does not charge itself a routing fee on channel `c1` to forward to `B` and node `B` would only charge a fee on channel `c1` if a paymant was to be forwarded to node `A`

So let us look at the payment amount and the policy of channel `c2`
the policy says:

```
 "fee_base_msat": "10000",
 "fee_rate_milli_msat": "100",

```

where the `payment_amount` is `4999` that means that you have to send an onion to B that has the following amount of msat

`payment_amount * 1000 + fee_base_msat + payment_amount * fee_rate_milli_msat / 1'000`

pluging in your numbers this means:

`4999000 + 10000 + 4999 * 100 / 1000 = 5009000 + int(499.9) = 5009000 + 499 = 5009499`

That is exactly the amount your node sends on its way to complete the onion. Also note that if your node would have been forwarding that payment from anouther node `E` the other node would have to use that result as the `payment_amount` with the above formula and your channel police from `c1` to forward the payment. That is what I meant in the first remark that the amounts are calculated from the receiver to the sender.

> **Comments:**
> **username_0**: Thank you for answering, However it seems that the lnd client rounds payments, if I enter `4999999` its rounded to `4999000` surely it should not do this? Edit: `queryroutes` takes Satoshis, not mSat, so the rounding will always occur regardless.  
> **username_0**: One issue I found now, that i cannot seem to explain is, in one case `n1_policy` is `'fee_base_msat': '489', 'fee_rate_milli_msat': '1000',` whilst `n2_policy` is `'fee_base_msat': '1000',  'fee_rate_milli_msat': '1',`. In this case `n1` received the payment, why would `lnd` choose `n1` policy instead of `n2` ?  

---

