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};
6use fedimint_gateway_common::{ConfigPayload, SetFeesPayload, SetMnemonicPayload};
7use fedimint_ln_common::client::GatewayApi;
8
9use crate::{CliOutput, CliOutputResult};
10
11/// Management commands for changing or displaying configuration, including
12/// setting fees per federation.
13#[derive(Subcommand)]
14pub enum ConfigCommands {
15    /// Gets each connected federation's JSON client config
16    ClientConfig {
17        #[clap(long)]
18        federation_id: Option<FederationId>,
19    },
20    /// Gets the Gateway's configured configuration for each federation
21    Display {
22        #[clap(long)]
23        federation_id: Option<FederationId>,
24    },
25    /// Set the gateway's lightning or transaction fees
26    SetFees {
27        #[clap(long)]
28        federation_id: Option<FederationId>,
29
30        #[clap(long)]
31        ln_base: Option<Amount>,
32
33        #[clap(long)]
34        ln_ppm: Option<u64>,
35
36        #[clap(long)]
37        tx_base: Option<Amount>,
38
39        #[clap(long)]
40        tx_ppm: Option<u64>,
41    },
42    /// Instructs the gateway to create a new mnemonic or set it to the provided
43    /// mnemonic
44    SetMnemonic {
45        #[clap(long)]
46        words: Option<String>,
47    },
48}
49
50impl ConfigCommands {
51    pub async fn handle(self, client: &GatewayApi, base_url: &SafeUrl) -> CliOutputResult {
52        match self {
53            Self::ClientConfig { federation_id } => {
54                let response =
55                    get_config(client, base_url, ConfigPayload { federation_id }).await?;
56
57                Ok(CliOutput::Config(response))
58            }
59            Self::Display { federation_id } => {
60                let info = get_info(client, base_url).await?;
61                let federations = info
62                    .federations
63                    .into_iter()
64                    .filter_map(|f| match federation_id {
65                        Some(id) if id == f.federation_id => Some(f.config),
66                        Some(_) => None,
67                        None => Some(f.config),
68                    })
69                    .collect::<Vec<_>>();
70                Ok(CliOutput::FederationConfigs(federations))
71            }
72            Self::SetFees {
73                federation_id,
74                ln_base,
75                ln_ppm,
76                tx_base,
77                tx_ppm,
78            } => {
79                set_fees(
80                    client,
81                    base_url,
82                    SetFeesPayload {
83                        federation_id,
84                        lightning_base: ln_base,
85                        lightning_parts_per_million: ln_ppm,
86                        transaction_base: tx_base,
87                        transaction_parts_per_million: tx_ppm,
88                    },
89                )
90                .await?;
91                Ok(CliOutput::Empty)
92            }
93            Self::SetMnemonic { words } => {
94                set_mnemonic(client, base_url, SetMnemonicPayload { words }).await?;
95                Ok(CliOutput::Empty)
96            }
97        }
98    }
99}