gateway_cli/
config_commands.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use clap::Subcommand;
use fedimint_core::config::FederationId;
use fedimint_core::Amount;
use ln_gateway::rpc::rpc_client::GatewayRpcClient;
use ln_gateway::rpc::{ConfigPayload, SetFeesPayload};

use crate::print_response;

#[derive(Subcommand)]
pub enum ConfigCommands {
    /// Gets each connected federation's JSON client config
    ClientConfig {
        #[clap(long)]
        federation_id: Option<FederationId>,
    },
    /// Gets the Gateway's configured configuration for each federation
    Display {
        #[clap(long)]
        federation_id: Option<FederationId>,
    },
    /// Set the gateway's lightning or transaction fees
    SetFees {
        #[clap(long)]
        federation_id: Option<FederationId>,

        #[clap(long)]
        ln_base: Option<Amount>,

        #[clap(long)]
        ln_ppm: Option<u64>,

        #[clap(long)]
        tx_base: Option<Amount>,

        #[clap(long)]
        tx_ppm: Option<u64>,
    },
}

impl ConfigCommands {
    pub async fn handle(
        self,
        create_client: impl Fn() -> GatewayRpcClient + Send + Sync,
    ) -> anyhow::Result<()> {
        match self {
            Self::ClientConfig { federation_id } => {
                let response = create_client()
                    .get_config(ConfigPayload { federation_id })
                    .await?;

                print_response(response);
            }
            Self::Display { federation_id } => {
                let info = create_client().get_info().await?;
                let federations = info
                    .federations
                    .into_iter()
                    .filter_map(|f| match federation_id {
                        Some(id) if id == f.federation_id => Some(f.config),
                        Some(_) => None,
                        None => Some(f.config),
                    })
                    .collect::<Vec<_>>();
                print_response(federations);
            }
            Self::SetFees {
                federation_id,
                ln_base,
                ln_ppm,
                tx_base,
                tx_ppm,
            } => {
                create_client()
                    .set_fees(SetFeesPayload {
                        federation_id,
                        lightning_base: ln_base,
                        lightning_parts_per_million: ln_ppm,
                        transaction_base: tx_base,
                        transaction_parts_per_million: tx_ppm,
                    })
                    .await?;
            }
        }

        Ok(())
    }
}