From e85224e0d9e944dab71f259d89d9ac6735fa9370 Mon Sep 17 00:00:00 2001 From: iuwqyir Date: Wed, 26 Feb 2025 00:13:12 +0200 Subject: [PATCH] move entity serialization into common package --- internal/common/block.go | 53 ++++++++++ internal/common/log.go | 63 ++++++++++++ internal/common/transaction.go | 92 +++++++++++++++++ internal/handlers/blocks_handlers.go | 57 +---------- internal/handlers/logs_handlers.go | 89 ++-------------- internal/handlers/transactions_handlers.go | 113 ++------------------- 6 files changed, 233 insertions(+), 234 deletions(-) diff --git a/internal/common/block.go b/internal/common/block.go index 39fcefe..1edf18b 100644 --- a/internal/common/block.go +++ b/internal/common/block.go @@ -32,6 +32,32 @@ type Block struct { InsertTimestamp time.Time `json:"insert_timestamp" ch:"insert_timestamp"` } +// BlockModel represents a simplified Block structure for Swagger documentation +type BlockModel struct { + ChainId string `json:"chain_id"` + Number uint64 `json:"number"` + Hash string `json:"hash"` + ParentHash string `json:"parent_hash"` + Timestamp uint64 `json:"timestamp"` + Nonce string `json:"nonce"` + Sha3Uncles string `json:"sha3_uncles"` + MixHash string `json:"mix_hash"` + Miner string `json:"miner"` + StateRoot string `json:"state_root"` + TransactionsRoot string `json:"transactions_root"` + ReceiptsRoot string `json:"receipts_root"` + LogsBloom string `json:"logs_bloom"` + Size uint64 `json:"size"` + ExtraData string `json:"extra_data"` + Difficulty string `json:"difficulty"` + TotalDifficulty string `json:"total_difficulty"` + TransactionCount uint64 `json:"transaction_count"` + GasLimit uint64 `json:"gas_limit"` + GasUsed uint64 `json:"gas_used"` + WithdrawalsRoot string `json:"withdrawals_root"` + BaseFeePerGas uint64 `json:"base_fee_per_gas"` +} + type BlockData struct { Block Block Transactions []Transaction @@ -46,3 +72,30 @@ type BlockHeader struct { } type RawBlock = map[string]interface{} + +func (b *Block) Serialize() BlockModel { + return BlockModel{ + ChainId: b.ChainId.String(), + Number: b.Number.Uint64(), + Hash: b.Hash, + ParentHash: b.ParentHash, + Timestamp: uint64(b.Timestamp.Unix()), + Nonce: b.Nonce, + Sha3Uncles: b.Sha3Uncles, + MixHash: b.MixHash, + Miner: b.Miner, + StateRoot: b.StateRoot, + TransactionsRoot: b.TransactionsRoot, + ReceiptsRoot: b.ReceiptsRoot, + LogsBloom: b.LogsBloom, + Size: b.Size, + ExtraData: b.ExtraData, + Difficulty: b.Difficulty.String(), + TotalDifficulty: b.TotalDifficulty.String(), + TransactionCount: b.TransactionCount, + GasLimit: b.GasLimit.Uint64(), + GasUsed: b.GasUsed.Uint64(), + WithdrawalsRoot: b.WithdrawalsRoot, + BaseFeePerGas: b.BaseFeePerGas, + } +} diff --git a/internal/common/log.go b/internal/common/log.go index 522886b..3eb4455 100644 --- a/internal/common/log.go +++ b/internal/common/log.go @@ -43,6 +43,33 @@ func (l *Log) GetTopic(index int) (string, error) { return "", fmt.Errorf("invalid topic index: %d", index) } +// LogModel represents a simplified Log structure for Swagger documentation +type LogModel struct { + ChainId string `json:"chain_id"` + BlockNumber uint64 `json:"block_number"` + BlockHash string `json:"block_hash"` + BlockTimestamp uint64 `json:"block_timestamp"` + TransactionHash string `json:"transaction_hash"` + TransactionIndex uint64 `json:"transaction_index"` + LogIndex uint64 `json:"log_index"` + Address string `json:"address"` + Data string `json:"data"` + Topics []string `json:"topics" swaggertype:"array,string"` +} + +type DecodedLogDataModel struct { + Name string `json:"name"` + Signature string `json:"signature"` + IndexedParams map[string]interface{} `json:"indexedParams" swaggertype:"object"` + NonIndexedParams map[string]interface{} `json:"nonIndexedParams" swaggertype:"object"` +} + +type DecodedLogModel struct { + LogModel + Decoded DecodedLogDataModel `json:"decoded"` + DecodedData DecodedLogDataModel `json:"decodedData" deprecated:"true"` // Deprecated: Use Decoded field instead +} + type RawLogs = []map[string]interface{} type RawReceipts = []RawReceipt type RawReceipt = map[string]interface{} @@ -199,3 +226,39 @@ func convertBytesAndNumericToHex(data interface{}) interface{} { return v } } + +func (l *Log) Serialize() LogModel { + allTopics := []string{l.Topic0, l.Topic1, l.Topic2, l.Topic3} + topics := make([]string, 0, len(allTopics)) + for _, topic := range allTopics { + if topic != "" { + topics = append(topics, topic) + } + } + return LogModel{ + ChainId: l.ChainId.String(), + BlockNumber: l.BlockNumber.Uint64(), + BlockHash: l.BlockHash, + BlockTimestamp: uint64(l.BlockTimestamp.Unix()), + TransactionHash: l.TransactionHash, + TransactionIndex: l.TransactionIndex, + LogIndex: l.LogIndex, + Address: l.Address, + Data: l.Data, + Topics: topics, + } +} + +func (l *DecodedLog) Serialize() DecodedLogModel { + decodedData := DecodedLogDataModel{ + Name: l.Decoded.Name, + Signature: l.Decoded.Signature, + IndexedParams: l.Decoded.IndexedParams, + NonIndexedParams: l.Decoded.NonIndexedParams, + } + return DecodedLogModel{ + LogModel: l.Log.Serialize(), + Decoded: decodedData, + DecodedData: decodedData, + } +} diff --git a/internal/common/transaction.go b/internal/common/transaction.go index 90e25ad..bfb2909 100644 --- a/internal/common/transaction.go +++ b/internal/common/transaction.go @@ -56,6 +56,51 @@ type DecodedTransaction struct { Decoded DecodedTransactionData `json:"decodedData"` } +// TransactionModel represents a simplified Transaction structure for Swagger documentation +type TransactionModel struct { + ChainId string `json:"chain_id"` + Hash string `json:"hash"` + Nonce uint64 `json:"nonce"` + BlockHash string `json:"block_hash"` + BlockNumber uint64 `json:"block_number"` + BlockTimestamp uint64 `json:"block_timestamp"` + TransactionIndex uint64 `json:"transaction_index"` + FromAddress string `json:"from_address"` + ToAddress string `json:"to_address"` + Value uint64 `json:"value"` + Gas uint64 `json:"gas"` + GasPrice uint64 `json:"gas_price"` + Data string `json:"data"` + FunctionSelector string `json:"function_selector"` + MaxFeePerGas uint64 `json:"max_fee_per_gas"` + MaxPriorityFeePerGas uint64 `json:"max_priority_fee_per_gas"` + TransactionType uint8 `json:"transaction_type"` + R string `json:"r"` + S string `json:"s"` + V string `json:"v"` + AccessListJson *string `json:"access_list_json"` + ContractAddress *string `json:"contract_address"` + GasUsed *uint64 `json:"gas_used"` + CumulativeGasUsed *uint64 `json:"cumulative_gas_used"` + EffectiveGasPrice *big.Int `json:"effective_gas_price"` + BlobGasUsed *uint64 `json:"blob_gas_used"` + BlobGasPrice *big.Int `json:"blob_gas_price"` + LogsBloom *string `json:"logs_bloom"` + Status *uint64 `json:"status"` +} + +type DecodedTransactionDataModel struct { + Name string `json:"name"` + Signature string `json:"signature"` + Inputs map[string]interface{} `json:"inputs"` +} + +type DecodedTransactionModel struct { + TransactionModel + Decoded DecodedTransactionDataModel `json:"decoded"` + DecodedData DecodedTransactionDataModel `json:"decodedData" deprecated:"true"` // Deprecated: Use Decoded field instead +} + func DecodeTransactions(chainId string, txs []Transaction) []*DecodedTransaction { decodedTxs := make([]*DecodedTransaction, len(txs)) abiCache := make(map[string]*abi.ABI) @@ -125,3 +170,50 @@ func (t *Transaction) Decode(functionABI *abi.Method) *DecodedTransaction { Inputs: decodedInputs, }} } + +func (t *Transaction) Serialize() TransactionModel { + return TransactionModel{ + ChainId: t.ChainId.String(), + Hash: t.Hash, + Nonce: t.Nonce, + BlockHash: t.BlockHash, + BlockNumber: t.BlockNumber.Uint64(), + BlockTimestamp: uint64(t.BlockTimestamp.Unix()), + TransactionIndex: t.TransactionIndex, + FromAddress: t.FromAddress, + ToAddress: t.ToAddress, + Value: t.Value.Uint64(), + Gas: t.Gas, + GasPrice: t.GasPrice.Uint64(), + Data: t.Data, + FunctionSelector: t.FunctionSelector, + MaxFeePerGas: t.MaxFeePerGas.Uint64(), + MaxPriorityFeePerGas: t.MaxPriorityFeePerGas.Uint64(), + TransactionType: t.TransactionType, + R: t.R.String(), + S: t.S.String(), + V: t.V.String(), + AccessListJson: t.AccessListJson, + ContractAddress: t.ContractAddress, + GasUsed: t.GasUsed, + CumulativeGasUsed: t.CumulativeGasUsed, + EffectiveGasPrice: t.EffectiveGasPrice, + BlobGasUsed: t.BlobGasUsed, + BlobGasPrice: t.BlobGasPrice, + LogsBloom: t.LogsBloom, + Status: t.Status, + } +} + +func (t *DecodedTransaction) Serialize() DecodedTransactionModel { + decodedData := DecodedTransactionDataModel{ + Name: t.Decoded.Name, + Signature: t.Decoded.Signature, + Inputs: t.Decoded.Inputs, + } + return DecodedTransactionModel{ + TransactionModel: t.Transaction.Serialize(), + Decoded: decodedData, + DecodedData: decodedData, + } +} diff --git a/internal/handlers/blocks_handlers.go b/internal/handlers/blocks_handlers.go index 4964db4..6c611db 100644 --- a/internal/handlers/blocks_handlers.go +++ b/internal/handlers/blocks_handlers.go @@ -8,32 +8,6 @@ import ( "github.com/thirdweb-dev/indexer/internal/storage" ) -// BlockModel represents a simplified Block structure for Swagger documentation -type BlockModel struct { - ChainId string `json:"chain_id"` - Number uint64 `json:"number"` - Hash string `json:"hash"` - ParentHash string `json:"parent_hash"` - Timestamp uint64 `json:"timestamp"` - Nonce string `json:"nonce"` - Sha3Uncles string `json:"sha3_uncles"` - MixHash string `json:"mix_hash"` - Miner string `json:"miner"` - StateRoot string `json:"state_root"` - TransactionsRoot string `json:"transactions_root"` - ReceiptsRoot string `json:"receipts_root"` - LogsBloom string `json:"logs_bloom"` - Size uint64 `json:"size"` - ExtraData string `json:"extra_data"` - Difficulty string `json:"difficulty"` - TotalDifficulty string `json:"total_difficulty"` - TransactionCount uint64 `json:"transaction_count"` - GasLimit uint64 `json:"gas_limit"` - GasUsed uint64 `json:"gas_used"` - WithdrawalsRoot string `json:"withdrawals_root"` - BaseFeePerGas uint64 `json:"base_fee_per_gas"` -} - // @Summary Get all blocks // @Description Retrieve all blocks // @Tags blocks @@ -49,7 +23,7 @@ type BlockModel struct { // @Param limit query int false "Number of items per page" default(5) // @Param aggregate query []string false "List of aggregate functions to apply" // @Param force_consistent_data query bool false "Force consistent data at the expense of query speed" -// @Success 200 {object} api.QueryResponse{data=[]BlockModel} +// @Success 200 {object} api.QueryResponse{data=[]common.BlockModel} // @Failure 400 {object} api.Error // @Failure 401 {object} api.Error // @Failure 500 {object} api.Error @@ -133,33 +107,10 @@ func handleBlocksRequest(c *gin.Context) { sendJSONResponse(c, queryResult) } -func serializeBlocks(blocks []common.Block) []BlockModel { - blockModels := make([]BlockModel, len(blocks)) +func serializeBlocks(blocks []common.Block) []common.BlockModel { + blockModels := make([]common.BlockModel, len(blocks)) for i, block := range blocks { - blockModels[i] = BlockModel{ - ChainId: block.ChainId.String(), - Number: block.Number.Uint64(), - Hash: block.Hash, - ParentHash: block.ParentHash, - Timestamp: uint64(block.Timestamp.Unix()), - Nonce: block.Nonce, - Sha3Uncles: block.Sha3Uncles, - MixHash: block.MixHash, - Miner: block.Miner, - StateRoot: block.StateRoot, - TransactionsRoot: block.TransactionsRoot, - ReceiptsRoot: block.ReceiptsRoot, - LogsBloom: block.LogsBloom, - Size: block.Size, - ExtraData: block.ExtraData, - Difficulty: block.Difficulty.String(), - TotalDifficulty: block.TotalDifficulty.String(), - TransactionCount: block.TransactionCount, - GasLimit: block.GasLimit.Uint64(), - GasUsed: block.GasUsed.Uint64(), - WithdrawalsRoot: block.WithdrawalsRoot, - BaseFeePerGas: block.BaseFeePerGas, - } + blockModels[i] = block.Serialize() } return blockModels } diff --git a/internal/handlers/logs_handlers.go b/internal/handlers/logs_handlers.go index 247b3a0..e9d0c57 100644 --- a/internal/handlers/logs_handlers.go +++ b/internal/handlers/logs_handlers.go @@ -21,33 +21,6 @@ var ( storageErr error ) -// LogModel represents a simplified Log structure for Swagger documentation -type LogModel struct { - ChainId string `json:"chain_id"` - BlockNumber uint64 `json:"block_number"` - BlockHash string `json:"block_hash"` - BlockTimestamp uint64 `json:"block_timestamp"` - TransactionHash string `json:"transaction_hash"` - TransactionIndex uint64 `json:"transaction_index"` - LogIndex uint64 `json:"log_index"` - Address string `json:"address"` - Data string `json:"data"` - Topics []string `json:"topics" swaggertype:"array,string"` -} - -type DecodedLogDataModel struct { - Name string `json:"name"` - Signature string `json:"signature"` - IndexedParams map[string]interface{} `json:"indexedParams" swaggertype:"object"` - NonIndexedParams map[string]interface{} `json:"nonIndexedParams" swaggertype:"object"` -} - -type DecodedLogModel struct { - LogModel - Decoded DecodedLogDataModel `json:"decoded"` - DecodedData DecodedLogDataModel `json:"decodedData" deprecated:"true"` // Deprecated: Use Decoded field instead -} - // @Summary Get all logs // @Description Retrieve all logs across all contracts // @Tags events @@ -63,7 +36,7 @@ type DecodedLogModel struct { // @Param limit query int false "Number of items per page" default(5) // @Param aggregate query []string false "List of aggregate functions to apply" // @Param force_consistent_data query bool false "Force consistent data at the expense of query speed" -// @Success 200 {object} api.QueryResponse{data=[]LogModel} +// @Success 200 {object} api.QueryResponse{data=[]common.LogModel} // @Failure 400 {object} api.Error // @Failure 401 {object} api.Error // @Failure 500 {object} api.Error @@ -88,7 +61,7 @@ func GetLogs(c *gin.Context) { // @Param limit query int false "Number of items per page" default(5) // @Param aggregate query []string false "List of aggregate functions to apply" // @Param force_consistent_data query bool false "Force consistent data at the expense of query speed" -// @Success 200 {object} api.QueryResponse{data=[]LogModel} +// @Success 200 {object} api.QueryResponse{data=[]common.LogModel} // @Failure 400 {object} api.Error // @Failure 401 {object} api.Error // @Failure 500 {object} api.Error @@ -115,7 +88,7 @@ func GetLogsByContract(c *gin.Context) { // @Param limit query int false "Number of items per page" default(5) // @Param aggregate query []string false "List of aggregate functions to apply" // @Param force_consistent_data query bool false "Force consistent data at the expense of query speed" -// @Success 200 {object} api.QueryResponse{data=[]DecodedLogModel} +// @Success 200 {object} api.QueryResponse{data=[]common.DecodedLogModel} // @Failure 400 {object} api.Error // @Failure 401 {object} api.Error // @Failure 500 {object} api.Error @@ -208,10 +181,10 @@ func handleLogsRequest(c *gin.Context, contractAddress, signature string, eventA return } if eventABI != nil { - decodedLogs := []DecodedLogModel{} + decodedLogs := []common.DecodedLogModel{} for _, log := range logsResult.Data { decodedLog := log.Decode(eventABI) - decodedLogs = append(decodedLogs, serializeDecodedLog(*decodedLog)) + decodedLogs = append(decodedLogs, decodedLog.Serialize()) } queryResult.Data = decodedLogs } else { @@ -244,58 +217,18 @@ func sendJSONResponse(c *gin.Context, response interface{}) { c.JSON(http.StatusOK, response) } -func serializeDecodedLogs(logs []*common.DecodedLog) []DecodedLogModel { - decodedLogModels := make([]DecodedLogModel, len(logs)) +func serializeDecodedLogs(logs []*common.DecodedLog) []common.DecodedLogModel { + decodedLogModels := make([]common.DecodedLogModel, len(logs)) for i, log := range logs { - decodedLogModels[i] = serializeDecodedLog(*log) + decodedLogModels[i] = log.Serialize() } return decodedLogModels } -func serializeDecodedLog(log common.DecodedLog) DecodedLogModel { - decodedData := DecodedLogDataModel{ - Name: log.Decoded.Name, - Signature: log.Decoded.Signature, - IndexedParams: log.Decoded.IndexedParams, - NonIndexedParams: log.Decoded.NonIndexedParams, - } - return DecodedLogModel{ - LogModel: serializeLog(log.Log), - Decoded: decodedData, - DecodedData: decodedData, - } -} - -func serializeLogs(logs []common.Log) []LogModel { - logModels := make([]LogModel, len(logs)) +func serializeLogs(logs []common.Log) []common.LogModel { + logModels := make([]common.LogModel, len(logs)) for i, log := range logs { - logModels[i] = serializeLog(log) + logModels[i] = log.Serialize() } return logModels } - -func serializeLog(log common.Log) LogModel { - return LogModel{ - ChainId: log.ChainId.String(), - BlockNumber: log.BlockNumber.Uint64(), - BlockHash: log.BlockHash, - BlockTimestamp: uint64(log.BlockTimestamp.Unix()), - TransactionHash: log.TransactionHash, - TransactionIndex: log.TransactionIndex, - LogIndex: log.LogIndex, - Address: log.Address, - Data: log.Data, - Topics: serializeTopics(log), - } -} - -func serializeTopics(log common.Log) []string { - topics := []string{log.Topic0, log.Topic1, log.Topic2, log.Topic3} - resultTopics := make([]string, 0, len(topics)) - for _, topic := range topics { - if topic != "" { - resultTopics = append(resultTopics, topic) - } - } - return resultTopics -} diff --git a/internal/handlers/transactions_handlers.go b/internal/handlers/transactions_handlers.go index 11dff8d..2d21320 100644 --- a/internal/handlers/transactions_handlers.go +++ b/internal/handlers/transactions_handlers.go @@ -1,7 +1,6 @@ package handlers import ( - "math/big" "net/http" "github.com/ethereum/go-ethereum/accounts/abi" @@ -15,51 +14,6 @@ import ( "github.com/thirdweb-dev/indexer/internal/storage" ) -// TransactionModel represents a simplified Transaction structure for Swagger documentation -type TransactionModel struct { - ChainId string `json:"chain_id"` - Hash string `json:"hash"` - Nonce uint64 `json:"nonce"` - BlockHash string `json:"block_hash"` - BlockNumber uint64 `json:"block_number"` - BlockTimestamp uint64 `json:"block_timestamp"` - TransactionIndex uint64 `json:"transaction_index"` - FromAddress string `json:"from_address"` - ToAddress string `json:"to_address"` - Value uint64 `json:"value"` - Gas uint64 `json:"gas"` - GasPrice uint64 `json:"gas_price"` - Data string `json:"data"` - FunctionSelector string `json:"function_selector"` - MaxFeePerGas uint64 `json:"max_fee_per_gas"` - MaxPriorityFeePerGas uint64 `json:"max_priority_fee_per_gas"` - TransactionType uint8 `json:"transaction_type"` - R string `json:"r"` - S string `json:"s"` - V string `json:"v"` - AccessListJson *string `json:"access_list_json"` - ContractAddress *string `json:"contract_address"` - GasUsed *uint64 `json:"gas_used"` - CumulativeGasUsed *uint64 `json:"cumulative_gas_used"` - EffectiveGasPrice *big.Int `json:"effective_gas_price"` - BlobGasUsed *uint64 `json:"blob_gas_used"` - BlobGasPrice *big.Int `json:"blob_gas_price"` - LogsBloom *string `json:"logs_bloom"` - Status *uint64 `json:"status"` -} - -type DecodedTransactionDataModel struct { - Name string `json:"name"` - Signature string `json:"signature"` - Inputs map[string]interface{} `json:"inputs"` -} - -type DecodedTransactionModel struct { - TransactionModel - Decoded DecodedTransactionDataModel `json:"decoded"` - DecodedData DecodedTransactionDataModel `json:"decodedData" deprecated:"true"` // Deprecated: Use Decoded field instead -} - // @Summary Get all transactions // @Description Retrieve all transactions across all contracts // @Tags transactions @@ -75,7 +29,7 @@ type DecodedTransactionModel struct { // @Param limit query int false "Number of items per page" default(5) // @Param aggregate query []string false "List of aggregate functions to apply" // @Param force_consistent_data query bool false "Force consistent data at the expense of query speed" -// @Success 200 {object} api.QueryResponse{data=[]TransactionModel} +// @Success 200 {object} api.QueryResponse{data=[]common.TransactionModel} // @Failure 400 {object} api.Error // @Failure 401 {object} api.Error // @Failure 500 {object} api.Error @@ -127,7 +81,7 @@ func GetTransactionsByContract(c *gin.Context) { // @Param limit query int false "Number of items per page" default(5) // @Param aggregate query []string false "List of aggregate functions to apply" // @Param force_consistent_data query bool false "Force consistent data at the expense of query speed" -// @Success 200 {object} api.QueryResponse{data=[]DecodedTransactionModel} +// @Success 200 {object} api.QueryResponse{data=[]common.DecodedTransactionModel} // @Failure 400 {object} api.Error // @Failure 401 {object} api.Error // @Failure 500 {object} api.Error @@ -220,10 +174,10 @@ func handleTransactionsRequest(c *gin.Context, contractAddress, signature string return } if functionABI != nil { - decodedTransactions := []DecodedTransactionModel{} + decodedTransactions := []common.DecodedTransactionModel{} for _, transaction := range transactionsResult.Data { decodedTransaction := transaction.Decode(functionABI) - decodedTransactions = append(decodedTransactions, serializeDecodedTransaction(*decodedTransaction)) + decodedTransactions = append(decodedTransactions, decodedTransaction.Serialize()) } queryResult.Data = decodedTransactions } else { @@ -240,65 +194,18 @@ func handleTransactionsRequest(c *gin.Context, contractAddress, signature string c.JSON(http.StatusOK, queryResult) } -func serializeDecodedTransactions(transactions []*common.DecodedTransaction) []DecodedTransactionModel { - decodedTransactionModels := make([]DecodedTransactionModel, len(transactions)) +func serializeDecodedTransactions(transactions []*common.DecodedTransaction) []common.DecodedTransactionModel { + decodedTransactionModels := make([]common.DecodedTransactionModel, len(transactions)) for i, transaction := range transactions { - decodedTransactionModels[i] = serializeDecodedTransaction(*transaction) + decodedTransactionModels[i] = transaction.Serialize() } return decodedTransactionModels } -func serializeDecodedTransaction(transaction common.DecodedTransaction) DecodedTransactionModel { - decodedData := DecodedTransactionDataModel{ - Name: transaction.Decoded.Name, - Signature: transaction.Decoded.Signature, - Inputs: transaction.Decoded.Inputs, - } - return DecodedTransactionModel{ - TransactionModel: serializeTransaction(transaction.Transaction), - Decoded: decodedData, - DecodedData: decodedData, - } -} - -func serializeTransactions(transactions []common.Transaction) []TransactionModel { - transactionModels := make([]TransactionModel, len(transactions)) +func serializeTransactions(transactions []common.Transaction) []common.TransactionModel { + transactionModels := make([]common.TransactionModel, len(transactions)) for i, transaction := range transactions { - transactionModels[i] = serializeTransaction(transaction) + transactionModels[i] = transaction.Serialize() } return transactionModels } - -func serializeTransaction(transaction common.Transaction) TransactionModel { - return TransactionModel{ - ChainId: transaction.ChainId.String(), - Hash: transaction.Hash, - Nonce: transaction.Nonce, - BlockHash: transaction.BlockHash, - BlockNumber: transaction.BlockNumber.Uint64(), - BlockTimestamp: uint64(transaction.BlockTimestamp.Unix()), - TransactionIndex: transaction.TransactionIndex, - FromAddress: transaction.FromAddress, - ToAddress: transaction.ToAddress, - Value: transaction.Value.Uint64(), - Gas: transaction.Gas, - GasPrice: transaction.GasPrice.Uint64(), - Data: transaction.Data, - FunctionSelector: transaction.FunctionSelector, - MaxFeePerGas: transaction.MaxFeePerGas.Uint64(), - MaxPriorityFeePerGas: transaction.MaxPriorityFeePerGas.Uint64(), - TransactionType: transaction.TransactionType, - R: transaction.R.String(), - S: transaction.S.String(), - V: transaction.V.String(), - AccessListJson: transaction.AccessListJson, - ContractAddress: transaction.ContractAddress, - GasUsed: transaction.GasUsed, - CumulativeGasUsed: transaction.CumulativeGasUsed, - EffectiveGasPrice: transaction.EffectiveGasPrice, - BlobGasUsed: transaction.BlobGasUsed, - BlobGasPrice: transaction.BlobGasPrice, - LogsBloom: transaction.LogsBloom, - Status: transaction.Status, - } -}