gateway_cli/
onchain_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
use bitcoin::address::NetworkUnchecked;
use clap::Subcommand;
use fedimint_core::BitcoinAmountOrAll;
use ln_gateway::rpc::rpc_client::GatewayRpcClient;
use ln_gateway::rpc::SendOnchainPayload;

use crate::print_response;

#[derive(Subcommand)]
pub enum OnchainCommands {
    /// Get a Bitcoin address from the gateway's lightning node's onchain
    /// wallet.
    Address,
    /// Send funds from the lightning node's on-chain wallet to a specified
    /// address.
    Send {
        /// The address to withdraw funds to.
        #[clap(long)]
        address: bitcoin::Address<NetworkUnchecked>,

        /// The amount to withdraw.
        /// Can be "all" to withdraw all funds, an amount + unit (e.g. "1000
        /// sats"), or a raw amount (e.g. "1000") which is denominated in
        /// millisats.
        #[clap(long)]
        amount: BitcoinAmountOrAll,

        /// The fee rate to use in satoshis per vbyte.
        #[clap(long)]
        fee_rate_sats_per_vbyte: u64,
    },
}

impl OnchainCommands {
    pub async fn handle(
        self,
        create_client: impl Fn() -> GatewayRpcClient + Send + Sync,
    ) -> anyhow::Result<()> {
        match self {
            Self::Address => {
                let response = create_client()
                    .get_ln_onchain_address()
                    .await?
                    .assume_checked();
                println!("{response}");
            }
            Self::Send {
                address,
                amount,
                fee_rate_sats_per_vbyte,
            } => {
                let response = create_client()
                    .send_onchain(SendOnchainPayload {
                        address,
                        amount,
                        fee_rate_sats_per_vbyte,
                    })
                    .await?;
                print_response(response);
            }
        }

        Ok(())
    }
}