Set
data structure to track unique event identifiers and prevent duplicate event processing.subscribeToLogs
that takes an endpoint as input, creates a new Web3 instance with that endpoint, and sets up a WebSocket subscription to listen for logs (events) matching the defined filter.Set
to mark it as processed.node.js
, a powerful JavaScript runtime environment that enables developers to run JavaScript code outside a web browser. For this project, it’s recommended to use at least version 18. You can download it from their website.
With node.js
installed, you’re ready to start using JavaScript. Now, you can set up your nodes.
package.json
file. Open your terminal, navigate to the desired directory, and run the following command:
package.json
file with default settings.
Next, we’ll install the required dependencies, which are the web3.js
, ethers.js
and the dotenv
package for loading environment variables from a .env
file:
.env
in the root directory of your project. This file will store your environment variables, including the WebSocket endpoints for the Ethereum nodes. In the .env
file, you can define the endpoints like this:
.env
file using the dotenv
package. At the top of your script, add the following line:
.env
file and make them accessible in your script using process.env
.
Now, instead of hardcoding the endpoints in your script, you can read them from the environment variables:
.env
file.
index.js
and paste the following code:
Web3
object from the web3
library and configuring the dotenv
package to load environment variables from a .env
file.
ENDPOINT_1
, ENDPOINT_2
, and ENDPOINT_3
and stored in the endpoints
array.
logsFilter
object is defined, which specifies the contract address of the Wrapped Ether (WETH) contract and the topic (event signature) for the “Transfer” event. This filter will be used to subscribe to only the desired events.
Set
data structure called seenEvents
is initialized to track unique event identifiers and prevent duplicate event processing.
subscribeToLogs
async function is defined, which takes an endpoint as input and sets up a WebSocket subscription to listen for logs (events) matching the logsFilter
.
subscribeToLogs
function, a new Web3
instance is created using the provided endpoint, and the web3.eth.subscribe("logs", logsFilter)
method is called to create a subscription.
subscription.on("data", ...)
callback is triggered. The callback generates a unique event identifier using the transaction hash and log index and checks if this identifier has been seen before in the seenEvents
Set.
seenEvents
Set and the event data is logged to the console along with the endpoint from which the event was received.
subscription.on("error", ...)
callback is set up to handle any errors during the subscription process, logging the error into the console.
endpoints.forEach(subscribeToLogs)
line iterates over the endpoints
array and calls the subscribeToLogs
function for each endpoint creates multiple WebSocket subscriptions to different Ethereum nodes.
ChainstackProvider
, learn more about it by reading ethers ChainstackProvider Documentation.ethers.js
library and environment variables for storing WebSocket endpoints. Here’s what’s happening:
ethers
object from the ethers.js
library and configuring the dotenv
package to load environment variables from a .env
file.
ENDPOINT_1
, ENDPOINT_2
, and ENDPOINT_3
and stored in the endpoints
array.
Set
data structure called seenEvents
is initialized to track unique event identifiers and prevent duplicate event processing.
subscribeToEvents
async function is defined, which takes an endpoint as input and sets up an event subscription for the WETH contract’s “Transfer” event.
subscribeToEvents
function, a new WebSocketProvider
instance is created using the provided endpoint, and a Contract
instance is created using the contract address, ABI, and provider.
contract.on("Transfer", ...)
method is used to subscribe to the “Transfer” event of the WETH contract.
seenEvents
Set, and the event details (sender, recipient, and amount transferred) are logged to the console along with the endpoint from which the event was received.
provider.on("error", ...)
callback is set up to handle any errors during the WebSocket connection, logging the error to the console.
endpoints.forEach(subscribeToEvents)
line iterates over the endpoints
array and calls the subscribeToEvents
function for each endpoint creates multiple WebSocket connections and event subscriptions to different Ethereum nodes.
ethers.js
, the application can monitor WETH transfer events reliably and consistently, even if one or more Ethereum nodes experience downtime or connectivity issues. Using environment variables to store endpoints makes managing and updating endpoint configurations easier without modifying the code directly.