-
Notifications
You must be signed in to change notification settings - Fork 0
/
proto_types.go
151 lines (146 loc) · 4.63 KB
/
proto_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
package watcher
import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethtypes "github.com/ethereum/go-ethereum/core/types"
prototypes "github.com/brc20-collab/brczero/x/evm/watcher/proto"
)
func transactionToProto(tr *Transaction) *prototypes.Transaction {
var to []byte
if tr.To != nil {
to = tr.To.Bytes()
}
return &prototypes.Transaction{
BlockHash: tr.BlockHash.Bytes(),
BlockNumber: tr.BlockNumber.String(),
From: tr.From.Bytes(),
Gas: uint64(tr.Gas),
GasPrice: tr.GasPrice.String(),
Hash: tr.Hash.Bytes(),
Input: tr.Input,
Nonce: uint64(tr.Nonce),
To: to,
TransactionIndex: uint64(*tr.TransactionIndex),
Value: tr.Value.String(),
V: tr.V.String(),
R: tr.R.String(),
S: tr.S.String(),
}
}
func protoToTransaction(tr *prototypes.Transaction) *Transaction {
blockHash := common.BytesToHash(tr.BlockHash)
blockNum := hexutil.MustDecodeBig(tr.BlockNumber)
gasPrice := hexutil.MustDecodeBig(tr.GasPrice)
var to *common.Address
if len(tr.To) > 0 {
addr := common.BytesToAddress(tr.To)
to = &addr
}
index := hexutil.Uint64(tr.TransactionIndex)
value := hexutil.MustDecodeBig(tr.Value)
v := hexutil.MustDecodeBig(tr.V)
r := hexutil.MustDecodeBig(tr.R)
s := hexutil.MustDecodeBig(tr.S)
return &Transaction{
BlockHash: &blockHash,
BlockNumber: (*hexutil.Big)(blockNum),
From: common.BytesToAddress(tr.From),
Gas: hexutil.Uint64(tr.Gas),
GasPrice: (*hexutil.Big)(gasPrice),
Hash: common.BytesToHash(tr.Hash),
Input: tr.Input,
Nonce: hexutil.Uint64(tr.Nonce),
To: to,
TransactionIndex: &index,
Value: (*hexutil.Big)(value),
V: (*hexutil.Big)(v),
R: (*hexutil.Big)(r),
S: (*hexutil.Big)(s),
}
}
func receiptToProto(tr *TransactionReceipt) *prototypes.TransactionReceipt {
logs := make([]*prototypes.Log, len(tr.Logs))
for i, log := range tr.Logs {
topics := make([][]byte, len(log.Topics))
for j, topic := range log.Topics {
topics[j] = topic.Bytes()
}
logs[i] = &prototypes.Log{
Address: log.Address.Bytes(),
Topics: topics,
Data: log.Data,
BlockNumber: log.BlockNumber,
TxHash: log.TxHash.Bytes(),
TxIndex: uint64(log.TxIndex),
BlockHash: log.BlockHash.Bytes(),
Index: uint64(log.Index),
Removed: log.Removed,
}
}
var contractAddr []byte
if tr.ContractAddress != nil {
contractAddr = tr.ContractAddress.Bytes()
}
var to []byte
if tr.To != nil {
to = tr.To.Bytes()
}
return &prototypes.TransactionReceipt{
Status: uint64(tr.Status),
CumulativeGasUsed: uint64(tr.CumulativeGasUsed),
LogsBloom: tr.LogsBloom.Bytes(),
Logs: logs,
TransactionHash: tr.TransactionHash,
ContractAddress: contractAddr,
GasUsed: uint64(tr.GasUsed),
BlockHash: tr.BlockHash,
BlockNumber: uint64(tr.BlockNumber),
TransactionIndex: uint64(tr.TransactionIndex),
From: tr.From,
To: to,
}
}
func protoToReceipt(tr *prototypes.TransactionReceipt) *TransactionReceipt {
logs := make([]*ethtypes.Log, len(tr.Logs))
for i, log := range tr.Logs {
topics := make([]common.Hash, len(log.Topics))
for j, topic := range log.Topics {
topics[j] = common.BytesToHash(topic)
}
logs[i] = ðtypes.Log{
Address: common.BytesToAddress(log.Address),
Topics: topics,
Data: log.Data,
BlockNumber: log.BlockNumber,
TxHash: common.BytesToHash(log.TxHash),
TxIndex: uint(log.TxIndex),
BlockHash: common.BytesToHash(log.BlockHash),
Index: uint(log.Index),
Removed: log.Removed,
}
}
var contractAddr *common.Address
if len(tr.ContractAddress) > 0 {
addr := common.BytesToAddress(tr.ContractAddress)
contractAddr = &addr
}
var to *common.Address
if len(tr.To) > 0 {
addr := common.BytesToAddress(tr.To)
to = &addr
}
return &TransactionReceipt{
Status: hexutil.Uint64(tr.Status),
CumulativeGasUsed: hexutil.Uint64(tr.CumulativeGasUsed),
LogsBloom: ethtypes.BytesToBloom(tr.LogsBloom),
Logs: logs,
TransactionHash: tr.TransactionHash,
ContractAddress: contractAddr,
GasUsed: hexutil.Uint64(tr.GasUsed),
BlockHash: tr.BlockHash,
BlockNumber: hexutil.Uint64(tr.BlockNumber),
TransactionIndex: hexutil.Uint64(tr.TransactionIndex),
From: tr.From,
To: to,
}
}