gateway_cli/
onchain_commands.rs

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