3

I have a node.js server where I am listening for an event that is emitted from a Solidity smart contract deployed on the Mumbai Polygon testnet.

First, I'm using the "web3-providers-ws" node package to create a provider, using keepalive and reconnection options:

let providerNetworkUrl;
let ccggContractAddress;

if (process.env.TESTNET === 'true') {

  console.log('using mumbai URLs')
  // on mumbai
  ccggContractAddress = process.env.CCGG_ADDRESS_MUMBAI_TESTNET;
  providerNetworkUrl = 'wss://polygon-mumbai.g.alchemy.com/v2/';
} else {
  // on polygon mainnet
  ccggContractAddress = process.env.CCGG_ADDRESS_POLYGON_MAINNET;
  providerNetworkUrl = 'wss://polygon-mumbai.g.alchemy.com/v2/';
}

const connectionUrl = providerNetworkUrl + process.env.alchemyApiKey;
console.log('connectionUrl: ', connectionUrl)

// Enable auto reconnection
const options = {

  clientConfig: {
    // Useful to keep a connection alive
    keepalive: true,
    keepaliveInterval: 20 * 60 * 60 * 1000 // keep alive for 20 min
  },

  // Enable auto reconnection
  reconnect: {
    auto: true,
    delay: 1000, // ms
    maxAttempts: 10,
    onTimeout: false
  }
};

let provider = new ethers.providers.WebSocketProvider(
  new Web3WsProvider(connectionUrl, options))

provider.on('end', (e) => {
  console.log('provider ended: ' + e);
})

provider.on('error', (e) => {
  console.log('provider errored: ' + e);
})

Then I'm using ethers.js to connect to the smart contract.

const wallet = new ethers.Wallet(process.env.PRIVATE_WALLET_KEY, provider)
console.log('my address (wallet): ', wallet.address)

const signer = wallet.connect(provider)

const ccggContract = new ethers.Contract(
  ccggContractAddress,
  ccggAbi,
  signer
)

Then finally I set up the listener for the "GuessSubmitted" event.

ccggContract.on('GuessSubmitted', async (guess, sender, betSize) => {

  console.log('GuessSubmitted')

})

The strange thing is that it all works great when I first start it up. The node server hears the events, logs "GuessSubmitted", and handles the event.

However, after a few hours, or overnight, I then go to play the game, the events are dispatched in Solidity, but the node server doesn't hear anything!!! what??

After looking at some other posts on the internet I even set up this ping function to keep the websocket connection alive, and I'm invoking it every 15 min:

async function ping() {

  const now = new Date();

  try {
    console.log('Sending a ping! ' + now.toUTCString());
    const blockNum = await provider.send('eth_blockNumber');
    console.log('Got blocknum: ', blockNum);
  }
  catch (err) {
    console.log('PING ERROR')
    console.log(err)
  }

}

// ping every 15 min
setInterval(ping, 1000 * 60 * 15);

console.log('provider created.')

The strange thing is that the connection doesn't seem to actually drop. I never see any "provider errored" or "provider ended" in the logs.

The logs look like this, with the ping going through fine every 15 min while never hearing any "GuessSubmitted" events anymore!!

I thought the issue could be with my eth connection provider, but I've tried now with Alchemy, Infura, and GetBlock!! This "stops hearing events" issue happens with all three so I am wondering what could possibly be the problem, if it is some underlying bug in the ethereum platform or if I am making some mistake in the code...

Thanks!

Jim
  • 3,821
  • 1
  • 28
  • 60
  • Just happened to me yet again! I think this is a ** very** critical issue with ethereum itself!!! Sad that there are still so many issues with it and no solution... – Jim Dec 14 '21 at 23:10
  • You say "...ping going through fine every 15 min...", but in your example output you only show "Sending a ping!..." log messages. There are no "Got blocknum:..." log messages. So checking the obvious, you are actually getting responses to your ping requests, correct? – kaliatech Dec 21 '21 at 13:29
  • @kaliatech yes, it is displaying the "pong" log every time. – Jim Dec 21 '21 at 16:23

2 Answers2

0

If you look at the web3.js project issues, there are number of reports about disconnect/close/reconnect not working as expected:

There is no one solution, afaict. A rewrite is in progress with intent to solve some of the issues:

kaliatech
  • 17,579
  • 5
  • 72
  • 84
  • I don't think the issue is related to "disconnect/close/reconnect" since I am seeing the "pong" log... it's an issue with the listener of solidity events being dispatched. – Jim Dec 21 '21 at 16:24
-1

Seems you are correctly setting up the WebSocketProvider and handling the events emitted by the smart contract.try This:

Check the event emission, Verify the contract address,Check for error messages,Test with a different environment: Try running the code on a local Ethereum testnet (e.g., Ganache) to see if the issue persists.Check any new versions of the web3-providers-ws package, ethers.js,

Test with a GetBlock again, sure it Will Work

Charly El
  • 10
  • 2
  • 2
    This appears likely to have been written (entirely or partially) by AI (e.g., ChatGPT). If you used an AI tool to assist with any answer, I would encourage you to delete it, as [posting of AI-generated content is banned here](//meta.stackoverflow.com/q/421831). **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was AI-generated, please leave feedback accordingly. The moderation team can use your help to identify issues. – NotTheDr01ds Jun 15 '23 at 00:10