Merge pull request #47 from TheBlueMatt/main
[ldk-java] / src / main / java / org / ldk / structs / Event.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 Event which you should probably take some action in response to.
12  * 
13  * Note that while Writeable and Readable are implemented for Event, you probably shouldn't use
14  * them directly as they don't round-trip exactly (for example FundingGenerationReady is never
15  * written as it makes no sense to respond to it after reconnecting to peers).
16  */
17 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
18 public class Event extends CommonBase {
19         private Event(Object _dummy, long ptr) { super(ptr); }
20         @Override @SuppressWarnings("deprecation")
21         protected void finalize() throws Throwable {
22                 super.finalize();
23                 if (ptr != 0) { bindings.Event_free(ptr); }
24         }
25         static Event constr_from_ptr(long ptr) {
26                 bindings.LDKEvent raw_val = bindings.LDKEvent_ref_from_ptr(ptr);
27                 if (raw_val.getClass() == bindings.LDKEvent.FundingGenerationReady.class) {
28                         return new FundingGenerationReady(ptr, (bindings.LDKEvent.FundingGenerationReady)raw_val);
29                 }
30                 if (raw_val.getClass() == bindings.LDKEvent.PaymentReceived.class) {
31                         return new PaymentReceived(ptr, (bindings.LDKEvent.PaymentReceived)raw_val);
32                 }
33                 if (raw_val.getClass() == bindings.LDKEvent.PaymentSent.class) {
34                         return new PaymentSent(ptr, (bindings.LDKEvent.PaymentSent)raw_val);
35                 }
36                 if (raw_val.getClass() == bindings.LDKEvent.PaymentPathFailed.class) {
37                         return new PaymentPathFailed(ptr, (bindings.LDKEvent.PaymentPathFailed)raw_val);
38                 }
39                 if (raw_val.getClass() == bindings.LDKEvent.PendingHTLCsForwardable.class) {
40                         return new PendingHTLCsForwardable(ptr, (bindings.LDKEvent.PendingHTLCsForwardable)raw_val);
41                 }
42                 if (raw_val.getClass() == bindings.LDKEvent.SpendableOutputs.class) {
43                         return new SpendableOutputs(ptr, (bindings.LDKEvent.SpendableOutputs)raw_val);
44                 }
45                 if (raw_val.getClass() == bindings.LDKEvent.PaymentForwarded.class) {
46                         return new PaymentForwarded(ptr, (bindings.LDKEvent.PaymentForwarded)raw_val);
47                 }
48                 if (raw_val.getClass() == bindings.LDKEvent.ChannelClosed.class) {
49                         return new ChannelClosed(ptr, (bindings.LDKEvent.ChannelClosed)raw_val);
50                 }
51                 assert false; return null; // Unreachable without extending the (internal) bindings interface
52         }
53
54         public final static class FundingGenerationReady extends Event {
55                 /**
56                  * The random channel_id we picked which you'll need to pass into
57                  * ChannelManager::funding_transaction_generated.
58                 */
59                 public final byte[] temporary_channel_id;
60                 /**
61                  * The value, in satoshis, that the output should have.
62                 */
63                 public final long channel_value_satoshis;
64                 /**
65                  * The script which should be used in the transaction output.
66                 */
67                 public final byte[] output_script;
68                 /**
69                  * The value passed in to ChannelManager::create_channel
70                 */
71                 public final long user_channel_id;
72                 private FundingGenerationReady(long ptr, bindings.LDKEvent.FundingGenerationReady obj) {
73                         super(null, ptr);
74                         this.temporary_channel_id = obj.temporary_channel_id;
75                         this.channel_value_satoshis = obj.channel_value_satoshis;
76                         this.output_script = obj.output_script;
77                         this.user_channel_id = obj.user_channel_id;
78                 }
79         }
80         public final static class PaymentReceived extends Event {
81                 /**
82                  * The hash for which the preimage should be handed to the ChannelManager.
83                 */
84                 public final byte[] payment_hash;
85                 /**
86                  * The value, in thousandths of a satoshi, that this payment is for. Note that you must
87                  * compare this to the expected value before accepting the payment (as otherwise you are
88                  * providing proof-of-payment for less than the value you expected!).
89                 */
90                 public final long amt;
91                 /**
92                  * Information for claiming this received payment, based on whether the purpose of the
93                  * payment is to pay an invoice or to send a spontaneous payment.
94                 */
95                 public final PaymentPurpose purpose;
96                 private PaymentReceived(long ptr, bindings.LDKEvent.PaymentReceived obj) {
97                         super(null, ptr);
98                         this.payment_hash = obj.payment_hash;
99                         this.amt = obj.amt;
100                         long purpose = obj.purpose;
101                         PaymentPurpose purpose_hu_conv = PaymentPurpose.constr_from_ptr(purpose);
102                         purpose_hu_conv.ptrs_to.add(this);
103                         this.purpose = purpose_hu_conv;
104                 }
105         }
106         public final static class PaymentSent extends Event {
107                 /**
108                  * The preimage to the hash given to ChannelManager::send_payment.
109                  * Note that this serves as a payment receipt, if you wish to have such a thing, you must
110                  * store it somehow!
111                 */
112                 public final byte[] payment_preimage;
113                 private PaymentSent(long ptr, bindings.LDKEvent.PaymentSent obj) {
114                         super(null, ptr);
115                         this.payment_preimage = obj.payment_preimage;
116                 }
117         }
118         public final static class PaymentPathFailed extends Event {
119                 /**
120                  * The hash which was given to ChannelManager::send_payment.
121                 */
122                 public final byte[] payment_hash;
123                 /**
124                  * Indicates the payment was rejected for some reason by the recipient. This implies that
125                  * the payment has failed, not just the route in question. If this is not set, you may
126                  * retry the payment via a different route.
127                 */
128                 public final boolean rejected_by_dest;
129                 /**
130                  * Any failure information conveyed via the Onion return packet by a node along the failed
131                  * payment route.
132                  * 
133                  * Should be applied to the [`NetworkGraph`] so that routing decisions can take into
134                  * account the update. [`NetGraphMsgHandler`] is capable of doing this.
135                  * 
136                  * [`NetworkGraph`]: crate::routing::network_graph::NetworkGraph
137                  * [`NetGraphMsgHandler`]: crate::routing::network_graph::NetGraphMsgHandler
138                 */
139                 public final Option_NetworkUpdateZ network_update;
140                 /**
141                  * For both single-path and multi-path payments, this is set if all paths of the payment have
142                  * failed. This will be set to false if (1) this is an MPP payment and (2) other parts of the
143                  * larger MPP payment were still in flight when this event was generated.
144                 */
145                 public final boolean all_paths_failed;
146                 /**
147                  * The payment path that failed.
148                 */
149                 public final RouteHop[] path;
150                 private PaymentPathFailed(long ptr, bindings.LDKEvent.PaymentPathFailed obj) {
151                         super(null, ptr);
152                         this.payment_hash = obj.payment_hash;
153                         this.rejected_by_dest = obj.rejected_by_dest;
154                         long network_update = obj.network_update;
155                         Option_NetworkUpdateZ network_update_hu_conv = Option_NetworkUpdateZ.constr_from_ptr(network_update);
156                         network_update_hu_conv.ptrs_to.add(this);
157                         this.network_update = network_update_hu_conv;
158                         this.all_paths_failed = obj.all_paths_failed;
159                         long[] path = obj.path;
160                         RouteHop[] path_conv_10_arr = new RouteHop[path.length];
161                         for (int k = 0; k < path.length; k++) {
162                                 long path_conv_10 = path[k];
163                                 RouteHop path_conv_10_hu_conv = new RouteHop(null, path_conv_10);
164                                 path_conv_10_hu_conv.ptrs_to.add(this);
165                                 path_conv_10_arr[k] = path_conv_10_hu_conv;
166                         }
167                         this.path = path_conv_10_arr;
168                 }
169         }
170         public final static class PendingHTLCsForwardable extends Event {
171                 /**
172                  * The minimum amount of time that should be waited prior to calling
173                  * process_pending_htlc_forwards. To increase the effort required to correlate payments,
174                  * you should wait a random amount of time in roughly the range (now + time_forwardable,
175                  * now + 5*time_forwardable).
176                 */
177                 public final long time_forwardable;
178                 private PendingHTLCsForwardable(long ptr, bindings.LDKEvent.PendingHTLCsForwardable obj) {
179                         super(null, ptr);
180                         this.time_forwardable = obj.time_forwardable;
181                 }
182         }
183         public final static class SpendableOutputs extends Event {
184                 /**
185                  * The outputs which you should store as spendable by you.
186                 */
187                 public final SpendableOutputDescriptor[] outputs;
188                 private SpendableOutputs(long ptr, bindings.LDKEvent.SpendableOutputs obj) {
189                         super(null, ptr);
190                         long[] outputs = obj.outputs;
191                         SpendableOutputDescriptor[] outputs_conv_27_arr = new SpendableOutputDescriptor[outputs.length];
192                         for (int b = 0; b < outputs.length; b++) {
193                                 long outputs_conv_27 = outputs[b];
194                                 SpendableOutputDescriptor outputs_conv_27_hu_conv = SpendableOutputDescriptor.constr_from_ptr(outputs_conv_27);
195                                 outputs_conv_27_hu_conv.ptrs_to.add(this);
196                                 outputs_conv_27_arr[b] = outputs_conv_27_hu_conv;
197                         }
198                         this.outputs = outputs_conv_27_arr;
199                 }
200         }
201         public final static class PaymentForwarded extends Event {
202                 /**
203                  * The fee, in milli-satoshis, which was earned as a result of the payment.
204                  * 
205                  * Note that if we force-closed the channel over which we forwarded an HTLC while the HTLC
206                  * was pending, the amount the next hop claimed will have been rounded down to the nearest
207                  * whole satoshi. Thus, the fee calculated here may be higher than expected as we still
208                  * claimed the full value in millisatoshis from the source. In this case,
209                  * `claim_from_onchain_tx` will be set.
210                  * 
211                  * If the channel which sent us the payment has been force-closed, we will claim the funds
212                  * via an on-chain transaction. In that case we do not yet know the on-chain transaction
213                  * fees which we will spend and will instead set this to `None`. It is possible duplicate
214                  * `PaymentForwarded` events are generated for the same payment iff `fee_earned_msat` is
215                  * `None`.
216                 */
217                 public final Option_u64Z fee_earned_msat;
218                 /**
219                  * If this is `true`, the forwarded HTLC was claimed by our counterparty via an on-chain
220                  * transaction.
221                 */
222                 public final boolean claim_from_onchain_tx;
223                 private PaymentForwarded(long ptr, bindings.LDKEvent.PaymentForwarded obj) {
224                         super(null, ptr);
225                         long fee_earned_msat = obj.fee_earned_msat;
226                         Option_u64Z fee_earned_msat_hu_conv = Option_u64Z.constr_from_ptr(fee_earned_msat);
227                         fee_earned_msat_hu_conv.ptrs_to.add(this);
228                         this.fee_earned_msat = fee_earned_msat_hu_conv;
229                         this.claim_from_onchain_tx = obj.claim_from_onchain_tx;
230                 }
231         }
232         public final static class ChannelClosed extends Event {
233                 /**
234                  * The channel_id of the channel which has been closed. Note that on-chain transactions
235                  * resolving the channel are likely still awaiting confirmation.
236                 */
237                 public final byte[] channel_id;
238                 /**
239                  * The reason the channel was closed.
240                 */
241                 public final ClosureReason reason;
242                 private ChannelClosed(long ptr, bindings.LDKEvent.ChannelClosed obj) {
243                         super(null, ptr);
244                         this.channel_id = obj.channel_id;
245                         long reason = obj.reason;
246                         ClosureReason reason_hu_conv = ClosureReason.constr_from_ptr(reason);
247                         reason_hu_conv.ptrs_to.add(this);
248                         this.reason = reason_hu_conv;
249                 }
250         }
251         /**
252          * Creates a copy of the Event
253          */
254         public Event clone() {
255                 long ret = bindings.Event_clone(this.ptr);
256                 if (ret < 1024) { return null; }
257                 Event ret_hu_conv = Event.constr_from_ptr(ret);
258                 ret_hu_conv.ptrs_to.add(this);
259                 return ret_hu_conv;
260         }
261
262         /**
263          * Utility method to constructs a new FundingGenerationReady-variant Event
264          */
265         public static Event funding_generation_ready(byte[] temporary_channel_id, long channel_value_satoshis, byte[] output_script, long user_channel_id) {
266                 long ret = bindings.Event_funding_generation_ready(temporary_channel_id, channel_value_satoshis, output_script, user_channel_id);
267                 if (ret < 1024) { return null; }
268                 Event ret_hu_conv = Event.constr_from_ptr(ret);
269                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
270                 return ret_hu_conv;
271         }
272
273         /**
274          * Utility method to constructs a new PaymentReceived-variant Event
275          */
276         public static Event payment_received(byte[] payment_hash, long amt, PaymentPurpose purpose) {
277                 long ret = bindings.Event_payment_received(payment_hash, amt, purpose.ptr);
278                 if (ret < 1024) { return null; }
279                 Event ret_hu_conv = Event.constr_from_ptr(ret);
280                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
281                 return ret_hu_conv;
282         }
283
284         /**
285          * Utility method to constructs a new PaymentSent-variant Event
286          */
287         public static Event payment_sent(byte[] payment_preimage) {
288                 long ret = bindings.Event_payment_sent(payment_preimage);
289                 if (ret < 1024) { return null; }
290                 Event ret_hu_conv = Event.constr_from_ptr(ret);
291                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
292                 return ret_hu_conv;
293         }
294
295         /**
296          * Utility method to constructs a new PaymentPathFailed-variant Event
297          */
298         public static Event payment_path_failed(byte[] payment_hash, boolean rejected_by_dest, Option_NetworkUpdateZ network_update, boolean all_paths_failed, RouteHop[] path) {
299                 long ret = bindings.Event_payment_path_failed(payment_hash, rejected_by_dest, network_update.ptr, all_paths_failed, path != null ? Arrays.stream(path).mapToLong(path_conv_10 -> path_conv_10 == null ? 0 : path_conv_10.ptr & ~1).toArray() : null);
300                 if (ret < 1024) { return null; }
301                 Event ret_hu_conv = Event.constr_from_ptr(ret);
302                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
303                 return ret_hu_conv;
304         }
305
306         /**
307          * Utility method to constructs a new PendingHTLCsForwardable-variant Event
308          */
309         public static Event pending_htlcs_forwardable(long time_forwardable) {
310                 long ret = bindings.Event_pending_htlcs_forwardable(time_forwardable);
311                 if (ret < 1024) { return null; }
312                 Event ret_hu_conv = Event.constr_from_ptr(ret);
313                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
314                 return ret_hu_conv;
315         }
316
317         /**
318          * Utility method to constructs a new SpendableOutputs-variant Event
319          */
320         public static Event spendable_outputs(SpendableOutputDescriptor[] outputs) {
321                 long ret = bindings.Event_spendable_outputs(outputs != null ? Arrays.stream(outputs).mapToLong(outputs_conv_27 -> outputs_conv_27.ptr).toArray() : null);
322                 if (ret < 1024) { return null; }
323                 Event ret_hu_conv = Event.constr_from_ptr(ret);
324                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
325                 return ret_hu_conv;
326         }
327
328         /**
329          * Utility method to constructs a new PaymentForwarded-variant Event
330          */
331         public static Event payment_forwarded(Option_u64Z fee_earned_msat, boolean claim_from_onchain_tx) {
332                 long ret = bindings.Event_payment_forwarded(fee_earned_msat.ptr, claim_from_onchain_tx);
333                 if (ret < 1024) { return null; }
334                 Event ret_hu_conv = Event.constr_from_ptr(ret);
335                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
336                 return ret_hu_conv;
337         }
338
339         /**
340          * Utility method to constructs a new ChannelClosed-variant Event
341          */
342         public static Event channel_closed(byte[] channel_id, ClosureReason reason) {
343                 long ret = bindings.Event_channel_closed(channel_id, reason.ptr);
344                 if (ret < 1024) { return null; }
345                 Event ret_hu_conv = Event.constr_from_ptr(ret);
346                 ret_hu_conv.ptrs_to.add(ret_hu_conv);
347                 return ret_hu_conv;
348         }
349
350         /**
351          * Serialize the Event object into a byte array which can be read by Event_read
352          */
353         public byte[] write() {
354                 byte[] ret = bindings.Event_write(this.ptr);
355                 return ret;
356         }
357
358 }