5
5
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
6
6
// accordance with one or both of these licenses.
7
7
8
+ //! Objects related to liquidity management.
9
+
8
10
use crate :: chain:: ChainSource ;
9
- use crate :: logger:: { log_debug, log_error, log_info, LdkLogger } ;
10
- use crate :: types:: { ChannelManager , KeysManager , LiquidityManager , PeerManager } ;
11
+ use crate :: connection:: ConnectionManager ;
12
+ use crate :: logger:: { log_debug, log_error, log_info, LdkLogger , Logger } ;
13
+ use crate :: types:: { ChannelManager , KeysManager , LiquidityManager , PeerManager , Wallet } ;
11
14
use crate :: { Config , Error } ;
12
15
13
16
use lightning:: ln:: channelmanager:: MIN_FINAL_CLTV_EXPIRY_DELTA ;
@@ -34,7 +37,7 @@ use tokio::sync::oneshot;
34
37
35
38
use std:: collections:: HashMap ;
36
39
use std:: ops:: Deref ;
37
- use std:: sync:: { Arc , Mutex } ;
40
+ use std:: sync:: { Arc , Mutex , RwLock } ;
38
41
use std:: time:: Duration ;
39
42
40
43
const LIQUIDITY_REQUEST_TIMEOUT_SECS : u64 = 5 ;
@@ -892,3 +895,113 @@ pub(crate) struct LSPS2BuyResponse {
892
895
intercept_scid : u64 ,
893
896
cltv_expiry_delta : u32 ,
894
897
}
898
+
899
+ /// A liquidity handler allowing to request channels via the [LSPS1] protocol.
900
+ ///
901
+ /// Should be retrieved by calling [`Node::lsps1_liquidity`].
902
+ ///
903
+ /// [LSPS1]: https://github.com/BitcoinAndLightningLayerSpecs/lsp/tree/main/LSPS1
904
+ /// [`Node::lsps1_liquidity`]: crate::Node::lsps1_liquidity
905
+ #[ derive( Clone ) ]
906
+ pub struct Lsps1Liquidity {
907
+ runtime : Arc < RwLock < Option < Arc < tokio:: runtime:: Runtime > > > > ,
908
+ wallet : Arc < Wallet > ,
909
+ connection_manager : Arc < ConnectionManager < Arc < Logger > > > ,
910
+ liquidity_source : Option < Arc < LiquiditySource < Arc < Logger > > > > ,
911
+ logger : Arc < Logger > ,
912
+ }
913
+
914
+ impl Lsps1Liquidity {
915
+ pub ( crate ) fn new (
916
+ runtime : Arc < RwLock < Option < Arc < tokio:: runtime:: Runtime > > > > , wallet : Arc < Wallet > ,
917
+ connection_manager : Arc < ConnectionManager < Arc < Logger > > > ,
918
+ liquidity_source : Option < Arc < LiquiditySource < Arc < Logger > > > > , logger : Arc < Logger > ,
919
+ ) -> Self {
920
+ Self { runtime, wallet, connection_manager, liquidity_source, logger }
921
+ }
922
+
923
+ /// Connects to the configured LSP and places an order for an inbound channel.
924
+ ///
925
+ /// The channel will be opened after one of the returned payment options has successfully been
926
+ /// paid.
927
+ pub fn request_channel (
928
+ & self , lsp_balance_sat : u64 , client_balance_sat : u64 , channel_expiry_blocks : u32 ,
929
+ announce_channel : bool ,
930
+ ) -> Result < LSPS1OrderStatus , Error > {
931
+ let liquidity_source =
932
+ self . liquidity_source . as_ref ( ) . ok_or ( Error :: LiquiditySourceUnavailable ) ?;
933
+
934
+ let ( lsp_node_id, lsp_address) = liquidity_source
935
+ . get_lsps1_service_details ( )
936
+ . ok_or ( Error :: LiquiditySourceUnavailable ) ?;
937
+
938
+ let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
939
+ let runtime = rt_lock. as_ref ( ) . unwrap ( ) ;
940
+
941
+ let con_node_id = lsp_node_id;
942
+ let con_addr = lsp_address. clone ( ) ;
943
+ let con_cm = Arc :: clone ( & self . connection_manager ) ;
944
+
945
+ // We need to use our main runtime here as a local runtime might not be around to poll
946
+ // connection futures going forward.
947
+ tokio:: task:: block_in_place ( move || {
948
+ runtime. block_on ( async move {
949
+ con_cm. connect_peer_if_necessary ( con_node_id, con_addr) . await
950
+ } )
951
+ } ) ?;
952
+
953
+ log_info ! ( self . logger, "Connected to LSP {}@{}. " , lsp_node_id, lsp_address) ;
954
+
955
+ let refund_address = self . wallet . get_new_address ( ) ?;
956
+
957
+ let liquidity_source = Arc :: clone ( & liquidity_source) ;
958
+ let response = tokio:: task:: block_in_place ( move || {
959
+ runtime. block_on ( async move {
960
+ liquidity_source
961
+ . lsps1_request_channel (
962
+ lsp_balance_sat,
963
+ client_balance_sat,
964
+ channel_expiry_blocks,
965
+ announce_channel,
966
+ refund_address,
967
+ )
968
+ . await
969
+ } )
970
+ } ) ?;
971
+
972
+ Ok ( response)
973
+ }
974
+
975
+ /// Connects to the configured LSP and checks for the status of a previously-placed order.
976
+ pub fn check_order_status ( & self , order_id : OrderId ) -> Result < LSPS1OrderStatus , Error > {
977
+ let liquidity_source =
978
+ self . liquidity_source . as_ref ( ) . ok_or ( Error :: LiquiditySourceUnavailable ) ?;
979
+
980
+ let ( lsp_node_id, lsp_address) = liquidity_source
981
+ . get_lsps1_service_details ( )
982
+ . ok_or ( Error :: LiquiditySourceUnavailable ) ?;
983
+
984
+ let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
985
+ let runtime = rt_lock. as_ref ( ) . unwrap ( ) ;
986
+
987
+ let con_node_id = lsp_node_id;
988
+ let con_addr = lsp_address. clone ( ) ;
989
+ let con_cm = Arc :: clone ( & self . connection_manager ) ;
990
+
991
+ // We need to use our main runtime here as a local runtime might not be around to poll
992
+ // connection futures going forward.
993
+ tokio:: task:: block_in_place ( move || {
994
+ runtime. block_on ( async move {
995
+ con_cm. connect_peer_if_necessary ( con_node_id, con_addr) . await
996
+ } )
997
+ } ) ?;
998
+
999
+ let liquidity_source = Arc :: clone ( & liquidity_source) ;
1000
+ let response = tokio:: task:: block_in_place ( move || {
1001
+ runtime
1002
+ . block_on ( async move { liquidity_source. lsps1_check_order_status ( order_id) . await } )
1003
+ } ) ?;
1004
+
1005
+ Ok ( response)
1006
+ }
1007
+ }
0 commit comments