Skip to content

Commit e9cd47c

Browse files
committed
Add ChannelState Commitments and SpendableBalance methods
Add a method to easily get the commitments of a channel (if they exist). Add a method to get the largest payment size that a channel will allow us to make.
1 parent 7ab6e5c commit e9cd47c

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/DotNetLightning.Core/Channel/ChannelTypes.fs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,70 @@ type ChannelState =
397397
| ErrFundingLost _
398398
| ErrFundingTimeOut _
399399
| 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+

src/DotNetLightning.Core/Utils/LNMoney.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ type LNMoney = | LNMoney of int64 with
3737
let satoshi = Checked.op_Multiply (amount) (decimal lnUnit)
3838
LNMoney(Checked.int64 satoshi)
3939

40+
static member FromMoney (money: Money) =
41+
LNMoney.Satoshis(money.Satoshi)
4042

4143
static member Coins(coins: decimal) =
4244
LNMoney.FromUnit(coins * (decimal LNMoneyUnit.BTC), LNMoneyUnit.MilliSatoshi)

0 commit comments

Comments
 (0)