Skip to content

Commit ffad819

Browse files
committed
Add max_total_routing_fee_msat for pay_invoice
In #2417 support for setting a max fee was added but it is not exposed when using the `pay_invoice` utility functions. This makes it so the user can pass in that parameter. One downside is that this removes the defualt value form before.
1 parent b3e7aac commit ffad819

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

lightning-invoice/src/payment.rs

+21-16
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ use core::time::Duration;
3434
///
3535
/// If you wish to use a different payment idempotency token, see [`pay_invoice_with_id`].
3636
pub fn pay_invoice<C: Deref>(
37-
invoice: &Bolt11Invoice, retry_strategy: Retry, channelmanager: C
37+
invoice: &Bolt11Invoice, retry_strategy: Retry, max_total_routing_fee_msat: Option<u64>,
38+
channelmanager: C
3839
) -> Result<PaymentId, PaymentError>
3940
where C::Target: AChannelManager,
4041
{
4142
let payment_id = PaymentId(invoice.payment_hash().into_inner());
42-
pay_invoice_with_id(invoice, payment_id, retry_strategy, channelmanager.get_cm())
43+
pay_invoice_with_id(invoice, payment_id, retry_strategy, max_total_routing_fee_msat, channelmanager.get_cm())
4344
.map(|()| payment_id)
4445
}
4546

@@ -54,12 +55,13 @@ where C::Target: AChannelManager,
5455
///
5556
/// See [`pay_invoice`] for a variant which uses the [`PaymentHash`] for the idempotency token.
5657
pub fn pay_invoice_with_id<C: Deref>(
57-
invoice: &Bolt11Invoice, payment_id: PaymentId, retry_strategy: Retry, channelmanager: C
58+
invoice: &Bolt11Invoice, payment_id: PaymentId, retry_strategy: Retry,
59+
max_total_routing_fee_msat: Option<u64>, channelmanager: C
5860
) -> Result<(), PaymentError>
5961
where C::Target: AChannelManager,
6062
{
6163
let amt_msat = invoice.amount_milli_satoshis().ok_or(PaymentError::Invoice("amount missing"))?;
62-
pay_invoice_using_amount(invoice, amt_msat, payment_id, retry_strategy, channelmanager.get_cm())
64+
pay_invoice_using_amount(invoice, amt_msat, payment_id, retry_strategy, max_total_routing_fee_msat, channelmanager.get_cm())
6365
}
6466

6567
/// Pays the given zero-value [`Bolt11Invoice`] using the given amount, retrying if needed based on
@@ -72,13 +74,13 @@ where C::Target: AChannelManager,
7274
/// If you wish to use a different payment idempotency token, see
7375
/// [`pay_zero_value_invoice_with_id`].
7476
pub fn pay_zero_value_invoice<C: Deref>(
75-
invoice: &Bolt11Invoice, amount_msats: u64, retry_strategy: Retry, channelmanager: C
77+
invoice: &Bolt11Invoice, amount_msats: u64, retry_strategy: Retry,
78+
max_total_routing_fee_msat: Option<u64>, channelmanager: C
7679
) -> Result<PaymentId, PaymentError>
7780
where C::Target: AChannelManager,
7881
{
7982
let payment_id = PaymentId(invoice.payment_hash().into_inner());
80-
pay_zero_value_invoice_with_id(invoice, amount_msats, payment_id, retry_strategy,
81-
channelmanager)
83+
pay_zero_value_invoice_with_id(invoice, amount_msats, payment_id, retry_strategy, max_total_routing_fee_msat, channelmanager)
8284
.map(|()| payment_id)
8385
}
8486

