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.MaybeTimeoutClaimableHTLC.class) {
37 return new MaybeTimeoutClaimableHTLC(ptr, (bindings.LDKBalance.MaybeTimeoutClaimableHTLC)raw_val);
39 if (raw_val.getClass() == bindings.LDKBalance.MaybePreimageClaimableHTLC.class) {
40 return new MaybePreimageClaimableHTLC(ptr, (bindings.LDKBalance.MaybePreimageClaimableHTLC)raw_val);
42 if (raw_val.getClass() == bindings.LDKBalance.CounterpartyRevokedOutputClaimable.class) {
43 return new CounterpartyRevokedOutputClaimable(ptr, (bindings.LDKBalance.CounterpartyRevokedOutputClaimable)raw_val);
45 assert false; return null; // Unreachable without extending the (internal) bindings interface
49 * The channel is not yet closed (or the commitment or closing transaction has not yet
50 * appeared in a block). The given balance is claimable (less on-chain fees) if the channel is
53 public final static class ClaimableOnChannelClose extends Balance {
55 * The amount available to claim, in satoshis, excluding the on-chain fees which will be
58 public final long amount_satoshis;
59 private ClaimableOnChannelClose(long ptr, bindings.LDKBalance.ClaimableOnChannelClose obj) {
61 this.amount_satoshis = obj.amount_satoshis;
65 * The channel has been closed, and the given balance is ours but awaiting confirmations until
66 * we consider it spendable.
68 public final static class ClaimableAwaitingConfirmations extends Balance {
70 * The amount available to claim, in satoshis, possibly excluding the on-chain fees which
71 * were spent in broadcasting the transaction.
73 public final long amount_satoshis;
75 * The height at which an [`Event::SpendableOutputs`] event will be generated for this
78 public final int confirmation_height;
79 private ClaimableAwaitingConfirmations(long ptr, bindings.LDKBalance.ClaimableAwaitingConfirmations obj) {
81 this.amount_satoshis = obj.amount_satoshis;
82 this.confirmation_height = obj.confirmation_height;
86 * The channel has been closed, and the given balance should be ours but awaiting spending
87 * transaction confirmation. If the spending transaction does not confirm in time, it is
88 * possible our counterparty can take the funds by broadcasting an HTLC timeout on-chain.
90 * Once the spending transaction confirms, before it has reached enough confirmations to be
91 * considered safe from chain reorganizations, the balance will instead be provided via
92 * [`Balance::ClaimableAwaitingConfirmations`].
94 public final static class ContentiousClaimable extends Balance {
96 * The amount available to claim, in satoshis, excluding the on-chain fees which will be
99 public final long amount_satoshis;
101 * The height at which the counterparty may be able to claim the balance if we have not
104 public final int timeout_height;
106 * The payment hash that locks this HTLC.
108 public final byte[] payment_hash;
110 * The preimage that can be used to claim this HTLC.
112 public final byte[] payment_preimage;
113 private ContentiousClaimable(long ptr, bindings.LDKBalance.ContentiousClaimable obj) {
115 this.amount_satoshis = obj.amount_satoshis;
116 this.timeout_height = obj.timeout_height;
117 this.payment_hash = obj.payment_hash;
118 this.payment_preimage = obj.payment_preimage;
122 * HTLCs which we sent to our counterparty which are claimable after a timeout (less on-chain
123 * fees) if the counterparty does not know the preimage for the HTLCs. These are somewhat
124 * likely to be claimed by our counterparty before we do.
126 public final static class MaybeTimeoutClaimableHTLC extends Balance {
128 * The amount potentially available to claim, in satoshis, excluding the on-chain fees
129 * which will be required to do so.
131 public final long amount_satoshis;
133 * The height at which we will be able to claim the balance if our counterparty has not
136 public final int claimable_height;
138 * The payment hash whose preimage our counterparty needs to claim this HTLC.
140 public final byte[] payment_hash;
141 private MaybeTimeoutClaimableHTLC(long ptr, bindings.LDKBalance.MaybeTimeoutClaimableHTLC obj) {
143 this.amount_satoshis = obj.amount_satoshis;
144 this.claimable_height = obj.claimable_height;
145 this.payment_hash = obj.payment_hash;
149 * HTLCs which we received from our counterparty which are claimable with a preimage which we
150 * do not currently have. This will only be claimable if we receive the preimage from the node
151 * to which we forwarded this HTLC before the timeout.
153 public final static class MaybePreimageClaimableHTLC extends Balance {
155 * The amount potentially available to claim, in satoshis, excluding the on-chain fees
156 * which will be required to do so.
158 public final long amount_satoshis;
160 * The height at which our counterparty will be able to claim the balance if we have not
161 * yet received the preimage and claimed it ourselves.
163 public final int expiry_height;
165 * The payment hash whose preimage we need to claim this HTLC.
167 public final byte[] payment_hash;
168 private MaybePreimageClaimableHTLC(long ptr, bindings.LDKBalance.MaybePreimageClaimableHTLC obj) {
170 this.amount_satoshis = obj.amount_satoshis;
171 this.expiry_height = obj.expiry_height;
172 this.payment_hash = obj.payment_hash;
176 * The channel has been closed, and our counterparty broadcasted a revoked commitment
179 * Thus, we're able to claim all outputs in the commitment transaction, one of which has the
182 public final static class CounterpartyRevokedOutputClaimable extends Balance {
184 * The amount, in satoshis, of the output which we can claim.
186 * Note that for outputs from HTLC balances this may be excluding some on-chain fees that
187 * were already spent.
189 public final long amount_satoshis;
190 private CounterpartyRevokedOutputClaimable(long ptr, bindings.LDKBalance.CounterpartyRevokedOutputClaimable obj) {
192 this.amount_satoshis = obj.amount_satoshis;
196 long ret = bindings.Balance_clone_ptr(this.ptr);
197 Reference.reachabilityFence(this);
202 * Creates a copy of the Balance
204 public Balance clone() {
205 long ret = bindings.Balance_clone(this.ptr);
206 Reference.reachabilityFence(this);
207 if (ret >= 0 && ret <= 4096) { return null; }
208 org.ldk.structs.Balance ret_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret);
209 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
214 * Utility method to constructs a new ClaimableOnChannelClose-variant Balance
216 public static Balance claimable_on_channel_close(long amount_satoshis) {
217 long ret = bindings.Balance_claimable_on_channel_close(amount_satoshis);
218 Reference.reachabilityFence(amount_satoshis);
219 if (ret >= 0 && ret <= 4096) { return null; }
220 org.ldk.structs.Balance ret_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret);
221 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
226 * Utility method to constructs a new ClaimableAwaitingConfirmations-variant Balance
228 public static Balance claimable_awaiting_confirmations(long amount_satoshis, int confirmation_height) {
229 long ret = bindings.Balance_claimable_awaiting_confirmations(amount_satoshis, confirmation_height);
230 Reference.reachabilityFence(amount_satoshis);
231 Reference.reachabilityFence(confirmation_height);
232 if (ret >= 0 && ret <= 4096) { return null; }
233 org.ldk.structs.Balance ret_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret);
234 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
239 * Utility method to constructs a new ContentiousClaimable-variant Balance
241 public static Balance contentious_claimable(long amount_satoshis, int timeout_height, byte[] payment_hash, byte[] payment_preimage) {
242 long ret = bindings.Balance_contentious_claimable(amount_satoshis, timeout_height, InternalUtils.check_arr_len(payment_hash, 32), InternalUtils.check_arr_len(payment_preimage, 32));
243 Reference.reachabilityFence(amount_satoshis);
244 Reference.reachabilityFence(timeout_height);
245 Reference.reachabilityFence(payment_hash);
246 Reference.reachabilityFence(payment_preimage);
247 if (ret >= 0 && ret <= 4096) { return null; }
248 org.ldk.structs.Balance ret_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret);
249 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
254 * Utility method to constructs a new MaybeTimeoutClaimableHTLC-variant Balance
256 public static Balance maybe_timeout_claimable_htlc(long amount_satoshis, int claimable_height, byte[] payment_hash) {
257 long ret = bindings.Balance_maybe_timeout_claimable_htlc(amount_satoshis, claimable_height, InternalUtils.check_arr_len(payment_hash, 32));
258 Reference.reachabilityFence(amount_satoshis);
259 Reference.reachabilityFence(claimable_height);
260 Reference.reachabilityFence(payment_hash);
261 if (ret >= 0 && ret <= 4096) { return null; }
262 org.ldk.structs.Balance ret_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret);
263 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
268 * Utility method to constructs a new MaybePreimageClaimableHTLC-variant Balance
270 public static Balance maybe_preimage_claimable_htlc(long amount_satoshis, int expiry_height, byte[] payment_hash) {
271 long ret = bindings.Balance_maybe_preimage_claimable_htlc(amount_satoshis, expiry_height, InternalUtils.check_arr_len(payment_hash, 32));
272 Reference.reachabilityFence(amount_satoshis);
273 Reference.reachabilityFence(expiry_height);
274 Reference.reachabilityFence(payment_hash);
275 if (ret >= 0 && ret <= 4096) { return null; }
276 org.ldk.structs.Balance ret_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret);
277 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
282 * Utility method to constructs a new CounterpartyRevokedOutputClaimable-variant Balance
284 public static Balance counterparty_revoked_output_claimable(long amount_satoshis) {
285 long ret = bindings.Balance_counterparty_revoked_output_claimable(amount_satoshis);
286 Reference.reachabilityFence(amount_satoshis);
287 if (ret >= 0 && ret <= 4096) { return null; }
288 org.ldk.structs.Balance ret_hu_conv = org.ldk.structs.Balance.constr_from_ptr(ret);
289 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
294 * Checks if two Balances contain equal inner contents.
295 * This ignores pointers and is_owned flags and looks at the values in fields.
297 public boolean eq(org.ldk.structs.Balance b) {
298 boolean ret = bindings.Balance_eq(this.ptr, b == null ? 0 : b.ptr);
299 Reference.reachabilityFence(this);
300 Reference.reachabilityFence(b);
304 @Override public boolean equals(Object o) {
305 if (!(o instanceof Balance)) return false;
306 return this.eq((Balance)o);
309 * The amount claimable, in satoshis. This excludes balances that we are unsure if we are able
310 * to claim, this is because we are waiting for a preimage or for a timeout to expire. For more
311 * information on these balances see [`Balance::MaybeTimeoutClaimableHTLC`] and
312 * [`Balance::MaybePreimageClaimableHTLC`].
314 * On-chain fees required to claim the balance are not included in this amount.
316 public long claimable_amount_satoshis() {
317 long ret = bindings.Balance_claimable_amount_satoshis(this.ptr);
318 Reference.reachabilityFence(this);