Skip to main content

fedimint_ln_common/
route_hints.rs

1// TODO: upstream serde support to LDK
2use fedimint_core::encoding::{Decodable, Encodable};
3use fedimint_core::secp256k1::PublicKey;
4use lightning_invoice::RoutingFees;
5use serde::{Deserialize, Serialize};
6
7#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize, Encodable, Decodable)]
8pub struct RouteHintHop {
9    /// The `node_id` of the non-target end of the route
10    pub src_node_id: PublicKey,
11    /// The `short_channel_id` of this channel
12    pub short_channel_id: u64,
13    /// Flat routing fee in millisatoshis
14    pub base_msat: u32,
15    /// Liquidity-based routing fee in millionths of a routed amount.
16    /// In other words, 10000 is 1%.
17    pub proportional_millionths: u32,
18    /// The difference in CLTV values between this node and the next node.
19    pub cltv_expiry_delta: u16,
20    /// The minimum value, in msat, which must be relayed to the next hop.
21    pub htlc_minimum_msat: Option<u64>,
22    /// The maximum value in msat available for routing with a single HTLC.
23    pub htlc_maximum_msat: Option<u64>,
24}
25
26/// A list of hops along a payment path terminating with a channel to the
27/// recipient.
28#[derive(Clone, Debug, Hash, Eq, PartialEq, Serialize, Deserialize, Encodable, Decodable)]
29pub struct RouteHint(pub Vec<RouteHintHop>);
30
31impl RouteHint {
32    pub fn to_ldk_route_hint(&self) -> lightning_invoice::RouteHint {
33        lightning_invoice::RouteHint(
34            self.0
35                .iter()
36                .map(|hop| lightning_invoice::RouteHintHop {
37                    src_node_id: hop.src_node_id,
38                    short_channel_id: hop.short_channel_id,
39                    fees: RoutingFees {
40                        base_msat: hop.base_msat,
41                        proportional_millionths: hop.proportional_millionths,
42                    },
43                    cltv_expiry_delta: hop.cltv_expiry_delta,
44                    htlc_minimum_msat: hop.htlc_minimum_msat,
45                    htlc_maximum_msat: hop.htlc_maximum_msat,
46                })
47                .collect(),
48        )
49    }
50}
51
52impl From<lightning_invoice::RouteHint> for RouteHint {
53    fn from(rh: lightning_invoice::RouteHint) -> Self {
54        RouteHint(rh.0.into_iter().map(Into::into).collect())
55    }
56}
57
58impl From<lightning_invoice::RouteHintHop> for RouteHintHop {
59    fn from(rhh: lightning_invoice::RouteHintHop) -> Self {
60        RouteHintHop {
61            src_node_id: rhh.src_node_id,
62            short_channel_id: rhh.short_channel_id,
63            base_msat: rhh.fees.base_msat,
64            proportional_millionths: rhh.fees.proportional_millionths,
65            cltv_expiry_delta: rhh.cltv_expiry_delta,
66            htlc_minimum_msat: rhh.htlc_minimum_msat,
67            htlc_maximum_msat: rhh.htlc_maximum_msat,
68        }
69    }
70}