fedimint_walletv2_client/
cli.rs1use std::{ffi, iter};
2
3use bitcoin::Address;
4use bitcoin::address::NetworkUnchecked;
5use clap::{Parser, Subcommand};
6use fedimint_api_client::api::FederationError;
7use fedimint_client_module::error::{ModuleLookupError, OperationLookupError};
8use fedimint_core::BitcoinAmountOrAll;
9use fedimint_core::core::Account;
10use fedimint_eventlog::EventLogId;
11use serde::Serialize;
12use serde_json::Value;
13
14use crate::{AwaitReceiveError, ReceiveError, SendError, WalletClientModule};
15
16#[derive(Parser, Serialize)]
17enum Opts {
18 #[command(subcommand)]
20 Info(InfoOpts),
21 SendFee,
23 ReceiveFee,
25 Send {
27 address: Address<NetworkUnchecked>,
28 value: BitcoinAmountOrAll,
30 #[arg(long)]
31 fee: Option<bitcoin::Amount>,
32 #[arg(long, default_value_t = Account::Primary)]
33 account: Account,
34 },
35 Receive {
41 #[arg(long, default_value_t = Account::Primary)]
42 account: Account,
43 },
44 AwaitReceive {
48 position: EventLogId,
51 #[arg(long, default_value_t = Account::Primary)]
52 account: Account,
53 },
54}
55
56#[derive(Clone, Subcommand, Serialize)]
57enum InfoOpts {
58 TotalValue,
60 BlockCount,
62 Feerate,
64 PendingTxChain,
66 TxChain,
68}
69
70pub(crate) async fn handle_cli_command(
71 wallet: &WalletClientModule,
72 args: &[ffi::OsString],
73) -> Result<Value, CliCommandError> {
74 let opts = Opts::parse_from(iter::once(&ffi::OsString::from("walletv2")).chain(args.iter()));
75
76 let value = match opts {
77 Opts::Info(subcommand) => match subcommand {
78 InfoOpts::TotalValue => json(wallet.total_value().await?),
79 InfoOpts::BlockCount => json(wallet.block_count().await?),
80 InfoOpts::Feerate => json(wallet.feerate().await?),
81 InfoOpts::PendingTxChain => json(wallet.pending_tx_chain().await?),
82 InfoOpts::TxChain => json(wallet.tx_chain().await?),
83 },
84 Opts::SendFee => json(wallet.send_fee().await?),
85 Opts::ReceiveFee => json(wallet.receive_fee().await?),
86 Opts::Send {
87 address,
88 value,
89 fee,
90 account,
91 } => {
92 let fee = match fee {
97 Some(fee) => fee,
98 None => wallet.send_fee().await?,
99 };
100
101 let value = match value {
102 BitcoinAmountOrAll::All => {
106 let balance = wallet.client_ctx.get_balance_for_btc(account).await?;
107 wallet.max_sendable_amount(account, balance, fee).await?
108 }
109 BitcoinAmountOrAll::Amount(value) => value,
110 };
111
112 json(
113 wallet
114 .await_final_send_operation_state(
115 wallet
116 .send(account, address, value, Some(fee), serde_json::Value::Null)
117 .await?,
118 )
119 .await?,
120 )
121 }
122 Opts::Receive { account } => json(wallet.receive(account).await?),
123 Opts::AwaitReceive { position, account } => {
124 json(wallet.await_receive(account, position).await?)
125 }
126 };
127
128 Ok(value)
129}
130
131fn json<T: Serialize>(value: T) -> Value {
132 serde_json::to_value(value).expect("JSON serialization failed")
133}
134
135#[derive(Debug, thiserror::Error)]
137pub(crate) enum CliCommandError {
138 #[error(transparent)]
140 Federation(#[from] FederationError),
141
142 #[error(transparent)]
144 Send(#[from] SendError),
145
146 #[error(transparent)]
148 Receive(#[from] ReceiveError),
149
150 #[error(transparent)]
152 Balance(#[from] ModuleLookupError),
153
154 #[error(transparent)]
156 OperationLookup(#[from] OperationLookupError),
157
158 #[error(transparent)]
160 AwaitReceive(#[from] AwaitReceiveError),
161}