ethers-io / ethers.js

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

Issues while listening to events #4784

Open bomb-on opened 4 months ago

bomb-on commented 4 months ago

Ethers Version

6.13.1

Search Terms

provider, events, eth_getFilterChanges, eth_getLogs

Describe the Problem

I'm observing strange problems while using ethers v6 and trying to listen to events, staring with errors like

@TODO Error: could not coalesce error (error={ "code": -32000, "message": "filter not found" }, payload={ "id": 8, "jsonrpc": "2.0", "method": "eth_getFilterChanges", "params": [ "0x3e8dc08f82f677b144b2e2b1acc9549f" ] }, code=UNKNOWN_ERROR, version=6.13.1)

This is not happening if I switch to a "non-official" chain's RPC provider endpoint (details in the code snippet).

However, if I switch to a "non-official" RPC provider endpoint, I occasionally get an error

Error: could not coalesce error (error={ "code": -32000, "message": "requested to block 47918748 after last accepted block 47918747" }, payload={ "id": 2271, "jsonrpc": "2.0", "method": "eth_getLogs", "params": [ { "address": "0xab4fe2d136efd7f8dfce3259a5e3c5e4c0130c80", "fromBlock": "0x2db2e5f", "toBlock": "0x2db2e9c", "topics": [ "0x2203b46b43cd4c862695ef2523f622840927113ac185c69b4f1141ebf713b791" ] } ] }, code=UNKNOWN_ERROR, version=6.13.1)

after which my script completely crashes.

None of this is happening if I use ethers v5 and an "official" RPC provider endpoint.

I would extremely appreciate if someone (@ricmoo probably would be the best) could clarify me few things here:

Code Snippet

// Version 5 code

import { ethers } from 'ethers';

const abi = [
  {
    anonymous: false,
    inputs: [
      {
        indexed: true,
        internalType: 'address',
        name: 'from',
        type: 'address'
      },
      {
        indexed: true,
        internalType: 'address',
        name: 'to',
        type: 'address'
      },
      {
        indexed: false,
        internalType: 'uint256',
        name: 'value',
        type: 'uint256'
      }
    ],
    name: 'Transfer',
    type: 'event'
  },
];
const contractAddress = '0x420FcA0121DC28039145009570975747295f2329';
let contract;
let provider;

export const initV5 = () => {
  provider = new ethers.providers.JsonRpcProvider('https://api.avax.network/ext/bc/C/rpc');  // "official" RPC provider endpoint
  contract = new ethers.Contract(contractAddress, abi, provider);

  contract.on('Transfer', async (from, to, value, event) => {
    const tx = await provider.getTransactionReceipt(event.log.transactionHash);
    console.log('TRANSFER', event, tx);
  });
};

// Version 6 code

import { ethers } from 'ethers';

const abi = [
  {
    anonymous: false,
    inputs: [
      {
        indexed: true,
        internalType: 'address',
        name: 'from',
        type: 'address'
      },
      {
        indexed: true,
        internalType: 'address',
        name: 'to',
        type: 'address'
      },
      {
        indexed: false,
        internalType: 'uint256',
        name: 'value',
        type: 'uint256'
      }
    ],
    name: 'Transfer',
    type: 'event'
  },
];
const contractAddress = '0x420FcA0121DC28039145009570975747295f2329';
let contract
let provider;

export const initV6 = () => {
  // provider = new ethers.JsonRpcProvider('https://rpc.ankr.com/avalanche');  // "non-official" RPC provider endpoint
  provider = new ethers.JsonRpcProvider('https://api.avax.network/ext/bc/C/rpc');  // "official" RPC provider endpoint
  contract = new ethers.Contract(contractAddress, abi, provider);

  contract.on('Transfer', async (from, to, value, event) => {
    const tx = await provider.getTransactionReceipt(event.log.transactionHash);
    console.log('TRANSFER', event, tx);
  });
};

Contract ABI

[
  {
    anonymous: false,
    inputs: [
      {
        indexed: true,
        internalType: 'address',
        name: 'from',
        type: 'address'
      },
      {
        indexed: true,
        internalType: 'address',
        name: 'to',
        type: 'address'
      },
      {
        indexed: false,
        internalType: 'uint256',
        name: 'value',
        type: 'uint256'
      }
    ],
    name: 'Transfer',
    type: 'event'
  },
]

