eth_newFilter creates a filter on the node that watches for new logs. Calling eth_getFilterChanges with that filter ID returns the logs recorded since your last call. Use this poll-based model when you want
a controlled polling interval, or when the node offers no native subscription for logs.
Prerequisites
- Node.js installed.
- The
web3(v4.x),web3-providers-ws, anddotenvpackages. - A WebSocket endpoint for a peaq node (
wss://quicknode1.peaq.xyz), set in a.envfile.
Instructions
1. Setting Up the Environment
- Install Dependencies: Make sure you install the necessary packages:
- Set ESM Module:
Add the following to your
package.jsonto allow for ESM modules.
- Configure Environment Variables:
Create a
.envfile in your project directory with:
2. How the filter works
eth_newFilter with { fromBlock: "latest" } creates a filter that captures logs from the current block onward. The node responds with a filter ID.
Polling eth_getFilterChanges with that ID returns the logs recorded since the last poll. The example polls every 5 seconds and prints each log. Both the filter creation and the poll check the node’s response
for an error and log it, so network problems and bad parameters surface immediately.
On a shutdown signal (Ctrl+C) the script calls eth_uninstallFilter with the filter ID to free the filter on the node, then disconnects the provider.
Code Example
Example logs:


