forked from 0xPolygonHermez/cdk-erigon
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreceipt.go
90 lines (79 loc) · 2.56 KB
/
receipt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package ethutils
import (
"math/big"
"github.com/holiman/uint256"
"github.com/ledgerwatch/log/v3"
"github.com/ledgerwatch/erigon-lib/chain"
"github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/hexutil"
"github.com/ledgerwatch/erigon/consensus/misc"
"github.com/ledgerwatch/erigon/core/types"
)
func MarshalReceipt(
receipt *types.Receipt,
txn types.Transaction,
chainConfig *chain.Config,
header *types.Header,
txnHash common.Hash,
signed bool,
) map[string]interface{} {
var chainId *big.Int
switch t := txn.(type) {
case *types.LegacyTx:
if t.Protected() {
chainId = types.DeriveChainId(&t.V).ToBig()
}
default:
chainId = txn.GetChainID().ToBig()
}
var from common.Address
if signed {
signer := types.LatestSignerForChainID(chainId)
from, _ = txn.Sender(*signer)
}
fields := map[string]interface{}{
"blockHash": receipt.BlockHash,
"blockNumber": hexutil.Uint64(receipt.BlockNumber.Uint64()),
"transactionHash": txnHash,
"transactionIndex": hexutil.Uint64(receipt.TransactionIndex),
"from": from,
"to": txn.GetTo(),
"type": hexutil.Uint(txn.Type()),
"gasUsed": hexutil.Uint64(receipt.GasUsed),
"cumulativeGasUsed": hexutil.Uint64(receipt.CumulativeGasUsed),
"contractAddress": nil,
"logs": receipt.Logs,
"logsBloom": types.CreateBloom(types.Receipts{receipt}),
}
if !chainConfig.IsLondon(header.Number.Uint64()) {
fields["effectiveGasPrice"] = (*hexutil.Big)(txn.GetPrice().ToBig())
} else {
baseFee, _ := uint256.FromBig(header.BaseFee)
gasPrice := new(big.Int).Add(header.BaseFee, txn.GetEffectiveGasTip(baseFee).ToBig())
fields["effectiveGasPrice"] = (*hexutil.Big)(gasPrice)
}
// Assign receipt status.
fields["status"] = hexutil.Uint64(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
}
// Set derived blob related fields
numBlobs := len(txn.GetBlobHashes())
if numBlobs > 0 {
if header.ExcessBlobGas == nil {
log.Warn("excess blob gas not set when trying to marshal blob tx")
} else {
blobGasPrice, err := misc.GetBlobGasPrice(chainConfig, *header.ExcessBlobGas)
if err != nil {
log.Error(err.Error())
}
fields["blobGasPrice"] = (*hexutil.Big)(blobGasPrice.ToBig())
fields["blobGasUsed"] = hexutil.Uint64(misc.GetBlobGasUsed(numBlobs))
}
}
return fields
}