mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-01-19 05:45:21 +01:00
lnrpc: refactor populating lnrpc.Transaction inside SubscribeTransaction
This commit is contained in:
parent
f022e557bf
commit
93e5de9306
@ -16,26 +16,20 @@ const (
|
||||
RegisterRPCMiddlewareURI = "/lnrpc.Lightning/RegisterRPCMiddleware"
|
||||
)
|
||||
|
||||
// RPCTransactionDetails returns a set of rpc transaction details.
|
||||
func RPCTransactionDetails(txns []*lnwallet.TransactionDetail) *TransactionDetails {
|
||||
txDetails := &TransactionDetails{
|
||||
Transactions: make([]*Transaction, len(txns)),
|
||||
}
|
||||
|
||||
for i, tx := range txns {
|
||||
// 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.
|
||||
// We also get unconfirmed transactions, so BlockHash can be nil.
|
||||
blockHash := ""
|
||||
if tx.BlockHash != nil {
|
||||
blockHash = tx.BlockHash.String()
|
||||
}
|
||||
|
||||
txDetails.Transactions[i] = &Transaction{
|
||||
return &Transaction{
|
||||
TxHash: tx.Hash.String(),
|
||||
Amount: int64(tx.Value),
|
||||
NumConfirmations: tx.NumConfirmations,
|
||||
@ -49,6 +43,16 @@ func RPCTransactionDetails(txns []*lnwallet.TransactionDetail) *TransactionDetai
|
||||
}
|
||||
}
|
||||
|
||||
// RPCTransactionDetails returns a set of rpc transaction details.
|
||||
func RPCTransactionDetails(txns []*lnwallet.TransactionDetail) *TransactionDetails {
|
||||
txDetails := &TransactionDetails{
|
||||
Transactions: make([]*Transaction, len(txns)),
|
||||
}
|
||||
|
||||
for i, tx := range txns {
|
||||
txDetails.Transactions[i] = RPCTransaction(tx)
|
||||
}
|
||||
|
||||
// Sort transactions by number of confirmations rather than height so
|
||||
// that unconfirmed transactions (height =0; confirmations =-1) will
|
||||
// follow the most recently set of confirmed transactions. If we sort
|
||||
|
29
rpcserver.go
29
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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user