-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstructs.go
237 lines (198 loc) · 4.28 KB
/
structs.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package web3
import (
"encoding/hex"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
)
var (
// ZeroAddress is an address of all zeros
ZeroAddress = Address{}
// ZeroHash is a hash of all zeros
ZeroHash = Hash{}
)
// Address is an Ethereum address
type Address [20]byte
// HexToAddress converts an hex string value to an address object
func HexToAddress(str string) Address {
a := Address{}
a.UnmarshalText([]byte(str))
return a
}
// BytesToAddress converts bytes to an address object
func BytesToAddress(b []byte) Address {
var a Address
size := len(b)
min := min(size, 20)
copy(a[20-min:], b[len(b)-min:])
return a
}
// UnmarshalText implements the unmarshal interface
func (a *Address) UnmarshalText(b []byte) error {
return unmarshalTextByte(a[:], b, 20)
}
// MarshalText implements the marshal interface
func (a Address) MarshalText() ([]byte, error) {
return []byte(a.String()), nil
}
func (a Address) String() string {
return "0x" + hex.EncodeToString(a[:])
}
func (a Address) CheckSum() string {
return common.HexToAddress(a.String()).Hex()
}
// Hash is an Ethereum hash
type Hash [32]byte
// HexToHash converts an hex string value to a hash object
func HexToHash(str string) Hash {
h := Hash{}
h.UnmarshalText([]byte(str))
return h
}
// BytesToHash converts bytes to a hash object
func BytesToHash(b []byte) Hash {
var h Hash
size := len(b)
min := min(size, 32)
copy(h[32-min:], b[len(b)-min:])
return h
}
// UnmarshalText implements the unmarshal interface
func (h *Hash) UnmarshalText(b []byte) error {
return unmarshalTextByte(h[:], b, 32)
}
// MarshalText implements the marshal interface
func (h Hash) MarshalText() ([]byte, error) {
return []byte(h.String()), nil
}
func (h Hash) String() string {
return "0x" + hex.EncodeToString(h[:])
}
func (h Hash) Location() string {
return h.String()
}
type Block struct {
Number uint64
Hash Hash
ParentHash Hash
Sha3Uncles Hash
TransactionsRoot Hash
StateRoot Hash
ReceiptsRoot Hash
Miner Address
Difficulty *big.Int
ExtraData []byte
GasLimit uint64
GasUsed uint64
Timestamp uint64
Transactions []*Transaction
TransactionsHashes []Hash
Uncles []Hash
}
type Transaction struct {
Hash Hash
From Address
To *Address
Input []byte
GasPrice uint64
Gas uint64
Value *big.Int
Nonce uint64
V []byte
R []byte
S []byte
BlockHash Hash
BlockNumber uint64
TxnIndex uint64
}
type CallMsg struct {
From Address
To *Address
Data []byte
GasPrice uint64
Value *big.Int
}
type LogFilter struct {
Address []Address
Topics []*Hash
BlockHash *Hash
From *BlockNumber
To *BlockNumber
}
func (l *LogFilter) SetFromUint64(num uint64) {
b := BlockNumber(num)
l.From = &b
}
func (l *LogFilter) SetToUint64(num uint64) {
b := BlockNumber(num)
l.To = &b
}
func (l *LogFilter) SetTo(b BlockNumber) {
l.To = &b
}
type Receipt struct {
TransactionHash Hash
TransactionIndex uint64
ContractAddress Address
BlockHash Hash
From Address
BlockNumber uint64
GasUsed uint64
CumulativeGasUsed uint64
LogsBloom []byte
Logs []*Log
}
type Log struct {
Removed bool
LogIndex uint64
TransactionIndex uint64
TransactionHash Hash
BlockHash Hash
BlockNumber uint64
Address Address
Topics []Hash
Data []byte
}
type BlockNumber int
const (
Latest BlockNumber = -1
Earliest = -2
Pending = -3
)
func (b BlockNumber) Location() string {
return b.String()
}
func (b BlockNumber) String() string {
switch b {
case Latest:
return "latest"
case Earliest:
return "earliest"
case Pending:
return "pending"
}
if b < 0 {
panic("internal. blocknumber is negative")
}
return fmt.Sprintf("0x%x", uint64(b))
}
func EncodeBlock(block ...BlockNumber) BlockNumber {
if len(block) != 1 {
return Latest
}
return block[0]
}
type BlockNumberOrHash interface {
Location() string
}
func (b *Block) Copy() *Block {
bb := new(Block)
*bb = *b
return bb
}
func min(i, j int) int {
if i < j {
return i
}
return j
}