Skip to content

Commit

Permalink
WIP Add LSPS2ServiceEvent handling
Browse files Browse the repository at this point in the history
.. so far with dummy values, we'll have to discuss how the API should
look like.
  • Loading branch information
tnull committed Dec 10, 2024
1 parent 5dc3678 commit cd2795d
Showing 1 changed file with 109 additions and 2 deletions.
111 changes: 109 additions & 2 deletions src/liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use lightning_liquidity::lsps1::msgs::{
ChannelInfo, LSPS1Options, OrderId, OrderParameters, PaymentInfo,
};
use lightning_liquidity::lsps2::client::LSPS2ClientConfig;
use lightning_liquidity::lsps2::event::LSPS2ClientEvent;
use lightning_liquidity::lsps2::msgs::OpeningFeeParams;
use lightning_liquidity::lsps2::event::{LSPS2ClientEvent, LSPS2ServiceEvent};
use lightning_liquidity::lsps2::msgs::{OpeningFeeParams, RawOpeningFeeParams};
use lightning_liquidity::lsps2::service::LSPS2ServiceConfig;
use lightning_liquidity::lsps2::utils::compute_opening_fee;
use lightning_liquidity::{LiquidityClientConfig, LiquidityServiceConfig};
Expand All @@ -39,6 +39,8 @@ use bitcoin::secp256k1::{PublicKey, Secp256k1};

use tokio::sync::oneshot;

use chrono::{DateTime, Utc};

use std::collections::HashMap;
use std::ops::Deref;
use std::sync::{Arc, Mutex, RwLock};
Expand Down Expand Up @@ -397,6 +399,111 @@ where
log_error!(self.logger, "Received unexpected LSPS1Client::OrderStatus event!");
}
},
Event::LSPS2Service(LSPS2ServiceEvent::GetInfo {
request_id,
counterparty_node_id,
token: _,
}) => {
if let Some(lsps2_service_handler) =
self.liquidity_manager.lsps2_service_handler().as_ref()
{
let min_fee_msat = 0;
let proportional = 0;
let mut valid_until: DateTime<Utc> = Utc::now();
valid_until += chrono::Duration::minutes(10);
let min_lifetime = 1008;
let max_client_to_self_delay = 144;
let min_payment_size_msat = 1000;
let max_payment_size_msat = 10_000_000_000;

let opening_fee_params = RawOpeningFeeParams {
min_fee_msat,
proportional,
valid_until,
min_lifetime,
max_client_to_self_delay,
min_payment_size_msat,
max_payment_size_msat,
};

let opening_fee_params_menu = vec![opening_fee_params];

if let Err(e) = lsps2_service_handler.opening_fee_params_generated(
&counterparty_node_id,
request_id,
opening_fee_params_menu,
) {
log_error!(
self.logger,
"Failed to handle generated opening fee params: {:?}",
e
);
}
} else {
log_error!(self.logger, "Failed to handle LSPS2ServiceEvent as LSPS2 liquidity service was not configured.",);
return;
}
},
Event::LSPS2Service(LSPS2ServiceEvent::BuyRequest {
request_id,
counterparty_node_id,
opening_fee_params: _,
payment_size_msat: _,
}) => {
if let Some(lsps2_service_handler) =
self.liquidity_manager.lsps2_service_handler().as_ref()
{
let user_channel_id = 0;
let scid = self.channel_manager.get_intercept_scid();
let cltv_expiry_delta = 72;
let client_trusts_lsp = true;

if let Err(e) = lsps2_service_handler.invoice_parameters_generated(
&counterparty_node_id,
request_id,
scid,
cltv_expiry_delta,
client_trusts_lsp,
user_channel_id,
) {
log_error!(self.logger, "Failed to provide invoice parameters: {:?}", e);
}
} else {
log_error!(self.logger, "Failed to handle LSPS2ServiceEvent as LSPS2 liquidity service was not configured.",);
return;
}
},
Event::LSPS2Service(LSPS2ServiceEvent::OpenChannel {
their_network_key,
amt_to_forward_msat,
opening_fee_msat: _,
user_channel_id,
intercept_scid: _,
}) => {
if self.liquidity_manager.lsps2_service_handler().is_none() {
log_error!(self.logger, "Failed to handle LSPS2ServiceEvent as LSPS2 liquidity service was not configured.",);
return;
};

let channel_size_sats = (amt_to_forward_msat / 1000) * 4;
let mut config = *self.channel_manager.get_current_default_configuration();
config
.channel_handshake_config
.max_inbound_htlc_value_in_flight_percent_of_channel = 100;
config.channel_config.forwarding_fee_base_msat = 0;
config.channel_config.forwarding_fee_proportional_millionths = 0;

if let Err(e) = self.channel_manager.create_channel(
their_network_key,
channel_size_sats,
0,
user_channel_id,
None,
Some(config),
) {
log_error!(self.logger, "Failed to open jit channel: {:?}", e);
}
},
Event::LSPS2Client(LSPS2ClientEvent::OpeningParametersReady {
request_id,
counterparty_node_id,
Expand Down

0 comments on commit cd2795d

Please sign in to comment.