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