Add helper for getting claimable balance
authorbenthecarman <benthecarman@live.com>
Fri, 2 Jun 2023 19:39:16 +0000 (14:39 -0500)
committerbenthecarman <benthecarman@live.com>
Wed, 7 Jun 2023 18:39:50 +0000 (13:39 -0500)
It is annoying to have to match across all the enums of `Balance` to
just pull out the `claimable_amount_satoshis` value. This helper makes
it easier if you just want to amount.

lightning/src/chain/channelmonitor.rs

index a48f169a4d5e605b330f49896bc191779dd389d7..d3052b5fed4087f6de9ccd9dd7fc3047106fbf60 100644 (file)
@@ -651,6 +651,40 @@ pub enum Balance {
        },
 }
 
+impl Balance {
+       /// The amount claimable, in satoshis. This excludes balances that we are unsure if we are able
+       /// to claim, this is because we are waiting for a preimage or for a timeout to expire. For more
+       /// information on these balances see [`Balance::MaybeTimeoutClaimableHTLC`] and
+       /// [`Balance::MaybePreimageClaimableHTLC`].
+       ///
+       /// On-chain fees required to claim the balance are not included in this amount.
+       pub fn claimable_amount_satoshis(&self) -> u64 {
+               match self {
+                       Balance::ClaimableOnChannelClose {
+                               claimable_amount_satoshis,
+                       } => *claimable_amount_satoshis,
+                       Balance::ClaimableAwaitingConfirmations {
+                               claimable_amount_satoshis,
+                               ..
+                       } => *claimable_amount_satoshis,
+                       Balance::ContentiousClaimable {
+                               claimable_amount_satoshis,
+                               ..
+                       } => *claimable_amount_satoshis,
+                       Balance::MaybeTimeoutClaimableHTLC {
+                               ..
+                       } => 0,
+                       Balance::MaybePreimageClaimableHTLC {
+                               ..
+                       } => 0,
+                       Balance::CounterpartyRevokedOutputClaimable {
+                               claimable_amount_satoshis,
+                               ..
+                       } => *claimable_amount_satoshis,
+               }
+       }
+}
+
 /// An HTLC which has been irrevocably resolved on-chain, and has reached ANTI_REORG_DELAY.
 #[derive(PartialEq, Eq)]
 struct IrrevocablyResolvedHTLC {