Merge pull request #54 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / ChainMonitor.java
1 package org.ldk.structs;
2
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
5 import org.ldk.util.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
8
9
10 /**
11  * An implementation of [`chain::Watch`] for monitoring channels.
12  * 
13  * Connected and disconnected blocks must be provided to `ChainMonitor` as documented by
14  * [`chain::Watch`]. May be used in conjunction with [`ChannelManager`] to monitor channels locally
15  * or used independently to monitor channels remotely. See the [module-level documentation] for
16  * details.
17  * 
18  * [`ChannelManager`]: crate::ln::channelmanager::ChannelManager
19  * [module-level documentation]: crate::chain::chainmonitor
20  */
21 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
22 public class ChainMonitor extends CommonBase {
23         ChainMonitor(Object _dummy, long ptr) { super(ptr); }
24         @Override @SuppressWarnings("deprecation")
25         protected void finalize() throws Throwable {
26                 super.finalize();
27                 if (ptr != 0) { bindings.ChainMonitor_free(ptr); }
28         }
29
30         /**
31          * Creates a new `ChainMonitor` used to watch on-chain activity pertaining to channels.
32          * 
33          * When an optional chain source implementing [`chain::Filter`] is provided, the chain monitor
34          * will call back to it indicating transactions and outputs of interest. This allows clients to
35          * pre-filter blocks or only fetch blocks matching a compact filter. Otherwise, clients may
36          * always need to fetch full blocks absent another means for determining which blocks contain
37          * transactions relevant to the watched channels.
38          */
39         public static ChainMonitor of(Option_FilterZ chain_source, BroadcasterInterface broadcaster, Logger logger, FeeEstimator feeest, Persist persister) {
40                 long ret = bindings.ChainMonitor_new(chain_source.ptr, broadcaster == null ? 0 : broadcaster.ptr, logger == null ? 0 : logger.ptr, feeest == null ? 0 : feeest.ptr, persister == null ? 0 : persister.ptr);
41                 if (ret >= 0 && ret <= 4096) { return null; }
42                 ChainMonitor ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new ChainMonitor(null, ret); }
43                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
44                 ret_hu_conv.ptrs_to.add(broadcaster);
45                 ret_hu_conv.ptrs_to.add(logger);
46                 ret_hu_conv.ptrs_to.add(feeest);
47                 ret_hu_conv.ptrs_to.add(persister);
48                 return ret_hu_conv;
49         }
50
51         /**
52          * Gets the balances in the contained [`ChannelMonitor`]s which are claimable on-chain or
53          * claims which are awaiting confirmation.
54          * 
55          * Includes the balances from each [`ChannelMonitor`] *except* those included in
56          * `ignored_channels`, allowing you to filter out balances from channels which are still open
57          * (and whose balance should likely be pulled from the [`ChannelDetails`]).
58          * 
59          * See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for
60          * inclusion in the return value.
61          */
62         public Balance[] get_claimable_balances(ChannelDetails[] ignored_channels) {
63                 long[] ret = bindings.ChainMonitor_get_claimable_balances(this.ptr, ignored_channels != null ? Arrays.stream(ignored_channels).mapToLong(ignored_channels_conv_16 -> ignored_channels_conv_16 == null ? 0 : ignored_channels_conv_16.ptr & ~1).toArray() : null);
64                 Balance[] ret_conv_9_arr = new Balance[ret.length];
65                 for (int j = 0; j < ret.length; j++) {
66                         long ret_conv_9 = ret[j];
67                         Balance ret_conv_9_hu_conv = Balance.constr_from_ptr(ret_conv_9);
68                         ret_conv_9_hu_conv.ptrs_to.add(this);
69                         ret_conv_9_arr[j] = ret_conv_9_hu_conv;
70                 }
71                 return ret_conv_9_arr;
72         }
73
74         /**
75          * Gets the [`LockedChannelMonitor`] for a given funding outpoint, returning an `Err` if no
76          * such [`ChannelMonitor`] is currently being monitored for.
77          * 
78          * Note that the result holds a mutex over our monitor set, and should not be held
79          * indefinitely.
80          */
81         public Result_LockedChannelMonitorNoneZ get_monitor(OutPoint funding_txo) {
82                 long ret = bindings.ChainMonitor_get_monitor(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1);
83                 if (ret >= 0 && ret <= 4096) { return null; }
84                 Result_LockedChannelMonitorNoneZ ret_hu_conv = Result_LockedChannelMonitorNoneZ.constr_from_ptr(ret);
85                 return ret_hu_conv;
86         }
87
88         /**
89          * Lists the funding outpoint of each [`ChannelMonitor`] being monitored.
90          * 
91          * Note that [`ChannelMonitor`]s are not removed when a channel is closed as they are always
92          * monitoring for on-chain state resolutions.
93          */
94         public OutPoint[] list_monitors() {
95                 long[] ret = bindings.ChainMonitor_list_monitors(this.ptr);
96                 OutPoint[] ret_conv_10_arr = new OutPoint[ret.length];
97                 for (int k = 0; k < ret.length; k++) {
98                         long ret_conv_10 = ret[k];
99                         OutPoint ret_conv_10_hu_conv = null; if (ret_conv_10 < 0 || ret_conv_10 > 4096) { ret_conv_10_hu_conv = new OutPoint(null, ret_conv_10); }
100                         ret_conv_10_hu_conv.ptrs_to.add(this);
101                         ret_conv_10_arr[k] = ret_conv_10_hu_conv;
102                 }
103                 return ret_conv_10_arr;
104         }
105
106         /**
107          * Indicates the persistence of a [`ChannelMonitor`] has completed after
108          * [`ChannelMonitorUpdateErr::TemporaryFailure`] was returned from an update operation.
109          * 
110          * Thus, the anticipated use is, at a high level:
111          * 1) This [`ChainMonitor`] calls [`Persist::update_persisted_channel`] which stores the
112          * update to disk and begins updating any remote (e.g. watchtower/backup) copies,
113          * returning [`ChannelMonitorUpdateErr::TemporaryFailure`],
114          * 2) once all remote copies are updated, you call this function with the
115          * `completed_update_id` that completed, and once all pending updates have completed the
116          * channel will be re-enabled.
117          * 
118          * Returns an [`APIError::APIMisuseError`] if `funding_txo` does not match any currently
119          * registered [`ChannelMonitor`]s.
120          */
121         public Result_NoneAPIErrorZ channel_monitor_updated(OutPoint funding_txo, MonitorUpdateId completed_update_id) {
122                 long ret = bindings.ChainMonitor_channel_monitor_updated(this.ptr, funding_txo == null ? 0 : funding_txo.ptr & ~1, completed_update_id == null ? 0 : completed_update_id.ptr & ~1);
123                 if (ret >= 0 && ret <= 4096) { return null; }
124                 Result_NoneAPIErrorZ ret_hu_conv = Result_NoneAPIErrorZ.constr_from_ptr(ret);
125                 return ret_hu_conv;
126         }
127
128         /**
129          * Constructs a new Listen which calls the relevant methods on this_arg.
130          * This copies the `inner` pointer in this_arg and thus the returned Listen must be freed before this_arg is
131          */
132         public Listen as_Listen() {
133                 long ret = bindings.ChainMonitor_as_Listen(this.ptr);
134                 if (ret >= 0 && ret <= 4096) { return null; }
135                 Listen ret_hu_conv = new Listen(null, ret);
136                 ret_hu_conv.ptrs_to.add(this);
137                 return ret_hu_conv;
138         }
139
140         /**
141          * Constructs a new Confirm which calls the relevant methods on this_arg.
142          * This copies the `inner` pointer in this_arg and thus the returned Confirm must be freed before this_arg is
143          */
144         public Confirm as_Confirm() {
145                 long ret = bindings.ChainMonitor_as_Confirm(this.ptr);
146                 if (ret >= 0 && ret <= 4096) { return null; }
147                 Confirm ret_hu_conv = new Confirm(null, ret);
148                 ret_hu_conv.ptrs_to.add(this);
149                 return ret_hu_conv;
150         }
151
152         /**
153          * Constructs a new Watch which calls the relevant methods on this_arg.
154          * This copies the `inner` pointer in this_arg and thus the returned Watch must be freed before this_arg is
155          */
156         public Watch as_Watch() {
157                 long ret = bindings.ChainMonitor_as_Watch(this.ptr);
158                 if (ret >= 0 && ret <= 4096) { return null; }
159                 Watch ret_hu_conv = new Watch(null, ret);
160                 ret_hu_conv.ptrs_to.add(this);
161                 return ret_hu_conv;
162         }
163
164         /**
165          * Constructs a new EventsProvider which calls the relevant methods on this_arg.
166          * This copies the `inner` pointer in this_arg and thus the returned EventsProvider must be freed before this_arg is
167          */
168         public EventsProvider as_EventsProvider() {
169                 long ret = bindings.ChainMonitor_as_EventsProvider(this.ptr);
170                 if (ret >= 0 && ret <= 4096) { return null; }
171                 EventsProvider ret_hu_conv = new EventsProvider(null, ret);
172                 ret_hu_conv.ptrs_to.add(this);
173                 return ret_hu_conv;
174         }
175
176 }