Update auto-generated bindings
[ldk-c-bindings] / lightning-c-bindings / src / lightning_invoice / payment.rs
1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
3 //
4 // This file is licensed under the license available in the LICENSE or LICENSE.md
5 // file in the root of this repository or, if no such file exists, the same
6 // license as that which applies to the original source files from which this
7 // source was automatically generated.
8
9 //! Convenient utilities for paying Lightning invoices.
10
11 use alloc::str::FromStr;
12 use alloc::string::String;
13 use core::ffi::c_void;
14 use core::convert::Infallible;
15 use bitcoin::hashes::Hash;
16 use crate::c_types::*;
17 #[cfg(feature="no-std")]
18 use alloc::{vec::Vec, boxed::Box};
19
20 /// Pays the given [`Bolt11Invoice`], retrying if needed based on [`Retry`].
21 ///
22 /// [`Bolt11Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
23 /// as the payment is still pending. If the payment succeeds, you must ensure that a second payment
24 /// with the same [`PaymentHash`] is never sent.
25 ///
26 /// If you wish to use a different payment idempotency token, see [`pay_invoice_with_id`].
27 #[no_mangle]
28 pub extern "C" fn pay_invoice(invoice: &crate::lightning_invoice::Bolt11Invoice, mut retry_strategy: crate::lightning::ln::outbound_payment::Retry, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_ThirtyTwoBytesPaymentErrorZ {
29         let mut ret = lightning_invoice::payment::pay_invoice::<_>(invoice.get_native_ref(), retry_strategy.into_native(), channelmanager.get_native_ref());
30         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::ThirtyTwoBytes { data: o.0 } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::payment::PaymentError::native_into(e) }).into() };
31         local_ret
32 }
33
34 /// Pays the given [`Bolt11Invoice`] with a custom idempotency key, retrying if needed based on
35 /// [`Retry`].
36 ///
37 /// Note that idempotency is only guaranteed as long as the payment is still pending. Once the
38 /// payment completes or fails, no idempotency guarantees are made.
39 ///
40 /// You should ensure that the [`Bolt11Invoice::payment_hash`] is unique and the same
41 /// [`PaymentHash`] has never been paid before.
42 ///
43 /// See [`pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency token.
44 #[no_mangle]
45 pub extern "C" fn pay_invoice_with_id(invoice: &crate::lightning_invoice::Bolt11Invoice, mut payment_id: crate::c_types::ThirtyTwoBytes, mut retry_strategy: crate::lightning::ln::outbound_payment::Retry, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_NonePaymentErrorZ {
46         let mut ret = lightning_invoice::payment::pay_invoice_with_id::<_>(invoice.get_native_ref(), ::lightning::ln::channelmanager::PaymentId(payment_id.data), retry_strategy.into_native(), channelmanager.get_native_ref());
47         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::payment::PaymentError::native_into(e) }).into() };
48         local_ret
49 }
50
51 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount, retrying if needed based on
52 /// [`Retry`].
53 ///
54 /// [`Bolt11Invoice::payment_hash`] is used as the [`PaymentId`], which ensures idempotency as long
55 /// as the payment is still pending. If the payment succeeds, you must ensure that a second payment
56 /// with the same [`PaymentHash`] is never sent.
57 ///
58 /// If you wish to use a different payment idempotency token, see
59 /// [`pay_zero_value_invoice_with_id`].
60 #[no_mangle]
61 pub extern "C" fn pay_zero_value_invoice(invoice: &crate::lightning_invoice::Bolt11Invoice, mut amount_msats: u64, mut retry_strategy: crate::lightning::ln::outbound_payment::Retry, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_ThirtyTwoBytesPaymentErrorZ {
62         let mut ret = lightning_invoice::payment::pay_zero_value_invoice::<_>(invoice.get_native_ref(), amount_msats, retry_strategy.into_native(), channelmanager.get_native_ref());
63         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { crate::c_types::ThirtyTwoBytes { data: o.0 } }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::payment::PaymentError::native_into(e) }).into() };
64         local_ret
65 }
66
67 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount and custom idempotency key,
68 /// retrying if needed based on [`Retry`].
69 ///
70 /// Note that idempotency is only guaranteed as long as the payment is still pending. Once the
71 /// payment completes or fails, no idempotency guarantees are made.
72 ///
73 /// You should ensure that the [`Bolt11Invoice::payment_hash`] is unique and the same
74 /// [`PaymentHash`] has never been paid before.
75 ///
76 /// See [`pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
77 /// idempotency token.
78 #[no_mangle]
79 pub extern "C" fn pay_zero_value_invoice_with_id(invoice: &crate::lightning_invoice::Bolt11Invoice, mut amount_msats: u64, mut payment_id: crate::c_types::ThirtyTwoBytes, mut retry_strategy: crate::lightning::ln::outbound_payment::Retry, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager) -> crate::c_types::derived::CResult_NonePaymentErrorZ {
80         let mut ret = lightning_invoice::payment::pay_zero_value_invoice_with_id::<_>(invoice.get_native_ref(), amount_msats, ::lightning::ln::channelmanager::PaymentId(payment_id.data), retry_strategy.into_native(), channelmanager.get_native_ref());
81         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { () /*o*/ }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::payment::PaymentError::native_into(e) }).into() };
82         local_ret
83 }
84
85 /// Sends payment probes over all paths of a route that would be used to pay the given invoice.
86 ///
87 /// See [`ChannelManager::send_preflight_probes`] for more information.
88 #[no_mangle]
89 pub extern "C" fn preflight_probe_invoice(invoice: &crate::lightning_invoice::Bolt11Invoice, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager, mut liquidity_limit_multiplier: crate::c_types::derived::COption_u64Z) -> crate::c_types::derived::CResult_CVec_C2Tuple_ThirtyTwoBytesThirtyTwoBytesZZProbingErrorZ {
90         let mut local_liquidity_limit_multiplier = if liquidity_limit_multiplier.is_some() { Some( { liquidity_limit_multiplier.take() }) } else { None };
91         let mut ret = lightning_invoice::payment::preflight_probe_invoice::<_>(invoice.get_native_ref(), channelmanager.get_native_ref(), local_liquidity_limit_multiplier);
92         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { let (mut orig_ret_0_0_0, mut orig_ret_0_0_1) = item; let mut local_ret_0_0 = (crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_0.0 }, crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_1.0 }).into(); local_ret_0_0 }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::payment::ProbingError::native_into(e) }).into() };
93         local_ret
94 }
95
96 /// Sends payment probes over all paths of a route that would be used to pay the given zero-value
97 /// invoice using the given amount.
98 ///
99 /// See [`ChannelManager::send_preflight_probes`] for more information.
100 #[no_mangle]
101 pub extern "C" fn preflight_probe_zero_value_invoice(invoice: &crate::lightning_invoice::Bolt11Invoice, mut amount_msat: u64, channelmanager: &crate::lightning::ln::channelmanager::ChannelManager, mut liquidity_limit_multiplier: crate::c_types::derived::COption_u64Z) -> crate::c_types::derived::CResult_CVec_C2Tuple_ThirtyTwoBytesThirtyTwoBytesZZProbingErrorZ {
102         let mut local_liquidity_limit_multiplier = if liquidity_limit_multiplier.is_some() { Some( { liquidity_limit_multiplier.take() }) } else { None };
103         let mut ret = lightning_invoice::payment::preflight_probe_zero_value_invoice::<_>(invoice.get_native_ref(), amount_msat, channelmanager.get_native_ref(), local_liquidity_limit_multiplier);
104         let mut local_ret = match ret { Ok(mut o) => crate::c_types::CResultTempl::ok( { let mut local_ret_0 = Vec::new(); for mut item in o.drain(..) { local_ret_0.push( { let (mut orig_ret_0_0_0, mut orig_ret_0_0_1) = item; let mut local_ret_0_0 = (crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_0.0 }, crate::c_types::ThirtyTwoBytes { data: orig_ret_0_0_1.0 }).into(); local_ret_0_0 }); }; local_ret_0.into() }).into(), Err(mut e) => crate::c_types::CResultTempl::err( { crate::lightning_invoice::payment::ProbingError::native_into(e) }).into() };
105         local_ret
106 }
107
108 /// An error that may occur when making a payment.
109 #[derive(Clone)]
110 #[must_use]
111 #[repr(C)]
112 pub enum PaymentError {
113         /// An error resulting from the provided [`Bolt11Invoice`] or payment hash.
114         Invoice(
115                 crate::c_types::Str),
116         /// An error occurring when sending a payment.
117         Sending(
118                 crate::lightning::ln::outbound_payment::RetryableSendFailure),
119 }
120 use lightning_invoice::payment::PaymentError as PaymentErrorImport;
121 pub(crate) type nativePaymentError = PaymentErrorImport;
122
123 impl PaymentError {
124         #[allow(unused)]
125         pub(crate) fn to_native(&self) -> nativePaymentError {
126                 match self {
127                         PaymentError::Invoice (ref a, ) => {
128                                 let mut a_nonref = Clone::clone(a);
129                                 nativePaymentError::Invoice (
130                                         a_nonref.into_str(),
131                                 )
132                         },
133                         PaymentError::Sending (ref a, ) => {
134                                 let mut a_nonref = Clone::clone(a);
135                                 nativePaymentError::Sending (
136                                         a_nonref.into_native(),
137                                 )
138                         },
139                 }
140         }
141         #[allow(unused)]
142         pub(crate) fn into_native(self) -> nativePaymentError {
143                 match self {
144                         PaymentError::Invoice (mut a, ) => {
145                                 nativePaymentError::Invoice (
146                                         a.into_str(),
147                                 )
148                         },
149                         PaymentError::Sending (mut a, ) => {
150                                 nativePaymentError::Sending (
151                                         a.into_native(),
152                                 )
153                         },
154                 }
155         }
156         #[allow(unused)]
157         pub(crate) fn from_native(native: &nativePaymentError) -> Self {
158                 match native {
159                         nativePaymentError::Invoice (ref a, ) => {
160                                 let mut a_nonref = Clone::clone(a);
161                                 PaymentError::Invoice (
162                                         a_nonref.into(),
163                                 )
164                         },
165                         nativePaymentError::Sending (ref a, ) => {
166                                 let mut a_nonref = Clone::clone(a);
167                                 PaymentError::Sending (
168                                         crate::lightning::ln::outbound_payment::RetryableSendFailure::native_into(a_nonref),
169                                 )
170                         },
171                 }
172         }
173         #[allow(unused)]
174         pub(crate) fn native_into(native: nativePaymentError) -> Self {
175                 match native {
176                         nativePaymentError::Invoice (mut a, ) => {
177                                 PaymentError::Invoice (
178                                         a.into(),
179                                 )
180                         },
181                         nativePaymentError::Sending (mut a, ) => {
182                                 PaymentError::Sending (
183                                         crate::lightning::ln::outbound_payment::RetryableSendFailure::native_into(a),
184                                 )
185                         },
186                 }
187         }
188 }
189 /// Frees any resources used by the PaymentError
190 #[no_mangle]
191 pub extern "C" fn PaymentError_free(this_ptr: PaymentError) { }
192 /// Creates a copy of the PaymentError
193 #[no_mangle]
194 pub extern "C" fn PaymentError_clone(orig: &PaymentError) -> PaymentError {
195         orig.clone()
196 }
197 #[no_mangle]
198 /// Utility method to constructs a new Invoice-variant PaymentError
199 pub extern "C" fn PaymentError_invoice(a: crate::c_types::Str) -> PaymentError {
200         PaymentError::Invoice(a, )
201 }
202 #[no_mangle]
203 /// Utility method to constructs a new Sending-variant PaymentError
204 pub extern "C" fn PaymentError_sending(a: crate::lightning::ln::outbound_payment::RetryableSendFailure) -> PaymentError {
205         PaymentError::Sending(a, )
206 }
207 /// Checks if two PaymentErrors contain equal inner contents.
208 /// This ignores pointers and is_owned flags and looks at the values in fields.
209 #[no_mangle]
210 pub extern "C" fn PaymentError_eq(a: &PaymentError, b: &PaymentError) -> bool {
211         if &a.to_native() == &b.to_native() { true } else { false }
212 }
213 /// An error that may occur when sending a payment probe.
214 #[derive(Clone)]
215 #[must_use]
216 #[repr(C)]
217 pub enum ProbingError {
218         /// An error resulting from the provided [`Bolt11Invoice`].
219         Invoice(
220                 crate::c_types::Str),
221         /// An error occurring when sending a payment probe.
222         Sending(
223                 crate::lightning::ln::outbound_payment::ProbeSendFailure),
224 }
225 use lightning_invoice::payment::ProbingError as ProbingErrorImport;
226 pub(crate) type nativeProbingError = ProbingErrorImport;
227
228 impl ProbingError {
229         #[allow(unused)]
230         pub(crate) fn to_native(&self) -> nativeProbingError {
231                 match self {
232                         ProbingError::Invoice (ref a, ) => {
233                                 let mut a_nonref = Clone::clone(a);
234                                 nativeProbingError::Invoice (
235                                         a_nonref.into_str(),
236                                 )
237                         },
238                         ProbingError::Sending (ref a, ) => {
239                                 let mut a_nonref = Clone::clone(a);
240                                 nativeProbingError::Sending (
241                                         a_nonref.into_native(),
242                                 )
243                         },
244                 }
245         }
246         #[allow(unused)]
247         pub(crate) fn into_native(self) -> nativeProbingError {
248                 match self {
249                         ProbingError::Invoice (mut a, ) => {
250                                 nativeProbingError::Invoice (
251                                         a.into_str(),
252                                 )
253                         },
254                         ProbingError::Sending (mut a, ) => {
255                                 nativeProbingError::Sending (
256                                         a.into_native(),
257                                 )
258                         },
259                 }
260         }
261         #[allow(unused)]
262         pub(crate) fn from_native(native: &nativeProbingError) -> Self {
263                 match native {
264                         nativeProbingError::Invoice (ref a, ) => {
265                                 let mut a_nonref = Clone::clone(a);
266                                 ProbingError::Invoice (
267                                         a_nonref.into(),
268                                 )
269                         },
270                         nativeProbingError::Sending (ref a, ) => {
271                                 let mut a_nonref = Clone::clone(a);
272                                 ProbingError::Sending (
273                                         crate::lightning::ln::outbound_payment::ProbeSendFailure::native_into(a_nonref),
274                                 )
275                         },
276                 }
277         }
278         #[allow(unused)]
279         pub(crate) fn native_into(native: nativeProbingError) -> Self {
280                 match native {
281                         nativeProbingError::Invoice (mut a, ) => {
282                                 ProbingError::Invoice (
283                                         a.into(),
284                                 )
285                         },
286                         nativeProbingError::Sending (mut a, ) => {
287                                 ProbingError::Sending (
288                                         crate::lightning::ln::outbound_payment::ProbeSendFailure::native_into(a),
289                                 )
290                         },
291                 }
292         }
293 }
294 /// Frees any resources used by the ProbingError
295 #[no_mangle]
296 pub extern "C" fn ProbingError_free(this_ptr: ProbingError) { }
297 /// Creates a copy of the ProbingError
298 #[no_mangle]
299 pub extern "C" fn ProbingError_clone(orig: &ProbingError) -> ProbingError {
300         orig.clone()
301 }
302 #[no_mangle]
303 /// Utility method to constructs a new Invoice-variant ProbingError
304 pub extern "C" fn ProbingError_invoice(a: crate::c_types::Str) -> ProbingError {
305         ProbingError::Invoice(a, )
306 }
307 #[no_mangle]
308 /// Utility method to constructs a new Sending-variant ProbingError
309 pub extern "C" fn ProbingError_sending(a: crate::lightning::ln::outbound_payment::ProbeSendFailure) -> ProbingError {
310         ProbingError::Sending(a, )
311 }
312 /// Checks if two ProbingErrors contain equal inner contents.
313 /// This ignores pointers and is_owned flags and looks at the values in fields.
314 #[no_mangle]
315 pub extern "C" fn ProbingError_eq(a: &ProbingError, b: &ProbingError) -> bool {
316         if &a.to_native() == &b.to_native() { true } else { false }
317 }