From 5e84db40a723fc0288455932e798594422ed510d Mon Sep 17 00:00:00 2001 From: Elias Rohrer Date: Tue, 10 Dec 2024 14:27:59 +0100 Subject: [PATCH] WIP Add `LSPS2ServiceEvent` handling .. so far with dummy values, we'll have to discuss how the API should look like. --- src/liquidity.rs | 111 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 2 deletions(-) diff --git a/src/liquidity.rs b/src/liquidity.rs index 21164290c..85df83747 100644 --- a/src/liquidity.rs +++ b/src/liquidity.rs @@ -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}; @@ -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}; @@ -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::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,