fedimint_walletv2_client/
cli.rs1use std::{ffi, iter};
2
3use bitcoin::Address;
4use bitcoin::address::NetworkUnchecked;
5use clap::{Parser, Subcommand};
6use fedimint_core::BitcoinAmountOrAll;
7use fedimint_eventlog::EventLogId;
8use serde::Serialize;
9use serde_json::Value;
10
11use crate::WalletClientModule;
12
13#[derive(Parser, Serialize)]
14enum Opts {
15 #[command(subcommand)]
17 Info(InfoOpts),
18 SendFee,
20 ReceiveFee,
22 Send {
24 address: Address<NetworkUnchecked>,
25 value: BitcoinAmountOrAll,
27 #[arg(long)]
28 fee: Option<bitcoin::Amount>,
29 },
30 Receive,
36 AwaitReceive {
40 position: EventLogId,
43 },
44}
45
46#[derive(Clone, Subcommand, Serialize)]
47enum InfoOpts {
48 TotalValue,
50 BlockCount,
52 Feerate,
54 PendingTxChain,
56 TxChain,
58}
59
60pub(crate) async fn handle_cli_command(
61 wallet: &WalletClientModule,
62 args: &[ffi::OsString],
63) -> anyhow::Result<Value> {
64 let opts = Opts::parse_from(iter::once(&ffi::OsString::from("walletv2")).chain(args.iter()));
65
66 let value = match opts {
67 Opts::Info(subcommand) => match subcommand {
68 InfoOpts::TotalValue => json(wallet.total_value().await?),
69 InfoOpts::BlockCount => json(wallet.block_count().await?),
70 InfoOpts::Feerate => json(wallet.feerate().await?),
71 InfoOpts::PendingTxChain => json(wallet.pending_tx_chain().await?),
72 InfoOpts::TxChain => json(wallet.tx_chain().await?),
73 },
74 Opts::SendFee => json(wallet.send_fee().await?),
75 Opts::ReceiveFee => json(wallet.receive_fee().await?),
76 Opts::Send {
77 address,
78 value,
79 fee,
80 } => {
81 let fee = match fee {
86 Some(fee) => fee,
87 None => wallet.send_fee().await?,
88 };
89
90 let value = match value {
91 BitcoinAmountOrAll::All => {
95 let balance = wallet.client_ctx.get_balance_for_btc().await?;
96 wallet.max_sendable_amount(balance, fee).await?
97 }
98 BitcoinAmountOrAll::Amount(value) => value,
99 };
100
101 json(
102 wallet
103 .await_final_send_operation_state(
104 wallet
105 .send(address, value, Some(fee), serde_json::Value::Null)
106 .await?,
107 )
108 .await?,
109 )
110 }
111 Opts::Receive => json(wallet.receive().await),
112 Opts::AwaitReceive { position } => json(wallet.await_receive(position).await?),
113 };
114
115 Ok(value)
116}
117
118fn json<T: Serialize>(value: T) -> Value {
119 serde_json::to_value(value).expect("JSON serialization failed")
120}