From 93e5de9306b8a21cd6309a7697edde3d37c5fd85 Mon Sep 17 00:00:00 2001 From: Bjarne Magnussen Date: Thu, 14 Oct 2021 08:41:25 +0200 Subject: [PATCH] lnrpc: refactor populating lnrpc.Transaction inside SubscribeTransaction --- lnrpc/rpc_utils.go | 52 +++++++++++++++++++++++++--------------------- rpcserver.go | 29 ++------------------------ 2 files changed, 30 insertions(+), 51 deletions(-) diff --git a/lnrpc/rpc_utils.go b/lnrpc/rpc_utils.go index 0f2d9bcd6..94b034bcc 100644 --- a/lnrpc/rpc_utils.go +++ b/lnrpc/rpc_utils.go @@ -16,6 +16,33 @@ const ( RegisterRPCMiddlewareURI = "/lnrpc.Lightning/RegisterRPCMiddleware" ) +// RPCTransaction returns a rpc transaction. +func RPCTransaction(tx *lnwallet.TransactionDetail) *Transaction { + var destAddresses []string + for _, destAddress := range tx.DestAddresses { + destAddresses = append(destAddresses, destAddress.EncodeAddress()) + } + + // We also get unconfirmed transactions, so BlockHash can be nil. + blockHash := "" + if tx.BlockHash != nil { + blockHash = tx.BlockHash.String() + } + + return &Transaction{ + TxHash: tx.Hash.String(), + Amount: int64(tx.Value), + NumConfirmations: tx.NumConfirmations, + BlockHash: blockHash, + BlockHeight: tx.BlockHeight, + TimeStamp: tx.Timestamp, + TotalFees: tx.TotalFees, + DestAddresses: destAddresses, + RawTxHex: hex.EncodeToString(tx.RawTx), + Label: tx.Label, + } +} + // RPCTransactionDetails returns a set of rpc transaction details. func RPCTransactionDetails(txns []*lnwallet.TransactionDetail) *TransactionDetails { txDetails := &TransactionDetails{ @@ -23,30 +50,7 @@ func RPCTransactionDetails(txns []*lnwallet.TransactionDetail) *TransactionDetai } for i, tx := range txns { - var destAddresses []string - for _, destAddress := range tx.DestAddresses { - destAddresses = append(destAddresses, destAddress.EncodeAddress()) - } - - // We also get unconfirmed transactions, so BlockHash can be - // nil. - blockHash := "" - if tx.BlockHash != nil { - blockHash = tx.BlockHash.String() - } - - txDetails.Transactions[i] = &Transaction{ - TxHash: tx.Hash.String(), - Amount: int64(tx.Value), - NumConfirmations: tx.NumConfirmations, - BlockHash: blockHash, - BlockHeight: tx.BlockHeight, - TimeStamp: tx.Timestamp, - TotalFees: tx.TotalFees, - DestAddresses: destAddresses, - RawTxHex: hex.EncodeToString(tx.RawTx), - Label: tx.Label, - } + txDetails.Transactions[i] = RPCTransaction(tx) } // Sort transactions by number of confirmations rather than height so diff --git a/rpcserver.go b/rpcserver.go index 28aad4e31..1bd151ef5 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -5314,38 +5314,13 @@ func (r *rpcServer) SubscribeTransactions(req *lnrpc.GetTransactionsRequest, for { select { case tx := <-txClient.ConfirmedTransactions(): - destAddresses := make([]string, 0, len(tx.DestAddresses)) - for _, destAddress := range tx.DestAddresses { - destAddresses = append(destAddresses, destAddress.EncodeAddress()) - } - detail := &lnrpc.Transaction{ - TxHash: tx.Hash.String(), - Amount: int64(tx.Value), - NumConfirmations: tx.NumConfirmations, - BlockHash: tx.BlockHash.String(), - BlockHeight: tx.BlockHeight, - TimeStamp: tx.Timestamp, - TotalFees: tx.TotalFees, - DestAddresses: destAddresses, - RawTxHex: hex.EncodeToString(tx.RawTx), - } + detail := lnrpc.RPCTransaction(tx) if err := updateStream.Send(detail); err != nil { return err } case tx := <-txClient.UnconfirmedTransactions(): - var destAddresses []string - for _, destAddress := range tx.DestAddresses { - destAddresses = append(destAddresses, destAddress.EncodeAddress()) - } - detail := &lnrpc.Transaction{ - TxHash: tx.Hash.String(), - Amount: int64(tx.Value), - TimeStamp: tx.Timestamp, - TotalFees: tx.TotalFees, - DestAddresses: destAddresses, - RawTxHex: hex.EncodeToString(tx.RawTx), - } + detail := lnrpc.RPCTransaction(tx) if err := updateStream.Send(detail); err != nil { return err }