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

Deploy contracts look up cli. #69

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
77 changes: 77 additions & 0 deletions blockchain/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package blockchain
import (
"bytes"
"context"
"crypto/md5"
"encoding/hex"
"errors"
"fmt"
"log"
"math/big"
"strconv"
"sync"
"time"

Expand All @@ -29,6 +32,7 @@ import (
"github.com/G7DAO/seer/indexer"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"google.golang.org/protobuf/proto"
)

Expand Down Expand Up @@ -98,6 +102,7 @@ type BlockchainClient interface {
DecodeProtoEntireBlockToJson(*bytes.Buffer) (*seer_common.BlocksBatchJson, error)
DecodeProtoEntireBlockToLabels(*bytes.Buffer, map[string]map[string]*indexer.AbiEntry, int) ([]indexer.EventLabel, []indexer.TransactionLabel, error)
DecodeProtoTransactionsToLabels([]string, map[uint64]uint64, map[string]map[string]*indexer.AbiEntry) ([]indexer.TransactionLabel, error)
TransactionReceipt(context.Context, common.Hash) (*types.Receipt, error)
ChainType() string
GetCode(context.Context, common.Address, uint64) ([]byte, error)
GetTransactionsLabels(uint64, uint64, map[string]map[string]*indexer.AbiEntry, int) ([]indexer.TransactionLabel, map[uint64]seer_common.BlockWithTransactions, error)
Expand Down Expand Up @@ -151,6 +156,78 @@ func DecodeTransactionInputData(contractABI *abi.ABI, data []byte) {
fmt.Printf("Method inputs: %v\n", inputsMap)
}

func GetDeployedContracts(client BlockchainClient, BlocksBatch *seer_common.BlocksBatchJson) (map[string]*indexer.EvmContract, error) {
deployedContracts := make(map[string]*indexer.EvmContract)
for _, block := range BlocksBatch.Blocks {
for _, transaction := range block.Transactions {
if (transaction.ToAddress == "" || transaction.ToAddress == "0x") && transaction.Input != "0x" {

// make get receipt call

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

transactionReceipt, err := client.TransactionReceipt(ctx, common.HexToHash(transaction.Hash))
if err != nil {
log.Printf("Failed to get transaction receipt: %v", err)
continue
}

if transactionReceipt.ContractAddress == (common.Address{}) {
continue
}

// Extract contract address as string
contractAddress := transactionReceipt.ContractAddress.String() // Assuming ContractAddress is of type common.Address

timestamp, err := strconv.ParseUint(block.Timestamp, 10, 64)
if err != nil {
log.Printf("Failed to parse block timestamp: %v", err)
// Handle the error appropriately (e.g., continue to the next block or return)
return nil, err
}

transactionIndex, err := strconv.ParseUint(transaction.TransactionIndex, 10, 64)
if err != nil {
log.Printf("Failed to parse transaction index: %v", err)
// Handle the error appropriately (e.g., continue to the next transaction or return)
return nil, err
}

// Compute the MD5 hash and convert it to a hexadecimal string
hash := md5.Sum([]byte(transaction.Input))
deployedBytecodeHash := hex.EncodeToString(hash[:])

// Assign EvmContract to the map
deployedContracts[contractAddress] = &indexer.EvmContract{
Address: contractAddress,
Bytecode: nil,
BytecodeHash: "",
DeployedBy: transaction.FromAddress,
DeployedBytecode: transaction.Input,
DeployedBytecodeHash: deployedBytecodeHash,
Abi: nil,
DeployedAtBlockNumber: transactionReceipt.BlockNumber.Uint64(),
DeployedAtBlockHash: block.Hash,
DeployedAtBlockTimestamp: timestamp,
TransactionHash: transaction.Hash,
TransactionIndex: transactionIndex,
Name: nil,
Statistics: nil,
SupportedStandards: nil,
}

// Try to get bytecode
}
}
}

log.Printf("Found %d deployed contracts", len(deployedContracts))

return deployedContracts, nil

}

func DeployBlocksLookUpAndUpdate(blockchain string) error {

// get all abi jobs without deployed block
Expand Down
56 changes: 56 additions & 0 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,62 @@ func CreateDatabaseOperationCommand() *cobra.Command {
cleanCommand.Flags().Uint64Var(&batchLimit, "batch-limit", 1000, "The number of rows to delete in each batch (default: 1000)")
cleanCommand.Flags().IntVar(&sleepTime, "sleep-time", 1, "The time to sleep between batches in seconds (default: 1)")

var startBlock, endBlock uint64
var baseDir string
var timeout int

findContractsCommand := &cobra.Command{
Use: "find-contracts",
Short: "Find contracts in the blockstorage",
PreRunE: func(cmd *cobra.Command, args []string) error {
indexerErr := indexer.CheckVariablesForIndexer()
if indexerErr != nil {
return indexerErr
}

storageErr := storage.CheckVariablesForStorage()
if storageErr != nil {
return storageErr
}

crawlerErr := crawler.CheckVariablesForCrawler()
if crawlerErr != nil {
return crawlerErr
}

syncErr := synchronizer.CheckVariablesForSynchronizer()
if syncErr != nil {
return syncErr
}

if chain == "" {
return fmt.Errorf("blockchain is required via --chain")
}

return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
indexer.InitDBConnection()

synchronizerInstance, synchonizerErr := synchronizer.NewSynchronizer(chain, baseDir, startBlock, endBlock, 0, timeout, 1)

if synchonizerErr != nil {
return synchonizerErr
}

synchronizerInstance.SyncContracts()

return nil
},
}

findContractsCommand.Flags().StringVar(&chain, "chain", "ethereum", "The blockchain to crawl (default: ethereum)")
findContractsCommand.Flags().Uint64Var(&startBlock, "start-block", 0, "The block number to start decoding from (default: latest block)")
findContractsCommand.Flags().Uint64Var(&endBlock, "end-block", 0, "The block number to end decoding at (default: latest block)")
findContractsCommand.Flags().StringVar(&baseDir, "base-dir", "", "The base directory to store the crawled data (default: '')")
findContractsCommand.Flags().IntVar(&timeout, "timeout", 30, "The timeout for the crawler in seconds (default: 30)")
indexCommand.AddCommand(findContractsCommand)

indexCommand.AddCommand(cleanCommand)

deploymentBlocksCommand := &cobra.Command{
Expand Down
Loading
Loading