Provider.on listener doesn´t work

Hello,

It doesn´t enter in listening looping through the blocks when I execute the following code:

const { ethers } = require('ethers');
const infuraProvider = new ethers.providers.InfuraProvider('homestead', process.env.INFURA_API_KEY);

infuraProvider.on('block', async (blockNumber) => {

            console.log("blockNumber ", blockNumber);

        });

Here´s the output of console.log("infuraProvider ", infuraProvider);

infuraProvider InfuraProvider {

  • _isProvider: true,*
  • _events: [],*
  • _emitted: { block: -2 },*
  • disableCcipRead: false,*
  • formatter: Formatter {*
  • formats: {*
  •  transaction: [Object],*
    
  •  transactionRequest: [Object],*
    
  •  receiptLog: [Object],*
    
  •  receipt: [Object],*
    
  •  block: [Object],*
    
  •  blockWithTransactions: [Object],*
    
  •  filter: [Object],*
    
  •  filterLog: [Object]*
    
  • }*
  • },*
  • anyNetwork: false,*
  • _network: {*
  • name: ‘homestead’,*
  • chainId: 1,*
  • ensAddress: ‘0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e’,*
  • _defaultProvider: [Function: func] { renetwork: [Function (anonymous)] }*
  • },*
  • _maxInternalBlockNumber: -1024,*
  • _lastBlockNumber: -2,*
  • _maxFilterBlockRange: 10,*
  • _pollingInterval: 4000,*
  • _fastQueryDate: 0,*
  • connection: {*
  • allowGzip: true,*
  • url: ‘https://mainnet.infura.io/v3/6021b8788d0648c285535dc157a95e6f’,*
  • throttleCallback: [Function: throttleCallback]*
  • },*
  • _nextId: 42,*
  • apiKey: ‘6021b8788d0648c285535dc157a95e6f’,*
  • projectId: ‘6021b8788d0648c285535dc157a95e6f’,*
  • projectSecret: null*
    }

Would appreciate your help on this one.

BR,
Sam

Hi Sam,
I saw your case on ConsenSys support and replied with the below. If someone else has any other ideas please chime in!

This appears to work for me:

$ cat 945979.js
const { ethers } = require('ethers');
const infuraProvider = new ethers.providers.InfuraProvider('homestead', process.env.INFURA_API_KEY);

infuraProvider.on('block', async (blockNumber) => {

console.log("blockNumber ", blockNumber);

});
$ INFURA_API_KEY="..." node 945979.js 
blockNumber  15651935
blockNumber  15651936
blockNumber  15651937

Can you provide the error or other output you are getting?

Warm regards,
Chris | Infura | ConsenSys