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

Feature/Bug, cannot choose not to request chain id on each call. #4903

Open
dlebee opened this issue Dec 18, 2024 · 1 comment
Open

Feature/Bug, cannot choose not to request chain id on each call. #4903

dlebee opened this issue Dec 18, 2024 · 1 comment
Assignees
Labels
investigate Under investigation and may be a bug. v5 Issues regarding legacy-v5

Comments

@dlebee
Copy link

dlebee commented Dec 18, 2024

Ethers Version

5.7.0

Search Terms

getNetwork() skip chain id

Describe the Problem

I want to be able to create a JsonRpcProvider and it not query eth_chainId all the time, but i noticed the method

getNetwork() of the provider always checks for the chain id has potentially changed in the rpc response from the one provided it the constructor.

Could it be possible to add a skip on that condition without me having to do the following.

  rpcProvider.getNetwork = async function(this: ethers.providers.JsonRpcProvider): Promise<any> {
    const network = await this._ready();

    // Make sure we are still connected to the same network; this is
    // only an external call for backends which can have the underlying
    // network change spontaneously
    // we don't want to do this. lol
    //const currentNetwork = await this.detectNetwork();
    // if (network.chainId !== currentNetwork.chainId) {

    //     // We are allowing network changes, things can get complex fast;
    //     // make sure you know what you are doing if you use "any"
    //     if (this.anyNetwork) {
    //         this._network = currentNetwork;

    //         // Reset all internal block number guards and caches
    //         this._lastBlockNumber = -2;
    //         this._fastBlockNumber = null;
    //         this._fastBlockNumberPromise = null;
    //         this._fastQueryDate = 0;
    //         this._emitted.block = -2;
    //         this._maxInternalBlockNumber = -1024;
    //         this._internalBlockNumber = null;

    //         // The "network" event MUST happen before this method resolves
    //         // so any events have a chance to unregister, so we stall an
    //         // additional event loop before returning from /this/ call
    //         this.emit("network", currentNetwork, network);
    //         await stall(0);

    //         return this._network;
    //     }

    //     const error = logger.makeError("underlying network changed", Logger.errors.NETWORK_ERROR, {
    //         event: "changed",
    //         network: network,
    //         detectedNetwork: currentNetwork
    //     });

    //     this.emit("error", error);
    //     throw error;
    // }

    return network;
};
}

Code Snippet

any call example this one.


provider.getGasPrice()

Contract ABI

N/A

Errors

N/A

Environment

node.js (older than v12)

Environment (Other)

all environments behave the same.

@dlebee dlebee added investigate Under investigation and may be a bug. v5 Issues regarding legacy-v5 labels Dec 18, 2024
@dlebee dlebee changed the title Add Bug Title Here Feature/Bug, cannot choose not to request chain id on each call. Dec 18, 2024
@dlebee
Copy link
Author

dlebee commented Dec 18, 2024

in the mean time I use this.

import * as ethers from 'ethers';

export class OptimizedJsonRpcBatchProvider extends ethers.providers.JsonRpcBatchProvider {

    private skipChainIdCheck: boolean = false;

    disableChainIdCheck() {
        this.skipChainIdCheck = true;
    }

    override async getNetwork(): Promise<ethers.ethers.providers.Network> {
        if (this.skipChainIdCheck)
            return await this._ready();

        return await super.getNetwork();
    }
}

export class OptimizedJsonRpcProvider extends ethers.providers.JsonRpcProvider {

    private skipChainIdCheck: boolean = false;

    disableChainIdCheck() {
        this.skipChainIdCheck = true;
    }

    override async getNetwork(): Promise<ethers.ethers.providers.Network> {
        if (this.skipChainIdCheck)
            return await this._ready();

        return await super.getNetwork();
    }
}

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. v5 Issues regarding legacy-v5
Projects
None yet
Development

No branches or pull requests

2 participants