a7add5e8ec3e86d71c56d3d9bfbd72081f1a4819
[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(Payer payer, Router router, Logger logger, EventHandler event_handler, 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          * You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
54          * never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
55          * for you.
56          */
57         public Result_PaymentIdPaymentErrorZ pay_invoice(Invoice invoice) {
58                 long ret = bindings.InvoicePayer_pay_invoice(this.ptr, invoice == null ? 0 : invoice.ptr);
59                 Reference.reachabilityFence(this);
60                 Reference.reachabilityFence(invoice);
61                 if (ret >= 0 && ret <= 4096) { return null; }
62                 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
63                 if (this != null) { this.ptrs_to.add(invoice); };
64                 return ret_hu_conv;
65         }
66
67         /**
68          * Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
69          * case a retry is needed.
70          * 
71          * You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
72          * never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
73          * for you.
74          */
75         public Result_PaymentIdPaymentErrorZ pay_zero_value_invoice(Invoice invoice, long amount_msats) {
76                 long ret = bindings.InvoicePayer_pay_zero_value_invoice(this.ptr, invoice == null ? 0 : invoice.ptr, amount_msats);
77                 Reference.reachabilityFence(this);
78                 Reference.reachabilityFence(invoice);
79                 Reference.reachabilityFence(amount_msats);
80                 if (ret >= 0 && ret <= 4096) { return null; }
81                 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
82                 if (this != null) { this.ptrs_to.add(invoice); };
83                 return ret_hu_conv;
84         }
85
86         /**
87          * Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
88          * case a retry is needed.
89          * 
90          * You should ensure that `payment_preimage` is unique and that its `payment_hash` has never
91          * been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so for you.
92          */
93         public Result_PaymentIdPaymentErrorZ pay_pubkey(byte[] pubkey, byte[] payment_preimage, long amount_msats, int final_cltv_expiry_delta) {
94                 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);
95                 Reference.reachabilityFence(this);
96                 Reference.reachabilityFence(pubkey);
97                 Reference.reachabilityFence(payment_preimage);
98                 Reference.reachabilityFence(amount_msats);
99                 Reference.reachabilityFence(final_cltv_expiry_delta);
100                 if (ret >= 0 && ret <= 4096) { return null; }
101                 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
102                 return ret_hu_conv;
103         }
104
105         /**
106          * Removes the payment cached by the given payment hash.
107          * 
108          * Should be called once a payment has failed or succeeded if not using [`InvoicePayer`] as an
109          * [`EventHandler`]. Otherwise, calling this method is unnecessary.
110          */
111         public void remove_cached_payment(byte[] payment_hash) {
112                 bindings.InvoicePayer_remove_cached_payment(this.ptr, InternalUtils.check_arr_len(payment_hash, 32));
113                 Reference.reachabilityFence(this);
114                 Reference.reachabilityFence(payment_hash);
115         }
116
117         /**
118          * Constructs a new EventHandler which calls the relevant methods on this_arg.
119          * This copies the `inner` pointer in this_arg and thus the returned EventHandler must be freed before this_arg is
120          */
121         public EventHandler as_EventHandler() {
122                 long ret = bindings.InvoicePayer_as_EventHandler(this.ptr);
123                 Reference.reachabilityFence(this);
124                 if (ret >= 0 && ret <= 4096) { return null; }
125                 EventHandler ret_hu_conv = new EventHandler(null, ret);
126                 if (ret_hu_conv != null) { ret_hu_conv.ptrs_to.add(this); };
127                 return ret_hu_conv;
128         }
129
130 }