ethers-io / ethers.js

Complete Ethereum library and wallet implementation in JavaScript.
https://ethers.org/
MIT License
7.9k stars 1.83k forks source link

Events stop to be fired (but rpc ping always answer back) #2969

Open 0xARROWK opened 2 years ago

0xARROWK commented 2 years ago

Ethers Version

v5.6.5

Search Terms

events stop firing

Describe the Problem

Hello ! I write this issue because whatever I try, listening to the new created pair stop to be fired after 2 or 3 hours. By reading some issues, I was able to setup a ping system that works well, pong is answered back even 4 hours after the PairCreated events stop to be fired, so I think that it's not an issue with my node url (I use moralis nodes). I have to restart my script to make it work again.

I saw many issues with the same problem, but nothing works for me :(

Please, anyone can help me ?

Thanks for taking the time to read.

Code Snippet

const streamBSCTokens = async () => {
        EXPECTED_PONG_BACK = 15000
        KEEP_ALIVE_CHECK_INTERVAL = 7500

        let pingTimeout = null
        let keepAliveInterval = null
        const provider = new ethers.providers.WebSocketProvider(config.nodes.bsc);

        provider._websocket.on('open', () => {
            keepAliveInterval = setInterval(() => {
                console.log(`Checking if the connection is alive, sending a ping`)

                provider._websocket.ping()

                pingTimeout = setTimeout(() => {
                    provider._websocket.terminate()
                }, EXPECTED_PONG_BACK)
            }, KEEP_ALIVE_CHECK_INTERVAL)

        })

        provider._websocket.on('close', () => {
            console.error(`The websocket connection was closed`)
            clearInterval(keepAliveInterval)
            clearTimeout(pingTimeout)
            streamBSCTokens()
        })

        provider._websocket.on('pong', () => {
            console.log(`Received pong, so connection is alive, clearing the timeout`)
            clearInterval(pingTimeout)
        })

        const factory = new ethers.Contract(
            "0xcA143Ce32Fe78f1f7019d7d551a6402fC5350c73",
            [
                'event PairCreated(address indexed token0, address indexed token1, address pair, uint)'
            ],
            provider
        );

        factory.on('PairCreated', async (token0, token1, pairAddress) => {
            console.log(pair0 + " : " + pair1 + " : " + pairAddress);
        }
}

Contract ABI

No response

Errors

No response

Environment

node.js (v12 or newer)

Environment (Other)

No response

James1847 commented 2 years ago

Got the same problems, please help me asap!!!!

James1847 commented 2 years ago

@ricmoo Hello sir, I used the quicknode ws url to do the same thing, everything works fine at first, after 2 or 3 hours, the events I listened got lesser, because I used the web3.py to do the same thing, I can find web3.py works fine, but ethers.js works abnormal.....