Errors

// This happens if I use ethers v6 and an "official" RPC provider endpoint (https://api.avax.network/ext/bc/C/rpc)

@TODO Error: could not coalesce error (error={ "code": -32000, "message": "filter not found" }, payload={ "id": 345, "jsonrpc": "2.0", "method": "eth_getFilterChanges", "params": [ "0x3e8dc08f82f677b144b2e2b1acc9549f" ] }, code=UNKNOWN_ERROR, version=6.13.1)
    at makeError (file:///Users/oxxo/Sites/web3/test/node_modules/ethers/lib.esm/utils/errors.js:124:21)
    at JsonRpcProvider.getRpcError (file:///Users/oxxo/Sites/web3/test/node_modules/ethers/lib.esm/providers/provider-jsonrpc.js:726:16)
    at file:///Users/oxxo/Sites/web3/test/node_modules/ethers/lib.esm/providers/provider-jsonrpc.js:298:45
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'UNKNOWN_ERROR',
  error: { code: -32000, message: 'filter not found' },
  payload: {
    method: 'eth_getFilterChanges',
    params: [ '0x3e8dc08f82f677b144b2e2b1acc9549f' ],
    id: 345,
    jsonrpc: '2.0'
  },
  shortMessage: 'could not coalesce error'
}

// This sometimes happens if I use a "non-official" RPC provider endpoint (e.g. https://rpc.ankr.com/avalanche)
// This happens with a different contract, but still, I haven't seen this error if I use ethers v5

Error: could not coalesce error (error={ "code": -32000, "message": "requested to block 47918748 after last accepted block 47918747" }, payload={ "id": 2271, "jsonrpc": "2.0", "method": "eth_getLogs", "params": [ { "address": "0xab4fe2d136efd7f8dfce3259a5e3c5e4c0130c80", "fromBlock": "0x2db2e5f", "toBlock": "0x2db2e9c", "topics": [ "0x2203b46b43cd4c862695ef2523f622840927113ac185c69b4f1141ebf713b791" ] } ] }, code=UNKNOWN_ERROR, version=6.13.1)
    at makeError (file:///Users/oxxo/Sites/web3/api.salvor.ninja/node_modules/ethers/lib.esm/utils/errors.js:124:21)
    at JsonRpcProvider.getRpcError (file:///Users/oxxo/Sites/web3/api.salvor.ninja/node_modules/ethers/lib.esm/providers/provider-jsonrpc.js:726:16)
    at file:///Users/oxxo/Sites/web3/api.salvor.ninja/node_modules/ethers/lib.esm/providers/provider-jsonrpc.js:298:45
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: 'UNKNOWN_ERROR',
  error: {
    code: -32000,
    message: 'requested to block 47918748 after last accepted block 47918747'
  },
  payload: {
    method: 'eth_getLogs',
    params: [
      {
        address: '0xab4fe2d136efd7f8dfce3259a5e3c5e4c0130c80',
        topics: [
          '0x2203b46b43cd4c862695ef2523f622840927113ac185c69b4f1141ebf713b791'
        ],
        fromBlock: '0x2db2e5f',
        toBlock: '0x2db2e9c'
      }
    ],
    id: 2271,
    jsonrpc: '2.0'
  },
  shortMessage: 'could not coalesce error'
}

Environment

node.js (v12 or newer)

Environment (Other)

No response

eshaan7 commented 3 months ago

Related: https://github.com/ethers-io/ethers.js/issues/4104

bomb-on commented 3 months ago

Related: #4104

Just to mention that I went through the linked issue myself before I reported mine and even tried to implement a logic where I would catch some of these errors (e.g. provider.on("debug", myDebugCatchingFunction); or provider.on("error", myErrorCatchingFunction);) but for some reason I just couldn't suppress constant printing out the whole error and stack trace so I thought I'm doing something wrong and decided to report here.

eshaan7 commented 3 months ago

@bomb-on; Yeah, I couldn't find a proper fix either. As a temporary fix, using polling: true option instead has been working for me.

AdamMomen commented 2 months ago

@eshaan7 did that cause any duplicate events?