[TS] Slightly improve logging in TS trait calls that fail
[ldk-java] / src / main / java / org / ldk / structs / Payer.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 java.lang.ref.Reference;
8 import javax.annotation.Nullable;
9
10 /**
11  * A trait defining behavior of an [`Invoice`] payer.
12  * 
13  * While the behavior of [`InvoicePayer`] provides idempotency of duplicate `send_*payment` calls
14  * with the same [`PaymentHash`], it is up to the `Payer` to provide idempotency across restarts.
15  * 
16  * [`ChannelManager`] provides idempotency for duplicate payments with the same [`PaymentId`].
17  * 
18  * In order to trivially ensure idempotency for payments, the default `Payer` implementation
19  * reuses the [`PaymentHash`] bytes as the [`PaymentId`]. Custom implementations wishing to
20  * provide payment idempotency with a different idempotency key (i.e. [`PaymentId`]) should map
21  * the [`Invoice`] or spontaneous payment target pubkey to their own idempotency key.
22  * 
23  * [`ChannelManager`]: lightning::ln::channelmanager::ChannelManager
24  */
25 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
26 public class Payer extends CommonBase {
27         final bindings.LDKPayer bindings_instance;
28         Payer(Object _dummy, long ptr) { super(ptr); bindings_instance = null; }
29         private Payer(bindings.LDKPayer arg) {
30                 super(bindings.LDKPayer_new(arg));
31                 this.ptrs_to.add(arg);
32                 this.bindings_instance = arg;
33         }
34         @Override @SuppressWarnings("deprecation")
35         protected void finalize() throws Throwable {
36                 if (ptr != 0) { bindings.Payer_free(ptr); } super.finalize();
37         }
38
39         public static interface PayerInterface {
40                 /**
41                  * Returns the payer's node id.
42                  */
43                 byte[] node_id();
44                 /**
45                  * Returns the payer's channels.
46                  */
47                 ChannelDetails[] first_hops();
48                 /**
49                  * Sends a payment over the Lightning Network using the given [`Route`].
50                  * 
51                  * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
52                  */
53                 Result_NonePaymentSendFailureZ send_payment(Route route, byte[] payment_hash, byte[] payment_secret, byte[] payment_id);
54                 /**
55                  * Sends a spontaneous payment over the Lightning Network using the given [`Route`].
56                  */
57                 Result_NonePaymentSendFailureZ send_spontaneous_payment(Route route, byte[] payment_preimage, byte[] payment_id);
58                 /**
59                  * Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
60                  */
61                 Result_NonePaymentSendFailureZ retry_payment(Route route, byte[] payment_id);
62                 /**
63                  * Signals that no further retries for the given payment will occur.
64                  */
65                 void abandon_payment(byte[] payment_id);
66                 /**
67                  * Construct an [`InFlightHtlcs`] containing information about currently used up liquidity
68                  * across payments.
69                  */
70                 InFlightHtlcs inflight_htlcs();
71         }
72         private static class LDKPayerHolder { Payer held; }
73         public static Payer new_impl(PayerInterface arg) {
74                 final LDKPayerHolder impl_holder = new LDKPayerHolder();
75                 impl_holder.held = new Payer(new bindings.LDKPayer() {
76                         @Override public byte[] node_id() {
77                                 byte[] ret = arg.node_id();
78                                 Reference.reachabilityFence(arg);
79                                 byte[] result = InternalUtils.check_arr_len(ret, 33);
80                                 return result;
81                         }
82                         @Override public long[] first_hops() {
83                                 ChannelDetails[] ret = arg.first_hops();
84                                 Reference.reachabilityFence(arg);
85                                 long[] result = ret != null ? Arrays.stream(ret).mapToLong(ret_conv_16 -> ret_conv_16 == null ? 0 : ret_conv_16.clone_ptr()).toArray() : null;
86                                 return result;
87                         }
88                         @Override public long send_payment(long route, byte[] payment_hash, byte[] payment_secret, byte[] payment_id) {
89                                 org.ldk.structs.Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new org.ldk.structs.Route(null, route); }
90                                 Result_NonePaymentSendFailureZ ret = arg.send_payment(route_hu_conv, payment_hash, payment_secret, payment_id);
91                                 Reference.reachabilityFence(arg);
92                                 long result = ret == null ? 0 : ret.clone_ptr();
93                                 return result;
94                         }
95                         @Override public long send_spontaneous_payment(long route, byte[] payment_preimage, byte[] payment_id) {
96                                 org.ldk.structs.Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new org.ldk.structs.Route(null, route); }
97                                 Result_NonePaymentSendFailureZ ret = arg.send_spontaneous_payment(route_hu_conv, payment_preimage, payment_id);
98                                 Reference.reachabilityFence(arg);
99                                 long result = ret == null ? 0 : ret.clone_ptr();
100                                 return result;
101                         }
102                         @Override public long retry_payment(long route, byte[] payment_id) {
103                                 org.ldk.structs.Route route_hu_conv = null; if (route < 0 || route > 4096) { route_hu_conv = new org.ldk.structs.Route(null, route); }
104                                 Result_NonePaymentSendFailureZ ret = arg.retry_payment(route_hu_conv, payment_id);
105                                 Reference.reachabilityFence(arg);
106                                 long result = ret == null ? 0 : ret.clone_ptr();
107                                 return result;
108                         }
109                         @Override public void abandon_payment(byte[] payment_id) {
110                                 arg.abandon_payment(payment_id);
111                                 Reference.reachabilityFence(arg);
112                         }
113                         @Override public long inflight_htlcs() {
114                                 InFlightHtlcs ret = arg.inflight_htlcs();
115                                 Reference.reachabilityFence(arg);
116                                 long result = ret == null ? 0 : ret.clone_ptr();
117                                 return result;
118                         }
119                 });
120                 return impl_holder.held;
121         }
122         /**
123          * Returns the payer's node id.
124          */
125         public byte[] node_id() {
126                 byte[] ret = bindings.Payer_node_id(this.ptr);
127                 Reference.reachabilityFence(this);
128                 return ret;
129         }
130
131         /**
132          * Returns the payer's channels.
133          */
134         public ChannelDetails[] first_hops() {
135                 long[] ret = bindings.Payer_first_hops(this.ptr);
136                 Reference.reachabilityFence(this);
137                 int ret_conv_16_len = ret.length;
138                 ChannelDetails[] ret_conv_16_arr = new ChannelDetails[ret_conv_16_len];
139                 for (int q = 0; q < ret_conv_16_len; q++) {
140                         long ret_conv_16 = ret[q];
141                         org.ldk.structs.ChannelDetails ret_conv_16_hu_conv = null; if (ret_conv_16 < 0 || ret_conv_16 > 4096) { ret_conv_16_hu_conv = new org.ldk.structs.ChannelDetails(null, ret_conv_16); }
142                         if (ret_conv_16_hu_conv != null) { ret_conv_16_hu_conv.ptrs_to.add(this); };
143                         ret_conv_16_arr[q] = ret_conv_16_hu_conv;
144                 }
145                 return ret_conv_16_arr;
146         }
147
148         /**
149          * Sends a payment over the Lightning Network using the given [`Route`].
150          * 
151          * Note that payment_secret (or a relevant inner pointer) may be NULL or all-0s to represent None
152          */
153         public Result_NonePaymentSendFailureZ send_payment(org.ldk.structs.Route route, byte[] payment_hash, @Nullable byte[] payment_secret, byte[] payment_id) {
154                 long ret = bindings.Payer_send_payment(this.ptr, route == null ? 0 : route.ptr, InternalUtils.check_arr_len(payment_hash, 32), InternalUtils.check_arr_len(payment_secret, 32), InternalUtils.check_arr_len(payment_id, 32));
155                 Reference.reachabilityFence(this);
156                 Reference.reachabilityFence(route);
157                 Reference.reachabilityFence(payment_hash);
158                 Reference.reachabilityFence(payment_secret);
159                 Reference.reachabilityFence(payment_id);
160                 if (ret >= 0 && ret <= 4096) { return null; }
161                 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
162                 if (this != null) { this.ptrs_to.add(route); };
163                 return ret_hu_conv;
164         }
165
166         /**
167          * Sends a spontaneous payment over the Lightning Network using the given [`Route`].
168          */
169         public Result_NonePaymentSendFailureZ send_spontaneous_payment(org.ldk.structs.Route route, byte[] payment_preimage, byte[] payment_id) {
170                 long ret = bindings.Payer_send_spontaneous_payment(this.ptr, route == null ? 0 : route.ptr, InternalUtils.check_arr_len(payment_preimage, 32), InternalUtils.check_arr_len(payment_id, 32));
171                 Reference.reachabilityFence(this);
172                 Reference.reachabilityFence(route);
173                 Reference.reachabilityFence(payment_preimage);
174                 Reference.reachabilityFence(payment_id);
175                 if (ret >= 0 && ret <= 4096) { return null; }
176                 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
177                 if (this != null) { this.ptrs_to.add(route); };
178                 return ret_hu_conv;
179         }
180
181         /**
182          * Retries a failed payment path for the [`PaymentId`] using the given [`Route`].
183          */
184         public Result_NonePaymentSendFailureZ retry_payment(org.ldk.structs.Route route, byte[] payment_id) {
185                 long ret = bindings.Payer_retry_payment(this.ptr, route == null ? 0 : route.ptr, InternalUtils.check_arr_len(payment_id, 32));
186                 Reference.reachabilityFence(this);
187                 Reference.reachabilityFence(route);
188                 Reference.reachabilityFence(payment_id);
189                 if (ret >= 0 && ret <= 4096) { return null; }
190                 Result_NonePaymentSendFailureZ ret_hu_conv = Result_NonePaymentSendFailureZ.constr_from_ptr(ret);
191                 if (this != null) { this.ptrs_to.add(route); };
192                 return ret_hu_conv;
193         }
194
195         /**
196          * Signals that no further retries for the given payment will occur.
197          */
198         public void abandon_payment(byte[] payment_id) {
199                 bindings.Payer_abandon_payment(this.ptr, InternalUtils.check_arr_len(payment_id, 32));
200                 Reference.reachabilityFence(this);
201                 Reference.reachabilityFence(payment_id);
202         }
203
204         /**
205          * Construct an [`InFlightHtlcs`] containing information about currently used up liquidity
206          * across payments.
207          */
208         public InFlightHtlcs inflight_htlcs() {
209                 long ret = bindings.Payer_inflight_htlcs(this.ptr);
210                 Reference.reachabilityFence(this);
211                 if (ret >= 0 && ret <= 4096) { return null; }
212                 org.ldk.structs.InFlightHtlcs ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InFlightHtlcs(null, ret); }
213                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
214                 return ret_hu_conv;
215         }
216
217 }