From: Matt Corallo Date: Mon, 14 Oct 2024 02:03:54 +0000 (+0000) Subject: Use `Balance`s to calculate balances instead of `ChannelDetails` X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=cfcd28b094a2ada3835c986154434d83e22165f7;p=ldk-sample Use `Balance`s to calculate balances instead of `ChannelDetails` LDK has deprecated the `ChannelDetails` balance retrieval because its generally not what users want. Instead, we use the `ChainMonitor` `Balance` list. --- diff --git a/src/cli.rs b/src/cli.rs index b528e04..3fe10dc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,13 +1,14 @@ use crate::disk::{self, INBOUND_PAYMENTS_FNAME, OUTBOUND_PAYMENTS_FNAME}; use crate::hex_utils; use crate::{ - ChannelManager, HTLCStatus, InboundPaymentInfoStorage, MillisatAmount, NetworkGraph, - OutboundPaymentInfoStorage, PaymentInfo, PeerManager, + ChainMonitor, ChannelManager, HTLCStatus, InboundPaymentInfoStorage, MillisatAmount, + NetworkGraph, OutboundPaymentInfoStorage, PaymentInfo, PeerManager, }; use bitcoin::hashes::sha256::Hash as Sha256; use bitcoin::hashes::Hash; use bitcoin::network::Network; use bitcoin::secp256k1::PublicKey; +use lightning::chain::channelmonitor::Balance; use lightning::ln::bolt11_payment::payment_parameters_from_invoice; use lightning::ln::bolt11_payment::payment_parameters_from_zero_amount_invoice; use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry}; @@ -46,8 +47,8 @@ pub(crate) struct LdkUserInfo { pub(crate) fn poll_for_user_input( peer_manager: Arc, channel_manager: Arc, - keys_manager: Arc, network_graph: Arc, - inbound_payments: Arc>, + chain_monitor: Arc, keys_manager: Arc, + network_graph: Arc, inbound_payments: Arc>, outbound_payments: Arc>, ldk_data_dir: String, network: Network, logger: Arc, fs_store: Arc, ) { @@ -461,7 +462,7 @@ pub(crate) fn poll_for_user_input( force_close_channel(channel_id, peer_pubkey, channel_manager.clone()); }, - "nodeinfo" => node_info(&channel_manager, &peer_manager), + "nodeinfo" => node_info(&channel_manager, &chain_monitor, &peer_manager), "listpeers" => list_peers(peer_manager.clone()), "signmessage" => { const MSG_STARTPOS: usize = "signmessage".len() + 1; @@ -515,14 +516,38 @@ fn help() { println!(" nodeinfo"); } -fn node_info(channel_manager: &Arc, peer_manager: &Arc) { +fn node_info( + channel_manager: &Arc, chain_monitor: &Arc, + peer_manager: &Arc, +) { println!("\t{{"); println!("\t\t node_pubkey: {}", channel_manager.get_our_node_id()); let chans = channel_manager.list_channels(); println!("\t\t num_channels: {}", chans.len()); println!("\t\t num_usable_channels: {}", chans.iter().filter(|c| c.is_usable).count()); - let local_balance_msat = chans.iter().map(|c| c.balance_msat).sum::(); - println!("\t\t local_balance_msat: {}", local_balance_msat); + let balances = chain_monitor.get_claimable_balances(&[]); + let local_balance_sat = balances.iter().map(|b| b.claimable_amount_satoshis()).sum::(); + println!("\t\t local_balance_sats: {}", local_balance_sat); + let close_fees_map = |b| match b { + &Balance::ClaimableOnChannelClose { transaction_fee_satoshis, .. } => { + transaction_fee_satoshis + }, + _ => 0, + }; + let close_fees_sats = balances.iter().map(close_fees_map).sum::(); + println!("\t\t eventual_close_fees_sats: {}", close_fees_sats); + let pending_payments_map = |b| match b { + &Balance::MaybeTimeoutClaimableHTLC { amount_satoshis, outbound_payment, .. } => { + if outbound_payment { + amount_satoshis + } else { + 0 + } + }, + _ => 0, + }; + let pending_payments = balances.iter().map(pending_payments_map).sum::(); + println!("\t\t pending_outbound_payments_sats: {}", pending_payments); println!("\t\t num_peers: {}", peer_manager.list_peers().len()); println!("\t}},"); } diff --git a/src/main.rs b/src/main.rs index d7e63c9..cfe5705 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1093,6 +1093,7 @@ async fn start_ldk() { // Start the CLI. let cli_channel_manager = Arc::clone(&channel_manager); + let cli_chain_monitor = Arc::clone(&chain_monitor); let cli_persister = Arc::clone(&persister); let cli_logger = Arc::clone(&logger); let cli_peer_manager = Arc::clone(&peer_manager); @@ -1100,6 +1101,7 @@ async fn start_ldk() { cli::poll_for_user_input( cli_peer_manager, cli_channel_manager, + cli_chain_monitor, keys_manager, network_graph, inbound_payments,