gateway_cli/
onchain_commands.rs

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