Skip to content

Commit

Permalink
Refactor send to fallback to default sending params
Browse files Browse the repository at this point in the history
Refactored the send method in SpontaneousPayment to combine
user-set SendingParameters with default values from Config.
If the user sets any parameter to None, the related default
value is used.
  • Loading branch information
slanesuke committed Aug 13, 2024
1 parent c15561e commit a43932d
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/payment/spontaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ impl SpontaneousPayment {
}

/// Send a spontaneous, or "keysend", payment given an amount in millisatoshi, node ID, and optional [`SendingParameters`].

pub fn send(
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
) -> Result<PaymentId, Error> {
Expand All @@ -70,13 +69,38 @@ impl SpontaneousPayment {
amount_msat,
);

if let Some(payment_params) = sending_parameters {
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);
if let Some(user_set_params) = sending_parameters {
if let Some(mut default_params) =
self.config.sending_parameters_config.as_ref().cloned()
{
default_params.max_total_routing_fee_msat = user_set_params
.max_total_routing_fee_msat
.or(default_params.max_total_routing_fee_msat);
default_params.max_total_cltv_expiry_delta = user_set_params
.max_total_cltv_expiry_delta
.or(default_params.max_total_cltv_expiry_delta);
default_params.max_path_count =
user_set_params.max_path_count.or(default_params.max_path_count);
default_params.max_channel_saturation_power_of_half = user_set_params
.max_channel_saturation_power_of_half
.or(default_params.max_channel_saturation_power_of_half);

route_params.max_total_routing_fee_msat = default_params.max_total_routing_fee_msat;
route_params.payment_params.max_total_cltv_expiry_delta =
default_params.max_total_cltv_expiry_delta.unwrap_or_default();
route_params.payment_params.max_path_count =
default_params.max_path_count.unwrap_or_default();
route_params.payment_params.max_channel_saturation_power_of_half =
default_params.max_channel_saturation_power_of_half.unwrap_or_default();
}
} else if let Some(default_params) = &self.config.sending_parameters_config {
route_params.max_total_routing_fee_msat = default_params.max_total_routing_fee_msat;
route_params.payment_params.max_total_cltv_expiry_delta =
default_params.max_total_cltv_expiry_delta.unwrap_or_default();
route_params.payment_params.max_path_count =
default_params.max_path_count.unwrap_or_default();
route_params.payment_params.max_channel_saturation_power_of_half =
default_params.max_channel_saturation_power_of_half.unwrap_or_default();
}

let recipient_fields = RecipientOnionFields::spontaneous_empty();
Expand Down

0 comments on commit a43932d

Please sign in to comment.