Skip to content

Commit 2b85ba9

Browse files
committed
Add SendingParameters to bolt11 send_using_amount
Added the optional `SendingParameters` to `send_using_amount` in `Bolt11Payment`. If the user provides sending params the values will be overridden otherwise they'll use the default values.
1 parent d6e6ff7 commit 2b85ba9

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ interface Bolt11Payment {
9696
[Throws=NodeError]
9797
PaymentId send([ByRef]Bolt11Invoice invoice, SendingParameters? sending_parameters);
9898
[Throws=NodeError]
99-
PaymentId send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat);
99+
PaymentId send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat, SendingParameters? sending_parameters);
100100
[Throws=NodeError]
101101
void send_probes([ByRef]Bolt11Invoice invoice);
102102
[Throws=NodeError]

src/payment/bolt11.rs

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,20 @@ impl Bolt11Payment {
190190
}
191191
}
192192

193-
/// Send a payment given an invoice and an amount in millisatoshi.
193+
/// Send a payment given an invoice and an amount in millisatoshis.
194194
///
195195
/// This will fail if the amount given is less than the value required by the given invoice.
196196
///
197197
/// This can be used to pay a so-called "zero-amount" invoice, i.e., an invoice that leaves the
198198
/// amount paid to be determined by the user.
199+
///
200+
/// If [`SendingParameters`] are provided they will override the node's default routing parameters
201+
/// on a per-field basis. Each field in `SendingParameters` that is set replaces the corresponding
202+
/// default value. Fields that are not set fall back to the node's configured defaults. If no
203+
/// `SendingParameters` are provided, the method fully relies on these defaults.
199204
pub fn send_using_amount(
200205
&self, invoice: &Bolt11Invoice, amount_msat: u64,
206+
sending_parameters: Option<SendingParameters>,
201207
) -> Result<PaymentId, Error> {
202208
let rt_lock = self.runtime.read().unwrap();
203209
if rt_lock.is_none() {
@@ -238,9 +244,43 @@ impl Bolt11Payment {
238244
.with_bolt11_features(features.clone())
239245
.map_err(|_| Error::InvalidInvoice)?;
240246
}
241-
let route_params =
247+
let mut route_params =
242248
RouteParameters::from_payment_params_and_value(payment_params, amount_msat);
243249

250+
if let Some(user_set_params) = sending_parameters {
251+
if let Some(mut default_params) =
252+
self.config.sending_parameters_config.as_ref().cloned()
253+
{
254+
default_params.max_total_routing_fee_msat = user_set_params
255+
.max_total_routing_fee_msat
256+
.or(default_params.max_total_routing_fee_msat);
257+
default_params.max_total_cltv_expiry_delta = user_set_params
258+
.max_total_cltv_expiry_delta
259+
.or(default_params.max_total_cltv_expiry_delta);
260+
default_params.max_path_count =
261+
user_set_params.max_path_count.or(default_params.max_path_count);
262+
default_params.max_channel_saturation_power_of_half = user_set_params
263+
.max_channel_saturation_power_of_half
264+
.or(default_params.max_channel_saturation_power_of_half);
265+
266+
route_params.max_total_routing_fee_msat = default_params.max_total_routing_fee_msat;
267+
route_params.payment_params.max_total_cltv_expiry_delta =
268+
default_params.max_total_cltv_expiry_delta.unwrap_or_default();
269+
route_params.payment_params.max_path_count =
270+
default_params.max_path_count.unwrap_or_default();
271+
route_params.payment_params.max_channel_saturation_power_of_half =
272+
default_params.max_channel_saturation_power_of_half.unwrap_or_default();
273+
}
274+
} else if let Some(default_params) = &self.config.sending_parameters_config {
275+
route_params.max_total_routing_fee_msat = default_params.max_total_routing_fee_msat;
276+
route_params.payment_params.max_total_cltv_expiry_delta =
277+
default_params.max_total_cltv_expiry_delta.unwrap_or_default();
278+
route_params.payment_params.max_path_count =
279+
default_params.max_path_count.unwrap_or_default();
280+
route_params.payment_params.max_channel_saturation_power_of_half =
281+
default_params.max_channel_saturation_power_of_half.unwrap_or_default();
282+
}
283+
244284
let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT);
245285
let recipient_fields = RecipientOnionFields::secret_only(*payment_secret);
246286

tests/common/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -541,7 +541,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
541541
let underpaid_amount = invoice_amount_2_msat - 1;
542542
assert_eq!(
543543
Err(NodeError::InvalidAmount),
544-
node_a.bolt11_payment().send_using_amount(&invoice, underpaid_amount)
544+
node_a.bolt11_payment().send_using_amount(&invoice, underpaid_amount, None)
545545
);
546546

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

551551
println!("\nA overpaid send");
552552
let payment_id =
553-
node_a.bolt11_payment().send_using_amount(&invoice, overpaid_amount_msat).unwrap();
553+
node_a.bolt11_payment().send_using_amount(&invoice, overpaid_amount_msat, None).unwrap();
554554
expect_event!(node_a, PaymentSuccessful);
555555
let received_amount = match node_b.wait_next_event() {
556556
ref e @ Event::PaymentReceived { amount_msat, .. } => {
@@ -584,7 +584,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
584584
println!("\nA send_using_amount");
585585
let payment_id = node_a
586586
.bolt11_payment()
587-
.send_using_amount(&variable_amount_invoice, determined_amount_msat)
587+
.send_using_amount(&variable_amount_invoice, determined_amount_msat, None)
588588
.unwrap();
589589

590590
expect_event!(node_a, PaymentSuccessful);

0 commit comments

Comments
 (0)