Skip to content

Commit

Permalink
fix(evm): tx receipt proper marshalling
Browse files Browse the repository at this point in the history
  • Loading branch information
onikonychev committed Oct 30, 2024
1 parent 9d0dbd5 commit 9701ac3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
36 changes: 33 additions & 3 deletions eth/rpc/backend/tx_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package backend

import (
"encoding/json"
"fmt"
"math"
"math/big"
Expand Down Expand Up @@ -134,10 +135,39 @@ func (b *Backend) getTransactionByHashPending(txHash gethcommon.Hash) (*rpc.EthT
type TransactionReceipt struct {
gethcore.Receipt

ContractAddress *gethcommon.Address `json:"contractAddress,omitempty"`
ContractAddress *gethcommon.Address
From gethcommon.Address
To *gethcommon.Address
EffectiveGasPrice *big.Int
EffectiveGasPrice *hexutil.Big
}

// MarshalJSON is necessary because without it non gethcore.Receipt fields are omitted
func (r *TransactionReceipt) MarshalJSON() ([]byte, error) {
// Marshal / unmarshal gethcore.Receipt to produce map[string]interface{}
receiptJson, err := json.Marshal(r.Receipt)
if err != nil {
return nil, err
}

var output map[string]interface{}
if err := json.Unmarshal(receiptJson, &output); err != nil {
return nil, err
}

// Add extra (non gethcore.Receipt) fields:
if r.ContractAddress != nil && *r.ContractAddress != (gethcommon.Address{}) {
output["contractAddress"] = r.ContractAddress
}
if r.From != (gethcommon.Address{}) {
output["from"] = r.From
}
if r.To != nil {
output["to"] = r.To
}
if r.EffectiveGasPrice != nil {
output["effectiveGasPrice"] = r.EffectiveGasPrice
}
return json.Marshal(output)
}

// GetTransactionReceipt returns the transaction receipt identified by hash.
Expand Down Expand Up @@ -253,7 +283,7 @@ func (b *Backend) GetTransactionReceipt(hash gethcommon.Hash) (*TransactionRecei
// tolerate the error for pruned node.
b.logger.Error("fetch basefee failed, node is pruned?", "height", res.Height, "error", err)
} else {
receipt.EffectiveGasPrice = dynamicTx.EffectiveGasPriceWeiPerGas(baseFeeWei)
receipt.EffectiveGasPrice = (*hexutil.Big)(dynamicTx.EffectiveGasPriceWeiPerGas(baseFeeWei))
}
}
return &receipt, nil
Expand Down
4 changes: 2 additions & 2 deletions eth/rpc/backend/tx_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,10 @@ func (s *BackendSuite) TestReceiptMarshalJson() {
ContractAddress: nil,
From: evmtest.NewEthPrivAcc().EthAddr,
To: &toAddr,
EffectiveGasPrice: big.NewInt(1),
EffectiveGasPrice: (*hexutil.Big)(big.NewInt(1)),
}

jsonBz, err := json.Marshal(tr)
jsonBz, err := tr.MarshalJSON()
s.Require().NoError(err)

gethReceipt := new(gethcore.Receipt)
Expand Down

0 comments on commit 9701ac3

Please sign in to comment.