Skip to content

Commit b9217dc

Browse files
committed
move entity serialization into common package
1 parent 9ffe714 commit b9217dc

File tree

6 files changed

+233
-234
lines changed

6 files changed

+233
-234
lines changed

internal/common/block.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,32 @@ type Block struct {
3232
InsertTimestamp time.Time `json:"insert_timestamp" ch:"insert_timestamp"`
3333
}
3434

35+
// BlockModel represents a simplified Block structure for Swagger documentation
36+
type BlockModel struct {
37+
ChainId string `json:"chain_id"`
38+
Number uint64 `json:"number"`
39+
Hash string `json:"hash"`
40+
ParentHash string `json:"parent_hash"`
41+
Timestamp uint64 `json:"timestamp"`
42+
Nonce string `json:"nonce"`
43+
Sha3Uncles string `json:"sha3_uncles"`
44+
MixHash string `json:"mix_hash"`
45+
Miner string `json:"miner"`
46+
StateRoot string `json:"state_root"`
47+
TransactionsRoot string `json:"transactions_root"`
48+
ReceiptsRoot string `json:"receipts_root"`
49+
LogsBloom string `json:"logs_bloom"`
50+
Size uint64 `json:"size"`
51+
ExtraData string `json:"extra_data"`
52+
Difficulty string `json:"difficulty"`
53+
TotalDifficulty string `json:"total_difficulty"`
54+
TransactionCount uint64 `json:"transaction_count"`
55+
GasLimit uint64 `json:"gas_limit"`
56+
GasUsed uint64 `json:"gas_used"`
57+
WithdrawalsRoot string `json:"withdrawals_root"`
58+
BaseFeePerGas uint64 `json:"base_fee_per_gas"`
59+
}
60+
3561
type BlockData struct {
3662
Block Block
3763
Transactions []Transaction
@@ -46,3 +72,30 @@ type BlockHeader struct {
4672
}
4773

4874
type RawBlock = map[string]interface{}
75+
76+
func (b *Block) Serialize() BlockModel {
77+
return BlockModel{
78+
ChainId: b.ChainId.String(),
79+
Number: b.Number.Uint64(),
80+
Hash: b.Hash,
81+
ParentHash: b.ParentHash,
82+
Timestamp: uint64(b.Timestamp.Unix()),
83+
Nonce: b.Nonce,
84+
Sha3Uncles: b.Sha3Uncles,
85+
MixHash: b.MixHash,
86+
Miner: b.Miner,
87+
StateRoot: b.StateRoot,
88+
TransactionsRoot: b.TransactionsRoot,
89+
ReceiptsRoot: b.ReceiptsRoot,
90+
LogsBloom: b.LogsBloom,
91+
Size: b.Size,
92+
ExtraData: b.ExtraData,
93+
Difficulty: b.Difficulty.String(),
94+
TotalDifficulty: b.TotalDifficulty.String(),
95+
TransactionCount: b.TransactionCount,
96+
GasLimit: b.GasLimit.Uint64(),
97+
GasUsed: b.GasUsed.Uint64(),
98+
WithdrawalsRoot: b.WithdrawalsRoot,
99+
BaseFeePerGas: b.BaseFeePerGas,
100+
}
101+
}

internal/common/log.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,33 @@ func (l *Log) GetTopic(index int) (string, error) {
4343
return "", fmt.Errorf("invalid topic index: %d", index)
4444
}
4545

46+
// LogModel represents a simplified Log structure for Swagger documentation
47+
type LogModel struct {
48+
ChainId string `json:"chain_id"`
49+
BlockNumber uint64 `json:"block_number"`
50+
BlockHash string `json:"block_hash"`
51+
BlockTimestamp uint64 `json:"block_timestamp"`
52+
TransactionHash string `json:"transaction_hash"`
53+
TransactionIndex uint64 `json:"transaction_index"`
54+
LogIndex uint64 `json:"log_index"`
55+
Address string `json:"address"`
56+
Data string `json:"data"`
57+
Topics []string `json:"topics" swaggertype:"array,string"`
58+
}
59+
60+
type DecodedLogDataModel struct {
61+
Name string `json:"name"`
62+
Signature string `json:"signature"`
63+
IndexedParams map[string]interface{} `json:"indexedParams" swaggertype:"object"`
64+
NonIndexedParams map[string]interface{} `json:"nonIndexedParams" swaggertype:"object"`
65+
}
66+
67+
type DecodedLogModel struct {
68+
LogModel
69+
Decoded DecodedLogDataModel `json:"decoded"`
70+
DecodedData DecodedLogDataModel `json:"decodedData" deprecated:"true"` // Deprecated: Use Decoded field instead
71+
}
72+
4673
type RawLogs = []map[string]interface{}
4774
type RawReceipts = []RawReceipt
4875
type RawReceipt = map[string]interface{}
@@ -199,3 +226,39 @@ func convertBytesAndNumericToHex(data interface{}) interface{} {
199226
return v
200227
}
201228
}
229+
230+
func (l *Log) Serialize() LogModel {
231+
allTopics := []string{l.Topic0, l.Topic1, l.Topic2, l.Topic3}
232+
topics := make([]string, 0, len(allTopics))
233+
for _, topic := range allTopics {
234+
if topic != "" {
235+
topics = append(topics, topic)
236+
}
237+
}
238+
return LogModel{
239+
ChainId: l.ChainId.String(),
240+
BlockNumber: l.BlockNumber.Uint64(),
241+
BlockHash: l.BlockHash,
242+
BlockTimestamp: uint64(l.BlockTimestamp.Unix()),
243+
TransactionHash: l.TransactionHash,
244+
TransactionIndex: l.TransactionIndex,
245+
LogIndex: l.LogIndex,
246+
Address: l.Address,
247+
Data: l.Data,
248+
Topics: topics,
249+
}
250+
}
251+
252+
func (l *DecodedLog) Serialize() DecodedLogModel {
253+
decodedData := DecodedLogDataModel{
254+
Name: l.Decoded.Name,
255+
Signature: l.Decoded.Signature,
256+
IndexedParams: l.Decoded.IndexedParams,
257+
NonIndexedParams: l.Decoded.NonIndexedParams,
258+
}
259+
return DecodedLogModel{
260+
LogModel: l.Log.Serialize(),
261+
Decoded: decodedData,
262+
DecodedData: decodedData,
263+
}
264+
}

