Skip to content

contract.on() fails on Chainstack with block range limit error - eth_newFilter should include fromBlock: "latest" #5061

@GCE333

Description

@GCE333

Ethers Version

6.15.0

Search Terms

contract.on, eth_newFilter, Chainstack, block range limit, fromBlock

Describe the Problem

When using contract.on() to listen for events with a ChainStack RPC provider, the event listener fails with a block range limit error. This issue appears to have started after ChainStack implemented stricter block range limits on their RPC endpoints.

Root Cause Analysis

Through testing with direct curl requests to the ChainStack RPC endpoint, I've identified that:

  1. This fails (no fromBlock parameter):
curl --request POST \
  --url https://base-mainnet.core.chainstack.com/YOUR_API_KEY \
  --header 'Content-Type: application/json' \
  --data '{
    "id": 1,
    "jsonrpc": "2.0",
    "method": "eth_newFilter",
    "params": [{
      "address": ["0x8909dc15e40173ff4699343b6eb8132c65e18ec6"],
      "topics": ["0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9"]
    }]
  }'
  1. This succeeds (with fromBlock: "latest"):
curl --request POST \
  --url https://base-mainnet.core.chainstack.com/YOUR_API_KEY \
  --header 'Content-Type: application/json' \
  --data '{
    "id": 2,
    "jsonrpc": "2.0",
    "method": "eth_newFilter",
    "params": [{
      "fromBlock": "latest",
      "address": ["0x8909dc15e40173ff4699343b6eb8132c65e18ec6"],
      "topics": ["0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9"]
    }]
  }'

Expected Behavior

When calling contract.on() to listen for contract events, the underlying eth_newFilter RPC call should include fromBlock: "latest" by default when no explicit block range is specified. This would:

  1. Comply with provider restrictions on block range limits
  2. Match the typical use case for event listeners (monitoring new events going forward)
  3. Maintain backward compatibility with other providers that don't enforce these limits

Proposed Solution

When contract.on() internally calls eth_newFilter, it should include fromBlock: "latest" in the filter parameters if no explicit block range has been specified by the user. This would align with the semantic meaning of "listening for new events" and would be compatible with providers that enforce block range limits.

Code Snippet

import { ethers } from 'ethers';

const provider = new ethers.JsonRpcProvider('https://base-mainnet.core.chainstack.com/YOUR_API_KEY');
const contractAddress = '0x8909dc15e40173ff4699343b6eb8132c65e18ec6';
const abi = [
  "event PairCreated(address indexed token0, address indexed token1, address pair, uint)"
];

const contract = new ethers.Contract(contractAddress, abi, provider);

// This fails with ChainStack after their recent changes
contract.on('PairCreated', (...args) => {
  console.log('Event received:', ...args);
});

Contract ABI

[ "event PairCreated(address indexed token0, address indexed token1, address pair, uint)" ]

Errors

@TODO Error: could not coalesce error (error={ "code": -32602, "message": "Block range limit exceeded. See more details at https://docs.chainstack.com/docs/limits#evm-range-limits" }, payload={ "id": 1, "jsonrpc": "2.0", "method": "eth_newFilter", "params": [ { "address": [ "0x8909dc15e40173ff4699343b6eb8132c65e18ec6" ], "topics": [ "0x0d3648bd0f6ba80134a33ba9275ac585d9d315f0ad8355cddefde31afa28d0e9" ] } ] }, code=UNKNOWN_ERROR, version=6.13.1)

Environment

node.js (v12 or newer)

Environment (Other)

No response

Metadata

Metadata

Assignees

Labels

investigateUnder investigation and may be a bug.v6Issues regarding v6

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions