@@ -1091,7 +1091,7 @@ impl Node {
10911091 }
10921092
10931093 fn open_channel_inner (
1094- & self , node_id : PublicKey , address : SocketAddress , channel_amount_sats : u64 ,
1094+ & self , node_id : PublicKey , address : SocketAddress , channel_amount_sats : Option < u64 > ,
10951095 push_to_counterparty_msat : Option < u64 > , channel_config : Option < ChannelConfig > ,
10961096 announce_for_forwarding : bool ,
10971097 ) -> Result < UserChannelId , Error > {
@@ -1111,8 +1111,38 @@ impl Node {
11111111 con_cm. connect_peer_if_necessary ( con_node_id, con_addr) . await
11121112 } ) ?;
11131113
1114- // Check funds availability after connection (includes anchor reserve calculation)
1115- self . check_sufficient_funds_for_channel ( channel_amount_sats, & node_id) ?;
1114+ let channel_amount_sats = match channel_amount_sats {
1115+ Some ( amount) => {
1116+ // Check funds availability after connection (includes anchor reserve
1117+ // calculation).
1118+ self . check_sufficient_funds_for_channel ( amount, & peer_info. node_id ) ?;
1119+ amount
1120+ } ,
1121+ None => {
1122+ // Determine max funding amount from all available on-chain funds.
1123+ let cur_anchor_reserve_sats =
1124+ total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
1125+ let new_channel_reserve =
1126+ self . new_channel_anchor_reserve_sats ( & peer_info. node_id ) ?;
1127+ let total_anchor_reserve_sats = cur_anchor_reserve_sats + new_channel_reserve;
1128+
1129+ let fee_rate =
1130+ self . fee_estimator . estimate_fee_rate ( ConfirmationTarget :: ChannelFunding ) ;
1131+
1132+ let amount =
1133+ self . wallet . get_max_funding_amount ( total_anchor_reserve_sats, fee_rate) ?;
1134+
1135+ log_info ! (
1136+ self . logger,
1137+ "Opening channel with all balance: {}sats (fee rate: {} sat/kw, anchor reserve: {}sats)" ,
1138+ amount,
1139+ fee_rate. to_sat_per_kwu( ) ,
1140+ total_anchor_reserve_sats,
1141+ ) ;
1142+
1143+ amount
1144+ } ,
1145+ } ;
11161146
11171147 let mut user_config = default_user_config ( & self . config ) ;
11181148 user_config. channel_handshake_config . announce_for_forwarding = announce_for_forwarding;
@@ -1153,6 +1183,25 @@ impl Node {
11531183 }
11541184 }
11551185
1186+ fn new_channel_anchor_reserve_sats ( & self , peer_node_id : & PublicKey ) -> Result < u64 , Error > {
1187+ let init_features = self
1188+ . peer_manager
1189+ . peer_by_node_id ( peer_node_id)
1190+ . ok_or ( Error :: ConnectionFailed ) ?
1191+ . init_features ;
1192+ let sats = self . config . anchor_channels_config . as_ref ( ) . map_or ( 0 , |c| {
1193+ if init_features. requires_anchors_zero_fee_htlc_tx ( )
1194+ && !c. trusted_peers_no_reserve . contains ( peer_node_id)
1195+ {
1196+ c. per_channel_reserve_sats
1197+ } else {
1198+ 0
1199+ }
1200+ } ) ;
1201+
1202+ Ok ( sats)
1203+ }
1204+
11561205 fn check_sufficient_funds_for_channel (
11571206 & self , amount_sats : u64 , peer_node_id : & PublicKey ,
11581207 ) -> Result < ( ) , Error > {
@@ -1171,21 +1220,8 @@ impl Node {
11711220 }
11721221
11731222 // Fail if we have less than the channel value + anchor reserve available (if applicable).
1174- let init_features = self
1175- . peer_manager
1176- . peer_by_node_id ( peer_node_id)
1177- . ok_or ( Error :: ConnectionFailed ) ?
1178- . init_features ;
1179- let required_funds_sats = amount_sats
1180- + self . config . anchor_channels_config . as_ref ( ) . map_or ( 0 , |c| {
1181- if init_features. requires_anchors_zero_fee_htlc_tx ( )
1182- && !c. trusted_peers_no_reserve . contains ( peer_node_id)
1183- {
1184- c. per_channel_reserve_sats
1185- } else {
1186- 0
1187- }
1188- } ) ;
1223+ let required_funds_sats =
1224+ amount_sats + self . new_channel_anchor_reserve_sats ( peer_node_id) ?;
11891225
11901226 if spendable_amount_sats < required_funds_sats {
11911227 log_error ! ( self . logger,
@@ -1222,7 +1258,7 @@ impl Node {
12221258 self . open_channel_inner (
12231259 node_id,
12241260 address,
1225- channel_amount_sats,
1261+ Some ( channel_amount_sats) ,
12261262 push_to_counterparty_msat,
12271263 channel_config,
12281264 false ,
@@ -1262,7 +1298,72 @@ impl Node {
12621298 self . open_channel_inner (
12631299 node_id,
12641300 address,
1265- channel_amount_sats,
1301+ Some ( channel_amount_sats) ,
1302+ push_to_counterparty_msat,
1303+ channel_config,
1304+ true ,
1305+ )
1306+ }
1307+
1308+ /// Connect to a node and open a new unannounced channel, using all available on-chain funds
1309+ /// minus fees and anchor reserves.
1310+ ///
1311+ /// To open an announced channel, see [`Node::open_announced_channel_with_all`].
1312+ ///
1313+ /// Disconnects and reconnects are handled automatically.
1314+ ///
1315+ /// If `push_to_counterparty_msat` is set, the given value will be pushed (read: sent) to the
1316+ /// channel counterparty on channel open. This can be useful to start out with the balance not
1317+ /// entirely shifted to one side, therefore allowing to receive payments from the getgo.
1318+ ///
1319+ /// Returns a [`UserChannelId`] allowing to locally keep track of the channel.
1320+ ///
1321+ /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats
1322+ pub fn open_channel_with_all (
1323+ & self , node_id : PublicKey , address : SocketAddress , push_to_counterparty_msat : Option < u64 > ,
1324+ channel_config : Option < ChannelConfig > ,
1325+ ) -> Result < UserChannelId , Error > {
1326+ self . open_channel_inner (
1327+ node_id,
1328+ address,
1329+ None ,
1330+ push_to_counterparty_msat,
1331+ channel_config,
1332+ false ,
1333+ )
1334+ }
1335+
1336+ /// Connect to a node and open a new announced channel, using all available on-chain funds
1337+ /// minus fees and anchor reserves.
1338+ ///
1339+ /// This will return an error if the node has not been sufficiently configured to operate as a
1340+ /// forwarding node that can properly announce its existence to the public network graph, i.e.,
1341+ /// [`Config::listening_addresses`] and [`Config::node_alias`] are unset.
1342+ ///
1343+ /// To open an unannounced channel, see [`Node::open_channel_with_all`].
1344+ ///
1345+ /// Disconnects and reconnects are handled automatically.
1346+ ///
1347+ /// If `push_to_counterparty_msat` is set, the given value will be pushed (read: sent) to the
1348+ /// channel counterparty on channel open. This can be useful to start out with the balance not
1349+ /// entirely shifted to one side, therefore allowing to receive payments from the getgo.
1350+ ///
1351+ /// Returns a [`UserChannelId`] allowing to locally keep track of the channel.
1352+ ///
1353+ /// [`AnchorChannelsConfig::per_channel_reserve_sats`]: crate::config::AnchorChannelsConfig::per_channel_reserve_sats
1354+ pub fn open_announced_channel_with_all (
1355+ & self , node_id : PublicKey , address : SocketAddress , push_to_counterparty_msat : Option < u64 > ,
1356+ channel_config : Option < ChannelConfig > ,
1357+ ) -> Result < UserChannelId , Error > {
1358+ if let Err ( err) = may_announce_channel ( & self . config ) {
1359+ log_error ! ( self . logger, "Failed to open announced channel as the node hasn't been sufficiently configured to act as a forwarding node: {err}" ) ;
1360+ return Err ( Error :: ChannelCreationFailed ) ;
1361+ }
1362+
1363+ self . open_channel_inner (
1364+ node_id,
1365+ address,
1366+ None ,
12661367 push_to_counterparty_msat,
12671368 channel_config,
12681369 true ,
0 commit comments