1

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
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error)
    process.exit(1)
  })

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);

    string public constant NFT_CONTRACT_CREATION = "NFT_CONTRACT_CREATION";
    address public immutable implementation;

    constructor(
        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

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();
require("hardhat-deploy");

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  defaultNetwork: "hardhat",
  networks: {
    localhost: {
      url: "http://127.0.0.1:8545/",
      //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.

3
  • Where is this contract deployed? You only have a local network in your config file.
    – PSS
    Apr 3 at 1:38
  • Where did you deploy this contract before? Apr 3 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
    Apr 3 at 12:21

1 Answer 1

0

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:

https://testnet.bttcscan.com/token/0xe7f1725e7734ce288f8367e1bb143e90bb3f0512#code

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.

2
  • 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
    Apr 3 at 12:28
  • to make sure that your contract is deployed call this method: await provider.getCode("0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512");
    – Adam Boudj
    Apr 3 at 14:09

Your Answer

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

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