[TS] Slightly improve logging in TS trait calls that fail
[ldk-java] / src / main / java / org / ldk / structs / InvoicePayer.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 /**
12  * A utility for paying [`Invoice`]s and sending spontaneous payments.
13  * 
14  * See [module-level documentation] for details.
15  * 
16  * [module-level documentation]: crate::payment
17  */
18 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
19 public class InvoicePayer extends CommonBase {
20         InvoicePayer(Object _dummy, long ptr) { super(ptr); }
21         @Override @SuppressWarnings("deprecation")
22         protected void finalize() throws Throwable {
23                 super.finalize();
24                 if (ptr != 0) { bindings.InvoicePayer_free(ptr); }
25         }
26
27         /**
28          * Creates an invoice payer that retries failed payment paths.
29          * 
30          * Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
31          * `retry` has been exceeded for a given [`Invoice`].
32          */
33         public static InvoicePayer of(org.ldk.structs.Payer payer, org.ldk.structs.Router router, org.ldk.structs.Logger logger, org.ldk.structs.EventHandler event_handler, org.ldk.structs.Retry retry) {
34                 long ret = bindings.InvoicePayer_new(payer == null ? 0 : payer.ptr, router == null ? 0 : router.ptr, logger == null ? 0 : logger.ptr, event_handler == null ? 0 : event_handler.ptr, retry.ptr);
35                 Reference.reachabilityFence(payer);
36                 Reference.reachabilityFence(router);
37                 Reference.reachabilityFence(logger);
38                 Reference.reachabilityFence(event_handler);
39                 Reference.reachabilityFence(retry);
40                 if (ret >= 0 && ret <= 4096) { return null; }
41                 org.ldk.structs.InvoicePayer ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new org.ldk.structs.InvoicePayer(null, ret); }
42                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(ret_hu_conv); };
43                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(payer); };
44                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(router); };
45                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(logger); };
46                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(event_handler); };
47                 return ret_hu_conv;
48         }
49
50         /**
51          * Pays the given [`Invoice`], caching it for later use in case a retry is needed.
52          * 
53          * [`Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
54          * as the payment is still pending. Once the payment completes or fails, you must ensure that
55          * a second payment with the same [`PaymentHash`] is never sent.
56          * 
57          * If you wish to use a different payment idempotency token, see
58          * [`Self::pay_invoice_with_id`].
59          */
60         public Result_PaymentIdPaymentErrorZ pay_invoice(org.ldk.structs.Invoice invoice) {
61                 long ret = bindings.InvoicePayer_pay_invoice(this.ptr, invoice == null ? 0 : invoice.ptr);
62                 Reference.reachabilityFence(this);
63                 Reference.reachabilityFence(invoice);
64                 if (ret >= 0 && ret <= 4096) { return null; }
65                 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
66                 if (this != null) { this.ptrs_to.add(invoice); };
67                 return ret_hu_conv;
68         }
69
70         /**
71          * Pays the given [`Invoice`] with a custom idempotency key, caching the invoice for later use
72          * in case a retry is needed.
73          * 
74          * Note that idempotency is only guaranteed as long as the payment is still pending. Once the
75          * payment completes or fails, no idempotency guarantees are made.
76          * 
77          * You should ensure that the [`Invoice::payment_hash`] is unique and the same [`PaymentHash`]
78          * has never been paid before.
79          * 
80          * See [`Self::pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency
81          * token.
82          */
83         public Result_NonePaymentErrorZ pay_invoice_with_id(org.ldk.structs.Invoice invoice, byte[] payment_id) {
84                 long ret = bindings.InvoicePayer_pay_invoice_with_id(this.ptr, invoice == null ? 0 : invoice.ptr, InternalUtils.check_arr_len(payment_id, 32));
85                 Reference.reachabilityFence(this);
86                 Reference.reachabilityFence(invoice);
87                 Reference.reachabilityFence(payment_id);
88                 if (ret >= 0 && ret <= 4096) { return null; }
89                 Result_NonePaymentErrorZ ret_hu_conv = Result_NonePaymentErrorZ.constr_from_ptr(ret);
90                 if (this != null) { this.ptrs_to.add(invoice); };
91                 return ret_hu_conv;
92         }
93
94         /**
95          * Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
96          * case a retry is needed.
97          * 
98          * [`Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
99          * as the payment is still pending. Once the payment completes or fails, you must ensure that
100          * a second payment with the same [`PaymentHash`] is never sent.
101          * 
102          * If you wish to use a different payment idempotency token, see
103          * [`Self::pay_zero_value_invoice_with_id`].
104          */
105         public Result_PaymentIdPaymentErrorZ pay_zero_value_invoice(org.ldk.structs.Invoice invoice, long amount_msats) {
106                 long ret = bindings.InvoicePayer_pay_zero_value_invoice(this.ptr, invoice == null ? 0 : invoice.ptr, amount_msats);
107                 Reference.reachabilityFence(this);
108                 Reference.reachabilityFence(invoice);
109                 Reference.reachabilityFence(amount_msats);
110                 if (ret >= 0 && ret <= 4096) { return null; }
111                 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
112                 if (this != null) { this.ptrs_to.add(invoice); };
113                 return ret_hu_conv;
114         }
115
116         /**
117          * Pays the given zero-value [`Invoice`] using the given amount and custom idempotency key,
118          * caching the invoice for later use in case a retry is needed.
119          * 
120          * Note that idempotency is only guaranteed as long as the payment is still pending. Once the
121          * payment completes or fails, no idempotency guarantees are made.
122          * 
123          * You should ensure that the [`Invoice::payment_hash`] is unique and the same [`PaymentHash`]
124          * has never been paid before.
125          * 
126          * See [`Self::pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
127          * idempotency token.
128          */
129         public Result_NonePaymentErrorZ pay_zero_value_invoice_with_id(org.ldk.structs.Invoice invoice, long amount_msats, byte[] payment_id) {
130                 long ret = bindings.InvoicePayer_pay_zero_value_invoice_with_id(this.ptr, invoice == null ? 0 : invoice.ptr, amount_msats, InternalUtils.check_arr_len(payment_id, 32));
131                 Reference.reachabilityFence(this);
132                 Reference.reachabilityFence(invoice);
133                 Reference.reachabilityFence(amount_msats);
134                 Reference.reachabilityFence(payment_id);
135                 if (ret >= 0 && ret <= 4096) { return null; }
136                 Result_NonePaymentErrorZ ret_hu_conv = Result_NonePaymentErrorZ.constr_from_ptr(ret);
137                 if (this != null) { this.ptrs_to.add(invoice); };
138                 return ret_hu_conv;
139         }
140
141         /**
142          * Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
143          * case a retry is needed.
144          * 
145          * The hash of the [`PaymentPreimage`] is used as the [`PaymentId`], which ensures idempotency
146          * as long as the payment is still pending. Once the payment completes or fails, you must
147          * ensure that a second payment with the same [`PaymentPreimage`] is never sent.
148          */
149         public Result_PaymentIdPaymentErrorZ pay_pubkey(byte[] pubkey, byte[] payment_preimage, long amount_msats, int final_cltv_expiry_delta) {
150                 long ret = bindings.InvoicePayer_pay_pubkey(this.ptr, InternalUtils.check_arr_len(pubkey, 33), InternalUtils.check_arr_len(payment_preimage, 32), amount_msats, final_cltv_expiry_delta);
151                 Reference.reachabilityFence(this);
152                 Reference.reachabilityFence(pubkey);
153                 Reference.reachabilityFence(payment_preimage);
154                 Reference.reachabilityFence(amount_msats);
155                 Reference.reachabilityFence(final_cltv_expiry_delta);
156                 if (ret >= 0 && ret <= 4096) { return null; }
157                 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
158                 return ret_hu_conv;
159         }
160
161         /**
162          * Pays `pubkey` an amount using the hash of the given preimage and a custom idempotency key,
163          * caching the invoice for later use in case a retry is needed.
164          * 
165          * Note that idempotency is only guaranteed as long as the payment is still pending. Once the
166          * payment completes or fails, no idempotency guarantees are made.
167          * 
168          * You should ensure that the [`PaymentPreimage`] is unique and the corresponding
169          * [`PaymentHash`] has never been paid before.
170          */
171         public Result_NonePaymentErrorZ pay_pubkey_with_id(byte[] pubkey, byte[] payment_preimage, byte[] payment_id, long amount_msats, int final_cltv_expiry_delta) {
172                 long ret = bindings.InvoicePayer_pay_pubkey_with_id(this.ptr, InternalUtils.check_arr_len(pubkey, 33), InternalUtils.check_arr_len(payment_preimage, 32), InternalUtils.check_arr_len(payment_id, 32), amount_msats, final_cltv_expiry_delta);
173                 Reference.reachabilityFence(this);
174                 Reference.reachabilityFence(pubkey);
175                 Reference.reachabilityFence(payment_preimage);
176                 Reference.reachabilityFence(payment_id);
177                 Reference.reachabilityFence(amount_msats);
178                 Reference.reachabilityFence(final_cltv_expiry_delta);
179                 if (ret >= 0 && ret <= 4096) { return null; }
180                 Result_NonePaymentErrorZ ret_hu_conv = Result_NonePaymentErrorZ.constr_from_ptr(ret);
181                 return ret_hu_conv;
182         }
183
184         /**
185          * Removes the payment cached by the given payment hash.
186          * 
187          * Should be called once a payment has failed or succeeded if not using [`InvoicePayer`] as an
188          * [`EventHandler`]. Otherwise, calling this method is unnecessary.
189          */
190         public void remove_cached_payment(byte[] payment_hash) {
191                 bindings.InvoicePayer_remove_cached_payment(this.ptr, InternalUtils.check_arr_len(payment_hash, 32));
192                 Reference.reachabilityFence(this);
193                 Reference.reachabilityFence(payment_hash);
194         }
195
196         /**
197          * Constructs a new EventHandler which calls the relevant methods on this_arg.
198          * This copies the `inner` pointer in this_arg and thus the returned EventHandler must be freed before this_arg is
199          */
200         public EventHandler as_EventHandler() {
201                 long ret = bindings.InvoicePayer_as_EventHandler(this.ptr);
202                 Reference.reachabilityFence(this);
203                 if (ret >= 0 && ret <= 4096) { return null; }
204                 EventHandler ret_hu_conv = new EventHandler(null, ret);
205                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
206                 return ret_hu_conv;
207         }
208
209 }