@@ -62,6 +62,17 @@ pub struct ChannelValueStat {
62
62
pub counterparty_dust_limit_msat : u64 ,
63
63
}
64
64
65
+ pub struct AvailableBalances {
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.
69
+ pub inbound_capacity_msat : u64 ,
70
+ /// Total amount available for us to send to our counterparty.
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,39 @@ 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 [`AvailableBalances`]'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_available_balances ( & self ) -> AvailableBalances {
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 ) ;
2343
- let outbound_capacity_msat = cmp:: max ( self . value_to_self_msat as i64
2344
- - outbound_stats. pending_htlcs_value_msat as i64
2345
- - self . counterparty_selected_channel_reserve_satoshis . unwrap_or ( 0 ) as i64 * 1000 ,
2346
- 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 ,
2353
- 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.
2351
+
2367
2352
let mut balance_msat = self . value_to_self_msat ;
2368
2353
for ref htlc in self . pending_inbound_htlcs . iter ( ) {
2369
2354
if let InboundHTLCState :: LocalRemoved ( InboundHTLCRemovalReason :: Fulfill ( _) ) = htlc. state {
2370
2355
balance_msat += htlc. amount_msat ;
2371
2356
}
2372
2357
}
2373
- balance_msat - self . get_outbound_pending_htlc_stats ( None ) . pending_htlcs_value_msat
2358
+ balance_msat -= outbound_stats. pending_htlcs_value_msat ;
2359
+
2360
+ let outbound_capacity_msat = cmp:: max ( self . value_to_self_msat as i64
2361
+ - outbound_stats. pending_htlcs_value_msat as i64
2362
+ - self . counterparty_selected_channel_reserve_satoshis . unwrap_or ( 0 ) as i64 * 1000 ,
2363
+ 0 ) as u64 ;
2364
+ AvailableBalances {
2365
+ inbound_capacity_msat : cmp:: max ( self . channel_value_satoshis as i64 * 1000
2366
+ - self . value_to_self_msat as i64
2367
+ - self . get_inbound_pending_htlc_stats ( None ) . pending_htlcs_value_msat as i64
2368
+ - self . holder_selected_channel_reserve_satoshis as i64 * 1000 ,
2369
+ 0 ) as u64 ,
2370
+ outbound_capacity_msat,
2371
+ next_outbound_htlc_limit_msat : cmp:: max ( cmp:: min ( outbound_capacity_msat as i64 ,
2372
+ self . counterparty_max_htlc_value_in_flight_msat as i64
2373
+ - outbound_stats. pending_htlcs_value_msat as i64 ) ,
2374
+ 0 ) as u64 ,
2375
+ balance_msat,
2376
+ }
2374
2377
}
2375
2378
2376
2379
pub fn get_holder_counterparty_selected_channel_reserve_satoshis ( & self ) -> ( u64 , Option < u64 > ) {
0 commit comments