Skip to content

Commit

Permalink
GetTransactionDataAndReceipt
Browse files Browse the repository at this point in the history
  • Loading branch information
HaoyangLiu committed Mar 17, 2021
1 parent 7949643 commit 0597d3e
Showing 1 changed file with 74 additions and 0 deletions.
74 changes: 74 additions & 0 deletions internal/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,80 @@ func (s *PublicTransactionPoolAPI) GetTransactionReceiptsByBlockNumber(ctx conte
return txRecipients, nil
}

// GetTransactionDataAndReceipt returns the original transaction data and transaction receipt for the given transaction hash.
func (s *PublicTransactionPoolAPI) GetTransactionDataAndReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash)
if tx == nil {
return nil, nil
}
receipts, err := s.b.GetReceipts(ctx, blockHash)
if err != nil {
return nil, err
}
if len(receipts) <= int(index) {
return nil, nil
}
receipt := receipts[index]

var signer types.Signer = types.FrontierSigner{}
if tx.Protected() {
signer = types.NewEIP155Signer(tx.ChainId())
}
from, _ := types.Sender(signer, tx)

rpcTransaction := newRPCTransaction(tx, blockHash, blockNumber, index)

txData := map[string]interface{}{
"blockHash": rpcTransaction.BlockHash.String(),
"blockNumber": rpcTransaction.BlockNumber.String(),
"from": rpcTransaction.From.String(),
"gas": rpcTransaction.Gas.String(),
"gasPrice": rpcTransaction.GasPrice.String(),
"hash": rpcTransaction.Hash.String(),
"input": rpcTransaction.Input.String(),
"nonce": rpcTransaction.Nonce.String(),
"to": rpcTransaction.To.String(),
"transactionIndex": rpcTransaction.TransactionIndex.String(),
"value": rpcTransaction.Value.String(),
"v": rpcTransaction.V.String(),
"r": rpcTransaction.R.String(),
"s": rpcTransaction.S.String(),
}

fields := map[string]interface{}{
"blockHash": blockHash,
"blockNumber": hexutil.Uint64(blockNumber),
"transactionHash": hash,
"transactionIndex": hexutil.Uint64(index),
"from": from,
"to": tx.To(),
"gasUsed": hexutil.Uint64(receipt.GasUsed),
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
"contractAddress": nil,
"logs": receipt.Logs,
"logsBloom": receipt.Bloom,
}

// Assign receipt status or post state.
if len(receipt.PostState) > 0 {
fields["root"] = hexutil.Bytes(receipt.PostState)
} else {
fields["status"] = hexutil.Uint(receipt.Status)
}
if receipt.Logs == nil {
fields["logs"] = [][]*types.Log{}
}
// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
if receipt.ContractAddress != (common.Address{}) {
fields["contractAddress"] = receipt.ContractAddress
}
result := map[string]interface{}{
"tx_data": txData,
"recipient": fields,
}
return result, nil
}

// GetTransactionReceipt returns the transaction receipt for the given transaction hash.
func (s *PublicTransactionPoolAPI) GetTransactionReceipt(ctx context.Context, hash common.Hash) (map[string]interface{}, error) {
tx, blockHash, blockNumber, index := rawdb.ReadTransaction(s.b.ChainDb(), hash)
Expand Down

0 comments on commit 0597d3e

Please sign in to comment.