internal/common/transaction.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,51 @@ type DecodedTransaction struct {
5656
Decoded DecodedTransactionData `json:"decodedData"`
5757
}
5858

59+
// TransactionModel represents a simplified Transaction structure for Swagger documentation
60+
type TransactionModel struct {
61+
ChainId string `json:"chain_id"`
62+
Hash string `json:"hash"`
63+
Nonce uint64 `json:"nonce"`
64+
BlockHash string `json:"block_hash"`
65+
BlockNumber uint64 `json:"block_number"`
66+
BlockTimestamp uint64 `json:"block_timestamp"`
67+
TransactionIndex uint64 `json:"transaction_index"`
68+
FromAddress string `json:"from_address"`
69+
ToAddress string `json:"to_address"`
70+
Value uint64 `json:"value"`
71+
Gas uint64 `json:"gas"`
72+
GasPrice uint64 `json:"gas_price"`
73+
Data string `json:"data"`
74+
FunctionSelector string `json:"function_selector"`
75+
MaxFeePerGas uint64 `json:"max_fee_per_gas"`
76+
MaxPriorityFeePerGas uint64 `json:"max_priority_fee_per_gas"`
77+
TransactionType uint8 `json:"transaction_type"`
78+
R string `json:"r"`
79+
S string `json:"s"`
80+
V string `json:"v"`
81+
AccessListJson *string `json:"access_list_json"`
82+
ContractAddress *string `json:"contract_address"`
83+
GasUsed *uint64 `json:"gas_used"`
84+
CumulativeGasUsed *uint64 `json:"cumulative_gas_used"`
85+
EffectiveGasPrice *big.Int `json:"effective_gas_price"`
86+
BlobGasUsed *uint64 `json:"blob_gas_used"`
87+
BlobGasPrice *big.Int `json:"blob_gas_price"`
88+
LogsBloom *string `json:"logs_bloom"`
89+
Status *uint64 `json:"status"`
90+
}
91+
92+
type DecodedTransactionDataModel struct {
93+
Name string `json:"name"`
94+
Signature string `json:"signature"`
95+
Inputs map[string]interface{} `json:"inputs"`
96+
}
97+
98+
type DecodedTransactionModel struct {
99+
TransactionModel
100+
Decoded DecodedTransactionDataModel `json:"decoded"`
101+
DecodedData DecodedTransactionDataModel `json:"decodedData" deprecated:"true"` // Deprecated: Use Decoded field instead
102+
}
103+
59104
func DecodeTransactions(chainId string, txs []Transaction) []*DecodedTransaction {
60105
decodedTxs := make([]*DecodedTransaction, len(txs))
61106
abiCache := make(map[string]*abi.ABI)
@@ -125,3 +170,50 @@ func (t *Transaction) Decode(functionABI *abi.Method) *DecodedTransaction {
125170
Inputs: decodedInputs,
126171
}}
127172
}
173+
174+
func (t *Transaction) Serialize() TransactionModel {
175+
return TransactionModel{
176+
ChainId: t.ChainId.String(),
177+
Hash: t.Hash,
178+
Nonce: t.Nonce,
179+
BlockHash: t.BlockHash,
180+
BlockNumber: t.BlockNumber.Uint64(),
181+
BlockTimestamp: uint64(t.BlockTimestamp.Unix()),
182+
TransactionIndex: t.TransactionIndex,
183+
FromAddress: t.FromAddress,
184+
ToAddress: t.ToAddress,
185+
Value: t.Value.Uint64(),
186+
Gas: t.Gas,
187+
GasPrice: t.GasPrice.Uint64(),
188+
Data: t.Data,
189+
FunctionSelector: t.FunctionSelector,
190+
MaxFeePerGas: t.MaxFeePerGas.Uint64(),
191+
MaxPriorityFeePerGas: t.MaxPriorityFeePerGas.Uint64(),
192+
TransactionType: t.TransactionType,
193+
R: t.R.String(),
194+
S: t.S.String(),
195+
V: t.V.String(),
196+
AccessListJson: t.AccessListJson,
197+
ContractAddress: t.ContractAddress,
198+
GasUsed: t.GasUsed,
199+
CumulativeGasUsed: t.CumulativeGasUsed,
200+
EffectiveGasPrice: t.EffectiveGasPrice,
201+
BlobGasUsed: t.BlobGasUsed,
202+
BlobGasPrice: t.BlobGasPrice,
203+
LogsBloom: t.LogsBloom,
204+
Status: t.Status,
205+
}
206+
}
207+
208+
func (t *DecodedTransaction) Serialize() DecodedTransactionModel {
209+
decodedData := DecodedTransactionDataModel{
210+
Name: t.Decoded.Name,
211+
Signature: t.Decoded.Signature,
212+
Inputs: t.Decoded.Inputs,
213+
}
214+
return DecodedTransactionModel{
215+
TransactionModel: t.Transaction.Serialize(),
216+
Decoded: decodedData,
217+
DecodedData: decodedData,
218+
}
219+
}

