+/// Number of blocks we wait on seeing a HTLC output being solved before we fail corresponding
+/// inbound HTLCs. This prevents us from failing backwards and then getting a reorg resulting in us
+/// losing money.
+///
+/// Note that this is a library-wide security assumption. If a reorg deeper than this number of
+/// blocks occurs, counterparties may be able to steal funds or claims made by and balances exposed
+/// by a [`ChannelMonitor`] may be incorrect.
+
+#[no_mangle]
+pub static ANTI_REORG_DELAY: u32 = lightning::chain::channelmonitor::ANTI_REORG_DELAY;
+/// Details about the balance(s) available for spending once the channel appears on chain.
+///
+/// See [`ChannelMonitor::get_claimable_balances`] for more details on when these will or will not
+/// be provided.
+#[derive(Clone)]
+#[must_use]
+#[repr(C)]
+pub enum Balance {
+ /// The channel is not yet closed (or the commitment or closing transaction has not yet
+ /// appeared in a block). The given balance is claimable (less on-chain fees) if the channel is
+ /// force-closed now.
+ ClaimableOnChannelClose {
+ /// The amount available to claim, in satoshis, excluding the on-chain fees which will be
+ /// required to do so.
+ amount_satoshis: u64,
+ },
+ /// The channel has been closed, and the given balance is ours but awaiting confirmations until
+ /// we consider it spendable.
+ ClaimableAwaitingConfirmations {
+ /// The amount available to claim, in satoshis, possibly excluding the on-chain fees which
+ /// were spent in broadcasting the transaction.
+ amount_satoshis: u64,
+ /// The height at which an [`Event::SpendableOutputs`] event will be generated for this
+ /// amount.
+ confirmation_height: u32,
+ },
+ /// The channel has been closed, and the given balance should be ours but awaiting spending
+ /// transaction confirmation. If the spending transaction does not confirm in time, it is
+ /// possible our counterparty can take the funds by broadcasting an HTLC timeout on-chain.
+ ///
+ /// Once the spending transaction confirms, before it has reached enough confirmations to be
+ /// considered safe from chain reorganizations, the balance will instead be provided via
+ /// [`Balance::ClaimableAwaitingConfirmations`].
+ ContentiousClaimable {
+ /// The amount available to claim, in satoshis, excluding the on-chain fees which will be
+ /// required to do so.
+ amount_satoshis: u64,
+ /// The height at which the counterparty may be able to claim the balance if we have not
+ /// done so.
+ timeout_height: u32,
+ /// The payment hash that locks this HTLC.
+ payment_hash: crate::c_types::ThirtyTwoBytes,
+ /// The preimage that can be used to claim this HTLC.
+ payment_preimage: crate::c_types::ThirtyTwoBytes,
+ },
+ /// HTLCs which we sent to our counterparty which are claimable after a timeout (less on-chain
+ /// fees) if the counterparty does not know the preimage for the HTLCs. These are somewhat
+ /// likely to be claimed by our counterparty before we do.
+ MaybeTimeoutClaimableHTLC {
+ /// The amount potentially available to claim, in satoshis, excluding the on-chain fees
+ /// which will be required to do so.
+ amount_satoshis: u64,
+ /// The height at which we will be able to claim the balance if our counterparty has not
+ /// done so.
+ claimable_height: u32,
+ /// The payment hash whose preimage our counterparty needs to claim this HTLC.
+ payment_hash: crate::c_types::ThirtyTwoBytes,
+ },
+ /// HTLCs which we received from our counterparty which are claimable with a preimage which we
+ /// do not currently have. This will only be claimable if we receive the preimage from the node
+ /// to which we forwarded this HTLC before the timeout.
+ MaybePreimageClaimableHTLC {
+ /// The amount potentially available to claim, in satoshis, excluding the on-chain fees
+ /// which will be required to do so.
+ amount_satoshis: u64,
+ /// The height at which our counterparty will be able to claim the balance if we have not
+ /// yet received the preimage and claimed it ourselves.
+ expiry_height: u32,
+ /// The payment hash whose preimage we need to claim this HTLC.
+ payment_hash: crate::c_types::ThirtyTwoBytes,
+ },
+ /// The channel has been closed, and our counterparty broadcasted a revoked commitment
+ /// transaction.
+ ///
+ /// Thus, we're able to claim all outputs in the commitment transaction, one of which has the
+ /// following amount.
+ CounterpartyRevokedOutputClaimable {
+ /// The amount, in satoshis, of the output which we can claim.
+ ///
+ /// Note that for outputs from HTLC balances this may be excluding some on-chain fees that
+ /// were already spent.
+ amount_satoshis: u64,
+ },
+}
+use lightning::chain::channelmonitor::Balance as BalanceImport;
+pub(crate) type nativeBalance = BalanceImport;
+
+impl Balance {
+ #[allow(unused)]
+ pub(crate) fn to_native(&self) -> nativeBalance {
+ match self {
+ Balance::ClaimableOnChannelClose {ref amount_satoshis, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ nativeBalance::ClaimableOnChannelClose {
+ amount_satoshis: amount_satoshis_nonref,
+ }
+ },
+ Balance::ClaimableAwaitingConfirmations {ref amount_satoshis, ref confirmation_height, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ let mut confirmation_height_nonref = Clone::clone(confirmation_height);
+ nativeBalance::ClaimableAwaitingConfirmations {
+ amount_satoshis: amount_satoshis_nonref,
+ confirmation_height: confirmation_height_nonref,
+ }
+ },
+ Balance::ContentiousClaimable {ref amount_satoshis, ref timeout_height, ref payment_hash, ref payment_preimage, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ let mut timeout_height_nonref = Clone::clone(timeout_height);
+ let mut payment_hash_nonref = Clone::clone(payment_hash);
+ let mut payment_preimage_nonref = Clone::clone(payment_preimage);
+ nativeBalance::ContentiousClaimable {
+ amount_satoshis: amount_satoshis_nonref,
+ timeout_height: timeout_height_nonref,
+ payment_hash: ::lightning::ln::PaymentHash(payment_hash_nonref.data),
+ payment_preimage: ::lightning::ln::PaymentPreimage(payment_preimage_nonref.data),
+ }
+ },
+ Balance::MaybeTimeoutClaimableHTLC {ref amount_satoshis, ref claimable_height, ref payment_hash, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ let mut claimable_height_nonref = Clone::clone(claimable_height);
+ let mut payment_hash_nonref = Clone::clone(payment_hash);
+ nativeBalance::MaybeTimeoutClaimableHTLC {
+ amount_satoshis: amount_satoshis_nonref,
+ claimable_height: claimable_height_nonref,
+ payment_hash: ::lightning::ln::PaymentHash(payment_hash_nonref.data),
+ }
+ },
+ Balance::MaybePreimageClaimableHTLC {ref amount_satoshis, ref expiry_height, ref payment_hash, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ let mut expiry_height_nonref = Clone::clone(expiry_height);
+ let mut payment_hash_nonref = Clone::clone(payment_hash);
+ nativeBalance::MaybePreimageClaimableHTLC {
+ amount_satoshis: amount_satoshis_nonref,
+ expiry_height: expiry_height_nonref,
+ payment_hash: ::lightning::ln::PaymentHash(payment_hash_nonref.data),
+ }
+ },
+ Balance::CounterpartyRevokedOutputClaimable {ref amount_satoshis, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ nativeBalance::CounterpartyRevokedOutputClaimable {
+ amount_satoshis: amount_satoshis_nonref,
+ }
+ },
+ }
+ }
+ #[allow(unused)]
+ pub(crate) fn into_native(self) -> nativeBalance {
+ match self {
+ Balance::ClaimableOnChannelClose {mut amount_satoshis, } => {
+ nativeBalance::ClaimableOnChannelClose {
+ amount_satoshis: amount_satoshis,
+ }
+ },
+ Balance::ClaimableAwaitingConfirmations {mut amount_satoshis, mut confirmation_height, } => {
+ nativeBalance::ClaimableAwaitingConfirmations {
+ amount_satoshis: amount_satoshis,
+ confirmation_height: confirmation_height,
+ }
+ },
+ Balance::ContentiousClaimable {mut amount_satoshis, mut timeout_height, mut payment_hash, mut payment_preimage, } => {
+ nativeBalance::ContentiousClaimable {
+ amount_satoshis: amount_satoshis,
+ timeout_height: timeout_height,
+ payment_hash: ::lightning::ln::PaymentHash(payment_hash.data),
+ payment_preimage: ::lightning::ln::PaymentPreimage(payment_preimage.data),
+ }
+ },
+ Balance::MaybeTimeoutClaimableHTLC {mut amount_satoshis, mut claimable_height, mut payment_hash, } => {
+ nativeBalance::MaybeTimeoutClaimableHTLC {
+ amount_satoshis: amount_satoshis,
+ claimable_height: claimable_height,
+ payment_hash: ::lightning::ln::PaymentHash(payment_hash.data),
+ }
+ },
+ Balance::MaybePreimageClaimableHTLC {mut amount_satoshis, mut expiry_height, mut payment_hash, } => {
+ nativeBalance::MaybePreimageClaimableHTLC {
+ amount_satoshis: amount_satoshis,
+ expiry_height: expiry_height,
+ payment_hash: ::lightning::ln::PaymentHash(payment_hash.data),
+ }
+ },
+ Balance::CounterpartyRevokedOutputClaimable {mut amount_satoshis, } => {
+ nativeBalance::CounterpartyRevokedOutputClaimable {
+ amount_satoshis: amount_satoshis,
+ }
+ },
+ }
+ }
+ #[allow(unused)]
+ pub(crate) fn from_native(native: &nativeBalance) -> Self {
+ match native {
+ nativeBalance::ClaimableOnChannelClose {ref amount_satoshis, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ Balance::ClaimableOnChannelClose {
+ amount_satoshis: amount_satoshis_nonref,
+ }
+ },
+ nativeBalance::ClaimableAwaitingConfirmations {ref amount_satoshis, ref confirmation_height, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ let mut confirmation_height_nonref = Clone::clone(confirmation_height);
+ Balance::ClaimableAwaitingConfirmations {
+ amount_satoshis: amount_satoshis_nonref,
+ confirmation_height: confirmation_height_nonref,
+ }
+ },
+ nativeBalance::ContentiousClaimable {ref amount_satoshis, ref timeout_height, ref payment_hash, ref payment_preimage, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ let mut timeout_height_nonref = Clone::clone(timeout_height);
+ let mut payment_hash_nonref = Clone::clone(payment_hash);
+ let mut payment_preimage_nonref = Clone::clone(payment_preimage);
+ Balance::ContentiousClaimable {
+ amount_satoshis: amount_satoshis_nonref,
+ timeout_height: timeout_height_nonref,
+ payment_hash: crate::c_types::ThirtyTwoBytes { data: payment_hash_nonref.0 },
+ payment_preimage: crate::c_types::ThirtyTwoBytes { data: payment_preimage_nonref.0 },
+ }
+ },
+ nativeBalance::MaybeTimeoutClaimableHTLC {ref amount_satoshis, ref claimable_height, ref payment_hash, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ let mut claimable_height_nonref = Clone::clone(claimable_height);
+ let mut payment_hash_nonref = Clone::clone(payment_hash);
+ Balance::MaybeTimeoutClaimableHTLC {
+ amount_satoshis: amount_satoshis_nonref,
+ claimable_height: claimable_height_nonref,
+ payment_hash: crate::c_types::ThirtyTwoBytes { data: payment_hash_nonref.0 },
+ }
+ },
+ nativeBalance::MaybePreimageClaimableHTLC {ref amount_satoshis, ref expiry_height, ref payment_hash, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ let mut expiry_height_nonref = Clone::clone(expiry_height);
+ let mut payment_hash_nonref = Clone::clone(payment_hash);
+ Balance::MaybePreimageClaimableHTLC {
+ amount_satoshis: amount_satoshis_nonref,
+ expiry_height: expiry_height_nonref,
+ payment_hash: crate::c_types::ThirtyTwoBytes { data: payment_hash_nonref.0 },
+ }
+ },
+ nativeBalance::CounterpartyRevokedOutputClaimable {ref amount_satoshis, } => {
+ let mut amount_satoshis_nonref = Clone::clone(amount_satoshis);
+ Balance::CounterpartyRevokedOutputClaimable {
+ amount_satoshis: amount_satoshis_nonref,
+ }
+ },
+ }
+ }
+ #[allow(unused)]
+ pub(crate) fn native_into(native: nativeBalance) -> Self {
+ match native {
+ nativeBalance::ClaimableOnChannelClose {mut amount_satoshis, } => {
+ Balance::ClaimableOnChannelClose {
+ amount_satoshis: amount_satoshis,
+ }
+ },
+ nativeBalance::ClaimableAwaitingConfirmations {mut amount_satoshis, mut confirmation_height, } => {
+ Balance::ClaimableAwaitingConfirmations {
+ amount_satoshis: amount_satoshis,
+ confirmation_height: confirmation_height,
+ }
+ },
+ nativeBalance::ContentiousClaimable {mut amount_satoshis, mut timeout_height, mut payment_hash, mut payment_preimage, } => {
+ Balance::ContentiousClaimable {
+ amount_satoshis: amount_satoshis,
+ timeout_height: timeout_height,
+ payment_hash: crate::c_types::ThirtyTwoBytes { data: payment_hash.0 },
+ payment_preimage: crate::c_types::ThirtyTwoBytes { data: payment_preimage.0 },
+ }
+ },
+ nativeBalance::MaybeTimeoutClaimableHTLC {mut amount_satoshis, mut claimable_height, mut payment_hash, } => {
+ Balance::MaybeTimeoutClaimableHTLC {
+ amount_satoshis: amount_satoshis,
+ claimable_height: claimable_height,
+ payment_hash: crate::c_types::ThirtyTwoBytes { data: payment_hash.0 },
+ }
+ },
+ nativeBalance::MaybePreimageClaimableHTLC {mut amount_satoshis, mut expiry_height, mut payment_hash, } => {
+ Balance::MaybePreimageClaimableHTLC {
+ amount_satoshis: amount_satoshis,
+ expiry_height: expiry_height,
+ payment_hash: crate::c_types::ThirtyTwoBytes { data: payment_hash.0 },
+ }
+ },
+ nativeBalance::CounterpartyRevokedOutputClaimable {mut amount_satoshis, } => {
+ Balance::CounterpartyRevokedOutputClaimable {
+ amount_satoshis: amount_satoshis,
+ }
+ },
+ }
+ }
+}
+/// Frees any resources used by the Balance
+#[no_mangle]
+pub extern "C" fn Balance_free(this_ptr: Balance) { }
+/// Creates a copy of the Balance
+#[no_mangle]
+pub extern "C" fn Balance_clone(orig: &Balance) -> Balance {
+ orig.clone()
+}
+#[allow(unused)]
+/// Used only if an object of this type is returned as a trait impl by a method
+pub(crate) extern "C" fn Balance_clone_void(this_ptr: *const c_void) -> *mut c_void {
+ Box::into_raw(Box::new(unsafe { (*(this_ptr as *const Balance)).clone() })) as *mut c_void
+}
+#[allow(unused)]
+/// Used only if an object of this type is returned as a trait impl by a method
+pub(crate) extern "C" fn Balance_free_void(this_ptr: *mut c_void) {
+ let _ = unsafe { Box::from_raw(this_ptr as *mut Balance) };
+}
+#[no_mangle]
+/// Utility method to constructs a new ClaimableOnChannelClose-variant Balance
+pub extern "C" fn Balance_claimable_on_channel_close(amount_satoshis: u64) -> Balance {
+ Balance::ClaimableOnChannelClose {
+ amount_satoshis,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new ClaimableAwaitingConfirmations-variant Balance
+pub extern "C" fn Balance_claimable_awaiting_confirmations(amount_satoshis: u64, confirmation_height: u32) -> Balance {
+ Balance::ClaimableAwaitingConfirmations {
+ amount_satoshis,
+ confirmation_height,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new ContentiousClaimable-variant Balance
+pub extern "C" fn Balance_contentious_claimable(amount_satoshis: u64, timeout_height: u32, payment_hash: crate::c_types::ThirtyTwoBytes, payment_preimage: crate::c_types::ThirtyTwoBytes) -> Balance {
+ Balance::ContentiousClaimable {
+ amount_satoshis,
+ timeout_height,
+ payment_hash,
+ payment_preimage,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new MaybeTimeoutClaimableHTLC-variant Balance
+pub extern "C" fn Balance_maybe_timeout_claimable_htlc(amount_satoshis: u64, claimable_height: u32, payment_hash: crate::c_types::ThirtyTwoBytes) -> Balance {
+ Balance::MaybeTimeoutClaimableHTLC {
+ amount_satoshis,
+ claimable_height,
+ payment_hash,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new MaybePreimageClaimableHTLC-variant Balance
+pub extern "C" fn Balance_maybe_preimage_claimable_htlc(amount_satoshis: u64, expiry_height: u32, payment_hash: crate::c_types::ThirtyTwoBytes) -> Balance {
+ Balance::MaybePreimageClaimableHTLC {
+ amount_satoshis,
+ expiry_height,
+ payment_hash,
+ }
+}
+#[no_mangle]
+/// Utility method to constructs a new CounterpartyRevokedOutputClaimable-variant Balance
+pub extern "C" fn Balance_counterparty_revoked_output_claimable(amount_satoshis: u64) -> Balance {
+ Balance::CounterpartyRevokedOutputClaimable {
+ amount_satoshis,
+ }
+}
+/// Checks if two Balances contain equal inner contents.
+/// This ignores pointers and is_owned flags and looks at the values in fields.
+#[no_mangle]
+pub extern "C" fn Balance_eq(a: &Balance, b: &Balance) -> bool {
+ if &a.to_native() == &b.to_native() { true } else { false }
+}
+/// 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.
+#[must_use]
+#[no_mangle]
+pub extern "C" fn Balance_claimable_amount_satoshis(this_arg: &crate::lightning::chain::channelmonitor::Balance) -> u64 {
+ let mut ret = this_arg.to_native().claimable_amount_satoshis();
+ ret
+}
+