Skip to content

Commit cffbe64

Browse files
committed
Add Channel.getSpendableBalance
Add a method to query a channel for the largest payment size that it will allow us to make.
1 parent 7ab6e5c commit cffbe64

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/DotNetLightning.Core/Channel/Channel.fs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,43 @@ module Channel =
119119
}
120120
[ WeSentChannelReestablish ourChannelReestablish ] |> Ok
121121

122+
let getSpendableBalance (cs: Channel): Option<Money> =
123+
match cs.State.Commitments with
124+
| None -> None
125+
| Some commitments ->
126+
let remoteCommit =
127+
match commitments.RemoteNextCommitInfo with
128+
| RemoteNextCommitInfo.Waiting info -> info.NextRemoteCommit
129+
| RemoteNextCommitInfo.Revoked _info -> commitments.RemoteCommit
130+
let reducedRes =
131+
remoteCommit.Spec.Reduce(
132+
commitments.RemoteChanges.ACKed,
133+
commitments.LocalChanges.Proposed
134+
)
135+
let reduced =
136+
match reducedRes with
137+
| Error err ->
138+
failwithf
139+
"reducing commit failed even though we have not proposed any changes\
140+
error: %A"
141+
err
142+
| Ok reduced -> reduced
143+
let dustLimit = commitments.RemoteParams.DustLimitSatoshis
144+
let fees =
145+
if commitments.LocalParams.IsFunder then
146+
Transactions.commitTxFee dustLimit reduced
147+
else
148+
Money.Zero
149+
let channelReserve = commitments.RemoteParams.ChannelReserveSatoshis
150+
let totalBalance = reduced.ToRemote.ToMoney()
151+
let untrimmedSpendableBalance = totalBalance - channelReserve - fees
152+
let htlcSuccessFee = reduced.FeeRatePerKw.ToFee(Transactions.Constants.HTLC_SUCCESS_WEIGHT)
153+
let htlcFee = reduced.FeeRatePerKw.ToFee(Transactions.Constants.COMMITMENT_TX_WEIGHT_PER_HTLC)
154+
let untrimmedMax = Money.Min(untrimmedSpendableBalance, htlcSuccessFee + dustLimit)
155+
let trimmedSpendableBalance = untrimmedSpendableBalance - htlcFee
156+
let spendableBalance = Money.Max(untrimmedMax, trimmedSpendableBalance)
157+
Some spendableBalance
158+
122159
let executeCommand (cs: Channel) (command: ChannelCommand): Result<ChannelEvent list, ChannelError> =
123160
match cs.State, command with
124161

src/DotNetLightning.Core/Channel/ChannelTypes.fs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,24 @@ 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+

0 commit comments

Comments
 (0)