Skip to content

Commit

Permalink
Add PaymentParameters to spontaneous send
Browse files Browse the repository at this point in the history
Added optional PaymentParameters to the send method in
SpontaneousPayment. If the user provides payment params
the values will be overridden otherwise they remain the
same. Updated the ldk_node.udl with these changes.
  • Loading branch information
slanesuke committed Aug 5, 2024
1 parent e05bcb0 commit 63fc07d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
2 changes: 1 addition & 1 deletion bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ interface Bolt12Payment {

interface SpontaneousPayment {
[Throws=NodeError]
PaymentId send(u64 amount_msat, PublicKey node_id);
PaymentId send(u64 amount_msat, PublicKey node_id, PaymentParameters? payment_parameters);
[Throws=NodeError]
void send_probes(u64 amount_msat, PublicKey node_id);
};
Expand Down
29 changes: 23 additions & 6 deletions src/payment/spontaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use crate::config::{Config, LDK_PAYMENT_RETRY_TIMEOUT};
use crate::error::Error;
use crate::logger::{log_error, log_info, FilesystemLogger, Logger};
use crate::payment::store::{
PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus, PaymentStore,
PaymentDetails, PaymentDirection, PaymentKind, PaymentParameters, PaymentStatus, PaymentStore,
};
use crate::types::{ChannelManager, KeysManager};

use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry, RetryableSendFailure};
use lightning::ln::{PaymentHash, PaymentPreimage};
use lightning::routing::router::{PaymentParameters, RouteParameters};
use lightning::routing::router::{PaymentParameters as PaymentParams, RouteParameters};
use lightning::sign::EntropySource;

use bitcoin::secp256k1::PublicKey;
Expand Down Expand Up @@ -41,8 +41,12 @@ impl SpontaneousPayment {
Self { runtime, channel_manager, keys_manager, payment_store, config, logger }
}

/// Send a spontaneous, aka. "keysend", payment
pub fn send(&self, amount_msat: u64, node_id: PublicKey) -> Result<PaymentId, Error> {
/// Send a spontaneous, or "keysend", payment given an amount in millisatoshi, node ID, and optional [`PaymentParameters`].
///
/// [`PaymentParameters`]: PaymentParameters
pub fn send(
&self, amount_msat: u64, node_id: PublicKey, payment_parameters: Option<PaymentParameters>,
) -> Result<PaymentId, Error> {
let rt_lock = self.runtime.read().unwrap();
if rt_lock.is_none() {
return Err(Error::NotRunning);
Expand All @@ -61,10 +65,23 @@ impl SpontaneousPayment {
}
}

let route_params = RouteParameters::from_payment_params_and_value(
PaymentParameters::from_node_id(node_id, self.config.default_cltv_expiry_delta),
let mut route_params = RouteParameters::from_payment_params_and_value(
PaymentParams::from_node_id(node_id, self.config.default_cltv_expiry_delta),
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 recipient_fields = RecipientOnionFields::spontaneous_empty();

match self.channel_manager.send_spontaneous_payment_with_retry(
Expand Down
2 changes: 1 addition & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
println!("\nA send_spontaneous_payment");
let keysend_amount_msat = 2500_000;
let keysend_payment_id =
node_a.spontaneous_payment().send(keysend_amount_msat, node_b.node_id()).unwrap();
node_a.spontaneous_payment().send(keysend_amount_msat, node_b.node_id(), None).unwrap();
expect_event!(node_a, PaymentSuccessful);
let received_keysend_amount = match node_b.wait_next_event() {
ref e @ Event::PaymentReceived { amount_msat, .. } => {
Expand Down

0 comments on commit 63fc07d

Please sign in to comment.