hardhat
Version:
Hardhat is an extensible developer tool that helps smart contract developers increase productivity by reliably bringing together the tools they want.
41 lines • 1.52 kB
JavaScript
import { HardhatError } from "@nomicfoundation/hardhat-errors";
import { ChainId } from "./chain-id.js";
/**
* This class validates that the current provider's chain ID matches
* an expected value. If the actual chain ID differs from the expected one, it throws a
* HardhatError to signal a network configuration mismatch. Once validated, further checks
* are skipped to avoid redundant validations.
*/
export class ChainIdValidatorHandler extends ChainId {
#expectedChainId;
#alreadyValidated = false;
constructor(provider, expectedChainId) {
super(provider);
this.#expectedChainId = expectedChainId;
}
isSupportedMethod(jsonRpcRequest) {
if (this.#alreadyValidated) {
return false;
}
if (jsonRpcRequest.method === "eth_chainId" ||
jsonRpcRequest.method === "net_version") {
return false;
}
return true;
}
async handle(jsonRpcRequest) {
if (!this.isSupportedMethod(jsonRpcRequest)) {
return jsonRpcRequest;
}
const actualChainId = await this.getChainId();
if (actualChainId !== this.#expectedChainId) {
throw new HardhatError(HardhatError.ERRORS.CORE.NETWORK.INVALID_GLOBAL_CHAIN_ID, {
configChainId: this.#expectedChainId,
connectionChainId: actualChainId,
});
}
this.#alreadyValidated = true;
return jsonRpcRequest;
}
}
//# sourceMappingURL=chain-id-handler.js.map