Watch pending transactions on Ethereum using ethersjs

/**
* script.js
**/const ethers = require('ethers');
require('dotenv').config();
const apiKey = { projectId: process.env.INFRA_PROJECT_ID };
const provider = new ethers.providers.InfuraWebSocketProvider('homestead', apiKey);
async function main() {
provider
.on('pending', pendingTransaction=> {
console.log(`new pending transaction received. ${pendingTransaction}`);
// do whatever you like.
});
}
main()
.catch(err => {
console.log(err);
});
Requirements
- Node.js Environment
- following dependencies
a. ethersjs
b. dotenv - Infra Account & your Infra project ID
Process
// create your own project directory
$ mkdir yourproject// initialize npm and install dependencies
$ npm init -y && npm install ethers dotenv// create your own javascript file and paste the snippet above
$ touch script.js// create .env file and paste your infra project id as INFRA_PROJECT_ID
$ touch .env
Donate
0x199012076Ea09f92D8C30C494E94738CFF449f57