1 // This file is Copyright its original authors, visible in version control
2 // history and in the source files from which this was generated.
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.
9 //! Convenient utilities for paying Lightning invoices.
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};
20 /// Pays the given [`Bolt11Invoice`], retrying if needed based on [`Retry`].
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.
26 /// If you wish to use a different payment idempotency token, see [`pay_invoice_with_id`].
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() };
34 /// Pays the given [`Bolt11Invoice`] with a custom idempotency key, retrying if needed based on
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.
40 /// You should ensure that the [`Bolt11Invoice::payment_hash`] is unique and the same
41 /// [`PaymentHash`] has never been paid before.
43 /// See [`pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency token.
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() };
51 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount, retrying if needed based on
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.
58 /// If you wish to use a different payment idempotency token, see
59 /// [`pay_zero_value_invoice_with_id`].
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() };
67 /// Pays the given zero-value [`Bolt11Invoice`] using the given amount and custom idempotency key,
68 /// retrying if needed based on [`Retry`].
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.
73 /// You should ensure that the [`Bolt11Invoice::payment_hash`] is unique and the same
74 /// [`PaymentHash`] has never been paid before.
76 /// See [`pay_zero_value_invoice`] for a variant which uses the [`PaymentHash`] for the
77 /// idempotency token.
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() };
85 /// Sends payment probes over all paths of a route that would be used to pay the given invoice.
87 /// See [`ChannelManager::send_preflight_probes`] for more information.
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() };
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.
99 /// See [`ChannelManager::send_preflight_probes`] for more information.
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() };
108 /// An error that may occur when making a payment.
112 pub enum PaymentError {
113 /// An error resulting from the provided [`Bolt11Invoice`] or payment hash.
115 crate::c_types::Str),
116 /// An error occurring when sending a payment.
118 crate::lightning::ln::outbound_payment::RetryableSendFailure),
120 use lightning_invoice::payment::PaymentError as PaymentErrorImport;
121 pub(crate) type nativePaymentError = PaymentErrorImport;
125 pub(crate) fn to_native(&self) -> nativePaymentError {
127 PaymentError::Invoice (ref a, ) => {
128 let mut a_nonref = Clone::clone(a);
129 nativePaymentError::Invoice (
133 PaymentError::Sending (ref a, ) => {
134 let mut a_nonref = Clone::clone(a);
135 nativePaymentError::Sending (
136 a_nonref.into_native(),
142 pub(crate) fn into_native(self) -> nativePaymentError {
144 PaymentError::Invoice (mut a, ) => {
145 nativePaymentError::Invoice (
149 PaymentError::Sending (mut a, ) => {
150 nativePaymentError::Sending (
157 pub(crate) fn from_native(native: &nativePaymentError) -> Self {
159 nativePaymentError::Invoice (ref a, ) => {
160 let mut a_nonref = Clone::clone(a);
161 PaymentError::Invoice (
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),
174 pub(crate) fn native_into(native: nativePaymentError) -> Self {
176 nativePaymentError::Invoice (mut a, ) => {
177 PaymentError::Invoice (
181 nativePaymentError::Sending (mut a, ) => {
182 PaymentError::Sending (
183 crate::lightning::ln::outbound_payment::RetryableSendFailure::native_into(a),
189 /// Frees any resources used by the PaymentError
191 pub extern "C" fn PaymentError_free(this_ptr: PaymentError) { }
192 /// Creates a copy of the PaymentError
194 pub extern "C" fn PaymentError_clone(orig: &PaymentError) -> PaymentError {
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, )
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, )
207 /// Checks if two PaymentErrors contain equal inner contents.
208 /// This ignores pointers and is_owned flags and looks at the values in fields.
210 pub extern "C" fn PaymentError_eq(a: &PaymentError, b: &PaymentError) -> bool {
211 if &a.to_native() == &b.to_native() { true } else { false }
213 /// An error that may occur when sending a payment probe.
217 pub enum ProbingError {
218 /// An error resulting from the provided [`Bolt11Invoice`].
220 crate::c_types::Str),
221 /// An error occurring when sending a payment probe.
223 crate::lightning::ln::outbound_payment::ProbeSendFailure),
225 use lightning_invoice::payment::ProbingError as ProbingErrorImport;
226 pub(crate) type nativeProbingError = ProbingErrorImport;
230 pub(crate) fn to_native(&self) -> nativeProbingError {
232 ProbingError::Invoice (ref a, ) => {
233 let mut a_nonref = Clone::clone(a);
234 nativeProbingError::Invoice (
238 ProbingError::Sending (ref a, ) => {
239 let mut a_nonref = Clone::clone(a);
240 nativeProbingError::Sending (
241 a_nonref.into_native(),
247 pub(crate) fn into_native(self) -> nativeProbingError {
249 ProbingError::Invoice (mut a, ) => {
250 nativeProbingError::Invoice (
254 ProbingError::Sending (mut a, ) => {
255 nativeProbingError::Sending (
262 pub(crate) fn from_native(native: &nativeProbingError) -> Self {
264 nativeProbingError::Invoice (ref a, ) => {
265 let mut a_nonref = Clone::clone(a);
266 ProbingError::Invoice (
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),
279 pub(crate) fn native_into(native: nativeProbingError) -> Self {
281 nativeProbingError::Invoice (mut a, ) => {
282 ProbingError::Invoice (
286 nativeProbingError::Sending (mut a, ) => {
287 ProbingError::Sending (
288 crate::lightning::ln::outbound_payment::ProbeSendFailure::native_into(a),
294 /// Frees any resources used by the ProbingError
296 pub extern "C" fn ProbingError_free(this_ptr: ProbingError) { }
297 /// Creates a copy of the ProbingError
299 pub extern "C" fn ProbingError_clone(orig: &ProbingError) -> ProbingError {
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, )
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, )
312 /// Checks if two ProbingErrors contain equal inner contents.
313 /// This ignores pointers and is_owned flags and looks at the values in fields.
315 pub extern "C" fn ProbingError_eq(a: &ProbingError, b: &ProbingError) -> bool {
316 if &a.to_native() == &b.to_native() { true } else { false }