Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use channel balance api in get balances (LND) #671

Merged
merged 3 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
34 changes: 23 additions & 11 deletions lnclient/lnd/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,30 +839,42 @@ func (svc *LNDService) GetBalances(ctx context.Context) (*lnclient.BalancesRespo
var nextMaxSpendable int64 = 0
var nextMaxReceivableMPP int64 = 0
var nextMaxSpendableMPP int64 = 0

resp, err := svc.client.ListChannels(ctx, &lnrpc.ListChannelsRequest{})
if err != nil {
logger.Logger.WithError(err).Error("Failed to list channels")
return nil, err
}

for _, channel := range resp.Channels {
// Unnecessary since ListChannels only returns active channels
if channel.Active {
channelMinSpendable := channel.LocalBalance * 1000
channelMinReceivable := channel.RemoteBalance * 1000
channelSpendable := max(channel.LocalBalance*1000-int64(channel.LocalConstraints.ChanReserveSat*1000), 0)
channelReceivable := max(channel.RemoteBalance*1000-int64(channel.RemoteConstraints.ChanReserveSat*1000), 0)

// 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, channelConstrainedSpendable)
nextMaxReceivable = max(nextMaxReceivable, channelConstrainedReceivable)

nextMaxSpendable = max(nextMaxSpendable, channelMinSpendable)
nextMaxReceivable = max(nextMaxReceivable, channelMinReceivable)
nextMaxSpendableMPP += channelConstrainedSpendable
nextMaxReceivableMPP += channelConstrainedReceivable

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

return &lnclient.BalancesResponse{
Onchain: *onchainBalance,
Lightning: lnclient.LightningBalanceResponse{
TotalSpendable: totalSpendable,
TotalReceivable: totalReceivable,
NextMaxSpendable: nextMaxSpendable,
NextMaxReceivable: nextMaxReceivable,
// TODO: return actuall MPP instead of 0
TotalSpendable: totalSpendable,
TotalReceivable: totalReceivable,
NextMaxSpendable: nextMaxSpendable,
NextMaxReceivable: nextMaxReceivable,
NextMaxSpendableMPP: nextMaxSpendableMPP,
NextMaxReceivableMPP: nextMaxReceivableMPP,
},
Expand Down
Loading