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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
use anyhow::bail;
use bitcoin::address::NetworkUnchecked;
use bitcoin::Address;
use clap::Subcommand;
use fedimint_core::config::FederationId;
use fedimint_core::{fedimint_build_code_version_env, Amount, BitcoinAmountOrAll};
use fedimint_mint_client::OOBNotes;
use ln_gateway::rpc::rpc_client::GatewayRpcClient;
use ln_gateway::rpc::{
BackupPayload, BalancePayload, ConfigPayload, ConnectFedPayload, DepositAddressPayload,
FederationRoutingFees, LeaveFedPayload, ReceiveEcashPayload, SetConfigurationPayload,
SpendEcashPayload, WithdrawPayload,
};
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>,
},
/// Check gateway's e-cash balance on the specified federation.
Balance {
#[clap(long)]
federation_id: FederationId,
},
/// Get the total on-chain, lightning, and eCash balances of the gateway.
GetBalances,
/// Generate a new peg-in address to a federation that the gateway can claim
/// e-cash for later.
Address {
#[clap(long)]
federation_id: FederationId,
},
/// Claim funds from a gateway federation to an on-chain address.
Withdraw {
#[clap(long)]
federation_id: FederationId,
/// The amount to withdraw
#[clap(long)]
amount: BitcoinAmountOrAll,
/// The address to send the funds to
#[clap(long)]
address: Address<NetworkUnchecked>,
},
/// 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,
},
/// Make a backup of snapshot of all e-cash.
Backup {
#[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,
/// Spend e-cash
SpendEcash {
#[clap(long)]
federation_id: FederationId,
amount: Amount,
#[clap(long)]
allow_overpay: bool,
#[clap(long, default_value_t = 60 * 60 * 24 * 7)]
timeout: u64,
#[clap(long)]
include_invite: bool,
},
/// Receive e-cash
ReceiveEcash {
#[clap(long)]
notes: OOBNotes,
#[arg(long = "no-wait", action = clap::ArgAction::SetFalse)]
wait: bool,
},
}
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::Balance { federation_id } => {
let response = create_client()
.get_balance(BalancePayload { federation_id })
.await?;
print_response(response);
}
Self::GetBalances => {
let response = create_client().get_balances().await?;
print_response(response);
}
Self::Address { federation_id } => {
let response = create_client()
.get_deposit_address(DepositAddressPayload { federation_id })
.await?;
print_response(response);
}
Self::Withdraw {
federation_id,
amount,
address,
} => {
let response = create_client()
.withdraw(WithdrawPayload {
federation_id,
amount,
address,
})
.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::Backup { federation_id } => {
create_client()
.backup(BackupPayload { federation_id })
.await?;
}
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::SpendEcash {
federation_id,
amount,
allow_overpay,
timeout,
include_invite,
} => {
let response = create_client()
.spend_ecash(SpendEcashPayload {
federation_id,
amount,
allow_overpay,
timeout,
include_invite,
})
.await?;
print_response(response);
}
Self::ReceiveEcash { notes, wait } => {
let response = create_client()
.receive_ecash(ReceiveEcashPayload { notes, wait })
.await?;
print_response(response);
}
Self::Seed => {
let response = create_client().get_mnemonic().await?;
print_response(response);
}
Self::Stop => {
create_client().stop().await?;
}
}
Ok(())
}
}