@@ -95,21 +97,20 @@ where C::Target: AChannelManager,
9597
/// idempotency token.
9698
pub fn pay_zero_value_invoice_with_id<C: Deref>(
9799
invoice: &Bolt11Invoice, amount_msats: u64, payment_id: PaymentId, retry_strategy: Retry,
98-
channelmanager: C
100+
max_total_routing_fee_msat: Option<u64>, channelmanager: C
99101
) -> Result<(), PaymentError>
100102
where C::Target: AChannelManager,
101103
{
102104
if invoice.amount_milli_satoshis().is_some() {
103105
Err(PaymentError::Invoice("amount unexpected"))
104106
} else {
105-
pay_invoice_using_amount(invoice, amount_msats, payment_id, retry_strategy,
106-
channelmanager.get_cm())
107+
pay_invoice_using_amount(invoice, amount_msats, payment_id, retry_strategy, max_total_routing_fee_msat, channelmanager.get_cm())
107108
}
108109
}
109110

110111
fn pay_invoice_using_amount<P: Deref>(
111112
invoice: &Bolt11Invoice, amount_msats: u64, payment_id: PaymentId, retry_strategy: Retry,
112-
payer: P
113+
max_total_routing_fee_msat: Option<u64>, payer: P
113114
) -> Result<(), PaymentError> where P::Target: Payer {
114115
let payment_hash = PaymentHash((*invoice.payment_hash()).into_inner());
115116
let mut recipient_onion = RecipientOnionFields::secret_only(*invoice.payment_secret());
@@ -121,7 +122,11 @@ fn pay_invoice_using_amount<P: Deref>(
121122
if let Some(features) = invoice.features() {
122123
payment_params = payment_params.with_bolt11_features(features.clone()).unwrap();
123124
}
124-
let route_params = RouteParameters::from_payment_params_and_value(payment_params, amount_msats);
125+
let route_params = RouteParameters {
126+
payment_params,
127+
final_value_msat: amount_msats,
128+
max_total_routing_fee_msat,
129+
};
125130

126131
payer.send_payment(payment_hash, recipient_onion, payment_id, route_params, retry_strategy)
127132
}
@@ -357,7 +362,7 @@ mod tests {
357362
let final_value_msat = invoice.amount_milli_satoshis().unwrap();
358363

359364
let payer = TestPayer::new().expect_send(Amount(final_value_msat));
360-
pay_invoice_using_amount(&invoice, final_value_msat, payment_id, Retry::Attempts(0), &payer).unwrap();
365+
pay_invoice_using_amount(&invoice, final_value_msat, payment_id, Retry::Attempts(0), None, &payer).unwrap();
361366
}
362367

363368
#[test]
@@ -368,7 +373,7 @@ mod tests {
368373
let amt_msat = 10_000;
369374

370375
let payer = TestPayer::new().expect_send(Amount(amt_msat));
371-
pay_invoice_using_amount(&invoice, amt_msat, payment_id, Retry::Attempts(0), &payer).unwrap();
376+
pay_invoice_using_amount(&invoice, amt_msat, payment_id, Retry::Attempts(0), None, &payer).unwrap();
372377
}
373378

374379
#[test]
@@ -382,7 +387,7 @@ mod tests {
382387
let invoice = invoice(payment_preimage);
383388
let amt_msat = 10_000;
384389

385-
match pay_zero_value_invoice(&invoice, amt_msat, Retry::Attempts(0), nodes[0].node) {
390+
match pay_zero_value_invoice(&invoice, amt_msat, Retry::Attempts(0), None, nodes[0].node) {
386391
Err(PaymentError::Invoice("amount unexpected")) => {},
387392
_ => panic!()
388393
}
@@ -418,7 +423,7 @@ mod tests {
418423
})
419424
.unwrap();
420425

421-
pay_invoice(&invoice, Retry::Attempts(0), nodes[0].node).unwrap();
426+
pay_invoice(&invoice, Retry::Attempts(0), None, nodes[0].node).unwrap();
422427
check_added_monitors(&nodes[0], 1);
423428
let send_event = SendEvent::from_node(&nodes[0]);
424429
nodes[1].node.handle_update_add_htlc(&nodes[0].node.get_our_node_id(), &send_event.msgs[0]);

0 commit comments

Comments
 (0)