Skip to content

Commit

Permalink
decode events (#125)
Browse files Browse the repository at this point in the history
### TL;DR
Added event decoding functionality for Ethereum logs, enabling the parsing and interpretation of indexed and non-indexed event parameters.

### What changed?
- Added `ConstructEventABI` function to parse event signatures into ABI format
- Implemented log decoding functionality to handle both indexed and non-indexed event parameters
- Created new `DecodedLog` and `DecodedLogData` structures to represent decoded event data
- Enhanced log handlers to support automatic event decoding when a signature is provided
- Added support for converting various data types (bytes, numerics) to hexadecimal format

### How to test?
1. Make a GET request to `/{chainId}/events/{contract}/{signature}` with a valid event signature
2. Verify that the response includes decoded event data with:
   - Event name and signature
   - Indexed parameters properly decoded
   - Non-indexed parameters converted to appropriate formats
   - All byte and numeric values properly converted to hex

### Why make this change?
To improve the readability and usability of event log data by automatically decoding raw event parameters into their proper types and formats. This makes it easier for developers to work with and understand event data without having to manually decode the raw logs.
  • Loading branch information
iuwqyir authored Dec 6, 2024
2 parents cdef621 + 0872cf9 commit 6b6d94f
Show file tree
Hide file tree
Showing 10 changed files with 876 additions and 99 deletions.
3 changes: 3 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ func ParseQueryParams(r *http.Request) (QueryParams, error) {
log.Error().Err(err).Msg("Error parsing query params")
return QueryParams{}, err
}
if params.Limit == 0 {
params.Limit = 5
}
return params, nil
}

