@@ -109,7 +109,7 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
109109
110110pub use balance:: { BalanceDetails , LightningBalance , PendingSweepBalance } ;
111111use bitcoin:: secp256k1:: PublicKey ;
112- use bitcoin:: Amount ;
112+ use bitcoin:: { Address , Amount } ;
113113#[ cfg( feature = "uniffi" ) ]
114114pub use builder:: ArcedNodeBuilder as Builder ;
115115pub use builder:: BuildError ;
@@ -1339,6 +1339,72 @@ impl Node {
13391339 }
13401340 }
13411341
1342+ /// Remove funds from an existing channel, sending them to an on-chain address.
1343+ ///
1344+ /// This provides for decreasing a channel's outbound liquidity without re-balancing or closing
1345+ /// it. Once negotiation with the counterparty is complete, the channel remains operational
1346+ /// while waiting for a new funding transaction to confirm.
1347+ ///
1348+ /// # Experimental API
1349+ ///
1350+ /// This API is experimental. Currently, a splice-out will be marked as an inbound payment if
1351+ /// paid to an address associated with the on-chain wallet, but this classification may change
1352+ /// in the future.
1353+ pub fn splice_out (
1354+ & self , user_channel_id : & UserChannelId , counterparty_node_id : PublicKey , address : & Address ,
1355+ splice_amount_sats : u64 ,
1356+ ) -> Result < ( ) , Error > {
1357+ let open_channels =
1358+ self . channel_manager . list_channels_with_counterparty ( & counterparty_node_id) ;
1359+ if let Some ( channel_details) =
1360+ open_channels. iter ( ) . find ( |c| c. user_channel_id == user_channel_id. 0 )
1361+ {
1362+ if splice_amount_sats > channel_details. outbound_capacity_msat {
1363+ return Err ( Error :: ChannelSplicingFailed ) ;
1364+ }
1365+
1366+ self . wallet . parse_and_validate_address ( address) ?;
1367+
1368+ let contribution = SpliceContribution :: SpliceOut {
1369+ outputs : vec ! [ bitcoin:: TxOut {
1370+ value: Amount :: from_sat( splice_amount_sats) ,
1371+ script_pubkey: address. script_pubkey( ) ,
1372+ } ] ,
1373+ } ;
1374+
1375+ let fee_rate = self . fee_estimator . estimate_fee_rate ( ConfirmationTarget :: ChannelFunding ) ;
1376+ let funding_feerate_per_kw: u32 = match fee_rate. to_sat_per_kwu ( ) . try_into ( ) {
1377+ Ok ( fee_rate) => fee_rate,
1378+ Err ( _) => {
1379+ debug_assert ! ( false , "FeeRate should always fit within u32" ) ;
1380+ log_error ! ( self . logger, "FeeRate should always fit within u32" ) ;
1381+ fee_estimator:: get_fallback_rate_for_target ( ConfirmationTarget :: ChannelFunding )
1382+ } ,
1383+ } ;
1384+
1385+ self . channel_manager
1386+ . splice_channel (
1387+ & channel_details. channel_id ,
1388+ & counterparty_node_id,
1389+ contribution,
1390+ funding_feerate_per_kw,
1391+ None ,
1392+ )
1393+ . map_err ( |e| {
1394+ log_error ! ( self . logger, "Failed to splice channel: {:?}" , e) ;
1395+ Error :: ChannelSplicingFailed
1396+ } )
1397+ } else {
1398+ log_error ! (
1399+ self . logger,
1400+ "Channel not found for user_channel_id {} and counterparty {}" ,
1401+ user_channel_id,
1402+ counterparty_node_id
1403+ ) ;
1404+ Err ( Error :: ChannelSplicingFailed )
1405+ }
1406+ }
1407+
13421408 /// Manually sync the LDK and BDK wallets with the current chain state and update the fee rate
13431409 /// cache.
13441410 ///
0 commit comments