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 * A utility for paying [`Invoice]`s.
13 @SuppressWarnings("unchecked") // We correctly assign various generic arrays
14 public class InvoicePayer extends CommonBase {
15 InvoicePayer(Object _dummy, long ptr) { super(ptr); }
16 @Override @SuppressWarnings("deprecation")
17 protected void finalize() throws Throwable {
19 if (ptr != 0) { bindings.InvoicePayer_free(ptr); }
23 * Creates an invoice payer that retries failed payment paths.
25 * Will forward any [`Event::PaymentPathFailed`] events to the decorated `event_handler` once
26 * `retry_attempts` has been exceeded for a given [`Invoice`].
28 public static InvoicePayer of(Payer payer, Router router, LockableScore scorer, Logger logger, EventHandler event_handler, RetryAttempts retry_attempts) {
29 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);
30 if (ret >= 0 && ret <= 4096) { return null; }
31 InvoicePayer ret_hu_conv = null; if (ret < 0 || ret > 4096) { ret_hu_conv = new InvoicePayer(null, ret); }
32 ret_hu_conv.ptrs_to.add(ret_hu_conv);
33 ret_hu_conv.ptrs_to.add(payer);
34 ret_hu_conv.ptrs_to.add(router);
35 ret_hu_conv.ptrs_to.add(scorer);
36 ret_hu_conv.ptrs_to.add(logger);
37 ret_hu_conv.ptrs_to.add(event_handler);
42 * Pays the given [`Invoice`], caching it for later use in case a retry is needed.
44 * You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
45 * never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
48 public Result_PaymentIdPaymentErrorZ pay_invoice(Invoice invoice) {
49 long ret = bindings.InvoicePayer_pay_invoice(this.ptr, invoice == null ? 0 : invoice.ptr & ~1);
50 if (ret >= 0 && ret <= 4096) { return null; }
51 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
52 this.ptrs_to.add(invoice);
57 * Pays the given zero-value [`Invoice`] using the given amount, caching it for later use in
58 * case a retry is needed.
60 * You should ensure that the `invoice.payment_hash()` is unique and the same payment_hash has
61 * never been paid before. Because [`InvoicePayer`] is stateless no effort is made to do so
64 public Result_PaymentIdPaymentErrorZ pay_zero_value_invoice(Invoice invoice, long amount_msats) {
65 long ret = bindings.InvoicePayer_pay_zero_value_invoice(this.ptr, invoice == null ? 0 : invoice.ptr & ~1, amount_msats);
66 if (ret >= 0 && ret <= 4096) { return null; }
67 Result_PaymentIdPaymentErrorZ ret_hu_conv = Result_PaymentIdPaymentErrorZ.constr_from_ptr(ret);
68 this.ptrs_to.add(invoice);
73 * Removes the payment cached by the given payment hash.
75 * Should be called once a payment has failed or succeeded if not using [`InvoicePayer`] as an
76 * [`EventHandler`]. Otherwise, calling this method is unnecessary.
78 public void remove_cached_payment(byte[] payment_hash) {
79 bindings.InvoicePayer_remove_cached_payment(this.ptr, InternalUtils.check_arr_len(payment_hash, 32));
83 * Constructs a new EventHandler which calls the relevant methods on this_arg.
84 * This copies the `inner` pointer in this_arg and thus the returned EventHandler must be freed before this_arg is
86 public EventHandler as_EventHandler() {
87 long ret = bindings.InvoicePayer_as_EventHandler(this.ptr);
88 if (ret >= 0 && ret <= 4096) { return null; }
89 EventHandler ret_hu_conv = new EventHandler(null, ret);
90 ret_hu_conv.ptrs_to.add(this);