@@ -108,6 +108,9 @@ pub trait LightningWallet: Send + Sync {
108108 & self , payment_hash : [ u8 ; 32 ] ,
109109 ) -> Pin < Box < dyn Future < Output = Option < ReceivedLightningPayment > > + Send + ' _ > > ;
110110
111+ /// Check if we already have a channel with the LSP
112+ fn has_channel_with_lsp ( & self ) -> bool ;
113+
111114 /// Open a channel with the LSP using on-chain funds
112115 fn open_channel_with_lsp (
113116 & self , amt : Amount ,
@@ -117,6 +120,16 @@ pub trait LightningWallet: Send + Sync {
117120 fn await_channel_pending (
118121 & self , channel_id : u128 ,
119122 ) -> Pin < Box < dyn Future < Output = OutPoint > + Send + ' _ > > ;
123+
124+ /// Splice funds from on-chain to an existing channel with the LSP
125+ fn splice_to_lsp_channel (
126+ & self , amt : Amount ,
127+ ) -> Pin < Box < dyn Future < Output = Result < u128 , Self :: Error > > + Send + ' _ > > ;
128+
129+ /// Wait for a splice pending notification, returns the splice outpoint
130+ fn await_splice_pending (
131+ & self , channel_id : u128 ,
132+ ) -> Pin < Box < dyn Future < Output = OutPoint > + Send + ' _ > > ;
120133}
121134
122135/// Represents a payment from the lightning wallet
@@ -176,15 +189,19 @@ pub enum RebalancerEvent {
176189/// Trait for handling rebalancer events
177190pub trait EventHandler : Send + Sync {
178191 /// Handle a rebalancer event
179- fn handle_event ( & self , event : RebalancerEvent ) ;
192+ fn handle_event ( & self , event : RebalancerEvent )
193+ -> Pin < Box < dyn Future < Output = ( ) > + Send + ' _ > > ;
180194}
181195
182196/// A no-op event handler that discards all events
183197#[ derive( Debug , Copy , Clone , Default ) ]
184198pub struct IgnoringEventHandler ;
185199
186200impl EventHandler for IgnoringEventHandler {
187- fn handle_event ( & self , _event : RebalancerEvent ) {
201+ fn handle_event (
202+ & self , _event : RebalancerEvent ,
203+ ) -> Pin < Box < dyn Future < Output = ( ) > + Send + ' _ > > {
204+ Box :: pin ( async move { } )
188205 // Do nothing
189206 }
190207}
@@ -260,11 +277,13 @@ where
260277 rebalance_id. as_hex( )
261278 ) ;
262279
263- self . event_handler . handle_event ( RebalancerEvent :: RebalanceInitiated {
264- trigger_id : params. id ,
265- trusted_rebalance_payment_id : rebalance_id,
266- amount_msat : transfer_amt. milli_sats ( ) ,
267- } ) ;
280+ self . event_handler
281+ . handle_event ( RebalancerEvent :: RebalanceInitiated {
282+ trigger_id : params. id ,
283+ trusted_rebalance_payment_id : rebalance_id,
284+ amount_msat : transfer_amt. milli_sats ( ) ,
285+ } )
286+ . await ;
268287
269288 let ln_payment = match self
270289 . ln_wallet
@@ -297,14 +316,16 @@ where
297316 ln_payment. id. as_hex( ) ,
298317 ) ;
299318
300- self . event_handler . handle_event ( RebalancerEvent :: RebalanceSuccessful {
301- trigger_id : params. id ,
302- trusted_rebalance_payment_id : rebalance_id,
303- ln_rebalance_payment_id : ln_payment. id ,
304- amount_msat : transfer_amt. milli_sats ( ) ,
305- fee_msat : ln_payment. fee_paid_msat . unwrap_or_default ( )
306- + trusted_payment. fee_paid_msat . unwrap_or_default ( ) ,
307- } ) ;
319+ self . event_handler
320+ . handle_event ( RebalancerEvent :: RebalanceSuccessful {
321+ trigger_id : params. id ,
322+ trusted_rebalance_payment_id : rebalance_id,
323+ ln_rebalance_payment_id : ln_payment. id ,
324+ amount_msat : transfer_amt. milli_sats ( ) ,
325+ fee_msat : ln_payment. fee_paid_msat . unwrap_or_default ( )
326+ + trusted_payment. fee_paid_msat . unwrap_or_default ( ) ,
327+ } )
328+ . await ;
308329 } ,
309330 Err ( e) => {
310331 log_info ! ( self . logger, "Rebalance trusted transaction failed with {e:?}" , ) ;
@@ -313,34 +334,55 @@ where
313334 }
314335 }
315336
316- /// Perform on-chain to lightning rebalance by opening a channel
337+ /// Perform on-chain to lightning rebalance by opening a channel or splicing into an existing one
317338 async fn do_onchain_rebalance ( & self , params : TriggerParams ) {
318- // This should open a channel with the LSP using available on-chain funds
319-
320339 let _ = self . balance_mutex . lock ( ) . await ;
321340
322- log_info ! ( self . logger, "Opening channel with LSP with on-chain funds" ) ;
341+ let ( channel_outpoint, user_channel_id) = if self . ln_wallet . has_channel_with_lsp ( ) {
342+ log_info ! ( self . logger, "Splicing into channel with LSP with on-chain funds" ) ;
323343
324- // todo for now we can only open a channel, eventually move to splicing
325- let user_chan_id = match self . ln_wallet . open_channel_with_lsp ( params. amount ) . await {
326- Ok ( chan_id) => chan_id,
327- Err ( e) => {
328- log_error ! ( self . logger, "Failed to open channel with LSP: {e:?}" ) ;
329- return ;
330- } ,
331- } ;
344+ let user_chan_id = match self . ln_wallet . splice_to_lsp_channel ( params. amount ) . await {
345+ Ok ( chan_id) => chan_id,
346+ Err ( e) => {
347+ log_error ! ( self . logger, "Failed to open channel with LSP: {e:?}" ) ;
348+ return ;
349+ } ,
350+ } ;
351+
352+ log_info ! ( self . logger, "Initiated splice opened with LSP" ) ;
353+
354+ let channel_outpoint = self . ln_wallet . await_splice_pending ( user_chan_id) . await ;
355+
356+ log_info ! ( self . logger, "Splice initiated at: {channel_outpoint}" ) ;
357+
358+ ( channel_outpoint, user_chan_id)
359+ } else {
360+ log_info ! ( self . logger, "Opening channel with LSP with on-chain funds" ) ;
332361
333- log_info ! ( self . logger, "Initiated channel opened with LSP" ) ;
362+ let user_chan_id = match self . ln_wallet . open_channel_with_lsp ( params. amount ) . await {
363+ Ok ( chan_id) => chan_id,
364+ Err ( e) => {
365+ log_error ! ( self . logger, "Failed to open channel with LSP: {e:?}" ) ;
366+ return ;
367+ } ,
368+ } ;
369+
370+ log_info ! ( self . logger, "Initiated channel opened with LSP" ) ;
334371
335- let channel_outpoint = self . ln_wallet . await_channel_pending ( user_chan_id) . await ;
372+ let channel_outpoint = self . ln_wallet . await_channel_pending ( user_chan_id) . await ;
336373
337- log_info ! ( self . logger, "Channel open succeeded at: {channel_outpoint}" , ) ;
374+ log_info ! ( self . logger, "Channel open succeeded at: {channel_outpoint}" ) ;
375+
376+ ( channel_outpoint, user_chan_id)
377+ } ;
338378
339- self . event_handler . handle_event ( RebalancerEvent :: OnChainRebalanceInitiated {
340- trigger_id : params. id ,
341- user_channel_id : user_chan_id,
342- channel_outpoint,
343- } ) ;
379+ self . event_handler
380+ . handle_event ( RebalancerEvent :: OnChainRebalanceInitiated {
381+ trigger_id : params. id ,
382+ user_channel_id,
383+ channel_outpoint,
384+ } )
385+ . await ;
344386 }
345387
346388 /// Stops the rebalancer, waits for any active rebalances to complete
0 commit comments