fedimint_walletv2_client/
cli.rs1use std::{ffi, iter};
2
3use bitcoin::Address;
4use bitcoin::address::NetworkUnchecked;
5use clap::{Parser, Subcommand};
6use fedimint_eventlog::EventLogId;
7use serde::Serialize;
8use serde_json::Value;
9
10use crate::WalletClientModule;
11
12#[derive(Parser, Serialize)]
13enum Opts {
14 #[command(subcommand)]
16 Info(InfoOpts),
17 SendFee,
19 ReceiveFee,
21 Send {
23 address: Address<NetworkUnchecked>,
24 value: bitcoin::Amount,
25 #[arg(long)]
26 fee: Option<bitcoin::Amount>,
27 },
28 Receive,
34 AwaitReceive {
38 position: EventLogId,
41 },
42}
43
44#[derive(Clone, Subcommand, Serialize)]
45enum InfoOpts {
46 TotalValue,
48 BlockCount,
50 Feerate,
52 PendingTxChain,
54 TxChain,
56}
57
58pub(crate) async fn handle_cli_command(
59 wallet: &WalletClientModule,
60 args: &[ffi::OsString],
61) -> anyhow::Result<Value> {
62 let opts = Opts::parse_from(iter::once(&ffi::OsString::from("walletv2")).chain(args.iter()));
63
64 let value = match opts {
65 Opts::Info(subcommand) => match subcommand {
66 InfoOpts::TotalValue => json(wallet.total_value().await?),
67 InfoOpts::BlockCount => json(wallet.block_count().await?),
68 InfoOpts::Feerate => json(wallet.feerate().await?),
69 InfoOpts::PendingTxChain => json(wallet.pending_tx_chain().await?),
70 InfoOpts::TxChain => json(wallet.tx_chain().await?),
71 },
72 Opts::SendFee => json(wallet.send_fee().await?),
73 Opts::ReceiveFee => json(wallet.receive_fee().await?),
74 Opts::Send {
75 address,
76 value,
77 fee,
78 } => json(
79 wallet
80 .await_final_send_operation_state(
81 wallet
82 .send(address, value, fee, serde_json::Value::Null)
83 .await?,
84 )
85 .await?,
86 ),
87 Opts::Receive => json(wallet.receive().await),
88 Opts::AwaitReceive { position } => json(wallet.await_receive(position).await?),
89 };
90
91 Ok(value)
92}
93
94fn json<T: Serialize>(value: T) -> Value {
95 serde_json::to_value(value).expect("JSON serialization failed")
96}