Created
February 27, 2023 04:42
-
-
Save libevm/308379ee47e5259282688af1606f79d4 to your computer and use it in GitHub Desktop.
Dump
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "context" | |
| "encoding/hex" | |
| "fmt" | |
| "log" | |
| "math/big" | |
| "github.com/ethereum/go-ethereum" | |
| "github.com/ethereum/go-ethereum/common" | |
| "github.com/ethereum/go-ethereum/crypto" | |
| "github.com/ethereum/go-ethereum/ethclient" | |
| ) | |
| func main() { | |
| // Connect to Ethereum client | |
| client, err := ethclient.Dial("https://eth-mainnet.g.alchemy.com/v2/[KEY]") | |
| if err != nil { | |
| log.Fatalf("Failed to connect to Ethereum client: %v", err) | |
| } | |
| transferEventSig := []byte("Transfer(address,address,uint256)") | |
| hash := crypto.Keccak256(transferEventSig) | |
| hexHash := hex.EncodeToString(hash) | |
| prefixedHash := "0x" + hexHash | |
| transferTopic := common.HexToHash(prefixedHash) | |
| tokenIdTopic := common.HexToHash("0x0000000000000000000000000000000000000000000000000000000000000123") | |
| fromBlock := big.NewInt(16715088) | |
| toBlock := big.NewInt(fromBlock.Int64() + 100) | |
| filterQuery := ðereum.FilterQuery{ | |
| FromBlock: fromBlock, | |
| ToBlock: toBlock, | |
| Topics: [][]common.Hash{{transferTopic, tokenIdTopic}}, | |
| } | |
| // Set up the filter | |
| logs, err := client.FilterLogs(context.Background(), *filterQuery) | |
| if err != nil { | |
| log.Fatalf("Failed to retrieve logs: %v", err) | |
| } | |
| // Print the transaction hash and event name for the first 5 logs | |
| count := 0 | |
| for _, log := range logs { | |
| fmt.Println(count) | |
| fmt.Printf("TxHash: %v\n", log.TxHash.Hex()) | |
| link := "https://etherscan.io/tx/" + log.TxHash.Hex() + "\n" | |
| fmt.Printf(link) | |
| count++ | |
| if count == 5 { | |
| break | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment