@@ -389,22 +389,29 @@ data class Normal(
389389 paysCommitTxFees -> Transactions .commitTxFee(commitments.params.remoteParams.dustLimit, parentCommitment.remoteCommit.spec)
390390 else -> 0 .sat
391391 }
392- if (parentCommitment.localCommit.spec.toLocal + fundingContribution.toMilliSatoshi() < parentCommitment.localChannelReserve(commitments.params).max(commitTxFees)) {
393- logger.warning { " cannot do splice: insufficient funds" }
394- spliceStatus.command.replyTo.complete(ChannelCommand .Commitment .Splice .Response .Failure .InsufficientFunds )
395- val actions = buildList {
396- add(ChannelAction .Message .Send (Warning (channelId, InvalidSpliceRequest (channelId).message)))
397- add(ChannelAction .Disconnect )
392+ val liquidityFees = when (val requestRemoteFunding = spliceStatus.command.requestRemoteFunding) {
393+ null -> 0 .msat
394+ else -> when (requestRemoteFunding.paymentDetails.paymentType) {
395+ LiquidityAds .PaymentType .FromChannelBalance -> requestRemoteFunding.fees(spliceStatus.command.feerate, isChannelCreation = false ).total.toMilliSatoshi()
396+ LiquidityAds .PaymentType .FromChannelBalanceForFutureHtlc -> requestRemoteFunding.fees(spliceStatus.command.feerate, isChannelCreation = false ).total.toMilliSatoshi()
397+ // Liquidity fees will be deducted from future HTLCs instead of being paid immediately.
398+ LiquidityAds .PaymentType .FromFutureHtlc -> 0 .msat
399+ LiquidityAds .PaymentType .FromFutureHtlcWithPreimage -> 0 .msat
400+ is LiquidityAds .PaymentType .Unknown -> 0 .msat
398401 }
399- Pair (this @Normal.copy(spliceStatus = SpliceStatus .None ), actions)
402+ }
403+ val liquidityFeesOwed = (liquidityFees - spliceStatus.command.currentFeeCredit).max(0 .msat)
404+ val balanceAfterFees = parentCommitment.localCommit.spec.toLocal + fundingContribution.toMilliSatoshi() - liquidityFeesOwed
405+ if (balanceAfterFees < parentCommitment.localChannelReserve(commitments.params).max(commitTxFees)) {
406+ logger.warning { " cannot do splice: insufficient funds (balanceAfterFees=$balanceAfterFees , liquidityFees=$liquidityFees , feeCredit=${spliceStatus.command.currentFeeCredit} )" }
407+ spliceStatus.command.replyTo.complete(ChannelCommand .Commitment .Splice .Response .Failure .InsufficientFunds (balanceAfterFees, liquidityFees, spliceStatus.command.currentFeeCredit))
408+ val action = listOf (ChannelAction .Message .Send (TxAbort (channelId, InvalidSpliceRequest (channelId).message)))
409+ Pair (this @Normal.copy(spliceStatus = SpliceStatus .Aborted ), action)
400410 } else if (spliceStatus.command.spliceOut?.scriptPubKey?.let { Helpers .Closing .isValidFinalScriptPubkey(it, allowAnySegwit = true ) } == false ) {
401411 logger.warning { " cannot do splice: invalid splice-out script" }
402412 spliceStatus.command.replyTo.complete(ChannelCommand .Commitment .Splice .Response .Failure .InvalidSpliceOutPubKeyScript )
403- val actions = buildList {
404- add(ChannelAction .Message .Send (Warning (channelId, InvalidSpliceRequest (channelId).message)))
405- add(ChannelAction .Disconnect )
406- }
407- Pair (this @Normal.copy(spliceStatus = SpliceStatus .None ), actions)
413+ val action = listOf (ChannelAction .Message .Send (TxAbort (channelId, InvalidSpliceRequest (channelId).message)))
414+ Pair (this @Normal.copy(spliceStatus = SpliceStatus .Aborted ), action)
408415 } else {
409416 val spliceInit = SpliceInit (
410417 channelId,
0 commit comments