1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import javax.annotation.Nullable;
11 * An Event which you should probably take some action in response to.
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).
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 {
23 if (ptr != 0) { bindings.Event_free(ptr); }
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);
30 if (raw_val.getClass() == bindings.LDKEvent.PaymentReceived.class) {
31 return new PaymentReceived(ptr, (bindings.LDKEvent.PaymentReceived)raw_val);
33 if (raw_val.getClass() == bindings.LDKEvent.PaymentSent.class) {
34 return new PaymentSent(ptr, (bindings.LDKEvent.PaymentSent)raw_val);
36 if (raw_val.getClass() == bindings.LDKEvent.PaymentFailed.class) {
37 return new PaymentFailed(ptr, (bindings.LDKEvent.PaymentFailed)raw_val);
39 if (raw_val.getClass() == bindings.LDKEvent.PendingHTLCsForwardable.class) {
40 return new PendingHTLCsForwardable(ptr, (bindings.LDKEvent.PendingHTLCsForwardable)raw_val);
42 if (raw_val.getClass() == bindings.LDKEvent.SpendableOutputs.class) {
43 return new SpendableOutputs(ptr, (bindings.LDKEvent.SpendableOutputs)raw_val);
45 if (raw_val.getClass() == bindings.LDKEvent.PaymentForwarded.class) {
46 return new PaymentForwarded(ptr, (bindings.LDKEvent.PaymentForwarded)raw_val);
48 assert false; return null; // Unreachable without extending the (internal) bindings interface
51 public final static class FundingGenerationReady extends Event {
53 * The random channel_id we picked which you'll need to pass into
54 * ChannelManager::funding_transaction_generated.
56 public final byte[] temporary_channel_id;
58 * The value, in satoshis, that the output should have.
60 public final long channel_value_satoshis;
62 * The script which should be used in the transaction output.
64 public final byte[] output_script;
66 * The value passed in to ChannelManager::create_channel
68 public final long user_channel_id;
69 private FundingGenerationReady(long ptr, bindings.LDKEvent.FundingGenerationReady obj) {
71 this.temporary_channel_id = obj.temporary_channel_id;
72 this.channel_value_satoshis = obj.channel_value_satoshis;
73 this.output_script = obj.output_script;
74 this.user_channel_id = obj.user_channel_id;
77 public final static class PaymentReceived extends Event {
79 * The hash for which the preimage should be handed to the ChannelManager.
81 public final byte[] payment_hash;
83 * The value, in thousandths of a satoshi, that this payment is for. Note that you must
84 * compare this to the expected value before accepting the payment (as otherwise you are
85 * providing proof-of-payment for less than the value you expected!).
87 public final long amt;
89 * Information for claiming this received payment, based on whether the purpose of the
90 * payment is to pay an invoice or to send a spontaneous payment.
92 public final PaymentPurpose purpose;
93 private PaymentReceived(long ptr, bindings.LDKEvent.PaymentReceived obj) {
95 this.payment_hash = obj.payment_hash;
97 long purpose = obj.purpose;
98 PaymentPurpose purpose_hu_conv = PaymentPurpose.constr_from_ptr(purpose);
99 purpose_hu_conv.ptrs_to.add(this);
100 this.purpose = purpose_hu_conv;
103 public final static class PaymentSent extends Event {
105 * The preimage to the hash given to ChannelManager::send_payment.
106 * Note that this serves as a payment receipt, if you wish to have such a thing, you must
109 public final byte[] payment_preimage;
110 private PaymentSent(long ptr, bindings.LDKEvent.PaymentSent obj) {
112 this.payment_preimage = obj.payment_preimage;
115 public final static class PaymentFailed extends Event {
117 * The hash which was given to ChannelManager::send_payment.
119 public final byte[] payment_hash;
121 * Indicates the payment was rejected for some reason by the recipient. This implies that
122 * the payment has failed, not just the route in question. If this is not set, you may
123 * retry the payment via a different route.
125 public final boolean rejected_by_dest;
126 private PaymentFailed(long ptr, bindings.LDKEvent.PaymentFailed obj) {
128 this.payment_hash = obj.payment_hash;
129 this.rejected_by_dest = obj.rejected_by_dest;
132 public final static class PendingHTLCsForwardable extends Event {
134 * The minimum amount of time that should be waited prior to calling
135 * process_pending_htlc_forwards. To increase the effort required to correlate payments,
136 * you should wait a random amount of time in roughly the range (now + time_forwardable,
137 * now + 5*time_forwardable).
139 public final long time_forwardable;
140 private PendingHTLCsForwardable(long ptr, bindings.LDKEvent.PendingHTLCsForwardable obj) {
142 this.time_forwardable = obj.time_forwardable;
145 public final static class SpendableOutputs extends Event {
147 * The outputs which you should store as spendable by you.
149 public final SpendableOutputDescriptor[] outputs;
150 private SpendableOutputs(long ptr, bindings.LDKEvent.SpendableOutputs obj) {
152 long[] outputs = obj.outputs;
153 SpendableOutputDescriptor[] outputs_conv_27_arr = new SpendableOutputDescriptor[outputs.length];
154 for (int b = 0; b < outputs.length; b++) {
155 long outputs_conv_27 = outputs[b];
156 SpendableOutputDescriptor outputs_conv_27_hu_conv = SpendableOutputDescriptor.constr_from_ptr(outputs_conv_27);
157 outputs_conv_27_hu_conv.ptrs_to.add(this);
158 outputs_conv_27_arr[b] = outputs_conv_27_hu_conv;
160 this.outputs = outputs_conv_27_arr;
163 public final static class PaymentForwarded extends Event {
165 * The fee, in milli-satoshis, which was earned as a result of the payment.
167 * Note that if we force-closed the channel over which we forwarded an HTLC while the HTLC
168 * was pending, the amount the next hop claimed will have been rounded down to the nearest
169 * whole satoshi. Thus, the fee calculated here may be higher than expected as we still
170 * claimed the full value in millisatoshis from the source. In this case,
171 * `claim_from_onchain_tx` will be set.
173 * If the channel which sent us the payment has been force-closed, we will claim the funds
174 * via an on-chain transaction. In that case we do not yet know the on-chain transaction
175 * fees which we will spend and will instead set this to `None`. It is possible duplicate
176 * `PaymentForwarded` events are generated for the same payment iff `fee_earned_msat` is
179 public final Option_u64Z fee_earned_msat;
181 * If this is `true`, the forwarded HTLC was claimed by our counterparty via an on-chain
184 public final boolean claim_from_onchain_tx;
185 private PaymentForwarded(long ptr, bindings.LDKEvent.PaymentForwarded obj) {
187 long fee_earned_msat = obj.fee_earned_msat;
188 Option_u64Z fee_earned_msat_hu_conv = Option_u64Z.constr_from_ptr(fee_earned_msat);
189 fee_earned_msat_hu_conv.ptrs_to.add(this);
190 this.fee_earned_msat = fee_earned_msat_hu_conv;
191 this.claim_from_onchain_tx = obj.claim_from_onchain_tx;
195 * Creates a copy of the Event
197 public Event clone() {
198 long ret = bindings.Event_clone(this.ptr);
199 if (ret < 1024) { return null; }
200 Event ret_hu_conv = Event.constr_from_ptr(ret);
201 ret_hu_conv.ptrs_to.add(this);
206 * Utility method to constructs a new FundingGenerationReady-variant Event
208 public static Event funding_generation_ready(byte[] temporary_channel_id, long channel_value_satoshis, byte[] output_script, long user_channel_id) {
209 long ret = bindings.Event_funding_generation_ready(temporary_channel_id, channel_value_satoshis, output_script, user_channel_id);
210 if (ret < 1024) { return null; }
211 Event ret_hu_conv = Event.constr_from_ptr(ret);
212 ret_hu_conv.ptrs_to.add(ret_hu_conv);
217 * Utility method to constructs a new PaymentReceived-variant Event
219 public static Event payment_received(byte[] payment_hash, long amt, PaymentPurpose purpose) {
220 long ret = bindings.Event_payment_received(payment_hash, amt, purpose.ptr);
221 if (ret < 1024) { return null; }
222 Event ret_hu_conv = Event.constr_from_ptr(ret);
223 ret_hu_conv.ptrs_to.add(ret_hu_conv);
228 * Utility method to constructs a new PaymentSent-variant Event
230 public static Event payment_sent(byte[] payment_preimage) {
231 long ret = bindings.Event_payment_sent(payment_preimage);
232 if (ret < 1024) { return null; }
233 Event ret_hu_conv = Event.constr_from_ptr(ret);
234 ret_hu_conv.ptrs_to.add(ret_hu_conv);
239 * Utility method to constructs a new PaymentFailed-variant Event
241 public static Event payment_failed(byte[] payment_hash, boolean rejected_by_dest) {
242 long ret = bindings.Event_payment_failed(payment_hash, rejected_by_dest);
243 if (ret < 1024) { return null; }
244 Event ret_hu_conv = Event.constr_from_ptr(ret);
245 ret_hu_conv.ptrs_to.add(ret_hu_conv);
250 * Utility method to constructs a new PendingHTLCsForwardable-variant Event
252 public static Event pending_htlcs_forwardable(long time_forwardable) {
253 long ret = bindings.Event_pending_htlcs_forwardable(time_forwardable);
254 if (ret < 1024) { return null; }
255 Event ret_hu_conv = Event.constr_from_ptr(ret);
256 ret_hu_conv.ptrs_to.add(ret_hu_conv);
261 * Utility method to constructs a new SpendableOutputs-variant Event
263 public static Event spendable_outputs(SpendableOutputDescriptor[] outputs) {
264 long ret = bindings.Event_spendable_outputs(outputs != null ? Arrays.stream(outputs).mapToLong(outputs_conv_27 -> outputs_conv_27.ptr).toArray() : null);
265 if (ret < 1024) { return null; }
266 Event ret_hu_conv = Event.constr_from_ptr(ret);
267 ret_hu_conv.ptrs_to.add(ret_hu_conv);
268 /* TODO 2 SpendableOutputDescriptor */;
273 * Utility method to constructs a new PaymentForwarded-variant Event
275 public static Event payment_forwarded(Option_u64Z fee_earned_msat, boolean claim_from_onchain_tx) {
276 long ret = bindings.Event_payment_forwarded(fee_earned_msat.ptr, claim_from_onchain_tx);
277 if (ret < 1024) { return null; }
278 Event ret_hu_conv = Event.constr_from_ptr(ret);
279 ret_hu_conv.ptrs_to.add(ret_hu_conv);
284 * Serialize the Event object into a byte array which can be read by Event_read
286 public byte[] write() {
287 byte[] ret = bindings.Event_write(this.ptr);