internal/handlers/blocks_handlers.go

Lines changed: 4 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,6 @@ import (
88
"github.com/thirdweb-dev/indexer/internal/storage"
99
)
1010

11-
// BlockModel represents a simplified Block structure for Swagger documentation
12-
type BlockModel struct {
13-
ChainId string `json:"chain_id"`
14-
Number uint64 `json:"number"`
15-
Hash string `json:"hash"`
16-
ParentHash string `json:"parent_hash"`
17-
Timestamp uint64 `json:"timestamp"`
18-
Nonce string `json:"nonce"`
19-
Sha3Uncles string `json:"sha3_uncles"`
20-
MixHash string `json:"mix_hash"`
21-
Miner string `json:"miner"`
22-
StateRoot string `json:"state_root"`
23-
TransactionsRoot string `json:"transactions_root"`
24-
ReceiptsRoot string `json:"receipts_root"`
25-
LogsBloom string `json:"logs_bloom"`
26-
Size uint64 `json:"size"`
27-
ExtraData string `json:"extra_data"`
28-
Difficulty string `json:"difficulty"`
29-
TotalDifficulty string `json:"total_difficulty"`
30-
TransactionCount uint64 `json:"transaction_count"`
31-
GasLimit uint64 `json:"gas_limit"`
32-
GasUsed uint64 `json:"gas_used"`
33-
WithdrawalsRoot string `json:"withdrawals_root"`
34-
BaseFeePerGas uint64 `json:"base_fee_per_gas"`
35-
}
36-
3711
// @Summary Get all blocks
3812
// @Description Retrieve all blocks
3913
// @Tags blocks
@@ -49,7 +23,7 @@ type BlockModel struct {
4923
// @Param limit query int false "Number of items per page" default(5)
5024
// @Param aggregate query []string false "List of aggregate functions to apply"
5125
// @Param force_consistent_data query bool false "Force consistent data at the expense of query speed"
52-
// @Success 200 {object} api.QueryResponse{data=[]BlockModel}
26+
// @Success 200 {object} api.QueryResponse{data=[]common.BlockModel}
5327
// @Failure 400 {object} api.Error
5428
// @Failure 401 {object} api.Error
5529
// @Failure 500 {object} api.Error
@@ -133,33 +107,10 @@ func handleBlocksRequest(c *gin.Context) {
133107
sendJSONResponse(c, queryResult)
134108
}
135109

136-
func serializeBlocks(blocks []common.Block) []BlockModel {
137-
blockModels := make([]BlockModel, len(blocks))
110+
func serializeBlocks(blocks []common.Block) []common.BlockModel {
111+
blockModels := make([]common.BlockModel, len(blocks))
138112
for i, block := range blocks {
139-
blockModels[i] = BlockModel{
140-
ChainId: block.ChainId.String(),
141-
Number: block.Number.Uint64(),
142-
Hash: block.Hash,
143-
ParentHash: block.ParentHash,
144-
Timestamp: uint64(block.Timestamp.Unix()),
145-
Nonce: block.Nonce,
146-
Sha3Uncles: block.Sha3Uncles,
147-
MixHash: block.MixHash,
148-
Miner: block.Miner,
149-
StateRoot: block.StateRoot,
150-
TransactionsRoot: block.TransactionsRoot,
151-
ReceiptsRoot: block.ReceiptsRoot,
152-
LogsBloom: block.LogsBloom,
153-
Size: block.Size,
154-
ExtraData: block.ExtraData,
155-
Difficulty: block.Difficulty.String(),
156-
TotalDifficulty: block.TotalDifficulty.String(),
157-
TransactionCount: block.TransactionCount,
158-
GasLimit: block.GasLimit.Uint64(),
159-
GasUsed: block.GasUsed.Uint64(),
160-
WithdrawalsRoot: block.WithdrawalsRoot,
161-
BaseFeePerGas: block.BaseFeePerGas,
162-
}
113+
blockModels[i] = block.Serialize()
163114
}
164115
return blockModels
165116
}

0 commit comments

Comments
 (0)