Skip to content

Commit

Permalink
Add PaymentParameters to bolt11 send_using_amount
Browse files Browse the repository at this point in the history
Added the optional PaymentParameters to send_using_amount in
Bolt11Payment. If the user provides payment params the values
will be overridden otherwise they remain the same. Updated
ldk_node.udl with these changes
  • Loading branch information
slanesuke committed Aug 1, 2024
1 parent c62189b commit b90f65e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ interface Bolt11Payment {
[Throws=NodeError]
PaymentId send([ByRef]Bolt11Invoice invoice, PaymentParameters? payment_parameters);
[Throws=NodeError]
PaymentId send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat);
PaymentId send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat, PaymentParameters? payment_parameters);
[Throws=NodeError]
void send_probes([ByRef]Bolt11Invoice invoice);
[Throws=NodeError]
Expand Down
19 changes: 17 additions & 2 deletions src/payment/bolt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,17 @@ impl Bolt11Payment {
}
}

/// Send a payment given an invoice and an amount in millisatoshi.
/// Send a payment given an invoice, an amount in millisatoshi, and optional [`PaymentParameters`].
///
/// This will fail if the amount given is less than the value required by the given invoice.
///
/// This can be used to pay a so-called "zero-amount" invoice, i.e., an invoice that leaves the
/// amount paid to be determined by the user.
///
/// [`PaymentParameters`]: PaymentParameters
pub fn send_using_amount(
&self, invoice: &Bolt11Invoice, amount_msat: u64,
payment_parameters: Option<PaymentParameters>,
) -> Result<PaymentId, Error> {
let rt_lock = self.runtime.read().unwrap();
if rt_lock.is_none() {
Expand Down Expand Up @@ -212,9 +215,21 @@ impl Bolt11Payment {
.with_bolt11_features(features.clone())
.map_err(|_| Error::InvalidInvoice)?;
}
let route_params =
let mut route_params =
RouteParameters::from_payment_params_and_value(payment_params, amount_msat);

if let Some(payment_params) = payment_parameters {
payment_params
.expiry_time
.map(|expiry| route_params.payment_params.expiry_time = Some(expiry));
payment_params
.max_total_routing_fee_msat
.map(|fee| route_params.max_total_routing_fee_msat = Some(fee));
payment_params
.max_total_cltv_expiry_delta
.map(|delta| route_params.payment_params.max_total_cltv_expiry_delta = delta);
}

let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT);
let recipient_fields = RecipientOnionFields::secret_only(*payment_secret);

Expand Down
6 changes: 3 additions & 3 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
let underpaid_amount = invoice_amount_2_msat - 1;
assert_eq!(
Err(NodeError::InvalidAmount),
node_a.bolt11_payment().send_using_amount(&invoice, underpaid_amount)
node_a.bolt11_payment().send_using_amount(&invoice, underpaid_amount, None)
);

println!("\nB overpaid receive");
Expand All @@ -550,7 +550,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(

println!("\nA overpaid send");
let payment_id =
node_a.bolt11_payment().send_using_amount(&invoice, overpaid_amount_msat).unwrap();
node_a.bolt11_payment().send_using_amount(&invoice, overpaid_amount_msat, None).unwrap();
expect_event!(node_a, PaymentSuccessful);
let received_amount = match node_b.wait_next_event() {
ref e @ Event::PaymentReceived { amount_msat, .. } => {
Expand Down Expand Up @@ -584,7 +584,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
println!("\nA send_using_amount");
let payment_id = node_a
.bolt11_payment()
.send_using_amount(&variable_amount_invoice, determined_amount_msat)
.send_using_amount(&variable_amount_invoice, determined_amount_msat, None)
.unwrap();

expect_event!(node_a, PaymentSuccessful);
Expand Down

0 comments on commit b90f65e

Please sign in to comment.