Skip to content

Commit

Permalink
Return proper response in eth_getTransactionReceipt
Browse files Browse the repository at this point in the history
The Ethereum JSON-API specification, requires also the following fields:
- contractAddress (present for contract creation),
- from,
- to (null for contract creation)
  • Loading branch information
m-Peter committed Mar 25, 2024
1 parent b97076e commit 158d794
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 6 deletions.
18 changes: 12 additions & 6 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ethereum/go-ethereum/rpc"
errs "github.com/onflow/flow-evm-gateway/api/errors"
"github.com/onflow/flow-evm-gateway/config"
"github.com/onflow/flow-evm-gateway/models"
"github.com/onflow/flow-evm-gateway/services/logs"
"github.com/onflow/flow-evm-gateway/services/requester"
"github.com/onflow/flow-evm-gateway/storage"
Expand Down Expand Up @@ -243,18 +244,23 @@ func (b *BlockChainAPI) GetTransactionByBlockNumberAndIndex(
func (b *BlockChainAPI) GetTransactionReceipt(
ctx context.Context,
hash common.Hash,
) (*types.Receipt, error) {
_, err := b.transactions.Get(hash)
) (map[string]interface{}, error) {
tx, err := b.transactions.Get(hash)
if err != nil {
return handleError[map[string]interface{}](b.logger, err)
}

receipt, err := b.receipts.GetByTransactionID(hash)
if err != nil {
return handleError[*types.Receipt](b.logger, err)
return handleError[map[string]interface{}](b.logger, err)
}

rcp, err := b.receipts.GetByTransactionID(hash)
txReceipt, err := models.MarshalReceipt(receipt, tx)
if err != nil {
return handleError[*types.Receipt](b.logger, err)
return handleError[map[string]interface{}](b.logger, err)
}

return rcp, nil
return txReceipt, nil
}

// Coinbase is the address that mining rewards will be sent to (alias for Etherbase).
Expand Down
53 changes: 53 additions & 0 deletions models/receipt.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"math/big"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
gethTypes "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"github.com/onflow/cadence"
Expand Down Expand Up @@ -92,3 +93,55 @@ func DecodeReceipt(event cadence.Event) (*gethTypes.Receipt, error) {

return receipt, nil
}

// MarshalReceipt takes a receipt and its associated transaction,
// and marshals the receipt to the proper structure needed by
// eth_getTransactionReceipt.
func MarshalReceipt(
receipt *gethTypes.Receipt,
tx Transaction,
) (map[string]interface{}, error) {
from, err := tx.From()
if err != nil {
return map[string]interface{}{}, err
}

txHash, err := tx.Hash()
if err != nil {
return map[string]interface{}{}, err
}

fields := map[string]interface{}{
"blockHash": receipt.BlockHash,
"blockNumber": hexutil.Uint64(receipt.BlockNumber.Uint64()),
"transactionHash": txHash,
"transactionIndex": hexutil.Uint64(receipt.TransactionIndex),
"from": from,
"to": tx.To(),
"gasUsed": hexutil.Uint64(receipt.GasUsed),
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
"contractAddress": nil,
"logs": receipt.Logs,
"logsBloom": receipt.Bloom,
"type": hexutil.Uint(tx.Type()),
"effectiveGasPrice": (*hexutil.Big)(receipt.EffectiveGasPrice),
}

fields["status"] = hexutil.Uint(receipt.Status)

if receipt.Logs == nil {
fields["logs"] = []*gethTypes.Log{}
}

if tx.Type() == gethTypes.BlobTxType {
fields["blobGasUsed"] = hexutil.Uint64(receipt.BlobGasUsed)
fields["blobGasPrice"] = (*hexutil.Big)(receipt.BlobGasPrice)
}

// If the ContractAddress is 20 0x0 bytes, assume it is not a contract creation
if receipt.ContractAddress != (common.Address{}) {
fields["contractAddress"] = receipt.ContractAddress
}

return fields, nil
}

0 comments on commit 158d794

Please sign in to comment.