I am trying to create a simple script to test the event listening functionality of Web3.js CreatedPairs. However, my script runs through the code once and then exits instead of continuing to listen for created pairs and I cannot figure out why.

const Web3 = require('web3')
const web3 = new Web3(Web3.givenProvider || 'https://mynodeishere');

const IUniswapV2Factory = require("@uniswap/v2-core/build/IUniswapV2Factory.json")
const UNI_FACTORY_ADDRESS = '0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f'
const uFactory = new web3.eth.Contract(IUniswapV2Factory.abi, UNI_FACTORY_ADDRESS)

const main = async () => {

    // Create event listener to listen to PairCreated
    uFactory.events.PairCreated({}, async (error, event) => {
        console.log(`New pair detected...\n`)



What ends up happening is when I run node ./myapp.js. The code gets stepped in to and logs New pair detected... to the console and then exits. This happening right away tells me that it is not executing when a new pair is detected and is instead just stepping through the code.

However, what I anticipated happening was for the app to continue to run and log to the console only when a new pair is detected. I am sure I am overlooking something small, but any help would be appreciated.

2 Answers 2


Try something like this instead:

const options = {
    fromBlock:  'latest'

    .on('data', event => console.log(event))

use WebSocket provider instead of HTTP provider since it doesn't support subscriptions

  • Thanks for the response. It would appear that your changes are not stepping in to the call back at all now. I have watched multiple pairs get created and have gotten no logs to the console. One difference is that for the fromBlock parameter I decided to use latest. Any ideas? Commented Mar 21, 2022 at 20:42

What ended up solving my issue was realizing that I was not using a WebService Provider. I was able to fix this by using the wss version of my node connection.

// example connection to a WSS provider
const web3 = new Web3('wss://mainnet.infura.io/ws/v3/<mytoken>');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.