@@ -1059,50 +1059,14 @@ impl Node {
10591059 let con_addr = peer_info. address . clone ( ) ;
10601060 let con_cm = Arc :: clone ( & self . connection_manager ) ;
10611061
1062- let cur_anchor_reserve_sats =
1063- total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
1064- let spendable_amount_sats =
1065- self . wallet . get_spendable_amount_sats ( cur_anchor_reserve_sats) . unwrap_or ( 0 ) ;
1066-
1067- // Fail early if we have less than the channel value available.
1068- if spendable_amount_sats < channel_amount_sats {
1069- log_error ! ( self . logger,
1070- "Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats" ,
1071- spendable_amount_sats, channel_amount_sats
1072- ) ;
1073- return Err ( Error :: InsufficientFunds ) ;
1074- }
1075-
10761062 // We need to use our main runtime here as a local runtime might not be around to poll
10771063 // connection futures going forward.
10781064 self . runtime . block_on ( async move {
10791065 con_cm. connect_peer_if_necessary ( con_node_id, con_addr) . await
10801066 } ) ?;
10811067
1082- // Fail if we have less than the channel value + anchor reserve available (if applicable).
1083- let init_features = self
1084- . peer_manager
1085- . peer_by_node_id ( & node_id)
1086- . ok_or ( Error :: ConnectionFailed ) ?
1087- . init_features ;
1088- let required_funds_sats = channel_amount_sats
1089- + self . config . anchor_channels_config . as_ref ( ) . map_or ( 0 , |c| {
1090- if init_features. requires_anchors_zero_fee_htlc_tx ( )
1091- && !c. trusted_peers_no_reserve . contains ( & node_id)
1092- {
1093- c. per_channel_reserve_sats
1094- } else {
1095- 0
1096- }
1097- } ) ;
1098-
1099- if spendable_amount_sats < required_funds_sats {
1100- log_error ! ( self . logger,
1101- "Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats" ,
1102- spendable_amount_sats, required_funds_sats
1103- ) ;
1104- return Err ( Error :: InsufficientFunds ) ;
1105- }
1068+ // Check funds availability after connection (includes anchor reserve calculation)
1069+ self . check_sufficient_funds_for_channel ( channel_amount_sats, & node_id) ?;
11061070
11071071 let mut user_config = default_user_config ( & self . config ) ;
11081072 user_config. channel_handshake_config . announce_for_forwarding = announce_for_forwarding;
@@ -1143,6 +1107,51 @@ impl Node {
11431107 }
11441108 }
11451109
1110+ fn check_sufficient_funds_for_channel (
1111+ & self , amount_sats : u64 , peer_node_id : & PublicKey ,
1112+ ) -> Result < ( ) , Error > {
1113+ let cur_anchor_reserve_sats =
1114+ total_anchor_channels_reserve_sats ( & self . channel_manager , & self . config ) ;
1115+ let spendable_amount_sats =
1116+ self . wallet . get_spendable_amount_sats ( cur_anchor_reserve_sats) . unwrap_or ( 0 ) ;
1117+
1118+ // Fail early if we have less than the channel value available.
1119+ if spendable_amount_sats < amount_sats {
1120+ log_error ! ( self . logger,
1121+ "Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats" ,
1122+ spendable_amount_sats, amount_sats
1123+ ) ;
1124+ return Err ( Error :: InsufficientFunds ) ;
1125+ }
1126+
1127+ // Fail if we have less than the channel value + anchor reserve available (if applicable).
1128+ let init_features = self
1129+ . peer_manager
1130+ . peer_by_node_id ( peer_node_id)
1131+ . ok_or ( Error :: ConnectionFailed ) ?
1132+ . init_features ;
1133+ let required_funds_sats = amount_sats
1134+ + self . config . anchor_channels_config . as_ref ( ) . map_or ( 0 , |c| {
1135+ if init_features. requires_anchors_zero_fee_htlc_tx ( )
1136+ && !c. trusted_peers_no_reserve . contains ( peer_node_id)
1137+ {
1138+ c. per_channel_reserve_sats
1139+ } else {
1140+ 0
1141+ }
1142+ } ) ;
1143+
1144+ if spendable_amount_sats < required_funds_sats {
1145+ log_error ! ( self . logger,
1146+ "Unable to create channel due to insufficient funds. Available: {}sats, Required: {}sats" ,
1147+ spendable_amount_sats, required_funds_sats
1148+ ) ;
1149+ return Err ( Error :: InsufficientFunds ) ;
1150+ }
1151+
1152+ Ok ( ( ) )
1153+ }
1154+
11461155 /// Connect to a node and open a new unannounced channel.
11471156 ///
11481157 /// To open an announced channel, see [`Node::open_announced_channel`].
0 commit comments