fedimint_ln_common/
config.rs

1pub use bitcoin::Network;
2use fedimint_core::core::ModuleKind;
3use fedimint_core::encoding::btc::NetworkLegacyEncodingWrapper;
4use fedimint_core::encoding::{Decodable, Encodable};
5use fedimint_core::envs::BitcoinRpcConfig;
6use fedimint_core::{Amount, msats, plugin_types_trait_impl_config};
7use lightning_invoice::RoutingFees;
8use serde::{Deserialize, Serialize};
9use threshold_crypto::serde_impl::SerdeSecret;
10
11use crate::LightningCommonInit;
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
14pub struct LightningGenParams {
15    pub local: LightningGenParamsLocal,
16    pub consensus: LightningGenParamsConsensus,
17}
18
19impl LightningGenParams {
20    pub fn regtest(bitcoin_rpc: BitcoinRpcConfig) -> Self {
21        Self {
22            local: LightningGenParamsLocal { bitcoin_rpc },
23            consensus: LightningGenParamsConsensus {
24                network: Network::Regtest,
25            },
26        }
27    }
28}
29
30#[derive(Debug, Clone, Serialize, Deserialize)]
31pub struct LightningGenParamsConsensus {
32    pub network: Network,
33}
34
35#[derive(Debug, Clone, Serialize, Deserialize)]
36pub struct LightningGenParamsLocal {
37    pub bitcoin_rpc: BitcoinRpcConfig,
38}
39
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct LightningConfig {
42    pub local: LightningConfigLocal,
43    pub private: LightningConfigPrivate,
44    pub consensus: LightningConfigConsensus,
45}
46
47#[derive(Clone, Debug, Serialize, Deserialize, Decodable, Encodable)]
48pub struct LightningConfigLocal {
49    /// Configures which bitcoin RPC to use
50    pub bitcoin_rpc: BitcoinRpcConfig,
51}
52
53#[derive(Debug, Clone, Serialize, Deserialize, Encodable, Decodable)]
54pub struct LightningConfigConsensus {
55    /// The threshold public keys for encrypting the LN preimage
56    pub threshold_pub_keys: threshold_crypto::PublicKeySet,
57    /// Fees charged for LN transactions
58    pub fee_consensus: FeeConsensus,
59    pub network: NetworkLegacyEncodingWrapper,
60}
61
62impl LightningConfigConsensus {
63    /// The number of decryption shares required
64    pub fn threshold(&self) -> usize {
65        self.threshold_pub_keys.threshold() + 1
66    }
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct LightningConfigPrivate {
71    // TODO: propose serde(with = "…") based protection upstream instead
72    /// Our secret key for decrypting preimages
73    pub threshold_sec_key: SerdeSecret<threshold_crypto::SecretKeyShare>,
74}
75
76#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, Encodable, Decodable)]
77pub struct LightningClientConfig {
78    pub threshold_pub_key: threshold_crypto::PublicKey,
79    pub fee_consensus: FeeConsensus,
80    pub network: NetworkLegacyEncodingWrapper,
81}
82
83impl std::fmt::Display for LightningClientConfig {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        write!(
86            f,
87            "LightningClientConfig {}",
88            serde_json::to_string(self).map_err(|_e| std::fmt::Error)?
89        )
90    }
91}
92
93// Wire together the configs for this module
94plugin_types_trait_impl_config!(
95    LightningCommonInit,
96    LightningGenParams,
97    LightningGenParamsLocal,
98    LightningGenParamsConsensus,
99    LightningConfig,
100    LightningConfigLocal,
101    LightningConfigPrivate,
102    LightningConfigConsensus,
103    LightningClientConfig
104);
105
106#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, Encodable, Decodable)]
107pub struct FeeConsensus {
108    pub contract_input: fedimint_core::Amount,
109    pub contract_output: fedimint_core::Amount,
110}
111
112impl Default for FeeConsensus {
113    fn default() -> Self {
114        Self {
115            contract_input: fedimint_core::Amount::ZERO,
116            contract_output: fedimint_core::Amount::ZERO,
117        }
118    }
119}
120
121/// Trait for converting a fee type to specific `Amount`,
122/// relative to a given payment `Amount`
123pub trait FeeToAmount {
124    /// Calculates fee `Amount` given a payment `Amount`
125    fn to_amount(&self, payment: &Amount) -> Amount;
126}
127
128impl FeeToAmount for RoutingFees {
129    fn to_amount(&self, payment: &Amount) -> Amount {
130        let base_fee = u64::from(self.base_msat);
131        let margin_fee: u64 = if self.proportional_millionths > 0 {
132            let fee_percent = 1_000_000 / u64::from(self.proportional_millionths);
133            payment.msats / fee_percent
134        } else {
135            0
136        };
137
138        msats(base_fee + margin_fee)
139    }
140}