use anyhow::Result;
use clap::Args;
use tonic::Request;
use crate::wallet::{GetBalanceRequest, ListAddressesRequest, ListTransactionsRequest};
use crate::InterceptedWalletServiceClient;
#[derive(Debug, Args)]
pub struct WalletPaginationCommand {
#[arg(long, default_value_t = 0)]
limit: u32,
#[arg(long, default_value_t = 0)]
offset: u32,
}
pub async fn get_wallet_balance(client: &mut InterceptedWalletServiceClient) -> Result<()> {
let balance = client
.get_balance(Request::new(GetBalanceRequest {}))
.await?
.into_inner();
println!("network: {}", balance.network);
println!("synced height: {}", balance.synced_height);
println!("confirmed: {} sat", balance.confirmed_sat);
println!(
"trusted pending: {} sat",
balance.trusted_pending_sat
);
println!(
"untrusted pending: {} sat",
balance.untrusted_pending_sat
);
println!("immature: {} sat", balance.immature_sat);
println!(
"trusted spendable: {} sat",
balance.trusted_spendable_sat
);
println!("total: {} sat", balance.total_sat);
Ok(())
}
pub async fn list_wallet_transactions(
client: &mut InterceptedWalletServiceClient,
command: &WalletPaginationCommand,
) -> Result<()> {
let response = client
.list_transactions(Request::new(ListTransactionsRequest {
limit: command.limit,
offset: command.offset,
}))
.await?
.into_inner();
println!("total: {}", response.total);
for transaction in response.transactions {
println!(
"txid: {}, received: {} sat, sent: {} sat, fee: {}, delta: {} sat, height: {}, confirmation_time: {}, first_seen: {}",
transaction.txid,
transaction.received_sat,
transaction.sent_sat,
transaction
.fee_sat
.map(|fee| format!("{fee} sat"))
.unwrap_or_else(|| "unknown".to_string()),
transaction.balance_delta_sat,
transaction
.confirmation_height
.map(|height| height.to_string())
.unwrap_or_else(|| "unconfirmed".to_string()),
transaction
.confirmation_time
.map(|timestamp| timestamp.to_string())
.unwrap_or_else(|| "none".to_string()),
transaction
.first_seen
.map(|timestamp| timestamp.to_string())
.unwrap_or_else(|| "none".to_string()),
);
}
Ok(())
}
pub async fn list_wallet_addresses(
client: &mut InterceptedWalletServiceClient,
command: &WalletPaginationCommand,
) -> Result<()> {
let response = client
.list_addresses(Request::new(ListAddressesRequest {
limit: command.limit,
offset: command.offset,
}))
.await?
.into_inner();
println!("total: {}", response.total);
for address in response.addresses {
println!(
"address: {}, keychain: {}, index: {}, used: {}, balance: {} sat, confirmed: {} sat",
address.address,
address.keychain().as_str_name(),
address.derivation_index,
address.used,
address.balance_sat,
address.confirmed_balance_sat,
);
}
Ok(())
}