Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable transactions look-up by signatures #117

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions internal/handlers/transactions_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ package handlers
import (
"net/http"

"github.com/ethereum/go-ethereum/crypto"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"github.com/thirdweb-dev/indexer/api"
"github.com/thirdweb-dev/indexer/internal/rpc"
"github.com/thirdweb-dev/indexer/internal/storage"
)

Expand Down Expand Up @@ -104,8 +106,8 @@ func GetTransactionsByContract(c *gin.Context) {
// @Router /{chainId}/transactions/{to}/{signature} [get]
func GetTransactionsByContractAndSignature(c *gin.Context) {
to := c.Param("to")
// TODO: Implement signature lookup before activating this
handleTransactionsRequest(c, to, "")
signature := c.Param("signature")
handleTransactionsRequest(c, to, signature)
}

func handleTransactionsRequest(c *gin.Context, contractAddress, signature string) {
Expand All @@ -122,10 +124,9 @@ func handleTransactionsRequest(c *gin.Context, contractAddress, signature string
}

signatureHash := ""
// TODO: implement signature lookup
// if signature != "" {
// signatureHash = crypto.Keccak256Hash([]byte(signature)).Hex()
// }
if signature != "" {
signatureHash = rpc.ExtractFunctionSelector(crypto.Keccak256Hash([]byte(signature)).Hex())
}

mainStorage, err := getMainStorage()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/rpc/serializer.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func serializeTransaction(chainId *big.Int, tx map[string]interface{}, blockTime
Gas: hexToUint64(tx["gas"]),
GasPrice: hexToBigInt(tx["gasPrice"]),
Data: interfaceToString(tx["input"]),
FunctionSelector: extractFunctionSelector(interfaceToString(tx["input"])),
FunctionSelector: ExtractFunctionSelector(interfaceToString(tx["input"])),
MaxFeePerGas: hexToBigInt(tx["maxFeePerGas"]),
MaxPriorityFeePerGas: hexToBigInt(tx["maxPriorityFeePerGas"]),
TransactionType: uint8(hexToUint64(tx["type"])),
Expand Down Expand Up @@ -270,7 +270,7 @@ func serializeTransaction(chainId *big.Int, tx map[string]interface{}, blockTime
/**
* Extracts the function selector (first 4 bytes) from a transaction input.
*/
func extractFunctionSelector(s string) string {
func ExtractFunctionSelector(s string) string {
if len(s) < 10 {
return ""
}
Expand Down
16 changes: 13 additions & 3 deletions internal/storage/clickhouse.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (c *ClickHouseConnector) GetBlocks(qf QueryFilter) (blocks []common.Block,
}

func (c *ClickHouseConnector) GetTransactions(qf QueryFilter) (QueryResult[common.Transaction], error) {
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"
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"
return executeQuery[common.Transaction](c, "transactions", columns, qf, scanTransaction)
}

Expand All @@ -359,7 +359,7 @@ func (c *ClickHouseConnector) GetAggregations(table string, qf QueryFilter) (Que
}
query = addContractAddress(table, query, qf.ContractAddress)
if qf.Signature != "" {
query += fmt.Sprintf(" AND topic_0 = '%s'", qf.Signature)
query = addSignatureClause(table, query, qf.Signature)
}
for key, value := range qf.FilterParams {
query = addFilterParams(key, strings.ToLower(value), query)
Expand Down Expand Up @@ -452,7 +452,7 @@ func (c *ClickHouseConnector) buildQuery(table, columns string, qf QueryFilter)

// Add signature clause
if qf.Signature != "" {
query += fmt.Sprintf(" AND topic_0 = '%s'", qf.Signature)
query = addSignatureClause(table, query, qf.Signature)
}
// Add filter params
for key, value := range qf.FilterParams {
Expand Down Expand Up @@ -516,6 +516,15 @@ func addContractAddress(table, query string, contractAddress string) string {
return query
}

func addSignatureClause(table, query, signature string) string {
if table == "logs" {
query += fmt.Sprintf(" AND topic_0 = '%s'", signature)
} else if table == "transactions" {
query += fmt.Sprintf(" AND function_selector = '%s'", signature)
}
return query
}

func getTopicValueFormat(topic string) string {
if topic == "" {
// if there is no indexed topic, indexer stores an empty string
Expand Down Expand Up @@ -545,6 +554,7 @@ func scanTransaction(rows driver.Rows) (common.Transaction, error) {
&tx.Gas,
&tx.GasPrice,
&tx.Data,
&tx.FunctionSelector,
&tx.MaxFeePerGas,
&tx.MaxPriorityFeePerGas,
&tx.TransactionType,
Expand Down
Loading