Watch pending transactions on Ethereum using ethersjs

https://app.rarible.com/token/0x60f80121c31a0d46b5279700f9df786054aa5ee5:102672:0x199012076ea09f92d8c30c494e94738cff449f57
/**
* 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

  1. Node.js Environment
  2. following dependencies
    a. ethersjs
    b. dotenv
  3. 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

interested in crypto and art

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Alfred to Roam

Adding Validators to Reactive Form

Random Creations : Week 1 — Android Random List App + 3D Renders

Make API Calls the Right Way in Angular

https://megictoolbox.blogspot.com/2022/01/color-foo-function-use-strict-angular.html

Migrating from JavaScript to TypeScript (from scratch)

What the THUNK? Using and understanding Thunk in your React/Redux app.

How to Improve LCP and Speed Index for Next.js Websites

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hiroyuki Tachibana

Hiroyuki Tachibana

interested in crypto and art

More from Medium

Signing Transactions With React, Permit Transactions EIPS712

The Tatum C# SDK is now live!

Accessing a smart contract on Ethereum Network

Laika Blockchain Lab (5 Jan 2022 Update)