Skip to content

Commit

Permalink
Merge pull request METADIUM#25 from Wade-wemade/dev
Browse files Browse the repository at this point in the history
Feat : eth_getReceipts API
  • Loading branch information
wmxcreator committed Nov 4, 2022
2 parents 071bafb + c376543 commit cbdfcd7
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
42 changes: 42 additions & 0 deletions internal/ethapi/api.go
Expand Up @@ -643,6 +643,48 @@ func (s *PublicBlockChainAPI) BlockNumber() hexutil.Uint64 {
return hexutil.Uint64(header.Number.Uint64())
}

// GetBlockReceipts returns all the transaction receipts for the given block hash.
func (s *PublicBlockChainAPI) GetReceiptsByHash(ctx context.Context, blockHash common.Hash) ([]map[string]interface{}, error) {
receipts, err1 := s.b.GetReceipts(ctx, blockHash)
if err1 != nil {
return nil, err1
}
block, err2 := s.b.BlockByHash(ctx, blockHash)
if err2 != nil {
return nil, err2
}

txs := block.Transactions()
if receipts.Len() != txs.Len() {
return nil, fmt.Errorf("the size of transactions and receipts is different in the block (%s)", blockHash.String())
}
fieldsList := make([]map[string]interface{}, 0, len(receipts))

for index, receipt := range receipts {

bigblock := new(big.Int).SetUint64(block.NumberU64())
signer := types.MakeSigner(s.b.ChainConfig(), bigblock)
from, _ := types.Sender(signer, txs[index])

fields := map[string]interface{}{
"blockHash": blockHash,
"blockNumber": bigblock,
"transactionHash": receipt.TxHash,
"transactionIndex": hexutil.Uint64(index),
"from": from,
"to": txs[index].To(),
"gasUsed": hexutil.Uint64(receipt.GasUsed),
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
"contractAddress": nil,
"logs": receipt.Logs,
"logsBloom": receipt.Bloom,
"type": hexutil.Uint(txs[index].Type()),
}
fieldsList = append(fieldsList, fields)
}
return fieldsList, nil
}

// GetBalance returns the amount of wei for the given address in the state of the
// given block number. The rpc.LatestBlockNumber and rpc.PendingBlockNumber meta
// block numbers are also allowed.
Expand Down
12 changes: 12 additions & 0 deletions internal/web3ext/web3ext.go
Expand Up @@ -611,6 +611,18 @@ web3._extend({
params: 1,
inputFormatter: [web3._extend.formatters.inputTransactionFormatter]
}),
new web3._extend.Method({
name: 'getReceiptsByHash',
call: 'eth_getReceiptsByHash',
params: 1,
outputFormatter: function(receipts) {
var formatted = [];
for (var i = 0; i < receipts.length; i++) {
formatted.push(web3._extend.formatters.outputTransactionReceiptFormatter(receipts[i]));
}
return formatted;
}
}),
new web3._extend.Method({
name: 'getHeaderByNumber',
call: 'eth_getHeaderByNumber',
Expand Down

0 comments on commit cbdfcd7

Please sign in to comment.