gateway_cli/
onchain_commands.rs

1use bitcoin::address::NetworkUnchecked;
2use clap::Subcommand;
3use fedimint_core::BitcoinAmountOrAll;
4use fedimint_core::util::SafeUrl;
5use fedimint_gateway_client::{get_ln_onchain_address, send_onchain};
6use fedimint_gateway_common::SendOnchainRequest;
7use fedimint_ln_common::client::GatewayApi;
8
9use crate::print_response;
10
11#[derive(Subcommand)]
12pub enum OnchainCommands {
13    /// Get a Bitcoin address from the gateway's lightning node's onchain
14    /// wallet.
15    Address,
16    /// Send funds from the lightning node's on-chain wallet to a specified
17    /// address.
18    Send {
19        /// The address to withdraw funds to.
20        #[clap(long)]
21        address: bitcoin::Address<NetworkUnchecked>,
22
23        /// The amount to withdraw.
24        /// Can be "all" to withdraw all funds, an amount + unit (e.g. "1000
25        /// sats"), or a raw amount (e.g. "1000") which is denominated in
26        /// millisats.
27        #[clap(long)]
28        amount: BitcoinAmountOrAll,
29
30        /// The fee rate to use in satoshis per vbyte.
31        #[clap(long)]
32        fee_rate_sats_per_vbyte: u64,
33    },
34}
35
36impl OnchainCommands {
37    pub async fn handle(self, client: &GatewayApi, base_url: &SafeUrl) -> anyhow::Result<()> {
38        match self {
39            Self::Address => {
40                let response = get_ln_onchain_address(client, base_url)
41                    .await?
42                    .assume_checked();
43                println!("{response}");
44            }
45            Self::Send {
46                address,
47                amount,
48                fee_rate_sats_per_vbyte,
49            } => {
50                let response = send_onchain(
51                    client,
52                    base_url,
53                    SendOnchainRequest {
54                        address,
55                        amount,
56                        fee_rate_sats_per_vbyte,
57                    },
58                )
59                .await?;
60                print_response(response);
61            }
62        }
63
64        Ok(())
65    }
66}