@@ -497,6 +497,26 @@ class WalletViewModel: ObservableObject {
497497 )
498498 }
499499
500+ private func maxChannelFundableAmount( fundableBalance: UInt64 ) async -> UInt64 {
501+ guard fundableBalance > 0 else { return 0 }
502+
503+ do {
504+ guard let feeRates = try await coreService. blocktank. fees ( refresh: false ) else {
505+ return fundableBalance
506+ }
507+ let feeRate = TransactionSpeed . normal. getFeeRate ( from: feeRates)
508+ let fee = try await lightningService. calculateTotalFee (
509+ address: onchainAddress,
510+ amountSats: fundableBalance,
511+ satsPerVByte: feeRate
512+ )
513+ return fundableBalance >= fee ? fundableBalance - fee : 0
514+ } catch {
515+ Logger . debug ( " Could not calculate channel funding fee " , context: " WalletViewModel " )
516+ return fundableBalance
517+ }
518+ }
519+
500520 /// Calculates the maximum sendable amount for onchain transactions
501521 /// - Parameters:
502522 /// - address: The destination address
@@ -674,13 +694,13 @@ class WalletViewModel: ObservableObject {
674694 totalBalanceSats = Int ( state. totalBalanceSats)
675695 maxSendLightningSats = Int ( state. maxSendLightningSats)
676696
677- // Update channel fundable balance (excludes Legacy UTXOs which can't be used for channels )
697+ // Update channel fundable balance (excludes Legacy UTXOs and accounts for funding tx fee )
678698 let ( selectedType, monitoredTypes) = LightningService . addressTypeStateFromUserDefaults ( )
679699 if let fundableBalance = try ? await lightningService. getChannelFundableBalance (
680700 selectedType: selectedType,
681701 monitoredTypes: monitoredTypes
682702 ) {
683- channelFundableBalanceSats = Int ( fundableBalance)
703+ channelFundableBalanceSats = await Int ( maxChannelFundableAmount ( fundableBalance: fundableBalance ) )
684704 }
685705
686706 // Get force close timelock from active transfers
0 commit comments