55// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
66// accordance with one or both of these licenses.
77
8+ use crate :: chain:: ChainSource ;
89use crate :: logger:: { log_debug, log_error, log_info, LdkLogger } ;
910use crate :: types:: { ChannelManager , KeysManager , LiquidityManager , PeerManager } ;
1011use crate :: { Config , Error } ;
@@ -15,9 +16,12 @@ use lightning::routing::router::{RouteHint, RouteHintHop};
1516use lightning_invoice:: { Bolt11Invoice , Bolt11InvoiceDescription , InvoiceBuilder , RoutingFees } ;
1617use lightning_liquidity:: events:: Event ;
1718use lightning_liquidity:: lsps0:: ser:: RequestId ;
19+ use lightning_liquidity:: lsps1:: client:: LSPS1ClientConfig ;
20+ use lightning_liquidity:: lsps2:: client:: LSPS2ClientConfig ;
1821use lightning_liquidity:: lsps2:: event:: LSPS2ClientEvent ;
1922use lightning_liquidity:: lsps2:: msgs:: OpeningFeeParams ;
2023use lightning_liquidity:: lsps2:: utils:: compute_opening_fee;
24+ use lightning_liquidity:: LiquidityClientConfig ;
2125
2226use bitcoin:: hashes:: { sha256, Hash } ;
2327use bitcoin:: secp256k1:: { PublicKey , Secp256k1 } ;
@@ -31,47 +35,126 @@ use std::time::Duration;
3135
3236const LIQUIDITY_REQUEST_TIMEOUT_SECS : u64 = 5 ;
3337
38+ struct LSPS1Service {
39+ node_id : PublicKey ,
40+ address : SocketAddress ,
41+ token : Option < String > ,
42+ client_config : LSPS1ClientConfig ,
43+ }
44+
3445struct LSPS2Service {
3546 node_id : PublicKey ,
3647 address : SocketAddress ,
3748 token : Option < String > ,
49+ client_config : LSPS2ClientConfig ,
3850 pending_fee_requests : Mutex < HashMap < RequestId , oneshot:: Sender < LSPS2FeeResponse > > > ,
3951 pending_buy_requests : Mutex < HashMap < RequestId , oneshot:: Sender < LSPS2BuyResponse > > > ,
4052}
4153
42- pub ( crate ) struct LiquiditySource < L : Deref >
54+ pub ( crate ) struct LiquiditySourceBuilder < L : Deref >
4355where
4456 L :: Target : LdkLogger ,
4557{
58+ lsps1_service : Option < LSPS1Service > ,
4659 lsps2_service : Option < LSPS2Service > ,
4760 channel_manager : Arc < ChannelManager > ,
4861 keys_manager : Arc < KeysManager > ,
49- liquidity_manager : Arc < LiquidityManager > ,
62+ chain_source : Arc < ChainSource > ,
5063 config : Arc < Config > ,
5164 logger : L ,
5265}
5366
54- impl < L : Deref > LiquiditySource < L >
67+ impl < L : Deref > LiquiditySourceBuilder < L >
5568where
5669 L :: Target : LdkLogger ,
5770{
58- pub ( crate ) fn new_lsps2 (
59- node_id : PublicKey , address : SocketAddress , token : Option < String > ,
71+ pub ( crate ) fn new (
6072 channel_manager : Arc < ChannelManager > , keys_manager : Arc < KeysManager > ,
61- liquidity_manager : Arc < LiquidityManager > , config : Arc < Config > , logger : L ,
73+ chain_source : Arc < ChainSource > , config : Arc < Config > , logger : L ,
6274 ) -> Self {
75+ let lsps1_service = None ;
76+ let lsps2_service = None ;
77+ Self {
78+ lsps1_service,
79+ lsps2_service,
80+ channel_manager,
81+ keys_manager,
82+ chain_source,
83+ config,
84+ logger,
85+ }
86+ }
87+
88+ pub ( crate ) fn lsps1_service (
89+ & mut self , node_id : PublicKey , address : SocketAddress , token : Option < String > ,
90+ ) -> & mut Self {
91+ // TODO: allow to set max_channel_fees_msat
92+ let client_config = LSPS1ClientConfig { max_channel_fees_msat : None } ;
93+ self . lsps1_service = Some ( LSPS1Service { node_id, address, token, client_config } ) ;
94+ self
95+ }
96+
97+ pub ( crate ) fn lsps2_service (
98+ & mut self , node_id : PublicKey , address : SocketAddress , token : Option < String > ,
99+ ) -> & mut Self {
100+ let client_config = LSPS2ClientConfig { } ;
63101 let pending_fee_requests = Mutex :: new ( HashMap :: new ( ) ) ;
64102 let pending_buy_requests = Mutex :: new ( HashMap :: new ( ) ) ;
65- let lsps2_service = Some ( LSPS2Service {
103+ self . lsps2_service = Some ( LSPS2Service {
66104 node_id,
67105 address,
68106 token,
107+ client_config,
69108 pending_fee_requests,
70109 pending_buy_requests,
71110 } ) ;
72- Self { lsps2_service , channel_manager , keys_manager , liquidity_manager , config , logger }
111+ self
73112 }
74113
114+ pub ( crate ) fn build ( self ) -> LiquiditySource < L > {
115+ let lsps1_client_config = self . lsps1_service . as_ref ( ) . map ( |s| s. client_config . clone ( ) ) ;
116+ let lsps2_client_config = self . lsps2_service . as_ref ( ) . map ( |s| s. client_config . clone ( ) ) ;
117+ let liquidity_client_config =
118+ Some ( LiquidityClientConfig { lsps1_client_config, lsps2_client_config } ) ;
119+
120+ let liquidity_manager = Arc :: new ( LiquidityManager :: new (
121+ Arc :: clone ( & self . keys_manager ) ,
122+ Arc :: clone ( & self . channel_manager ) ,
123+ Some ( Arc :: clone ( & self . chain_source ) ) ,
124+ None ,
125+ None ,
126+ liquidity_client_config,
127+ ) ) ;
128+
129+ LiquiditySource {
130+ lsps1_service : self . lsps1_service ,
131+ lsps2_service : self . lsps2_service ,
132+ channel_manager : self . channel_manager ,
133+ keys_manager : self . keys_manager ,
134+ liquidity_manager,
135+ config : self . config ,
136+ logger : self . logger ,
137+ }
138+ }
139+ }
140+
141+ pub ( crate ) struct LiquiditySource < L : Deref >
142+ where
143+ L :: Target : LdkLogger ,
144+ {
145+ lsps1_service : Option < LSPS1Service > ,
146+ lsps2_service : Option < LSPS2Service > ,
147+ channel_manager : Arc < ChannelManager > ,
148+ keys_manager : Arc < KeysManager > ,
149+ liquidity_manager : Arc < LiquidityManager > ,
150+ config : Arc < Config > ,
151+ logger : L ,
152+ }
153+
154+ impl < L : Deref > LiquiditySource < L >
155+ where
156+ L :: Target : LdkLogger ,
157+ {
75158 pub ( crate ) fn set_peer_manager ( & self , peer_manager : Arc < PeerManager > ) {
76159 let process_msgs_callback = move || peer_manager. process_events ( ) ;
77160 self . liquidity_manager . set_process_msgs_callback ( process_msgs_callback) ;
@@ -81,7 +164,11 @@ where
81164 self . liquidity_manager . as_ref ( )
82165 }
83166
84- pub ( crate ) fn get_liquidity_source_details ( & self ) -> Option < ( PublicKey , SocketAddress ) > {
167+ pub ( crate ) fn get_lsps1_service_details ( & self ) -> Option < ( PublicKey , SocketAddress ) > {
168+ self . lsps1_service . as_ref ( ) . map ( |s| ( s. node_id , s. address . clone ( ) ) )
169+ }
170+
171+ pub ( crate ) fn get_lsps2_service_details ( & self ) -> Option < ( PublicKey , SocketAddress ) > {
85172 self . lsps2_service . as_ref ( ) . map ( |s| ( s. node_id , s. address . clone ( ) ) )
86173 }
87174
0 commit comments