This Recipe shows you how to leverage subscriptions, WSS endpoints, and web3.js to monitor incoming transactions in real-time.
Overview
In this Recipe, we leverage the web3.js library to monitor real-time incoming transactions to a specified address.
We’ll use a WebSocket endpoint to listen for new blocks, then inspect the new block to find transactions we might be interested in.
Environment setup
Install node.js in case it is not installed yet.
Create a new directory for your project, then install the web3.js library:
npm install web3
Get your Chainstack endpoint
To run this code, you will need a Chainstack account and an Ethereum archive node.
The code
Here is a breakdown of the code:
Web3
class is imported from the web3
package.Web3
instance is created, connected to an Ethereum node through a WebSocket URL (NODE_URL
).walletAddress
.subscribeToNewBlocks
is an asynchronous function that establishes a subscription to the newBlockHeaders
event using web3.eth.subscribe
.data
event) and another for handling errors (error
event).handleNewBlock
function is triggered whenever a new block header is received.walletAddress
.web3.eth.getBlock
.to
address) matches the walletAddress
.from
), recipient address (to
), and the value transferred in Ether.handleError
function logs any errors that occur during the subscription or block handling process.subscribeToNewBlocks()
, initiating the subscription and monitoring process.Run the code
Now you can get your endpoint and paste it into the NODE_URL
const. Then you can run it with node YOUR_SCRIPT_NAME
This Recipe shows you how to leverage subscriptions, WSS endpoints, and web3.js to monitor incoming transactions in real-time.
Overview
In this Recipe, we leverage the web3.js library to monitor real-time incoming transactions to a specified address.
We’ll use a WebSocket endpoint to listen for new blocks, then inspect the new block to find transactions we might be interested in.
Environment setup
Install node.js in case it is not installed yet.
Create a new directory for your project, then install the web3.js library:
npm install web3
Get your Chainstack endpoint
To run this code, you will need a Chainstack account and an Ethereum archive node.
The code
Here is a breakdown of the code:
Web3
class is imported from the web3
package.Web3
instance is created, connected to an Ethereum node through a WebSocket URL (NODE_URL
).walletAddress
.subscribeToNewBlocks
is an asynchronous function that establishes a subscription to the newBlockHeaders
event using web3.eth.subscribe
.data
event) and another for handling errors (error
event).handleNewBlock
function is triggered whenever a new block header is received.walletAddress
.web3.eth.getBlock
.to
address) matches the walletAddress
.from
), recipient address (to
), and the value transferred in Ether.handleError
function logs any errors that occur during the subscription or block handling process.subscribeToNewBlocks()
, initiating the subscription and monitoring process.Run the code
Now you can get your endpoint and paste it into the NODE_URL
const. Then you can run it with node YOUR_SCRIPT_NAME