Expand Down
212 changes: 203 additions & 9 deletions docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const docTemplate = `{
},
{
"type": "integer",
"default": 5,
"description": "Number of items per page",
"name": "limit",
"in": "query"
Expand Down Expand Up @@ -200,6 +201,7 @@ const docTemplate = `{
},
{
"type": "integer",
"default": 5,
"description": "Number of items per page",
"name": "limit",
"in": "query"
Expand Down Expand Up @@ -265,7 +267,7 @@ const docTemplate = `{
"BasicAuth": []
}
],
"description": "Retrieve logs for a specific contract and event signature",
"description": "Retrieve logs for a specific contract and event signature. When a valid event signature is provided, the response includes decoded log data with both indexed and non-indexed parameters.",
"consumes": [
"application/json"
],
Expand Down Expand Up @@ -293,7 +295,7 @@ const docTemplate = `{
},
{
"type": "string",
"description": "Event signature",
"description": "Event signature (e.g., 'Transfer(address,address,uint256)')",
"name": "signature",
"in": "path",
"required": true
Expand Down Expand Up @@ -330,6 +332,7 @@ const docTemplate = `{
},
{
"type": "integer",
"default": 5,
"description": "Number of items per page",
"name": "limit",
"in": "query"
Expand Down Expand Up @@ -359,7 +362,7 @@ const docTemplate = `{
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.LogModel"
"$ref": "#/definitions/handlers.DecodedLogModel"
}
}
}
Expand Down Expand Up @@ -446,6 +449,7 @@ const docTemplate = `{
},
{
"type": "integer",
"default": 5,
"description": "Number of items per page",
"name": "limit",
"in": "query"
Expand Down Expand Up @@ -569,6 +573,7 @@ const docTemplate = `{
},
{
"type": "integer",
"default": 5,
"description": "Number of items per page",
"name": "limit",
"in": "query"
Expand Down Expand Up @@ -634,7 +639,7 @@ const docTemplate = `{
"BasicAuth": []
}
],
"description": "Retrieve transactions for a specific contract and signature (Not implemented yet)",
"description": "Retrieve transactions for a specific contract and signature. When a valid function signature is provided, the response includes decoded transaction data with function inputs.",
"consumes": [
"application/json"
],
Expand Down Expand Up @@ -662,7 +667,7 @@ const docTemplate = `{
},
{
"type": "string",
"description": "Function signature",
"description": "Function signature (e.g., 'transfer(address,uint256)')",
"name": "signature",
"in": "path",
"required": true
Expand Down Expand Up @@ -699,6 +704,7 @@ const docTemplate = `{
},
{
"type": "integer",
"default": 5,
"description": "Number of items per page",
"name": "limit",
"in": "query"
Expand Down Expand Up @@ -728,7 +734,7 @@ const docTemplate = `{
"data": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.TransactionModel"
"$ref": "#/definitions/handlers.DecodedTransactionModel"
}
}
}
Expand Down Expand Up @@ -817,9 +823,10 @@ const docTemplate = `{
"properties": {
"aggregations": {
"description": "@Description Aggregation results",
"type": "object",
"additionalProperties": {
"type": "string"
"type": "array",
"items": {
"type": "object",
"additionalProperties": true
}
},
"data": {
Expand All @@ -835,6 +842,169 @@ const docTemplate = `{
}
}
},
"handlers.DecodedLogDataModel": {
"type": "object",
"properties": {
"inputs": {
"type": "object",
"additionalProperties": true
},
"name": {
"type": "string"
},
"signature": {
"type": "string"
}
}
},
"handlers.DecodedLogModel": {
"type": "object",
"properties": {
"address": {
"type": "string"
},
"block_hash": {
"type": "string"
},
"block_number": {
"type": "string"
},
"block_timestamp": {
"type": "integer"
},
"chain_id": {
"type": "string"
},
"data": {
"type": "string"
},
"decoded": {
"$ref": "#/definitions/handlers.DecodedLogDataModel"
},
"log_index": {
"type": "integer"
},
"topics": {
"type": "array",
"items": {
"type": "string"
}
},
"transaction_hash": {
"type": "string"
},
"transaction_index": {
"type": "integer"
}
}
},
"handlers.DecodedTransactionDataModel": {
"type": "object",
"properties": {
"inputs": {
"type": "object",
"additionalProperties": true
},
"name": {
"type": "string"
},
"signature": {
"type": "string"
}
}
},
"handlers.DecodedTransactionModel": {
"type": "object",
"properties": {
"access_list_json": {
"type": "string"
},
"blob_gas_price": {
"type": "string"
},
"blob_gas_used": {
"type": "integer"
},
"block_hash": {
"type": "string"
},
"block_number": {
"type": "string"
},
"block_timestamp": {
"type": "integer"
},
"chain_id": {
"type": "string"
},
"contract_address": {
"type": "string"
},
"cumulative_gas_used": {
"type": "integer"
},
"data": {
"type": "string"
},
"decoded": {
"$ref": "#/definitions/handlers.DecodedTransactionDataModel"
},
"effective_gas_price": {
"type": "string"
},
"from_address": {
"type": "string"
},
"gas": {
"type": "integer"
},
"gas_price": {
"type": "string"
},
"gas_used": {
"type": "integer"
},
"hash": {
"type": "string"
},
"logs_bloom": {
"type": "string"
},
"max_fee_per_gas": {
"type": "string"
},
"max_priority_fee_per_gas": {
"type": "string"
},
"nonce": {
"type": "integer"
},
"r": {
"type": "string"
},
"s": {
"type": "string"
},
"status": {
"type": "integer"
},
"to_address": {
"type": "string"
},
"transaction_index": {
"type": "integer"
},
"transaction_type": {
"type": "integer"
},
"v": {
"type": "string"
},
"value": {
"type": "string"
}
}
},
"handlers.LogModel": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -879,6 +1049,12 @@ const docTemplate = `{
"access_list_json": {
"type": "string"
},
"blob_gas_price": {
"type": "string"
},
"blob_gas_used": {
"type": "integer"
},
"block_hash": {
"type": "string"
},
Expand All @@ -891,9 +1067,18 @@ const docTemplate = `{
"chain_id": {
"type": "string"
},
"contract_address": {
"type": "string"
},
"cumulative_gas_used": {
"type": "integer"
},
"data": {
"type": "string"
},
"effective_gas_price": {
"type": "string"
},
"from_address": {
"type": "string"
},
Expand All @@ -903,9 +1088,15 @@ const docTemplate = `{
"gas_price": {
"type": "string"
},
"gas_used": {
"type": "integer"
},
"hash": {
"type": "string"
},
"logs_bloom": {
"type": "string"
},
"max_fee_per_gas": {
"type": "string"
},
Expand All @@ -921,6 +1112,9 @@ const docTemplate = `{
"s": {
"type": "string"
},
"status": {
"type": "integer"
},
"to_address": {
"type": "string"
},
Expand Down
Loading

0 comments on commit 6b6d94f

Please sign in to comment.