Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
Web3.js Script exits while listening to CreatedPairs

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`)

    })
}

main()

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.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In this context, Web3.givenProvider is null, so the HTTP provider is used.

To be able to subscribe to events, you need to use a WSS provider.

// example connection to a WSS provider
const web3 = new Web3('wss://mainnet.infura.io/ws/v3/<yourtoken>');
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda