In this guide, we will explore how to effectively set up and use ethereum-watcher, a powerful event listener for the Ethereum Blockchain written in Golang. This tool will enable you to monitor and track on-chain events seamlessly, providing a bridge between your applications and the Ethereum chain.
Understanding the Basics
Imagine a post office delivering letters (events) in a town (the Ethereum Blockchain). The post office workers (ethereum-watcher) observe which letters are being sent out (on-chain events). They can notify residents (applications) when specific letters arrive for them without the residents needing to check their mailboxes constantly.
Getting Started
Installation
- To install ethereum-watcher, simply run the following command in your terminal:
go get github.com/HydroProtocol/ethereum-watcher
Sample Commands
After the installation, you can use some sample commands to get started:
- Display Basic Help Info:
docker run hydroprotocolio/ethereum-watcher:master bin/ethereum-watcher help
docker run hydroprotocolio/ethereum-watcher:master bin/ethereum-watcher new-block-number
docker run hydroprotocolio/ethereum-watcher:master bin/ethereum-watcher usdt-transfer
Key Features
- Plug-in Friendly: Easily add plugins to listen for any type of on-chain event.
- Fork Tolerance: Sends revert messages to subscribers during a blockchain fork.
Usage Overview
To effectively use ethereum-watcher, you will primarily interact with two key structures:
- Watcher: An HTTP client that continuously polls for newly mined blocks and processes registered plugins to track specific events.
- ReceiptLogWatcher: Optimized for querying logs for certain events in a batch rather than one at a time.
Example Implementations
Here are some code snippets to help you listen for events.
Print Number of Newly Mined Blocks:
package main
import (
context "context"
fmt "fmt"
plugin "github.com/HydroProtocol/ethereum-watcher/plugin"
structs "github.com/HydroProtocol/ethereum-watcher/structs"
)
func main() {
api := "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
w := NewHttpBasedEthWatcher(context.Background(), api)
w.RegisterBlockPlugin(plugin.NewBlockNumPlugin(func(i uint64, b bool) {
fmt.Println(i, b)
}))
w.RunTillExit()
}
Listen for New ERC20 Transfer Events:
package main
import (
context "context"
fmt "fmt"
plugin "github.com/HydroProtocol/ethereum-watcher/plugin"
structs "github.com/HydroProtocol/ethereum-watcher/structs"
logrus "github.com/sirupsen/logrus"
)
func main() {
api := "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID"
w := NewHttpBasedEthWatcher(context.Background(), api)
w.RegisterTxReceiptPlugin(plugin.NewERC20TransferPlugin(
func(token, from, to string, amount decimal.Decimal, isRemove bool) {
logrus.Infof("New ERC20 Transfer: token(%s), %s - %s, amount: %s, isRemoved: %t", token, from, to, amount, isRemove)
},
))
w.RunTillExit()
}
Troubleshooting
If you encounter any issues when using ethereum-watcher, here are some tips to guide you:
- Ensure that you have installed all necessary dependencies and that your Go environment is set up correctly.
- Check your network connection to ensure you can connect with the Ethereum node.
- If you encounter errors related to plugins, validate that your plugins are correctly implemented and registered.
- Refer to the official documentation on GitHub for additional guidance and updates.
- For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
With its user-friendly features and capabilities, ethereum-watcher stands out as an essential tool for developers looking to engage with the Ethereum Blockchain effectively. By leveraging its plug-in architecture and event-handling capabilities, you can significantly streamline your blockchain interactions.
At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.