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 * A utility that keeps track of [`SpendableOutputDescriptor`]s, persists them in a given
13 * [`KVStore`] and regularly retries sweeping them based on a callback given to the constructor
16 * Users should call [`Self::track_spendable_outputs`] for any [`SpendableOutputDescriptor`]s received via [`Event::SpendableOutputs`].
18 * This needs to be notified of chain state changes either via its [`Listen`] or [`Confirm`]
19 * implementation and hence has to be connected with the utilized chain data sources.
21 * If chain data is provided via the [`Confirm`] interface or via filtered blocks, users are
22 * required to give their chain data sources (i.e., [`Filter`] implementation) to the respective
25 * [`Event::SpendableOutputs`]: crate::events::Event::SpendableOutputs
27 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
28 public class OutputSweeper extends CommonBase {
29 OutputSweeper(Object _dummy, long ptr) { super(ptr); }
30 @Override @SuppressWarnings("deprecation")
31 protected void finalize() throws Throwable {
33 if (ptr != 0) { bindings.OutputSweeper_free(ptr); }
37 * Constructs a new [`OutputSweeper`].
39 * If chain data is provided via the [`Confirm`] interface or via filtered blocks, users also
40 * need to register their [`Filter`] implementation via the given `chain_data_source`.
42 public static OutputSweeper of(org.ldk.structs.BestBlock best_block, org.ldk.structs.BroadcasterInterface broadcaster, org.ldk.structs.FeeEstimator fee_estimator, org.ldk.structs.Option_FilterZ chain_data_source, org.ldk.structs.OutputSpender output_spender, org.ldk.structs.ChangeDestinationSource change_destination_source, org.ldk.structs.KVStore kv_store, org.ldk.structs.Logger logger) {
43 long ret = bindings.OutputSweeper_new(best_block.ptr, broadcaster.ptr, fee_estimator.ptr, chain_data_source.ptr, output_spender.ptr, change_destination_source.ptr, kv_store.ptr, logger.ptr);
44 Reference.reachabilityFence(best_block);
45 Reference.reachabilityFence(broadcaster);
46 Reference.reachabilityFence(fee_estimator);
47 Reference.reachabilityFence(chain_data_source);
48 Reference.reachabilityFence(output_spender);
49 Reference.reachabilityFence(change_destination_source);
50 Reference.reachabilityFence(kv_store);
51 Reference.reachabilityFence(logger);
52 if (ret >= 0 && ret <= 4096) { return null; }
53 org.ldk.structs.OutputSweeper ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.OutputSweeper(null, ret); }
54 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
55 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(best_block); };
56 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(broadcaster); };
57 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(fee_estimator); };
58 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(chain_data_source); };
59 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(output_spender); };
60 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(change_destination_source); };
61 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(kv_store); };
62 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
67 * Tells the sweeper to track the given outputs descriptors.
69 * Usually, this should be called based on the values emitted by the
70 * [`Event::SpendableOutputs`].
72 * The given `exclude_static_outputs` flag controls whether the sweeper will filter out
73 * [`SpendableOutputDescriptor::StaticOutput`]s, which may be handled directly by the on-chain
74 * wallet implementation.
76 * If `delay_until_height` is set, we will delay the spending until the respective block
77 * height is reached. This can be used to batch spends, e.g., to reduce on-chain fees.
79 * Returns `Err` on persistence failure, in which case the call may be safely retried.
81 * [`Event::SpendableOutputs`]: crate::events::Event::SpendableOutputs
83 * Note that channel_id (or a relevant inner pointer) may be NULL or all-0s to represent None
85 public Result_NoneNoneZ track_spendable_outputs(SpendableOutputDescriptor[] output_descriptors, @Nullable org.ldk.structs.ChannelId channel_id, boolean exclude_static_outputs, org.ldk.structs.Option_u32Z delay_until_height) {
86 long ret = bindings.OutputSweeper_track_spendable_outputs(this.ptr, output_descriptors != null ? Arrays.stream(output_descriptors).mapToLong(output_descriptors_conv_27 -> output_descriptors_conv_27.ptr).toArray() : null, channel_id == null ? 0 : channel_id.ptr, exclude_static_outputs, delay_until_height.ptr);
87 Reference.reachabilityFence(this);
88 Reference.reachabilityFence(output_descriptors);
89 Reference.reachabilityFence(channel_id);
90 Reference.reachabilityFence(exclude_static_outputs);
91 Reference.reachabilityFence(delay_until_height);
92 if (ret >= 0 && ret <= 4096) { return null; }
93 Result_NoneNoneZ ret_hu_conv = Result_NoneNoneZ.constr_from_ptr(ret);
94 for (SpendableOutputDescriptor output_descriptors_conv_27: output_descriptors) { if (this != null) { this.ptrs_to.add(output_descriptors_conv_27); }; };
95 if (this != null) { this.ptrs_to.add(channel_id); };
96 if (this != null) { this.ptrs_to.add(delay_until_height); };
101 * Returns a list of the currently tracked spendable outputs.
103 public TrackedSpendableOutput[] tracked_spendable_outputs() {
104 long[] ret = bindings.OutputSweeper_tracked_spendable_outputs(this.ptr);
105 Reference.reachabilityFence(this);
106 int ret_conv_24_len = ret.length;
107 TrackedSpendableOutput[] ret_conv_24_arr = new TrackedSpendableOutput[ret_conv_24_len];
108 for (int y = 0; y < ret_conv_24_len; y++) {
109 long ret_conv_24 = ret[y];
110 org.ldk.structs.TrackedSpendableOutput ret_conv_24_hu_conv = null; if (ret_conv_24 < 0 || ret_conv_24 > 4096) { ret_conv_24_hu_conv = new org.ldk.structs.TrackedSpendableOutput(null, ret_conv_24); }
111 if (ret_conv_24_hu_conv != null) { ret_conv_24_hu_conv.ptrs_to.add(this); };
112 ret_conv_24_arr[y] = ret_conv_24_hu_conv;
114 return ret_conv_24_arr;
118 * Gets the latest best block which was connected either via the [`Listen`] or
119 * [`Confirm`] interfaces.
121 public BestBlock current_best_block() {
122 long ret = bindings.OutputSweeper_current_best_block(this.ptr);
123 Reference.reachabilityFence(this);
124 if (ret >= 0 && ret <= 4096) { return null; }
125 org.ldk.structs.BestBlock ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.BestBlock(null, ret); }
126 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
131 * Constructs a new Listen which calls the relevant methods on this_arg.
132 * This copies the `inner` pointer in this_arg and thus the returned Listen must be freed before this_arg is
134 public Listen as_Listen() {
135 long ret = bindings.OutputSweeper_as_Listen(this.ptr);
136 Reference.reachabilityFence(this);
137 if (ret >= 0 && ret <= 4096) { return null; }
138 Listen ret_hu_conv = new Listen(null, ret);
139 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
144 * Constructs a new Confirm which calls the relevant methods on this_arg.
145 * This copies the `inner` pointer in this_arg and thus the returned Confirm must be freed before this_arg is
147 public Confirm as_Confirm() {
148 long ret = bindings.OutputSweeper_as_Confirm(this.ptr);
149 Reference.reachabilityFence(this);
150 if (ret >= 0 && ret <= 4096) { return null; }
151 Confirm ret_hu_conv = new Confirm(null, ret);
152 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
157 * Read a OutputSweeper from a byte array, created by OutputSweeper_write
159 public static Result_OutputSweeperDecodeErrorZ read(byte[] ser, org.ldk.structs.BroadcasterInterface arg_a, org.ldk.structs.FeeEstimator arg_b, org.ldk.structs.Option_FilterZ arg_c, org.ldk.structs.OutputSpender arg_d, org.ldk.structs.ChangeDestinationSource arg_e, org.ldk.structs.KVStore arg_f, org.ldk.structs.Logger arg_g) {
160 long ret = bindings.OutputSweeper_read(ser, arg_a.ptr, arg_b.ptr, arg_c.ptr, arg_d.ptr, arg_e.ptr, arg_f.ptr, arg_g.ptr);
161 Reference.reachabilityFence(ser);
162 Reference.reachabilityFence(arg_a);
163 Reference.reachabilityFence(arg_b);
164 Reference.reachabilityFence(arg_c);
165 Reference.reachabilityFence(arg_d);
166 Reference.reachabilityFence(arg_e);
167 Reference.reachabilityFence(arg_f);
168 Reference.reachabilityFence(arg_g);
169 if (ret >= 0 && ret <= 4096) { return null; }
170 Result_OutputSweeperDecodeErrorZ ret_hu_conv = Result_OutputSweeperDecodeErrorZ.constr_from_ptr(ret);
171 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_a); };
172 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_b); };
173 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_c); };
174 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_d); };
175 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_e); };
176 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_f); };
177 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(arg_g); };