Skip to content

Commit

Permalink
fix: lnd channel spendable amounts, improved naming, add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rolznz committed Sep 22, 2024
1 parent e6ae411 commit 91388b9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
14 changes: 8 additions & 6 deletions lnclient/ldk/ldk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1464,15 +1464,17 @@ func (ls *LDKService) GetBalances(ctx context.Context) (*lnclient.BalancesRespon
channels := ls.node.ListChannels()
for _, channel := range channels {
if channel.IsUsable {
channelMinSpendable := min(int64(channel.OutboundCapacityMsat), int64(*channel.CounterpartyOutboundHtlcMaximumMsat))
channelMinReceivable := min(int64(channel.InboundCapacityMsat), int64(*channel.InboundHtlcMaximumMsat))
// spending or receiving amount may be constrained by channel configuration (e.g. ACINQ does this)
channelConstrainedSpendable := min(int64(channel.OutboundCapacityMsat), int64(*channel.CounterpartyOutboundHtlcMaximumMsat))
channelConstrainedReceivable := min(int64(channel.InboundCapacityMsat), int64(*channel.InboundHtlcMaximumMsat))

nextMaxSpendable = max(nextMaxSpendable, channelMinSpendable)
nextMaxReceivable = max(nextMaxReceivable, channelMinReceivable)
nextMaxSpendable = max(nextMaxSpendable, channelConstrainedSpendable)
nextMaxReceivable = max(nextMaxReceivable, channelConstrainedReceivable)

nextMaxSpendableMPP += channelMinSpendable
nextMaxReceivableMPP += channelMinReceivable
nextMaxSpendableMPP += channelConstrainedSpendable
nextMaxReceivableMPP += channelConstrainedReceivable

// these are what the wallet can send and receive, but not necessarily in one go
totalSpendable += int64(channel.OutboundCapacityMsat)
totalReceivable += int64(channel.InboundCapacityMsat)
}
Expand Down
20 changes: 11 additions & 9 deletions lnclient/lnd/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,20 +849,22 @@ func (svc *LNDService) GetBalances(ctx context.Context) (*lnclient.BalancesRespo
for _, channel := range resp.Channels {
// Unnecessary since ListChannels only returns active channels
if channel.Active {
channelSpendable := min(max(channel.LocalBalance*1000-int64(channel.LocalConstraints.ChanReserveSat*1000), 0), int64(channel.RemoteConstraints.MaxPendingAmtMsat))
channelReceivable := min(max(channel.RemoteBalance*1000-int64(channel.RemoteConstraints.ChanReserveSat*1000), 0), int64(channel.LocalConstraints.MaxPendingAmtMsat))
channelSpendable := max(channel.LocalBalance*1000-int64(channel.LocalConstraints.ChanReserveSat*1000), 0)
channelReceivable := max(channel.RemoteBalance*1000-int64(channel.RemoteConstraints.ChanReserveSat*1000), 0)

channelMinSpendable := min(channelSpendable, int64(channel.RemoteConstraints.MaxPendingAmtMsat))
channelMinReceivable := min(channelReceivable, int64(channel.LocalConstraints.MaxPendingAmtMsat))
// spending or receiving amount may be constrained by channel configuration (e.g. ACINQ does this)
channelConstrainedSpendable := min(channelSpendable, int64(channel.RemoteConstraints.MaxPendingAmtMsat))
channelConstrainedReceivable := min(channelReceivable, int64(channel.LocalConstraints.MaxPendingAmtMsat))

nextMaxSpendable = max(nextMaxSpendable, channelMinSpendable)
nextMaxReceivable = max(nextMaxReceivable, channelMinReceivable)
nextMaxSpendable = max(nextMaxSpendable, channelConstrainedSpendable)
nextMaxReceivable = max(nextMaxReceivable, channelConstrainedReceivable)

nextMaxSpendableMPP += channelConstrainedSpendable
nextMaxReceivableMPP += channelConstrainedReceivable

// these are what the wallet can send and receive, but not necessarily in one go
totalSpendable += channelSpendable
totalReceivable += channelReceivable

nextMaxSpendableMPP += channelMinSpendable
nextMaxReceivableMPP += channelMinReceivable
}
}

Expand Down

0 comments on commit 91388b9

Please sign in to comment.