Skip to content

Commit c76f2c4

Browse files
committed
lndclient: update listTransactions to return full transaction
1 parent 475c7f2 commit c76f2c4

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

lndclient/lightning_client.go

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ type LightningClient interface {
4545

4646
// ListTransactions returns all known transactions of the backing lnd
4747
// node.
48-
ListTransactions(ctx context.Context) ([]*wire.MsgTx, error)
48+
ListTransactions(ctx context.Context) ([]Transaction, error)
4949

5050
// ListChannels retrieves all channels of the backing lnd node.
5151
ListChannels(ctx context.Context) ([]ChannelInfo, error)
@@ -257,6 +257,35 @@ func (c Initiator) String() string {
257257
}
258258
}
259259

260+
// Transaction represents an on chain transaction.
261+
type Transaction struct {
262+
// Tx is the on chain transaction.
263+
Tx *wire.MsgTx
264+
265+
// TxHash is the transaction hash string.
266+
TxHash string
267+
268+
// Timestamp is the timestamp our wallet has for the transaction.
269+
Timestamp time.Time
270+
271+
// Amount is the balance change that this transaction had on addresses
272+
// controlled by our wallet.
273+
Amount btcutil.Amount
274+
275+
// Fee is the amount of fees our wallet committed to this transaction.
276+
// Note that this field is not exhaustive, as it does not account for
277+
// fees taken from inputs that that wallet doesn't know it owns (for
278+
// example, the fees taken from our channel balance when we close a
279+
// channel).
280+
Fee btcutil.Amount
281+
282+
// Confirmations is the number of confirmations the transaction has.
283+
Confirmations int32
284+
285+
// Label is an optional label set for on chain transactions.
286+
Label string
287+
}
288+
260289
var (
261290
// ErrMalformedServerResponse is returned when the swap and/or prepay
262291
// invoice is malformed.
@@ -675,7 +704,7 @@ func unmarshalInvoice(resp *lnrpc.Invoice) (*Invoice, error) {
675704
}
676705

677706
// ListTransactions returns all known transactions of the backing lnd node.
678-
func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx, error) {
707+
func (s *lightningClient) ListTransactions(ctx context.Context) ([]Transaction, error) {
679708
rpcCtx, cancel := context.WithTimeout(ctx, rpcTimeout)
680709
defer cancel()
681710

@@ -686,8 +715,8 @@ func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx,
686715
return nil, err
687716
}
688717

689-
txs := make([]*wire.MsgTx, 0, len(resp.Transactions))
690-
for _, respTx := range resp.Transactions {
718+
txs := make([]Transaction, len(resp.Transactions))
719+
for i, respTx := range resp.Transactions {
691720
rawTx, err := hex.DecodeString(respTx.RawTxHex)
692721
if err != nil {
693722
return nil, err
@@ -697,7 +726,16 @@ func (s *lightningClient) ListTransactions(ctx context.Context) ([]*wire.MsgTx,
697726
if err := tx.Deserialize(bytes.NewReader(rawTx)); err != nil {
698727
return nil, err
699728
}
700-
txs = append(txs, &tx)
729+
730+
txs[i] = Transaction{
731+
Tx: &tx,
732+
TxHash: tx.TxHash().String(),
733+
Timestamp: time.Unix(respTx.TimeStamp, 0),
734+
Amount: btcutil.Amount(respTx.Amount),
735+
Fee: btcutil.Amount(respTx.TotalFees),
736+
Confirmations: respTx.NumConfirmations,
737+
Label: respTx.Label,
738+
}
701739
}
702740

703741
return txs, nil

test/lightning_client_mock.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,12 @@ func (h *mockLightningClient) LookupInvoice(_ context.Context,
160160

161161
// ListTransactions returns all known transactions of the backing lnd node.
162162
func (h *mockLightningClient) ListTransactions(
163-
ctx context.Context) ([]*wire.MsgTx, error) {
163+
_ context.Context) ([]lndclient.Transaction, error) {
164164

165165
h.lnd.lock.Lock()
166166
txs := h.lnd.Transactions
167167
h.lnd.lock.Unlock()
168+
168169
return txs, nil
169170
}
170171

test/lnd_services_mock.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ type LndMockServices struct {
157157
Signature []byte
158158
SignatureMsg string
159159

160-
Transactions []*wire.MsgTx
160+
Transactions []lndclient.Transaction
161161

162162
// Invoices is a set of invoices that have been created by the mock,
163163
// keyed by hash string.
@@ -188,7 +188,9 @@ func (s *LndMockServices) NotifyHeight(height int32) error {
188188
// AddRelevantTx marks the given transaction as relevant.
189189
func (s *LndMockServices) AddTx(tx *wire.MsgTx) {
190190
s.lock.Lock()
191-
s.Transactions = append(s.Transactions, tx.Copy())
191+
s.Transactions = append(s.Transactions, lndclient.Transaction{
192+
Tx: tx.Copy(),
193+
})
192194
s.lock.Unlock()
193195
}
194196

0 commit comments

Comments
 (0)