Skip to main content

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