I expect I should be able to easily call a function on my contract using this code in a script.js file:

const {ethers} = require("hardhat");

async function main() {
  console.log(`network: ${JSON.stringify(await ethers.provider.getNetwork())}`); //logs chainId is 31337 as expected
  const address = "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512"; //this is the address of the deployed NFTFactory contract
  const validAddress = ethers.utils.getAddress(address);
  console.log(validAddress); // should log the same address if it is valid, and it does
  const nftFactory = await ethers.getContractAt("NFTFactory", address);
  const implementation = await nftFactory.getImpl(); // THIS LINE THROWS

  .then(() => process.exit(0))
  .catch((error) => {

But I get an error

Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="getImpl()", data="0x", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.7.0) ...

Here is the contract/function I'm trying to call (irrelevant lines removed):

pragma solidity ^0.8.2;

contract NFTFactory
    event NFTContractCreated (address nftContract, address owner, string baseURI);

    address public immutable implementation;

        address _implementation
        implementation = _implementation;

    function getImpl() public view returns(address) {
        return implementation;

What might be the issue? If I compile/deploy the same contract in remix, the function executes successfully.

Edit: In case it helps, here is my hardhat.config.js


/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  defaultNetwork: "hardhat",
  networks: {
    localhost: {
      url: "",
      //accounts: provided by hardhat
      chainId: 31337
  namedAccounts: {
    deployer: {
        default: 0, // here this will by default take the first account as deployer
  solidity: "0.8.18"

Edit2/Solution: I was running my script using "yarn hardhat run scripts/script.js". Guided by the comments and proposed answer I confirmed that the contract(s) didn't exist on the network that my script would have been interacting with (from my config file I believe this would have been the 'defaultNetwork' of 'hardhat'). I instead ran "yarn hardhat run --network localhost scripts/script.js" and the function call succeeded. Thanks all for the help.

  • Where is this contract deployed? You only have a local network in your config file.
    – PSS
    Commented Apr 3, 2023 at 1:38
  • Where did you deploy this contract before? Commented Apr 3, 2023 at 6:28
  • It was my understanding that running hardhat scripts causes a new instance of a local blockchain to be spun-up and the contracts deployed to it - in this case chainId 31337. I could be misunderstanding this so will investigate.
    – graemek
    Commented Apr 3, 2023 at 12:21

1 Answer 1


It appears that the address 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 may be incorrect.

I searched for this address across many networks, and the only instance where it was associated with a contract (a ERC20) was found here:


You can also verify by yourself at: https://blockscan.com/address/0xe7f1725e7734ce288f8367e1bb143e90bb3f0512

It functions on Remix because the contract exists there. However, the address you are attempting to call doesn't contain any contracts with the getImpl() function.

  • That's surprising an unrelated contract shares the address, but I got that address from the logs by running 'yarn hardhat node' which displays ""NFTFactory" ... deployed at 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 with 411235 gas". Thus because I thought running the script spins-up the same local blockchain and with the same deployments, that the address stays the same.
    – graemek
    Commented Apr 3, 2023 at 12:28
  • to make sure that your contract is deployed call this method: await provider.getCode("0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512");
    – Adam Boudj
    Commented Apr 3, 2023 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.