forked from onrik/ethrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
355 lines (311 loc) · 9.73 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
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
package ethrpc
import (
"bytes"
"encoding/json"
"math/big"
"unsafe"
)
// Syncing - object with syncing data info
type Syncing struct {
IsSyncing bool
StartingBlock int
CurrentBlock int
HighestBlock int
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (s *Syncing) UnmarshalJSON(data []byte) error {
proxy := new(proxySyncing)
if err := json.Unmarshal(data, proxy); err != nil {
return err
}
proxy.IsSyncing = true
*s = *(*Syncing)(unsafe.Pointer(proxy))
return nil
}
// T - input transaction object
type T struct {
From string
To string
Gas int
GasPrice *big.Int
Value *big.Int
Data string
Nonce int
}
// MarshalJSON implements the json.Unmarshaler interface.
func (t T) MarshalJSON() ([]byte, error) {
params := map[string]interface{}{
"from": t.From,
}
if t.To != "" {
params["to"] = t.To
}
if t.Gas > 0 {
params["gas"] = IntToHex(t.Gas)
}
if t.GasPrice != nil {
params["gasPrice"] = BigToHex(*t.GasPrice)
}
if t.Value != nil {
params["value"] = BigToHex(*t.Value)
}
if t.Data != "" {
params["data"] = t.Data
}
if t.Nonce > 0 {
params["nonce"] = IntToHex(t.Nonce)
}
return json.Marshal(params)
}
// Transaction - transaction object
type Transaction struct {
BlockHash string
BlockNumber *int
From string
Gas int
GasPrice big.Int
MaxFeePerGas *big.Int
MaxPriorityFeePerGas *big.Int
Hash string
Input string
Nonce int
To string
TransactionIndex *int
Value big.Int
Type int
AccessesList []interface{}
ChainID *int
V string
R string
S string
}
func (t *Transaction) MarshalJSON() ([]byte, error) {
proxy := (*proxyTransaction)(unsafe.Pointer(t))
return json.Marshal(proxy)
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *Transaction) UnmarshalJSON(data []byte) error {
proxy := new(proxyTransaction)
if err := json.Unmarshal(data, proxy); err != nil {
return err
}
*t = *(*Transaction)(unsafe.Pointer(proxy))
return nil
}
// Log - log object
type Log struct {
Removed bool
LogIndex int
TransactionIndex int
TransactionHash string
BlockNumber int
BlockHash string
Address string
Data string
Topics []string
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (log *Log) UnmarshalJSON(data []byte) error {
proxy := new(proxyLog)
if err := json.Unmarshal(data, proxy); err != nil {
return err
}
*log = *(*Log)(unsafe.Pointer(proxy))
return nil
}
// FilterParams - Filter parameters object
type FilterParams struct {
FromBlock string `json:"fromBlock,omitempty"`
ToBlock string `json:"toBlock,omitempty"`
Address []string `json:"address,omitempty"`
Topics [][]string `json:"topics,omitempty"`
}
// TransactionReceipt - transaction receipt object
type TransactionReceipt struct {
TransactionHash string
TransactionIndex int
BlockHash string
BlockNumber int
CumulativeGasUsed int
GasUsed int
ContractAddress string
Logs []Log
LogsBloom string
Root string
Status string
}
// UnmarshalJSON implements the json.Unmarshaler interface.
func (t *TransactionReceipt) UnmarshalJSON(data []byte) error {
proxy := new(proxyTransactionReceipt)
if err := json.Unmarshal(data, proxy); err != nil {
return err
}
*t = *(*TransactionReceipt)(unsafe.Pointer(proxy))
return nil
}
// Block - block object
type Block struct {
Number int
Hash string
ParentHash string
Nonce string
Sha3Uncles string
LogsBloom string
TransactionsRoot string
StateRoot string
Miner string
Difficulty big.Int
TotalDifficulty big.Int
ExtraData string
Size int
GasLimit int
GasUsed int
Timestamp int
Uncles []string
Transactions []Transaction
}
type proxySyncing struct {
IsSyncing bool `json:"-"`
StartingBlock hexInt `json:"startingBlock"`
CurrentBlock hexInt `json:"currentBlock"`
HighestBlock hexInt `json:"highestBlock"`
}
type proxyTransaction struct {
BlockHash string `json:"blockHash"`
BlockNumber *hexInt `json:"blockNumber"`
From string `json:"from"`
Gas hexInt `json:"gas"`
GasPrice hexBig `json:"gasPrice"`
MaxFeePerGas *hexBig `json:"maxFeePerGas"`
MaxPriorityFeePerGas *hexBig `json:"maxPriorityFeePerGas"`
Hash string `json:"hash"`
Input string `json:"input"`
Nonce hexInt `json:"nonce"`
To string `json:"to"`
TransactionIndex *hexInt `json:"transactionIndex"`
Value hexBig `json:"value"`
Type hexInt `json:"type"`
AccessesList []interface{} `json:"accessList"`
ChainID *hexInt `json:"chainId"`
V string `json:"v"`
R string `json:"r"`
S string `json:"s"`
}
type proxyLog struct {
Removed bool `json:"removed"`
LogIndex hexInt `json:"logIndex"`
TransactionIndex hexInt `json:"transactionIndex"`
TransactionHash string `json:"transactionHash"`
BlockNumber hexInt `json:"blockNumber"`
BlockHash string `json:"blockHash"`
Address string `json:"address"`
Data string `json:"data"`
Topics []string `json:"topics"`
}
type proxyTransactionReceipt struct {
TransactionHash string `json:"transactionHash"`
TransactionIndex hexInt `json:"transactionIndex"`
BlockHash string `json:"blockHash"`
BlockNumber hexInt `json:"blockNumber"`
CumulativeGasUsed hexInt `json:"cumulativeGasUsed"`
GasUsed hexInt `json:"gasUsed"`
ContractAddress string `json:"contractAddress,omitempty"`
Logs []Log `json:"logs"`
LogsBloom string `json:"logsBloom"`
Root string `json:"root"`
Status string `json:"status,omitempty"`
}
type hexInt int
func (i hexInt) MarshalJSON() ([]byte, error) {
hex := IntToHex(int(i))
return json.Marshal(hex)
}
func (i *hexInt) UnmarshalJSON(data []byte) error {
result, err := ParseInt(string(bytes.Trim(data, `"`)))
*i = hexInt(result)
return err
}
type hexBig big.Int
func (i hexBig) MarshalJSON() ([]byte, error) {
hex := BigToHex(big.Int(i))
return json.Marshal(hex)
}
func (i *hexBig) UnmarshalJSON(data []byte) error {
result, err := ParseBigInt(string(bytes.Trim(data, `"`)))
*i = hexBig(result)
return err
}
type proxyBlock interface {
toBlock() Block
}
type proxyBlockWithTransactions struct {
Number hexInt `json:"number"`
Hash string `json:"hash"`
ParentHash string `json:"parentHash"`
Nonce string `json:"nonce"`
Sha3Uncles string `json:"sha3Uncles"`
LogsBloom string `json:"logsBloom"`
TransactionsRoot string `json:"transactionsRoot"`
StateRoot string `json:"stateRoot"`
Miner string `json:"miner"`
Difficulty hexBig `json:"difficulty"`
TotalDifficulty hexBig `json:"totalDifficulty"`
ExtraData string `json:"extraData"`
Size hexInt `json:"size"`
GasLimit hexInt `json:"gasLimit"`
GasUsed hexInt `json:"gasUsed"`
Timestamp hexInt `json:"timestamp"`
Uncles []string `json:"uncles"`
Transactions []proxyTransaction `json:"transactions"`
}
func (proxy *proxyBlockWithTransactions) toBlock() Block {
return *(*Block)(unsafe.Pointer(proxy))
}
type proxyBlockWithoutTransactions struct {
Number hexInt `json:"number"`
Hash string `json:"hash"`
ParentHash string `json:"parentHash"`
Nonce string `json:"nonce"`
Sha3Uncles string `json:"sha3Uncles"`
LogsBloom string `json:"logsBloom"`
TransactionsRoot string `json:"transactionsRoot"`
StateRoot string `json:"stateRoot"`
Miner string `json:"miner"`
Difficulty hexBig `json:"difficulty"`
TotalDifficulty hexBig `json:"totalDifficulty"`
ExtraData string `json:"extraData"`
Size hexInt `json:"size"`
GasLimit hexInt `json:"gasLimit"`
GasUsed hexInt `json:"gasUsed"`
Timestamp hexInt `json:"timestamp"`
Uncles []string `json:"uncles"`
Transactions []string `json:"transactions"`
}
func (proxy *proxyBlockWithoutTransactions) toBlock() Block {
block := Block{
Number: int(proxy.Number),
Hash: proxy.Hash,
ParentHash: proxy.ParentHash,
Nonce: proxy.Nonce,
Sha3Uncles: proxy.Sha3Uncles,
LogsBloom: proxy.LogsBloom,
TransactionsRoot: proxy.TransactionsRoot,
StateRoot: proxy.StateRoot,
Miner: proxy.Miner,
Difficulty: big.Int(proxy.Difficulty),
TotalDifficulty: big.Int(proxy.TotalDifficulty),
ExtraData: proxy.ExtraData,
Size: int(proxy.Size),
GasLimit: int(proxy.GasLimit),
GasUsed: int(proxy.GasUsed),
Timestamp: int(proxy.Timestamp),
Uncles: proxy.Uncles,
}
block.Transactions = make([]Transaction, len(proxy.Transactions))
for i := range proxy.Transactions {
block.Transactions[i] = Transaction{
Hash: proxy.Transactions[i],
}
}
return block
}