@@ -397,3 +397,70 @@ type ChannelState =
397
397
| ErrFundingLost _
398
398
| ErrFundingTimeOut _
399
399
| ErrInformationLeak _ -> Abnormal
400
+
401
+ member this.Commitments : Option < Commitments > =
402
+ match this with
403
+ | WaitForInitInternal
404
+ | WaitForOpenChannel _
405
+ | WaitForAcceptChannel _
406
+ | WaitForFundingCreated _
407
+ | WaitForFundingSigned _ -> None
408
+ | WaitForFundingConfirmed data -> Some ( data :> IHasCommitments) .Commitments
409
+ | WaitForFundingLocked data -> Some ( data :> IHasCommitments) .Commitments
410
+ | Normal data -> Some ( data :> IHasCommitments) .Commitments
411
+ | Shutdown data -> Some ( data :> IHasCommitments) .Commitments
412
+ | Negotiating data -> Some ( data :> IHasCommitments) .Commitments
413
+ | Closing data -> Some ( data :> IHasCommitments) .Commitments
414
+ | Closed _
415
+ | Offline _
416
+ | Syncing _
417
+ | ErrFundingLost _
418
+ | ErrFundingTimeOut _
419
+ | ErrInformationLeak _ -> None
420
+
421
+ member this.SpendableBalance : Option < LNMoney > =
422
+ match this.Commitments with
423
+ | None -> None
424
+ | Some commitments ->
425
+ let remoteCommit =
426
+ match commitments.RemoteNextCommitInfo with
427
+ | RemoteNextCommitInfo.Waiting info -> info.NextRemoteCommit
428
+ | RemoteNextCommitInfo.Revoked _ info -> commitments.RemoteCommit
429
+ let reducedRes =
430
+ remoteCommit.Spec.Reduce(
431
+ commitments.RemoteChanges.ACKed,
432
+ commitments.LocalChanges.Proposed
433
+ )
434
+ let reduced =
435
+ match reducedRes with
436
+ | Error err ->
437
+ failwithf
438
+ " reducing commit failed even though we have not proposed any changes\
439
+ error: %A "
440
+ err
441
+ | Ok reduced -> reduced
442
+ let fees =
443
+ if commitments.LocalParams.IsFunder then
444
+ Transactions.commitTxFee commitments.RemoteParams.DustLimitSatoshis reduced
445
+ |> LNMoney.FromMoney
446
+ else
447
+ LNMoney.Zero
448
+ let channelReserve =
449
+ commitments.RemoteParams.ChannelReserveSatoshis
450
+ |> LNMoney.FromMoney
451
+ let totalBalance = reduced.ToRemote
452
+ let untrimmedSpendableBalance = totalBalance - channelReserve - fees
453
+ let htlcSuccessFee =
454
+ reduced.FeeRatePerKw.ToFee( Transactions.Constants.HTLC_ SUCCESS_ WEIGHT)
455
+ |> LNMoney.FromMoney
456
+ let htlcFee =
457
+ reduced.FeeRatePerKw.ToFee( Transactions.Constants.COMMITMENT_ TX_ WEIGHT_ PER_ HTLC)
458
+ |> LNMoney.FromMoney
459
+ let dustLimit =
460
+ commitments.RemoteParams.DustLimitSatoshis
461
+ |> LNMoney.FromMoney
462
+ let untrimmedMax = LNMoney.Min( untrimmedSpendableBalance, htlcSuccessFee + dustLimit)
463
+ let trimmedSpendableBalance = untrimmedSpendableBalance - htlcFee
464
+ let spendableBalance = LNMoney.Max( untrimmedMax, trimmedSpendableBalance)
465
+ Some spendableBalance
466
+
0 commit comments