Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return proper response in eth_getTransactionReceipt #179

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading