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 ;
@@ -896,3 +899,113 @@ pub(crate) struct LSPS2BuyResponse {
896
899
intercept_scid : u64 ,
897
900
cltv_expiry_delta : u32 ,
898
901
}
902
+
903
+ /// A liquidity handler allowing to request channels via the [bLIP-51 / LSPS1] protocol.
904
+ ///
905
+ /// Should be retrieved by calling [`Node::lsps1_liquidity`].
906
+ ///
907
+ /// [bLIP-51 / LSPS1]: https://github.com/lightning/blips/blob/master/blip-0051.md
908
+ /// [`Node::lsps1_liquidity`]: crate::Node::lsps1_liquidity
909
+ #[ derive( Clone ) ]
910
+ pub struct LSPS1Liquidity {
911
+ runtime : Arc < RwLock < Option < Arc < tokio:: runtime:: Runtime > > > > ,
912
+ wallet : Arc < Wallet > ,
913
+ connection_manager : Arc < ConnectionManager < Arc < Logger > > > ,
914
+ liquidity_source : Option < Arc < LiquiditySource < Arc < Logger > > > > ,
915
+ logger : Arc < Logger > ,
916
+ }
917
+
918
+ impl LSPS1Liquidity {
919
+ pub ( crate ) fn new (
920
+ runtime : Arc < RwLock < Option < Arc < tokio:: runtime:: Runtime > > > > , wallet : Arc < Wallet > ,
921
+ connection_manager : Arc < ConnectionManager < Arc < Logger > > > ,
922
+ liquidity_source : Option < Arc < LiquiditySource < Arc < Logger > > > > , logger : Arc < Logger > ,
923
+ ) -> Self {
924
+ Self { runtime, wallet, connection_manager, liquidity_source, logger }
925
+ }
926
+
927
+ /// Connects to the configured LSP and places an order for an inbound channel.
928
+ ///
929
+ /// The channel will be opened after one of the returned payment options has successfully been
930
+ /// paid.
931
+ pub fn request_channel (
932
+ & self , lsp_balance_sat : u64 , client_balance_sat : u64 , channel_expiry_blocks : u32 ,
933
+ announce_channel : bool ,
934
+ ) -> Result < LSPS1OrderStatus , Error > {
935
+ let liquidity_source =
936
+ self . liquidity_source . as_ref ( ) . ok_or ( Error :: LiquiditySourceUnavailable ) ?;
937
+
938
+ let ( lsp_node_id, lsp_address) = liquidity_source
939
+ . get_lsps1_service_details ( )
940
+ . ok_or ( Error :: LiquiditySourceUnavailable ) ?;
941
+
942
+ let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
943
+ let runtime = rt_lock. as_ref ( ) . unwrap ( ) ;
944
+
945
+ let con_node_id = lsp_node_id;
946
+ let con_addr = lsp_address. clone ( ) ;
947
+ let con_cm = Arc :: clone ( & self . connection_manager ) ;
948
+
949
+ // We need to use our main runtime here as a local runtime might not be around to poll
950
+ // connection futures going forward.
951
+ tokio:: task:: block_in_place ( move || {
952
+ runtime. block_on ( async move {
953
+ con_cm. connect_peer_if_necessary ( con_node_id, con_addr) . await
954
+ } )
955
+ } ) ?;
956
+
957
+ log_info ! ( self . logger, "Connected to LSP {}@{}. " , lsp_node_id, lsp_address) ;
958
+
959
+ let refund_address = self . wallet . get_new_address ( ) ?;
960
+
961
+ let liquidity_source = Arc :: clone ( & liquidity_source) ;
962
+ let response = tokio:: task:: block_in_place ( move || {
963
+ runtime. block_on ( async move {
964
+ liquidity_source
965
+ . lsps1_request_channel (
966
+ lsp_balance_sat,
967
+ client_balance_sat,
968
+ channel_expiry_blocks,
969
+ announce_channel,
970
+ refund_address,
971
+ )
972
+ . await
973
+ } )
974
+ } ) ?;
975
+
976
+ Ok ( response)
977
+ }
978
+
979
+ /// Connects to the configured LSP and checks for the status of a previously-placed order.
980
+ pub fn check_order_status ( & self , order_id : OrderId ) -> Result < LSPS1OrderStatus , Error > {
981
+ let liquidity_source =
982
+ self . liquidity_source . as_ref ( ) . ok_or ( Error :: LiquiditySourceUnavailable ) ?;
983
+
984
+ let ( lsp_node_id, lsp_address) = liquidity_source
985
+ . get_lsps1_service_details ( )
986
+ . ok_or ( Error :: LiquiditySourceUnavailable ) ?;
987
+
988
+ let rt_lock = self . runtime . read ( ) . unwrap ( ) ;
989
+ let runtime = rt_lock. as_ref ( ) . unwrap ( ) ;
990
+
991
+ let con_node_id = lsp_node_id;
992
+ let con_addr = lsp_address. clone ( ) ;
993
+ let con_cm = Arc :: clone ( & self . connection_manager ) ;
994
+
995
+ // We need to use our main runtime here as a local runtime might not be around to poll
996
+ // connection futures going forward.
997
+ tokio:: task:: block_in_place ( move || {
998
+ runtime. block_on ( async move {
999
+ con_cm. connect_peer_if_necessary ( con_node_id, con_addr) . await
1000
+ } )
1001
+ } ) ?;
1002
+
1003
+ let liquidity_source = Arc :: clone ( & liquidity_source) ;
1004
+ let response = tokio:: task:: block_in_place ( move || {
1005
+ runtime
1006
+ . block_on ( async move { liquidity_source. lsps1_check_order_status ( order_id) . await } )
1007
+ } ) ?;
1008
+
1009
+ Ok ( response)
1010
+ }
1011
+ }
0 commit comments