|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/gin-gonic/gin" |
| 5 | + "github.com/rs/zerolog/log" |
| 6 | + "github.com/thirdweb-dev/indexer/api" |
| 7 | + "github.com/thirdweb-dev/indexer/internal/storage" |
| 8 | +) |
| 9 | + |
| 10 | +// BlockModel represents a simplified Block structure for Swagger documentation |
| 11 | +type BlockModel struct { |
| 12 | + ChainId string `json:"chain_id"` |
| 13 | + Number string `json:"number"` |
| 14 | + Hash string `json:"hash"` |
| 15 | + ParentHash string `json:"parent_hash"` |
| 16 | + Timestamp uint64 `json:"timestamp"` |
| 17 | + Nonce string `json:"nonce"` |
| 18 | + Sha3Uncles string `json:"sha3_uncles"` |
| 19 | + LogsBloom string `json:"logs_bloom"` |
| 20 | + ReceiptsRoot string `json:"receipts_root"` |
| 21 | + Difficulty string `json:"difficulty"` |
| 22 | + TotalDifficulty string `json:"total_difficulty"` |
| 23 | + Size uint64 `json:"size"` |
| 24 | + ExtraData string `json:"extra_data"` |
| 25 | + GasLimit uint64 `json:"gas_limit"` |
| 26 | + GasUsed uint64 `json:"gas_used"` |
| 27 | + BaseFeePerGas string `json:"base_fee_per_gas"` |
| 28 | + WithdrawalsRoot string `json:"withdrawals_root"` |
| 29 | +} |
| 30 | + |
| 31 | +// @Summary Get all blocks |
| 32 | +// @Description Retrieve all blocks |
| 33 | +// @Tags blocks |
| 34 | +// @Accept json |
| 35 | +// @Produce json |
| 36 | +// @Security BasicAuth |
| 37 | +// @Param chainId path string true "Chain ID" |
| 38 | +// @Param filter query string false "Filter parameters" |
| 39 | +// @Param group_by query string false "Field to group results by" |
| 40 | +// @Param sort_by query string false "Field to sort results by" |
| 41 | +// @Param sort_order query string false "Sort order (asc or desc)" |
| 42 | +// @Param page query int false "Page number for pagination" |
| 43 | +// @Param limit query int false "Number of items per page" default(5) |
| 44 | +// @Param aggregate query []string false "List of aggregate functions to apply" |
| 45 | +// @Success 200 {object} api.QueryResponse{data=[]BlockModel} |
| 46 | +// @Failure 400 {object} api.Error |
| 47 | +// @Failure 401 {object} api.Error |
| 48 | +// @Failure 500 {object} api.Error |
| 49 | +// @Router /{chainId}/blocks [get] |
| 50 | +func GetBlocks(c *gin.Context) { |
| 51 | + handleBlocksRequest(c) |
| 52 | +} |
| 53 | + |
| 54 | +func handleBlocksRequest(c *gin.Context) { |
| 55 | + chainId, err := api.GetChainId(c) |
| 56 | + if err != nil { |
| 57 | + api.BadRequestErrorHandler(c, err) |
| 58 | + return |
| 59 | + } |
| 60 | + |
| 61 | + queryParams, err := api.ParseQueryParams(c.Request) |
| 62 | + if err != nil { |
| 63 | + api.BadRequestErrorHandler(c, err) |
| 64 | + return |
| 65 | + } |
| 66 | + |
| 67 | + mainStorage, err := getMainStorage() |
| 68 | + if err != nil { |
| 69 | + log.Error().Err(err).Msg("Error getting main storage") |
| 70 | + api.InternalErrorHandler(c) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + // Prepare the QueryFilter |
| 75 | + qf := storage.QueryFilter{ |
| 76 | + FilterParams: queryParams.FilterParams, |
| 77 | + ChainId: chainId, |
| 78 | + SortBy: queryParams.SortBy, |
| 79 | + SortOrder: queryParams.SortOrder, |
| 80 | + Page: queryParams.Page, |
| 81 | + Limit: queryParams.Limit, |
| 82 | + } |
| 83 | + |
| 84 | + // Initialize the QueryResult |
| 85 | + queryResult := api.QueryResponse{ |
| 86 | + Meta: api.Meta{ |
| 87 | + ChainId: chainId.Uint64(), |
| 88 | + Page: queryParams.Page, |
| 89 | + Limit: queryParams.Limit, |
| 90 | + TotalItems: 0, |
| 91 | + TotalPages: 0, // TODO: Implement total pages count |
| 92 | + }, |
| 93 | + Data: nil, |
| 94 | + Aggregations: nil, |
| 95 | + } |
| 96 | + |
| 97 | + // If aggregates or groupings are specified, retrieve them |
| 98 | + if len(queryParams.Aggregates) > 0 || len(queryParams.GroupBy) > 0 { |
| 99 | + qf.Aggregates = queryParams.Aggregates |
| 100 | + qf.GroupBy = queryParams.GroupBy |
| 101 | + |
| 102 | + aggregatesResult, err := mainStorage.GetAggregations("blocks", qf) |
| 103 | + if err != nil { |
| 104 | + log.Error().Err(err).Msg("Error querying aggregates") |
| 105 | + // TODO: might want to choose BadRequestError if it's due to not-allowed functions |
| 106 | + api.InternalErrorHandler(c) |
| 107 | + return |
| 108 | + } |
| 109 | + queryResult.Aggregations = aggregatesResult.Aggregates |
| 110 | + queryResult.Meta.TotalItems = len(aggregatesResult.Aggregates) |
| 111 | + } else { |
| 112 | + // Retrieve blocks data |
| 113 | + blocksResult, err := mainStorage.GetBlocks(qf) |
| 114 | + if err != nil { |
| 115 | + log.Error().Err(err).Msg("Error querying blocks") |
| 116 | + // TODO: might want to choose BadRequestError if it's due to not-allowed functions |
| 117 | + api.InternalErrorHandler(c) |
| 118 | + return |
| 119 | + } |
| 120 | + |
| 121 | + queryResult.Data = blocksResult.Data |
| 122 | + queryResult.Meta.TotalItems = len(blocksResult.Data) |
| 123 | + } |
| 124 | + |
| 125 | + sendJSONResponse(c, queryResult) |
| 126 | +} |
0 commit comments