1616
1717package ethrpc
1818
19- import "github.com/hyperledger/firefly-signer/pkg/ethtypes"
19+ import (
20+ "context"
21+ "encoding/json"
22+ "math/big"
23+
24+ "github.com/hyperledger/firefly-signer/pkg/ethtypes"
25+ )
2026
2127// TxReceiptJSONRPC is the receipt obtained over JSON/RPC from the ethereum client, with gas used, logs and contract address
2228type TxReceiptJSONRPC struct {
29+ TransactionHash ethtypes.HexBytes0xPrefix `json:"transactionHash"`
30+ TransactionIndex * ethtypes.HexInteger `json:"transactionIndex"`
2331 BlockHash ethtypes.HexBytes0xPrefix `json:"blockHash"`
2432 BlockNumber * ethtypes.HexInteger `json:"blockNumber"`
25- ContractAddress * ethtypes.Address0xHex `json:"contractAddress"`
26- CumulativeGasUsed * ethtypes.HexInteger `json:"cumulativeGasUsed"`
2733 From * ethtypes.Address0xHex `json:"from"`
34+ To * ethtypes.Address0xHex `json:"to"`
35+ CumulativeGasUsed * ethtypes.HexInteger `json:"cumulativeGasUsed"`
36+ EffectiveGasPrice * ethtypes.HexInteger `json:"effectiveGasPrice"`
2837 GasUsed * ethtypes.HexInteger `json:"gasUsed"`
38+ ContractAddress * ethtypes.Address0xHex `json:"contractAddress"`
2939 Logs []* LogJSONRPC `json:"logs"`
40+ LogsBloom ethtypes.HexBytes0xPrefix `json:"logsBloom"`
41+ Type * ethtypes.HexInteger `json:"type"`
3042 Status * ethtypes.HexInteger `json:"status"`
31- To * ethtypes.Address0xHex `json:"to"`
32- TransactionHash ethtypes.HexBytes0xPrefix `json:"transactionHash"`
33- TransactionIndex * ethtypes.HexInteger `json:"transactionIndex"`
3443 RevertReason * ethtypes.HexBytes0xPrefix `json:"revertReason"`
3544}
3645
46+ func (txr * TxReceiptJSONRPC ) MarshalFormat (ctx context.Context , format JSONFormatOptions ) (_ []byte , err error ) {
47+ logsArray := make ([]json.RawMessage , len (txr .Logs ))
48+ for i , l := range txr .Logs {
49+ if logsArray [i ], err = l .MarshalFormat (ctx , format ); err != nil {
50+ return nil , err
51+ }
52+ }
53+ formatMap := map [string ]any {
54+ "transactionHash" : ([]byte )(txr .TransactionHash ),
55+ "transactionIndex" : (* big .Int )(txr .TransactionIndex ),
56+ "blockHash" : ([]byte )(txr .BlockHash ),
57+ "blockNumber" : (* big .Int )(txr .BlockNumber ),
58+ "from" : (* [20 ]byte )(txr .From ),
59+ "to" : (* [20 ]byte )(txr .To ),
60+ "cumulativeGasUsed" : (* big .Int )(txr .CumulativeGasUsed ),
61+ "effectiveGasPrice" : (* big .Int )(txr .EffectiveGasPrice ),
62+ "gasUsed" : (* big .Int )(txr .GasUsed ),
63+ "contractAddress" : (* [20 ]byte )(txr .ContractAddress ),
64+ "logs" : logsArray ,
65+ "logsBloom" : ([]byte )(txr .LogsBloom ),
66+ "status" : (* big .Int )(txr .Status ),
67+ "type" : (* big .Int )(txr .Type ),
68+ }
69+ if txr .RevertReason != nil {
70+ formatMap ["revertReason" ] = ([]byte )(* txr .RevertReason )
71+ }
72+ return format .MarshalFormattedMap (ctx , formatMap )
73+ }
74+
3775// TxInfoJSONRPC is the transaction info obtained over JSON/RPC from the ethereum client, with input data
3876type TxInfoJSONRPC struct {
3977 BlockHash ethtypes.HexBytes0xPrefix `json:"blockHash"` // null if pending
@@ -43,12 +81,32 @@ type TxInfoJSONRPC struct {
4381 GasPrice * ethtypes.HexInteger `json:"gasPrice"`
4482 Hash ethtypes.HexBytes0xPrefix `json:"hash"`
4583 Input ethtypes.HexBytes0xPrefix `json:"input"`
46- R * ethtypes.HexInteger `json:"r"`
47- S * ethtypes.HexInteger `json:"s"`
84+ Nonce * ethtypes.HexInteger `json:"nonce"`
4885 To * ethtypes.Address0xHex `json:"to"`
4986 TransactionIndex * ethtypes.HexInteger `json:"transactionIndex"` // null if pending
50- V * ethtypes.HexInteger `json:"v"`
5187 Value * ethtypes.HexInteger `json:"value"`
88+ V * ethtypes.HexInteger `json:"v"`
89+ R * ethtypes.HexInteger `json:"r"`
90+ S * ethtypes.HexInteger `json:"s"`
91+ }
92+
93+ func (txi * TxInfoJSONRPC ) MarshalFormat (ctx context.Context , format JSONFormatOptions ) (_ []byte , err error ) {
94+ return format .MarshalFormattedMap (ctx , map [string ]any {
95+ "blockHash" : ([]byte )(txi .BlockHash ),
96+ "blockNumber" : (* big .Int )(txi .BlockNumber ),
97+ "from" : (* [20 ]byte )(txi .From ),
98+ "gas" : (* big .Int )(txi .Gas ),
99+ "gasPrice" : (* big .Int )(txi .GasPrice ),
100+ "hash" : ([]byte )(txi .Hash ),
101+ "input" : ([]byte )(txi .Input ),
102+ "nonce" : (* big .Int )(txi .Nonce ),
103+ "to" : (* [20 ]byte )(txi .To ),
104+ "transactionIndex" : (* big .Int )(txi .TransactionIndex ),
105+ "value" : (* big .Int )(txi .Value ),
106+ "v" : (* big .Int )(txi .V ),
107+ "r" : (* big .Int )(txi .R ),
108+ "s" : (* big .Int )(txi .S ),
109+ })
52110}
53111
54112type LogFilterJSONRPC struct {
@@ -70,6 +128,24 @@ type LogJSONRPC struct {
70128 Topics []ethtypes.HexBytes0xPrefix `json:"topics"`
71129}
72130
131+ func (l * LogJSONRPC ) MarshalFormat (ctx context.Context , format JSONFormatOptions ) (_ []byte , err error ) {
132+ topicsArray := make ([]any , len (l .Topics ))
133+ for i , t := range l .Topics {
134+ topicsArray [i ] = ([]byte )(t )
135+ }
136+ return format .MarshalFormattedMap (ctx , map [string ]any {
137+ "removed" : l .Removed ,
138+ "logIndex" : (* big .Int )(l .LogIndex ),
139+ "transactionIndex" : (* big .Int )(l .TransactionIndex ),
140+ "blockNumber" : (* big .Int )(l .BlockNumber ),
141+ "transactionHash" : ([]byte )(l .TransactionHash ),
142+ "blockHash" : ([]byte )(l .BlockHash ),
143+ "address" : (* [20 ]byte )(l .Address ),
144+ "data" : ([]byte )(l .Data ),
145+ "topics" : topicsArray ,
146+ })
147+ }
148+
73149// BlockInfoJSONRPC are the info fields we parse from the JSON/RPC response, and cache
74150type BlockInfoJSONRPC struct {
75151 Number * ethtypes.HexInteger `json:"number"`
@@ -79,3 +155,18 @@ type BlockInfoJSONRPC struct {
79155 LogsBloom ethtypes.HexBytes0xPrefix `json:"logsBloom"`
80156 Transactions []ethtypes.HexBytes0xPrefix `json:"transactions"`
81157}
158+
159+ func (bi * BlockInfoJSONRPC ) MarshalFormat (ctx context.Context , format JSONFormatOptions ) (_ []byte , err error ) {
160+ txnArray := make ([]any , len (bi .Transactions ))
161+ for i , t := range bi .Transactions {
162+ txnArray [i ] = ([]byte )(t )
163+ }
164+ return format .MarshalFormattedMap (ctx , map [string ]any {
165+ "number" : (* big .Int )(bi .Number ),
166+ "hash" : ([]byte )(bi .Hash ),
167+ "parentHash" : ([]byte )(bi .ParentHash ),
168+ "timestamp" : (* big .Int )(bi .Timestamp ),
169+ "logsBloom" : ([]byte )(bi .LogsBloom ),
170+ "transactions" : txnArray ,
171+ })
172+ }
0 commit comments