gateway_cli/general_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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
use anyhow::bail;
use clap::Subcommand;
use fedimint_core::config::FederationId;
use fedimint_core::fedimint_build_code_version_env;
use fedimint_eventlog::{EventKind, EventLogId};
use ln_gateway::rpc::rpc_client::GatewayRpcClient;
use ln_gateway::rpc::{
ConfigPayload, ConnectFedPayload, FederationRoutingFees, LeaveFedPayload, PaymentLogPayload,
SetConfigurationPayload,
};
use crate::print_response;
#[derive(Clone)]
pub struct PerFederationRoutingFees {
federation_id: FederationId,
routing_fees: FederationRoutingFees,
}
impl std::str::FromStr for PerFederationRoutingFees {
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Some((federation_id, routing_fees)) = s.split_once(',') {
Ok(Self {
federation_id: federation_id.parse()?,
routing_fees: routing_fees.parse()?,
})
} else {
bail!("Wrong format, please provide: <federation id>,<base msat>,<proportional to millionths part>");
}
}
}
impl From<PerFederationRoutingFees> for (FederationId, FederationRoutingFees) {
fn from(val: PerFederationRoutingFees) -> Self {
(val.federation_id, val.routing_fees)
}
}
#[derive(Subcommand)]
pub enum GeneralCommands {
/// Display the version hash of the CLI.
VersionHash,
/// Display high-level information about the gateway.
Info,
/// Display config information about the federation(s) the gateway is
/// connected to.
Config {
#[clap(long)]
federation_id: Option<FederationId>,
},
/// Get the total on-chain, lightning, and eCash balances of the gateway.
GetBalances,
/// Register the gateway with a federation.
ConnectFed {
/// Invite code to connect to the federation
invite_code: String,
/// Activate usage of Tor (or not) as the connector for the federation
/// client
#[cfg(feature = "tor")]
use_tor: Option<bool>,
/// Indicates if the client should be recovered from a mnemonic
#[clap(long)]
recover: Option<bool>,
},
/// Leave a federation.
LeaveFed {
#[clap(long)]
federation_id: FederationId,
},
/// Prints the seed phrase for the gateway
Seed,
/// Set or update the gateway configuration.
SetConfiguration {
#[clap(long)]
password: Option<String>,
#[clap(long)]
num_route_hints: Option<u32>,
/// Default routing fee for all new federations. Setting it won't affect
/// existing federations
#[clap(long)]
routing_fees: Option<FederationRoutingFees>,
#[clap(long)]
network: Option<bitcoin::Network>,
/// Format federation id,base msat,proportional to millionths part. Any
/// other federations not given here will keep their current fees.
#[clap(long)]
per_federation_routing_fees: Option<Vec<PerFederationRoutingFees>>,
},
/// Safely stop the gateway
Stop,
/// List the transactions that the gateway has processed
PaymentLog {
#[clap(long)]
end_position: Option<EventLogId>,
#[clap(long, default_value_t = 25)]
pagination_size: usize,
#[clap(long)]
federation_id: FederationId,
#[clap(long)]
event_kinds: Vec<EventKind>,
},
}
impl GeneralCommands {
#[allow(clippy::too_many_lines)]
pub async fn handle(
self,
create_client: impl Fn() -> GatewayRpcClient + Send + Sync,
) -> anyhow::Result<()> {
match self {
Self::VersionHash => {
println!("{}", fedimint_build_code_version_env!());
}
Self::Info => {
// For backwards-compatibility, fallback to the original POST endpoint if the
// GET endpoint fails
// FIXME: deprecated >= 0.3.0
let client = create_client();
let response = match client.get_info().await {
Ok(res) => res,
Err(_) => client.get_info_legacy().await?,
};
print_response(response);
}
Self::Config { federation_id } => {
let response = create_client()
.get_config(ConfigPayload { federation_id })
.await?;
print_response(response);
}
Self::GetBalances => {
let response = create_client().get_balances().await?;
print_response(response);
}
Self::ConnectFed {
invite_code,
#[cfg(feature = "tor")]
use_tor,
recover,
} => {
let response = create_client()
.connect_federation(ConnectFedPayload {
invite_code,
#[cfg(feature = "tor")]
use_tor,
recover,
})
.await?;
print_response(response);
}
Self::LeaveFed { federation_id } => {
let response = create_client()
.leave_federation(LeaveFedPayload { federation_id })
.await?;
print_response(response);
}
Self::SetConfiguration {
password,
num_route_hints,
routing_fees,
network,
per_federation_routing_fees,
} => {
let per_federation_routing_fees = per_federation_routing_fees
.map(|input| input.into_iter().map(Into::into).collect());
create_client()
.set_configuration(SetConfigurationPayload {
password,
num_route_hints,
routing_fees,
network,
per_federation_routing_fees,
})
.await?;
}
Self::Seed => {
let response = create_client().get_mnemonic().await?;
print_response(response);
}
Self::Stop => {
create_client().stop().await?;
}
Self::PaymentLog {
end_position,
pagination_size,
federation_id,
event_kinds,
} => {
let payment_log = create_client()
.payment_log(PaymentLogPayload {
end_position,
pagination_size,
federation_id,
event_kinds,
})
.await?;
print_response(payment_log);
}
}
Ok(())
}
}