@@ -15,6 +15,9 @@ use tokio::sync::broadcast;
1515use tokio_util:: sync:: CancellationToken ;
1616
1717use crate :: mdk:: error:: { MdkError , SpliceError } ;
18+ use crate :: mdk:: max_sendable:: {
19+ compute_estimate, ChannelSnapshot , MaxSendableConfig , MaxSendableError , MaxSendableEstimate ,
20+ } ;
1821use crate :: mdk:: mdk_api:: client:: MdkApiClient ;
1922use crate :: mdk:: mdk_api:: types:: {
2023 CheckoutCustomer , CreateCheckoutRequest , PaymentEntry , PaymentReceivedRequest ,
@@ -37,6 +40,7 @@ pub struct MdkClient {
3740 api : Arc < MdkApiClient > ,
3841 lsp_pubkey : PublicKey ,
3942 splice_cfg : SpliceConfig ,
43+ max_sendable_cfg : MaxSendableConfig ,
4044 event_tx : broadcast:: Sender < MdkEvent > ,
4145 event_handler : Option < EventHandler > ,
4246 shutdown : CancellationToken ,
@@ -77,6 +81,7 @@ impl MdkClient {
7781 let lsp_pubkey = PublicKey :: from_str ( & config. infra . lsp_node_id )
7882 . map_err ( |e| MdkError :: InvalidInput ( format ! ( "bad lsp_node_id: {e}" ) ) ) ?;
7983 let splice_cfg = config. splice . clone ( ) ;
84+ let max_sendable_cfg = config. max_sendable . clone ( ) ;
8085
8186 let node = build_node ( config, handle. clone ( ) ) ?;
8287 let http_client = build_http_client ( socks_proxy. as_deref ( ) ) ?;
@@ -92,6 +97,7 @@ impl MdkClient {
9297 api,
9398 lsp_pubkey,
9499 splice_cfg,
100+ max_sendable_cfg,
95101 event_tx,
96102 event_handler,
97103 shutdown : CancellationToken :: new ( ) ,
@@ -137,6 +143,20 @@ impl MdkClient {
137143 self . lsp_pubkey
138144 }
139145
146+ /// Best-effort estimate of the largest amount that can flow out
147+ /// over Lightning right now, with routing-fee headroom subtracted.
148+ /// Computed inline from `node.list_channels()` on every call so
149+ /// the result reflects in-flight HTLCs and reserve as of *now*.
150+ pub fn max_sendable ( & self ) -> Result < MaxSendableEstimate , MaxSendableError > {
151+ let snaps: Vec < ChannelSnapshot > = self
152+ . node
153+ . list_channels ( )
154+ . iter ( )
155+ . map ( ChannelSnapshot :: from)
156+ . collect ( ) ;
157+ compute_estimate ( & snaps, & self . lsp_pubkey , & self . max_sendable_cfg )
158+ }
159+
140160 /// Splice `amount_sats` of confirmed on-chain funds into the
141161 /// existing channel identified by `user_channel_id`, with the
142162 /// LSP as counterparty.
0 commit comments