1 package org.ldk.structs;
3 import org.ldk.impl.bindings;
4 import org.ldk.enums.*;
6 import java.util.Arrays;
7 import java.lang.ref.Reference;
8 import javax.annotation.Nullable;
12 * A utility for paying [`Invoice`]s and sending spontaneous payments.
14 * See [module-level documentation] for details.
16 * [module-level documentation]: crate::payment
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 {
24 if (ptr != 0) { bindings.InvoicePayer_free(ptr); }
28 * Creates an invoice payer that retries failed payment paths.
30 * Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
31 * `retry_attempts` has been exceeded for a given [`Invoice`].
33 public static InvoicePayer of(Payer payer, Router router, MultiThreadedLockableScore scorer, Logger logger, EventHandler event_handler, RetryAttempts retry_attempts) {
34 long ret = bindings.InvoicePayer_new(payer == null ? 0 : payer.ptr, router == null ? 0 : router.ptr, scorer == null ? 0 : scorer.ptr & ~1, logger == null ? 0 : logger.ptr, event_handler == null ? 0 : event_handler.ptr, retry_attempts == null ? 0 : retry_attempts.ptr & ~1);
35 Reference.reachabilityFence(payer);
36 Reference.reachabilityFence(router);
37 Reference.reachabilityFence(scorer);
38 Reference.reachabilityFence(logger);
39 Reference.reachabilityFence(event_handler);
40 Reference.reachabilityFence(retry_attempts);
41 if (ret >= 0 && ret <= 4096) { return null; }
42 InvoicePayer ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new InvoicePayer(null, ret); }
43 ret_hu_conv.ptrs_to.add(ret_hu_conv);
44 ret_hu_conv.ptrs_to.add(payer);
45 ret_hu_conv.ptrs_to.add(router);
46 ret_hu_conv.ptrs_to.add(scorer);
47 ret_hu_conv.ptrs_to.add(logger);
48 ret_hu_conv.ptrs_to.add(event_handler);
53 * Pays the given [`Invoice`], caching it for later use in case a retry is needed.
55 * You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
56 * never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
59 public Result_PaymentIdPaymentErrorZ pay_invoice(Invoice invoice) {
60 long ret = bindings.InvoicePayer_pay_invoice(this.ptr, invoice == null ? 0 : invoice.ptr & ~1);
61 Reference.reachabilityFence(this);
62 Reference.reachabilityFence(invoice);
63 if (ret >= 0 && ret <= 4096) { return null; }
64 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
65 this.ptrs_to.add(invoice);
70 * Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
71 * case a retry is needed.
73 * You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
74 * never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
77 public Result_PaymentIdPaymentErrorZ pay_zero_value_invoice(Invoice invoice, long amount_msats) {
78 long ret = bindings.InvoicePayer_pay_zero_value_invoice(this.ptr, invoice == null ? 0 : invoice.ptr & ~1, amount_msats);
79 Reference.reachabilityFence(this);
80 Reference.reachabilityFence(invoice);
81 Reference.reachabilityFence(amount_msats);
82 if (ret >= 0 && ret <= 4096) { return null; }
83 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
84 this.ptrs_to.add(invoice);
89 * Pays `pubkey` an amount using the hash of the given preimage, caching it for later use in
90 * case a retry is needed.
92 * You should ensure that `payment_preimage` is unique and that its `payment_hash` has never
93 * been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so for you.
95 public Result_PaymentIdPaymentErrorZ pay_pubkey(byte[] pubkey, byte[] payment_preimage, long amount_msats, int final_cltv_expiry_delta) {
96 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);
97 Reference.reachabilityFence(this);
98 Reference.reachabilityFence(pubkey);
99 Reference.reachabilityFence(payment_preimage);
100 Reference.reachabilityFence(amount_msats);
101 Reference.reachabilityFence(final_cltv_expiry_delta);
102 if (ret >= 0 && ret <= 4096) { return null; }
103 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
108 * Removes the payment cached by the given payment hash.
110 * Should be called once a payment has failed or succeeded if not using [`InvoicePayer`] as an
111 * [`EventHandler`]. Otherwise, calling this method is unnecessary.
113 public void remove_cached_payment(byte[] payment_hash) {
114 bindings.InvoicePayer_remove_cached_payment(this.ptr, InternalUtils.check_arr_len(payment_hash, 32));
115 Reference.reachabilityFence(this);
116 Reference.reachabilityFence(payment_hash);
120 * Constructs a new EventHandler which calls the relevant methods on this_arg.
121 * This copies the `inner` pointer in this_arg and thus the returned EventHandler must be freed before this_arg is
123 public EventHandler as_EventHandler() {
124 long ret = bindings.InvoicePayer_as_EventHandler(this.ptr);
125 Reference.reachabilityFence(this);
126 if (ret >= 0 && ret <= 4096) { return null; }
127 EventHandler ret_hu_conv = new EventHandler(null, ret);
128 ret_hu_conv.ptrs_to.add(this);