forked from umbracle/ethgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.go
404 lines (342 loc) · 7.64 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
package ethgo
import (
"encoding/hex"
"fmt"
"math/big"
"strconv"
"strings"
)
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(completeHex(str, 20))
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
}
// Address implements the ethgo.Key interface Address method.
func (a Address) Address() Address {
return a
}
// Sign implements the ethgo.Key interface Sign method.
func (a Address) Sign(hash []byte) ([]byte, error) {
panic("an address cannot sign messages")
}
// 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
}
// Bytes returns the bytes of the Address
func (a Address) Bytes() []byte {
return a[:]
}
func (a Address) String() string {
return a.checksumEncode()
}
func (a Address) checksumEncode() string {
address := strings.ToLower(hex.EncodeToString(a[:]))
hash := hex.EncodeToString(Keccak256([]byte(address)))
ret := "0x"
for i := 0; i < len(address); i++ {
character := string(address[i])
num, _ := strconv.ParseInt(string(hash[i]), 16, 64)
if num > 7 {
ret += strings.ToUpper(character)
} else {
ret += character
}
}
return ret
}
// 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(completeHex(str, 32))
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
}
// Bytes returns the bytes of the Hash
func (h Hash) Bytes() []byte {
return h[:]
}
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
MixHash Hash
Nonce [8]byte
Transactions []*Transaction
TransactionsHashes []Hash
Uncles []Hash
}
func (b *Block) Copy() *Block {
bb := new(Block)
*bb = *b
if b.Difficulty != nil {
bb.Difficulty = new(big.Int).Set(b.Difficulty)
}
bb.ExtraData = append(bb.ExtraData[:0], b.ExtraData...)
bb.Transactions = make([]*Transaction, len(b.Transactions))
for indx, txn := range b.Transactions {
bb.Transactions[indx] = txn.Copy()
}
return bb
}
type TransactionType int
const (
TransactionLegacy TransactionType = 0
// eip-2930
TransactionAccessList TransactionType = 1
// eip-1559
TransactionDynamicFee TransactionType = 2
)
type Transaction struct {
Type TransactionType
// legacy values
Hash Hash
From Address
To *Address
Input []byte
GasPrice uint64
Gas uint64
Value *big.Int
Nonce uint64
V []byte
R []byte
S []byte
// jsonrpc values
BlockHash Hash
BlockNumber uint64
TxnIndex uint64
// eip-2930 values
ChainID *big.Int
AccessList AccessList
// eip-1559 values
MaxPriorityFeePerGas *big.Int
MaxFeePerGas *big.Int
}
func (t *Transaction) Copy() *Transaction {
tt := new(Transaction)
*tt = *t
if t.To != nil {
to := Address(*t.To)
tt.To = &to
}
tt.Input = append(tt.Input[:0], t.Input...)
if t.Value != nil {
tt.Value = new(big.Int).Set(t.Value)
}
tt.V = append(tt.V[:0], t.V...)
tt.R = append(tt.R[:0], t.R...)
tt.S = append(tt.S[:0], t.S...)
if t.ChainID != nil {
tt.ChainID = new(big.Int).Set(t.ChainID)
}
if t.MaxPriorityFeePerGas != nil {
tt.MaxPriorityFeePerGas = new(big.Int).Set(t.MaxPriorityFeePerGas)
}
if t.MaxFeePerGas != nil {
tt.MaxFeePerGas = new(big.Int).Set(t.MaxFeePerGas)
}
tt.AccessList = t.AccessList.Copy()
return tt
}
type AccessEntry struct {
Address Address `json:"address"`
Storage []Hash `json:"storageKeys"`
}
type AccessList []AccessEntry
func (a *AccessList) Copy() AccessList {
aa := AccessList{}
for _, i := range *a {
entry := AccessEntry{
Address: i.Address,
Storage: []Hash{},
}
entry.Storage = append(entry.Storage, i.Storage...)
aa = append(aa, entry)
}
return aa
}
type CallMsg struct {
From Address
To *Address
Data []byte
GasPrice uint64
Gas *big.Int
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
Status uint64
To *Address
}
func (r *Receipt) Copy() *Receipt {
rr := new(Receipt)
*rr = *r
rr.LogsBloom = append(rr.LogsBloom[:0], r.LogsBloom...)
rr.Logs = make([]*Log, len(r.Logs))
for indx, log := range r.Logs {
rr.Logs[indx] = log.Copy()
}
return rr
}
type Log struct {
Removed bool
LogIndex uint64
TransactionIndex uint64
TransactionHash Hash
BlockHash Hash
BlockNumber uint64
Address Address
Topics []Hash
Data []byte
}
func (l *Log) Copy() *Log {
ll := new(Log)
*ll = *l
ll.Data = append(ll.Data[:0], l.Data...)
return ll
}
type BlockNumber int
const (
Latest BlockNumber = -1
Earliest BlockNumber = -2
Pending BlockNumber = -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 min(i, j int) int {
if i < j {
return i
}
return j
}
type Key interface {
Address() Address
Sign(hash []byte) ([]byte, error)
}
func completeHex(str string, num int) []byte {
num = num * 2
str = strings.TrimPrefix(str, "0x")
size := len(str)
if size < num {
for i := size; i < num; i++ {
str = "0" + str
}
} else {
diff := size - num
str = str[diff:]
}
return []byte("0x" + str)
}
type OverrideAccount struct {
Nonce *uint64
Code *[]byte
Balance *big.Int
State *map[Hash]Hash
StateDiff *map[Hash]Hash
}
type StateOverride map[Address]OverrideAccount