Skip to content

Commit

Permalink
Merge pull request #114 from G7DAO/expand-event-decoding
Browse files Browse the repository at this point in the history
Update event decoding logic.
  • Loading branch information
Andrei-Dolgolev authored Nov 21, 2024
2 parents 52e0735 + 90e8ee0 commit d7c2ef4
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 40 deletions.
50 changes: 13 additions & 37 deletions blockchain/common/decoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"log"
"math/big"
"os"
"strings"

Expand Down Expand Up @@ -200,13 +199,14 @@ func DecodeTransactionInputDataToInterface(contractABI *abi.ABI, data []byte) (m

func DecodeLogArgsToLabelData(contractABI *abi.ABI, topics []string, data string) (map[string]interface{}, error) {

topic0 := topics[0]
var topicHashes []common.Hash

// Convert the topic0 string to common.Hash
topic0Hash := common.HexToHash(topic0)
for _, topic := range topics {
topicHashes = append(topicHashes, common.HexToHash(topic))
}

event, err := contractABI.EventByID(
topic0Hash,
topicHashes[0],
)
if err != nil {
log.Fatal(err)
Expand All @@ -224,41 +224,17 @@ func DecodeLogArgsToLabelData(contractABI *abi.ABI, topics []string, data string
labelData["name"] = event.Name
labelData["args"] = make(map[string]interface{})

i := 1
// Extract indexed parameters from topics
indexed := make([]abi.Argument, 0)
for _, input := range event.Inputs {
var arg interface{}
if input.Indexed {
// Note: topic[0] is the event signature, so indexed params start from topic[1]
switch input.Type.T {
case abi.AddressTy:
arg = common.HexToAddress(topics[i]).Hex()
case abi.BytesTy:
arg = common.HexToHash(topics[i]).Hex()
case abi.FixedBytesTy:
if input.Type.Size == 32 {
arg = common.HexToHash(topics[i]).Hex()
} else {
arg = common.BytesToHash(common.Hex2Bytes(topics[i][2:])).Hex() // for other fixed sizes
}
case abi.UintTy:
arg = new(big.Int).SetBytes(common.Hex2Bytes(topics[i][2:]))
case abi.BoolTy:
arg = new(big.Int).SetBytes(common.Hex2Bytes(topics[i][2:])).Cmp(big.NewInt(0)) != 0
case abi.StringTy:
argBytes, err := hex.DecodeString(strings.TrimPrefix(topics[i], "0x"))
if err != nil {
return nil, fmt.Errorf("failed to decode hex string to normal string: %v", err)
}
arg = string(argBytes)
default:
log.Fatalf("Unsupported indexed type: %s", input.Type.String())
}
i++
} else {
arg = "NON-INDEXED" // Placeholder for non-indexed arguments, which will be unpacked later
indexed = append(indexed, input)
}
labelData["args"].(map[string]interface{})[input.Name] = arg
}

// parse topics into map
err = abi.ParseTopicsIntoMap(labelData["args"].(map[string]interface{}), indexed, topicHashes[1:])
if err != nil {
return nil, err
}

// Unpack the data bytes into the args map
Expand Down
4 changes: 2 additions & 2 deletions indexer/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ func (p *PostgreSQLpgx) ReadUpdates(blockchain string, fromBlock uint64, custome
)
SELECT
latest_block_number,
(SELECT array_agg(path) FROM path) as paths,
(SELECT array_agg(DISTINCT path) FROM path) as paths,
(SELECT json_agg(json_build_object(customer_id, abis)) FROM reformatted_jobs) as jobs
FROM
latest_block_of_path
Expand Down Expand Up @@ -1469,7 +1469,7 @@ func (p *PostgreSQLpgx) RetrievePathsAndBlockBounds(blockchain string, blockNumb
order by block_number asc
limit 1
)
select ARRAY_AGG(path) as paths, (SELECT first_block_number FROM earliest_block_of_path) as min_block_number, (SELECT latest_block_number FROM latest_block_of_path) as max_block_number from path
select ARRAY_AGG( DISTINCT path) as paths, (SELECT first_block_number FROM earliest_block_of_path) as min_block_number, (SELECT latest_block_number FROM latest_block_of_path) as max_block_number from path
`, BlocksTableName(blockchain), BlocksTableName(blockchain), BlocksTableName(blockchain))

err = conn.QueryRow(context.Background(), query, blockNumber, blockNumber-uint64(minBlocksToSync)).Scan(&paths, &minBlockNumber, &maxBlockNumber)
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package version

var SeerVersion string = "0.3.12"
var SeerVersion string = "0.3.13"

0 comments on commit d7c2ef4

Please sign in to comment.