@@ -62,6 +62,17 @@ pub struct ChannelValueStat {
62
62
pub counterparty_dust_limit_msat : u64 ,
63
63
}
64
64
65
+ pub struct AvailableBalance {
66
+ /// The amount that would go to us if we close the channel, ignoring any on-chain fees.
67
+ pub balance_msat : u64 ,
68
+ /// Total amount available for our counterparty to send to us, ignoring HTLCs.
69
+ pub inbound_capacity_msat : u64 ,
70
+ /// Total amount available for us to send to our counterparty, ignoring HTLCs.
71
+ pub outbound_capacity_msat : u64 ,
72
+ /// The maximum value we can assign to the next outbound HTLC
73
+ pub next_outbound_htlc_limit_msat : u64 ,
74
+ }
75
+
65
76
#[ derive( Debug , Clone , Copy , PartialEq ) ]
66
77
enum FeeUpdateState {
67
78
// Inbound states mirroring InboundHTLCState
@@ -2330,47 +2341,38 @@ impl<Signer: Sign> Channel<Signer> {
2330
2341
stats
2331
2342
}
2332
2343
2333
- /// Get the available (ie not including pending HTLCs) inbound and outbound balance, plus the
2334
- /// amount available for a single HTLC send, all in msat.
2344
+ /// Get the available balances, see [`AvailableBalance`]'s fields for more info.
2335
2345
/// Doesn't bother handling the
2336
2346
/// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC
2337
2347
/// corner case properly.
2338
- /// The channel reserve is subtracted from each balance.
2339
- /// See also [`Channel::get_balance_msat`]
2340
- pub fn get_inbound_outbound_available_balance_msat ( & self ) -> ( u64 , u64 , u64 ) {
2348
+ pub fn get_inbound_outbound_available_balance_msat ( & self ) -> AvailableBalance {
2341
2349
// Note that we have to handle overflow due to the above case.
2342
2350
let outbound_stats = self . get_outbound_pending_htlc_stats ( None ) ;
2351
+
2352
+ let mut balance_msat = self . value_to_self_msat - outbound_stats. pending_htlcs_value_msat ;
2353
+ for ref htlc in self . pending_inbound_htlcs . iter ( ) {
2354
+ if let InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: Fulfill ( _) ) = htlc. state {
2355
+ balance_msat += htlc. amount_msat ;
2356
+ }
2357
+ }
2358
+
2343
2359
let outbound_capacity_msat = cmp:: max ( self . value_to_self_msat as i64
2344
2360
- outbound_stats. pending_htlcs_value_msat as i64
2345
2361
- self . counterparty_selected_channel_reserve_satoshis . unwrap_or ( 0 ) as i64 * 1000 ,
2346
2362
0 ) as u64 ;
2347
- (
2348
- cmp:: max ( self . channel_value_satoshis as i64 * 1000
2349
- - self . value_to_self_msat as i64
2350
- - self . get_inbound_pending_htlc_stats ( None ) . pending_htlcs_value_msat as i64
2351
- - self . holder_selected_channel_reserve_satoshis as i64 * 1000 ,
2352
- 0 ) as u64 ,
2363
+ AvailableBalance {
2364
+ inbound_capacity_msat : cmp:: max ( self . channel_value_satoshis as i64 * 1000
2365
+ - self . value_to_self_msat as i64
2366
+ - self . get_inbound_pending_htlc_stats ( None ) . pending_htlcs_value_msat as i64
2367
+ - self . holder_selected_channel_reserve_satoshis as i64 * 1000 ,
2368
+ 0 ) as u64 ,
2353
2369
outbound_capacity_msat,
2354
- cmp:: max ( cmp:: min ( outbound_capacity_msat as i64 ,
2355
- self . counterparty_max_htlc_value_in_flight_msat as i64
2356
- - outbound_stats. pending_htlcs_value_msat as i64 ) ,
2357
- 0 ) as u64
2358
- )
2359
- }
2360
-
2361
- /// Get our total balance in msat.
2362
- /// This is the amount that would go to us if we close the channel, ignoring any on-chain fees.
2363
- /// See also [`Channel::get_inbound_outbound_available_balance_msat`]
2364
- pub fn get_balance_msat ( & self ) -> u64 {
2365
- // Include our local balance, plus any inbound HTLCs we know the preimage for, minus any
2366
- // HTLCs sent or which will be sent after commitment signed's are exchanged.
2367
- let mut balance_msat = self . value_to_self_msat ;
2368
- for ref htlc in self . pending_inbound_htlcs . iter ( ) {
2369
- if let InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: Fulfill ( _) ) = htlc. state {
2370
- balance_msat += htlc. amount_msat ;
2371
- }
2370
+ next_outbound_htlc_limit_msat : cmp:: max ( cmp:: min ( outbound_capacity_msat as i64 ,
2371
+ self . counterparty_max_htlc_value_in_flight_msat as i64
2372
+ - outbound_stats. pending_htlcs_value_msat as i64 ) ,
2373
+ 0 ) as u64 ,
2374
+ balance_msat,
2372
2375
}
2373
- balance_msat - self . get_outbound_pending_htlc_stats ( None ) . pending_htlcs_value_msat
2374
2376
}
2375
2377
2376
2378
pub fn get_holder_counterparty_selected_channel_reserve_satoshis ( & self ) -> ( u64 , Option < u64 > ) {
0 commit comments