Skip to content

Commit 4d37714

Browse files
committed
lndclient: add ListChannels to LightningClient
1 parent 3316c4b commit 4d37714

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

lndclient/lightning_client.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
1818
"github.com/lightningnetwork/lnd/lntypes"
1919
"github.com/lightningnetwork/lnd/lnwire"
20+
"github.com/lightningnetwork/lnd/routing/route"
2021
"github.com/lightningnetwork/lnd/zpay32"
2122
"google.golang.org/grpc"
2223
"google.golang.org/grpc/codes"
@@ -46,6 +47,9 @@ type LightningClient interface {
4647
// node.
4748
ListTransactions(ctx context.Context) ([]*wire.MsgTx, error)
4849

50+
// ListChannels retrieves all channels of the backing lnd node.
51+
ListChannels(ctx context.Context) ([]ChannelInfo, error)
52+
4953
// ChannelBackup retrieves the backup for a particular channel. The
5054
// backup is returned as an encrypted chanbackup.Single payload.
5155
ChannelBackup(context.Context, wire.OutPoint) ([]byte, error)
@@ -65,6 +69,29 @@ type Info struct {
6569
Uris []string
6670
}
6771

72+
// ChannelInfo stores unpacked per-channel info.
73+
type ChannelInfo struct {
74+
// Active indecates whether the channel is active.
75+
Active bool
76+
77+
// ChannelID holds the unique channel ID for the channel. The first 3 bytes
78+
// are the block height, the next 3 the index within the block, and the last
79+
// 2 bytes are the /output index for the channel.
80+
ChannelID uint64
81+
82+
// PubKeyBytes is the raw bytes of the public key of the remote node.
83+
PubKeyBytes route.Vertex
84+
85+
// Capacity is the total amount of funds held in this channel.
86+
Capacity btcutil.Amount
87+
88+
// LocalBalance is the current balance of this node in this channel.
89+
LocalBalance btcutil.Amount
90+
91+
// RemoteBalance is the counterparty's current balance in this channel.
92+
RemoteBalance btcutil.Amount
93+
}
94+
6895
var (
6996
// ErrMalformedServerResponse is returned when the swap and/or prepay
7097
// invoice is malformed.
@@ -495,6 +522,41 @@ func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx,
495522
return txs, nil
496523
}
497524

525+
// ListChannels retrieves all channels of the backing lnd node.
526+
func (s *lightningClient) ListChannels(ctx context.Context) (
527+
[]ChannelInfo, error) {
528+
529+
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
530+
defer cancel()
531+
532+
response, err := s.client.ListChannels(
533+
s.adminMac.WithMacaroonAuth(rpcCtx),
534+
&lnrpc.ListChannelsRequest{},
535+
)
536+
if err != nil {
537+
return nil, err
538+
}
539+
540+
result := make([]ChannelInfo, len(response.Channels))
541+
for i, channel := range response.Channels {
542+
remoteVertex, err := route.NewVertexFromStr(channel.RemotePubkey)
543+
if err != nil {
544+
return nil, err
545+
}
546+
547+
result[i] = ChannelInfo{
548+
Active: channel.Active,
549+
ChannelID: channel.ChanId,
550+
PubKeyBytes: remoteVertex,
551+
Capacity: btcutil.Amount(channel.Capacity),
552+
LocalBalance: btcutil.Amount(channel.LocalBalance),
553+
RemoteBalance: btcutil.Amount(channel.RemoteBalance),
554+
}
555+
}
556+
557+
return result, nil
558+
}
559+
498560
// ChannelBackup retrieves the backup for a particular channel. The backup is
499561
// returned as an encrypted chanbackup.Single payload.
500562
func (s *lightningClient) ChannelBackup(ctx context.Context,

test/lightning_client_mock.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ func (h *mockLightningClient) ListTransactions(
168168
return txs, nil
169169
}
170170

171+
// ListChannels retrieves all channels of the backing lnd node.
172+
func (h *mockLightningClient) ListChannels(ctx context.Context) (
173+
[]lndclient.ChannelInfo, error) {
174+
175+
return h.lnd.Channels, nil
176+
}
177+
171178
// ChannelBackup retrieves the backup for a particular channel. The
172179
// backup is returned as an encrypted chanbackup.Single payload.
173180
func (h *mockLightningClient) ChannelBackup(context.Context, wire.OutPoint) ([]byte, error) {

test/lnd_services_mock.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,8 @@ type LndMockServices struct {
163163
// keyed by hash string.
164164
Invoices map[lntypes.Hash]*lndclient.Invoice
165165

166+
Channels []lndclient.ChannelInfo
167+
166168
WaitForFinished func()
167169

168170
lock sync.Mutex

0 commit comments

Comments
 (0)