Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issues while listening to events #4784

Open
bomb-on opened this issue Jul 13, 2024 · 4 comments
Open

Issues while listening to events #4784

bomb-on opened this issue Jul 13, 2024 · 4 comments
Assignees
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6

Comments

@bomb-on
Copy link

bomb-on commented Jul 13, 2024

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:

  • why do I always see filter not found error if I use ethers v6 and never in v5?
  • why don't I see the same error if I use ethers v6 with a "non-official" provider RPC endpoint?
  • why is requested to block 47918748 after last accepted block 47918747 error happening when I try to get transaction receipt and what is a suggested method to catch such error instead of experiencing crashes? Is a simple try/catch block within my listener enough?

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

@bomb-on bomb-on added investigate Under investigation and may be a bug. v6 Issues regarding v6 labels Jul 13, 2024
@eshaan7
Copy link

eshaan7 commented Aug 6, 2024

Related: #4104

@bomb-on
Copy link
Author

bomb-on commented Aug 6, 2024

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
Copy link

eshaan7 commented Aug 7, 2024

@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
Copy link

@eshaan7 did that cause any duplicate events?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
investigate Under investigation and may be a bug. v6 Issues regarding v6
Projects
None yet
Development

No branches or pull requests

4 participants