Skip to content

Commit 40f6ba7

Browse files
committed
Add an "offchain balance" accessor to Balance
In a previous commit we introduced the concept of the "offchain balance" to `HolderCommitmentTransactionBalance` to better capture the type of balance that end-user walets likely wish to expose. Here we expose an accessor on `Balance` to fetch that concept across difference `Balance` variants easily. Fixes #4241
1 parent e66bc64 commit 40f6ba7

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

lightning/src/chain/channelmonitor.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,39 @@ impl Balance {
10111011
Balance::MaybePreimageClaimableHTLC { .. } => 0,
10121012
}
10131013
}
1014+
1015+
/// The "offchain balance", in satoshis.
1016+
///
1017+
/// When the channel has yet to close, this returns the balance we are owed, ignoring fees,
1018+
/// reserve values, anchors, and dust limits. This more closely corresponds with the sum of our
1019+
/// inbound and outbound payments and may be more useful as the balance displayed in an
1020+
/// end-user wallet. Still, it is somewhat misleading from an on-chain-funds-available
1021+
/// perspective.
1022+
///
1023+
/// For pending payments, splice behavior, or behavior after a channel has been closed, this
1024+
/// behaves the same as [`Self::claimable_amount_satoshis`].
1025+
#[rustfmt::skip]
1026+
pub fn offchain_amount_satoshis(&self) -> u64 {
1027+
match self {
1028+
Balance::ClaimableOnChannelClose {
1029+
balance_candidates, confirmed_balance_candidate_index, ..
1030+
} => {
1031+
if *confirmed_balance_candidate_index != 0 {
1032+
let candidate = &balance_candidates[*confirmed_balance_candidate_index];
1033+
candidate.amount_offchain_satoshis.unwrap_or(candidate.amount_satoshis)
1034+
} else {
1035+
balance_candidates.last().map(|balance| balance.amount_offchain_satoshis.unwrap_or(balance.amount_satoshis)).unwrap_or(0)
1036+
}
1037+
},
1038+
Balance::ClaimableAwaitingConfirmations { amount_satoshis, .. }|
1039+
Balance::ContentiousClaimable { amount_satoshis, .. }|
1040+
Balance::CounterpartyRevokedOutputClaimable { amount_satoshis, .. }
1041+
=> *amount_satoshis,
1042+
Balance::MaybeTimeoutClaimableHTLC { amount_satoshis, outbound_payment, .. }
1043+
=> if *outbound_payment { 0 } else { *amount_satoshis },
1044+
Balance::MaybePreimageClaimableHTLC { .. } => 0,
1045+
}
1046+
}
10141047
}
10151048

10161049
/// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY.

0 commit comments

Comments
 (0)