1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
12 * Details about the balance(s) available for spending once the channel appears on chain.
14 * See [`ChannelMonitor::get_claimable_balances`] for more details on when these will or will not
17 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
18 public class Balance extends CommonBase {
19 private Balance(Object _dummy, long ptr) { super(ptr); }
20 @Override @SuppressWarnings("deprecation")
21 protected void finalize() throws Throwable {
23 if (ptr != 0) { bindings.Balance_free(ptr); }
25 static Balance constr_from_ptr(long ptr) {
26 bindings.LDKBalance raw_val = bindings.LDKBalance_ref_from_ptr(ptr);
27 if (raw_val.getClass() == bindings.LDKBalance.ClaimableOnChannelClose.class) {
28 return new ClaimableOnChannelClose(ptr, (bindings.LDKBalance.ClaimableOnChannelClose)raw_val);
30 if (raw_val.getClass() == bindings.LDKBalance.ClaimableAwaitingConfirmations.class) {
31 return new ClaimableAwaitingConfirmations(ptr, (bindings.LDKBalance.ClaimableAwaitingConfirmations)raw_val);
33 if (raw_val.getClass() == bindings.LDKBalance.ContentiousClaimable.class) {
34 return new ContentiousClaimable(ptr, (bindings.LDKBalance.ContentiousClaimable)raw_val);
36 if (raw_val.getClass() == bindings.LDKBalance.MaybeClaimableHTLCAwaitingTimeout.class) {
37 return new MaybeClaimableHTLCAwaitingTimeout(ptr, (bindings.LDKBalance.MaybeClaimableHTLCAwaitingTimeout)raw_val);
39 assert false; return null; // Unreachable without extending the (internal) bindings interface
43 * The channel is not yet closed (or the commitment or closing transaction has not yet
44 * appeared in a block). The given balance is claimable (less on-chain fees) if the channel is
47 public final static class ClaimableOnChannelClose extends Balance {
49 * The amount available to claim, in satoshis, excluding the on-chain fees which will be
52 public final long claimable_amount_satoshis;
53 private ClaimableOnChannelClose(long ptr, bindings.LDKBalance.ClaimableOnChannelClose obj) {
55 this.claimable_amount_satoshis = obj.claimable_amount_satoshis;
59 * The channel has been closed, and the given balance is ours but awaiting confirmations until
60 * we consider it spendable.
62 public final static class ClaimableAwaitingConfirmations extends Balance {
64 * The amount available to claim, in satoshis, possibly excluding the on-chain fees which
65 * were spent in broadcasting the transaction.
67 public final long claimable_amount_satoshis;
69 * The height at which an [`Event::SpendableOutputs`] event will be generated for this
72 public final int confirmation_height;
73 private ClaimableAwaitingConfirmations(long ptr, bindings.LDKBalance.ClaimableAwaitingConfirmations obj) {
75 this.claimable_amount_satoshis = obj.claimable_amount_satoshis;
76 this.confirmation_height = obj.confirmation_height;
80 * The channel has been closed, and the given balance should be ours but awaiting spending
81 * transaction confirmation. If the spending transaction does not confirm in time, it is
82 * possible our counterparty can take the funds by broadcasting an HTLC timeout on-chain.
84 * Once the spending transaction confirms, before it has reached enough confirmations to be
85 * considered safe from chain reorganizations, the balance will instead be provided via
86 * [`Balance::ClaimableAwaitingConfirmations`].
88 public final static class ContentiousClaimable extends Balance {
90 * The amount available to claim, in satoshis, excluding the on-chain fees which will be
93 public final long claimable_amount_satoshis;
95 * The height at which the counterparty may be able to claim the balance if we have not
98 public final int timeout_height;
99 private ContentiousClaimable(long ptr, bindings.LDKBalance.ContentiousClaimable obj) {
101 this.claimable_amount_satoshis = obj.claimable_amount_satoshis;
102 this.timeout_height = obj.timeout_height;
106 * HTLCs which we sent to our counterparty which are claimable after a timeout (less on-chain
107 * fees) if the counterparty does not know the preimage for the HTLCs. These are somewhat
108 * likely to be claimed by our counterparty before we do.
110 public final static class MaybeClaimableHTLCAwaitingTimeout extends Balance {
112 * The amount available to claim, in satoshis, excluding the on-chain fees which will be
115 public final long claimable_amount_satoshis;
117 * The height at which we will be able to claim the balance if our counterparty has not
120 public final int claimable_height;
121 private MaybeClaimableHTLCAwaitingTimeout(long ptr, bindings.LDKBalance.MaybeClaimableHTLCAwaitingTimeout obj) {
123 this.claimable_amount_satoshis = obj.claimable_amount_satoshis;
124 this.claimable_height = obj.claimable_height;
128 long ret = bindings.Balance_clone_ptr(this.ptr);
129 Reference.reachabilityFence(this);
134 * Creates a copy of the Balance
136 public Balance clone() {
137 long ret = bindings.Balance_clone(this.ptr);
138 Reference.reachabilityFence(this);
139 if (ret >= 0 && ret <= 4096) { return null; }
140 Balance ret_hu_conv = Balance.constr_from_ptr(ret);
141 ret_hu_conv.ptrs_to.add(this);
146 * Utility method to constructs a new ClaimableOnChannelClose-variant Balance
148 public static Balance claimable_on_channel_close(long claimable_amount_satoshis) {
149 long ret = bindings.Balance_claimable_on_channel_close(claimable_amount_satoshis);
150 Reference.reachabilityFence(claimable_amount_satoshis);
151 if (ret >= 0 && ret <= 4096) { return null; }
152 Balance ret_hu_conv = Balance.constr_from_ptr(ret);
153 ret_hu_conv.ptrs_to.add(ret_hu_conv);
158 * Utility method to constructs a new ClaimableAwaitingConfirmations-variant Balance
160 public static Balance claimable_awaiting_confirmations(long claimable_amount_satoshis, int confirmation_height) {
161 long ret = bindings.Balance_claimable_awaiting_confirmations(claimable_amount_satoshis, confirmation_height);
162 Reference.reachabilityFence(claimable_amount_satoshis);
163 Reference.reachabilityFence(confirmation_height);
164 if (ret >= 0 && ret <= 4096) { return null; }
165 Balance ret_hu_conv = Balance.constr_from_ptr(ret);
166 ret_hu_conv.ptrs_to.add(ret_hu_conv);
171 * Utility method to constructs a new ContentiousClaimable-variant Balance
173 public static Balance contentious_claimable(long claimable_amount_satoshis, int timeout_height) {
174 long ret = bindings.Balance_contentious_claimable(claimable_amount_satoshis, timeout_height);
175 Reference.reachabilityFence(claimable_amount_satoshis);
176 Reference.reachabilityFence(timeout_height);
177 if (ret >= 0 && ret <= 4096) { return null; }
178 Balance ret_hu_conv = Balance.constr_from_ptr(ret);
179 ret_hu_conv.ptrs_to.add(ret_hu_conv);
184 * Utility method to constructs a new MaybeClaimableHTLCAwaitingTimeout-variant Balance
186 public static Balance maybe_claimable_htlcawaiting_timeout(long claimable_amount_satoshis, int claimable_height) {
187 long ret = bindings.Balance_maybe_claimable_htlcawaiting_timeout(claimable_amount_satoshis, claimable_height);
188 Reference.reachabilityFence(claimable_amount_satoshis);
189 Reference.reachabilityFence(claimable_height);
190 if (ret >= 0 && ret <= 4096) { return null; }
191 Balance ret_hu_conv = Balance.constr_from_ptr(ret);
192 ret_hu_conv.ptrs_to.add(ret_hu_conv);
197 * Checks if two Balances contain equal inner contents.
198 * This ignores pointers and is_owned flags and looks at the values in fields.
200 public boolean eq(Balance b) {
201 boolean ret = bindings.Balance_eq(this.ptr, b == null ? 0 : b.ptr & ~1);
202 Reference.reachabilityFence(this);
203 Reference.reachabilityFence(b);
207 @Override public boolean equals(Object o) {
208 if (!(o instanceof Balance)) return false;
209 return this.eq((Balance)o);