-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtypes.go
159 lines (142 loc) · 3.92 KB
/
types.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package common
import (
"encoding/json"
"fmt"
"strconv"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/types"
"github.com/holiman/uint256"
)
type Address struct {
Addr [20]byte
}
type Block struct {
Number uint64
BaseFeePerGas uint256.Int
Difficulty uint256.Int
ExtraData []byte
GasLimit uint64
GasUsed uint64
Hash [32]byte
LogsBloom []byte
Miner Address
MixHash [32]byte
Nonce string
ParentHash [32]byte
ReceiptsRoot [32]byte
Sha3Uncles [32]byte
Size uint64
StateRoot [32]byte
Timestamp uint64
TotalDifficulty uint64
Transactions Transactions
TransactionsRoot [32]byte
Uncles [][32]byte
BlobGasUsed *uint64
ExcessBlobGas *uint64
}
type Transactions struct {
Hashes [][32]byte
Full []Transaction // transaction needs to be defined
}
// Updated as earlier, txn data fetched from rpc was not able to unmarshal
// into the struct
type Transaction struct {
AccessList types.AccessList `json:"accessList"`
Hash common.Hash `json:"hash"`
Nonce hexutil.Uint64 `json:"nonce"`
BlockHash common.Hash `json:"blockHash"` // Pointer because it's nullable
BlockNumber hexutil.Uint64 `json:"blockNumber"` // Pointer because it's nullable
TransactionIndex hexutil.Uint64 `json:"transactionIndex"`
From *common.Address `json:"from"`
To *common.Address `json:"to"` // Pointer because 'to' can be null for contract creation
Value hexutil.Big `json:"value"`
GasPrice hexutil.Big `json:"gasPrice"`
Gas hexutil.Uint64 `json:"gas"`
Input hexutil.Bytes `json:"input"`
ChainID hexutil.Big `json:"chainId"`
TransactionType hexutil.Uint `json:"type"`
Signature *Signature `json:"signature"`
MaxFeePerGas hexutil.Big `json:"maxFeePerGas"`
MaxPriorityFeePerGas hexutil.Big `json:"maxPriorityFeePerGas"`
MaxFeePerBlobGas hexutil.Big `json:"maxFeePerBlobGas"`
BlobVersionedHashes []common.Hash `json:"blobVersionedHashes"`
}
type Signature struct {
R string
S string
V uint64
YParity Parity
}
type Parity struct {
Value bool
}
func Default() *Transactions {
return &Transactions{
Full: []Transaction{},
}
}
func (t *Transactions) HashesFunc() [][32]byte {
if len(t.Hashes) > 0 {
return t.Hashes
}
hashes := make([][32]byte, len(t.Full))
for i := range t.Full {
hashes[i] = t.Full[i].Hash // Use the Hash field directly
}
return hashes
}
func (t Transactions) MarshalJSON() ([]byte, error) {
if len(t.Hashes) > 0 {
return json.Marshal(t.Hashes)
}
return json.Marshal(t.Full)
}
type BlockTag struct {
Latest bool
Finalized bool
Number uint64
}
func (b BlockTag) String() string {
if b.Latest {
return "latest"
}
if b.Finalized {
return "finalized"
}
return fmt.Sprintf("0x%x", b.Number)
}
func (b *BlockTag) UnmarshalJSON(data []byte) error {
var block string
if err := json.Unmarshal(data, &block); err != nil {
return err
}
switch block {
case "latest":
b.Latest = true
case "finalized":
b.Finalized = true
default:
var err error
b.Number, err = parseBlockNumber(block)
if err != nil {
return err
}
}
return nil
}
func parseBlockNumber(block string) (uint64, error) {
if len(block) > 2 && block[:2] == "0x" {
return parseHexUint64(block[2:])
}
return parseDecimalUint64(block)
}
func parseHexUint64(hexStr string) (uint64, error) {
return strconv.ParseUint(hexStr, 16, 64)
}
func parseDecimalUint64(decStr string) (uint64, error) {
return strconv.ParseUint(decStr, 10, 64)
}
// Example error structs can be defined here
// type BlockNotFoundError struct {}