Skip to content

Commit aa4eac3

Browse files
committed
feat: enable transactions look-up by signatures
1 parent f5ae429 commit aa4eac3

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

internal/handlers/transactions_handlers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handlers
33
import (
44
"net/http"
55

6+
"github.com/ethereum/go-ethereum/crypto"
67
"github.com/gin-gonic/gin"
78
"github.com/rs/zerolog/log"
89
"github.com/thirdweb-dev/indexer/api"
@@ -104,8 +105,8 @@ func GetTransactionsByContract(c *gin.Context) {
104105
// @Router /{chainId}/transactions/{to}/{signature} [get]
105106
func GetTransactionsByContractAndSignature(c *gin.Context) {
106107
to := c.Param("to")
107-
// TODO: Implement signature lookup before activating this
108-
handleTransactionsRequest(c, to, "")
108+
signature := c.Param("signature")
109+
handleTransactionsRequest(c, to, signature)
109110
}
110111

111112
func handleTransactionsRequest(c *gin.Context, contractAddress, signature string) {
@@ -122,10 +123,9 @@ func handleTransactionsRequest(c *gin.Context, contractAddress, signature string
122123
}
123124

124125
signatureHash := ""
125-
// TODO: implement signature lookup
126-
// if signature != "" {
127-
// signatureHash = crypto.Keccak256Hash([]byte(signature)).Hex()
128-
// }
126+
if signature != "" {
127+
signatureHash = crypto.Keccak256Hash([]byte(signature)).Hex()
128+
}
129129

130130
mainStorage, err := getMainStorage()
131131
if err != nil {

internal/rpc/serializer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func serializeTransaction(chainId *big.Int, tx map[string]interface{}, blockTime
185185
Gas: hexToUint64(tx["gas"]),
186186
GasPrice: hexToBigInt(tx["gasPrice"]),
187187
Data: interfaceToString(tx["input"]),
188-
FunctionSelector: extractFunctionSelector(interfaceToString(tx["input"])),
188+
FunctionSelector: ExtractFunctionSelector(interfaceToString(tx["input"])),
189189
MaxFeePerGas: hexToBigInt(tx["maxFeePerGas"]),
190190
MaxPriorityFeePerGas: hexToBigInt(tx["maxPriorityFeePerGas"]),
191191
TransactionType: uint8(hexToUint64(tx["type"])),
@@ -270,7 +270,7 @@ func serializeTransaction(chainId *big.Int, tx map[string]interface{}, blockTime
270270
/**
271271
* Extracts the function selector (first 4 bytes) from a transaction input.
272272
*/
273-
func extractFunctionSelector(s string) string {
273+
func ExtractFunctionSelector(s string) string {
274274
if len(s) < 10 {
275275
return ""
276276
}

internal/storage/clickhouse.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
zLog "github.com/rs/zerolog/log"
1919
config "github.com/thirdweb-dev/indexer/configs"
2020
"github.com/thirdweb-dev/indexer/internal/common"
21+
"github.com/thirdweb-dev/indexer/internal/rpc"
2122
)
2223

2324
type ClickHouseConnector struct {
@@ -339,7 +340,7 @@ func (c *ClickHouseConnector) GetBlocks(qf QueryFilter) (blocks []common.Block,
339340
}
340341

341342
func (c *ClickHouseConnector) GetTransactions(qf QueryFilter) (QueryResult[common.Transaction], error) {
342-
columns := "chain_id, hash, nonce, block_hash, block_number, block_timestamp, transaction_index, from_address, to_address, value, gas, gas_price, data, max_fee_per_gas, max_priority_fee_per_gas, transaction_type, r, s, v, access_list"
343+
columns := "chain_id, hash, nonce, block_hash, block_number, block_timestamp, transaction_index, from_address, to_address, value, gas, gas_price, data, function_selector, max_fee_per_gas, max_priority_fee_per_gas, transaction_type, r, s, v, access_list"
343344
return executeQuery[common.Transaction](c, "transactions", columns, qf, scanTransaction)
344345
}
345346

@@ -359,7 +360,7 @@ func (c *ClickHouseConnector) GetAggregations(table string, qf QueryFilter) (Que
359360
}
360361
query = addContractAddress(table, query, qf.ContractAddress)
361362
if qf.Signature != "" {
362-
query += fmt.Sprintf(" AND topic_0 = '%s'", qf.Signature)
363+
query = addSignatureClause(table, query, qf.Signature)
363364
}
364365
for key, value := range qf.FilterParams {
365366
query = addFilterParams(key, strings.ToLower(value), query)
@@ -452,7 +453,7 @@ func (c *ClickHouseConnector) buildQuery(table, columns string, qf QueryFilter)
452453

453454
// Add signature clause
454455
if qf.Signature != "" {
455-
query += fmt.Sprintf(" AND topic_0 = '%s'", qf.Signature)
456+
query = addSignatureClause(table, query, qf.Signature)
456457
}
457458
// Add filter params
458459
for key, value := range qf.FilterParams {
@@ -516,6 +517,15 @@ func addContractAddress(table, query string, contractAddress string) string {
516517
return query
517518
}
518519

520+
func addSignatureClause(table, query, signature string) string {
521+
if table == "logs" {
522+
query += fmt.Sprintf(" AND topic_0 = '%s'", signature)
523+
} else if table == "transactions" {
524+
query += fmt.Sprintf(" AND function_selector = '%s'", rpc.ExtractFunctionSelector(signature))
525+
}
526+
return query
527+
}
528+
519529
func getTopicValueFormat(topic string) string {
520530
if topic == "" {
521531
// if there is no indexed topic, indexer stores an empty string
@@ -545,6 +555,7 @@ func scanTransaction(rows driver.Rows) (common.Transaction, error) {
545555
&tx.Gas,
546556
&tx.GasPrice,
547557
&tx.Data,
558+
&tx.FunctionSelector,
548559
&tx.MaxFeePerGas,
549560
&tx.MaxPriorityFeePerGas,
550561
&tx.TransactionType,

0 commit comments

Comments
 (0)