@@ -169,6 +169,7 @@ pub use {
169169} ;
170170
171171use crate :: scoring:: setup_background_pathfinding_scores_sync;
172+ use crate :: wallet:: FundingAmount ;
172173
173174#[ cfg( feature = "uniffi" ) ]
174175uniffi:: include_scaffolding!( "ldk_node" ) ;
@@ -1092,7 +1093,7 @@ impl Node {
10921093 }
10931094
10941095 fn open_channel_inner (
1095- & self , node_id : PublicKey , address : SocketAddress , channel_amount_sats : u64 ,
1096+ & self , node_id : PublicKey , address : SocketAddress , channel_amount_sats : FundingAmount ,
10961097 push_to_counterparty_msat : Option < u64 > , channel_config : Option < ChannelConfig > ,
10971098 announce_for_forwarding : bool ,
10981099 ) -> Result < UserChannelId , Error > {
@@ -1112,8 +1113,38 @@ impl Node {
11121113 con_cm. connect_peer_if_necessary ( con_node_id, con_addr) . await
11131114 } ) ?;
11141115
1115- // Check funds availability after connection (includes anchor reserve calculation)
1116- self . check_sufficient_funds_for_channel ( channel_amount_sats, & node_id) ?;
1116+ let channel_amount_sats = match channel_amount_sats {
1117+ FundingAmount :: Exact { amount_sats } => {
1118+ // Check funds availability after connection (includes anchor reserve
1119+ // calculation).
1120+ self . check_sufficient_funds_for_channel ( amount_sats, & peer_info. node_id ) ?;
1121+ amount_sats
1122+ } ,
1123+ FundingAmount :: Max => {
1124+ // Determine max funding amount from all available on-chain funds.
1125+ let cur_anchor_reserve_sats =
1126+ total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
1127+ let new_channel_reserve =
1128+ self . new_channel_anchor_reserve_sats ( & peer_info. node_id ) ?;
1129+ let total_anchor_reserve_sats = cur_anchor_reserve_sats + new_channel_reserve;
1130+
1131+ let fee_rate =
1132+ self . fee_estimator . estimate_fee_rate ( ConfirmationTarget :: ChannelFunding ) ;
1133+
1134+ let amount =
1135+ self . wallet . get_max_funding_amount ( total_anchor_reserve_sats, fee_rate) ?;
1136+
1137+ log_info ! (
1138+ self . logger,
1139+ "Opening channel with all balance: {}sats (fee rate: {} sat/kw, anchor reserve: {}sats)" ,
1140+ amount,
1141+ fee_rate. to_sat_per_kwu( ) ,
1142+ total_anchor_reserve_sats,
1143+ ) ;
1144+
1145+ amount
1146+ } ,
1147+ } ;
11171148
11181149 let mut user_config = default_user_config ( & self . config ) ;
11191150 user_config. channel_handshake_config . announce_for_forwarding = announce_for_forwarding;
@@ -1156,6 +1187,25 @@ impl Node {
11561187 }
11571188 }
11581189
1190+ fn new_channel_anchor_reserve_sats ( & self , peer_node_id : & PublicKey ) -> Result < u64 , Error > {
1191+ let init_features = self
1192+ . peer_manager
1193+ . peer_by_node_id ( peer_node_id)
1194+ . ok_or ( Error :: ConnectionFailed ) ?
1195+ . init_features ;
1196+ let sats = self . config . anchor_channels_config . as_ref ( ) . map_or ( 0 , |c| {
1197+ if init_features. requires_anchors_zero_fee_htlc_tx ( )
1198+ && !c. trusted_peers_no_reserve . contains ( peer_node_id)
1199+ {
1200+ c. per_channel_reserve_sats
1201+ } else {
1202+ 0
1203+ }
1204+ } ) ;
1205+
1206+ Ok ( sats)
1207+ }
1208+
11591209 fn check_sufficient_funds_for_channel (
11601210 & self , amount_sats : u64 , peer_node_id : & PublicKey ,
11611211 ) -> Result < ( ) , Error > {
@@ -1174,21 +1224,8 @@ impl Node {
11741224 }
11751225
11761226 // Fail if we have less than the channel value + anchor reserve available (if applicable).
1177- let init_features = self
1178- . peer_manager
1179- . peer_by_node_id ( peer_node_id)
1180- . ok_or ( Error :: ConnectionFailed ) ?
1181- . init_features ;
1182- let required_funds_sats = amount_sats
1183- + self . config . anchor_channels_config . as_ref ( ) . map_or ( 0 , |c| {
1184- if init_features. requires_anchors_zero_fee_htlc_tx ( )
1185- && !c. trusted_peers_no_reserve . contains ( peer_node_id)
1186- {
1187- c. per_channel_reserve_sats
1188- } else {
1189- 0
1190- }
1191- } ) ;
1227+ let required_funds_sats =
1228+ amount_sats + self . new_channel_anchor_reserve_sats ( peer_node_id) ?;
11921229
11931230 if spendable_amount_sats < required_funds_sats {
11941231 log_error ! ( self . logger,
@@ -1225,7 +1262,7 @@ impl Node {
12251262 self . open_channel_inner (
12261263 node_id,
12271264 address,
1228- channel_amount_sats,
1265+ FundingAmount :: Exact { amount_sats : channel_amount_sats } ,
12291266 push_to_counterparty_msat,
12301267 channel_config,
12311268 false ,
@@ -1265,7 +1302,72 @@ impl Node {
12651302 self . open_channel_inner (
12661303 node_id,
12671304 address,
1268- channel_amount_sats,
1305+ FundingAmount :: Exact { amount_sats : channel_amount_sats } ,
1306+ push_to_counterparty_msat,
1307+ channel_config,
1308+ true ,
1309+ )
1310+ }
1311+
1312+ /// Connect to a node and open a new unannounced channel, using all available on-chain funds
1313+ /// minus fees and anchor reserves.
1314+ ///
1315+ /// To open an announced channel, see [`Node::open_announced_channel_with_all`].
1316+ ///
1317+ /// Disconnects and reconnects are handled automatically.
1318+ ///
1319+ /// If `push_to_counterparty_msat` is set, the given value will be pushed (read: sent) to the
1320+ /// channel counterparty on channel open. This can be useful to start out with the balance not
1321+ /// entirely shifted to one side, therefore allowing to receive payments from the getgo.
1322+ ///
1323+ /// Returns a [`UserChannelId`] allowing to locally keep track of the channel.
1324+ ///
1325+ /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats
1326+ pub fn open_channel_with_all (
1327+ & self , node_id : PublicKey , address : SocketAddress , push_to_counterparty_msat : Option < u64 > ,
1328+ channel_config : Option < ChannelConfig > ,
1329+ ) -> Result < UserChannelId , Error > {
1330+ self . open_channel_inner (
1331+ node_id,
1332+ address,
1333+ FundingAmount :: Max ,
1334+ push_to_counterparty_msat,
1335+ channel_config,
1336+ false ,
1337+ )
1338+ }
1339+
1340+ /// Connect to a node and open a new announced channel, using all available on-chain funds
1341+ /// minus fees and anchor reserves.
1342+ ///
1343+ /// This will return an error if the node has not been sufficiently configured to operate as a
1344+ /// forwarding node that can properly announce its existence to the public network graph, i.e.,
1345+ /// [`Config::listening_addresses`] and [`Config::node_alias`] are unset.
1346+ ///
1347+ /// To open an unannounced channel, see [`Node::open_channel_with_all`].
1348+ ///
1349+ /// Disconnects and reconnects are handled automatically.
1350+ ///
1351+ /// If `push_to_counterparty_msat` is set, the given value will be pushed (read: sent) to the
1352+ /// channel counterparty on channel open. This can be useful to start out with the balance not
1353+ /// entirely shifted to one side, therefore allowing to receive payments from the getgo.
1354+ ///
1355+ /// Returns a [`UserChannelId`] allowing to locally keep track of the channel.
1356+ ///
1357+ /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats
1358+ pub fn open_announced_channel_with_all (
1359+ & self , node_id : PublicKey , address : SocketAddress , push_to_counterparty_msat : Option < u64 > ,
1360+ channel_config : Option < ChannelConfig > ,
1361+ ) -> Result < UserChannelId , Error > {
1362+ if let Err ( err) = may_announce_channel ( & self . config ) {
1363+ log_error ! ( self . logger, "Failed to open announced channel as the node hasn't been sufficiently configured to act as a forwarding node: {err}" ) ;
1364+ return Err ( Error :: ChannelCreationFailed ) ;
1365+ }
1366+
1367+ self . open_channel_inner (
1368+ node_id,
1369+ address,
1370+ FundingAmount :: Max ,
12691371 push_to_counterparty_msat,
12701372 channel_config,
12711373 true ,
0 commit comments