Skip to main content

gateway_cli/
config_commands.rs

1use clap::Subcommand;
2use fedimint_core::Amount;
3use fedimint_core::config::FederationId;
4use fedimint_core::util::SafeUrl;
5use fedimint_gateway_client::{get_config, get_info, set_fees, set_mnemonic, set_payment_policy};
6use fedimint_gateway_common::{
7    ConfigPayload, SetFeesPayload, SetMnemonicPayload, SetPaymentPolicyPayload,
8};
9use fedimint_ln_common::client::GatewayApi;
10
11use crate::{CliOutput, CliOutputResult};
12
13/// Management commands for changing or displaying configuration, including
14/// setting fees per federation.
15#[derive(Subcommand)]
16pub enum ConfigCommands {
17    /// Gets each connected federation's JSON client config
18    ClientConfig {
19        #[clap(long)]
20        federation_id: Option<FederationId>,
21    },
22    /// Gets the Gateway's configured configuration for each federation
23    Display {
24        #[clap(long)]
25        federation_id: Option<FederationId>,
26    },
27    /// Set the gateway's lightning or transaction fees
28    SetFees {
29        #[clap(long)]
30        federation_id: Option<FederationId>,
31
32        #[clap(long)]
33        ln_base: Option<Amount>,
34
35        #[clap(long)]
36        ln_ppm: Option<u64>,
37
38        #[clap(long)]
39        tx_base: Option<Amount>,
40
41        #[clap(long)]
42        tx_ppm: Option<u64>,
43    },
44    /// Set which payments the gateway performs on behalf of a federation's
45    /// clients. Applies to every connected federation unless a federation id
46    /// is given.
47    SetPaymentPolicy {
48        #[clap(long)]
49        federation_id: Option<FederationId>,
50
51        /// Whether to accept incoming Lightning payments for the federation's
52        /// clients. Turning this off also fails back the payments of invoices
53        /// that were already issued.
54        #[clap(long)]
55        receive_enabled: Option<bool>,
56    },
57    /// Instructs the gateway to create a new mnemonic or set it to the provided
58    /// mnemonic
59    SetMnemonic {
60        #[clap(long)]
61        words: Option<String>,
62    },
63}
64
65impl ConfigCommands {
66    pub async fn handle(self, client: &GatewayApi, base_url: &SafeUrl) -> CliOutputResult {
67        match self {
68            Self::ClientConfig { federation_id } => {
69                let response =
70                    get_config(client, base_url, ConfigPayload { federation_id }).await?;
71
72                Ok(CliOutput::Config(response))
73            }
74            Self::Display { federation_id } => {
75                let info = get_info(client, base_url).await?;
76                let federations = info
77                    .federations
78                    .into_iter()
79                    .filter_map(|f| match federation_id {
80                        Some(id) if id == f.federation_id => Some(f.config),
81                        Some(_) => None,
82                        None => Some(f.config),
83                    })
84                    .collect::<Vec<_>>();
85                Ok(CliOutput::FederationConfigs(federations))
86            }
87            Self::SetFees {
88                federation_id,
89                ln_base,
90                ln_ppm,
91                tx_base,
92                tx_ppm,
93            } => {
94                set_fees(
95                    client,
96                    base_url,
97                    SetFeesPayload {
98                        federation_id,
99                        lightning_base: ln_base,
100                        lightning_parts_per_million: ln_ppm,
101                        transaction_base: tx_base,
102                        transaction_parts_per_million: tx_ppm,
103                    },
104                )
105                .await?;
106                Ok(CliOutput::Empty)
107            }
108            Self::SetPaymentPolicy {
109                federation_id,
110                receive_enabled,
111            } => {
112                set_payment_policy(
113                    client,
114                    base_url,
115                    SetPaymentPolicyPayload {
116                        federation_id,
117                        receive_enabled,
118                    },
119                )
120                .await?;
121                Ok(CliOutput::Empty)
122            }
123            Self::SetMnemonic { words } => {
124                set_mnemonic(client, base_url, SetMnemonicPayload { words }).await?;
125                Ok(CliOutput::Empty)
126            }
127        }
128    }
129}