How to call function of contract deployed to hardhat forking mainnet

Hi everyone. I am trying to use my own contract's functions deployed to hardhat forking mainnet. To do that I have some steps like this:

  • I added the forking configuration in hardhat.config.js
networks: {
  hardhat: {
    forking: {
      url: "https://eth-mainnet.alchemyapi.io/v2/<key>",
    }
  }
}
  • I ran npx hardhat node in a terminal to start rpc server.
  • I deployed the contract by using hardhat forking as network. So I took the deployed contract address. I used the network parameter as --network hardhat Then it gave me deployed contract address. This address I guess is in the mainnet forking.
  • I wrote a js file to interact with this contract and call its functions.(For testing I added a simple pure function in contract, returning a uint256)

But when I try to run this script (to call contract function), it throws an error.

Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="myTestFunction()", data="0x", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.6.3)
    at Logger.makeError (C:\Users\***\node_modules\@ethersproject\logger\lib\index.js:233:21)
    at Logger.throwError (C:\Users\***\node_modules\@ethersproject\logger\lib\index.js:242:20)
    at Interface.decodeFunctionResult (C:\Users\***\node_modules\@ethersproject\abi\lib\interface.js:388:23)
    at Contract.<anonymous> (C:\Users\***\node_modules\@ethersproject\contracts\lib\index.js:395:56)
    at step (C:\Users\***\node_modules\@ethersproject\contracts\lib\index.js:48:23)
    at Object.next (C:\Users\***\node_modules\@ethersproject\contracts\lib\index.js:29:53)
    at fulfilled (C:\Users\***\node_modules\@ethersproject\contracts\lib\index.js:20:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  reason: null,
  code: 'CALL_EXCEPTION',
  method: 'myTestFunction()',
  data: '0x',
  errorArgs: null,
  errorName: null,
  errorSignature: null,
  address: '0xB9d9e972100a1dD01cd441774b45b5821e136043',
  args: [],
  transaction: {
    data: '0xd100387c',
    to: '0xB9d9e972100a1dD01cd441774b45b5821e136043',
    from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266'
  }
}

When I deploy to --network goerli, it works well. But in --network hardhat it occurs an error. How can I solve this?

I tried to deploy my contract into the goerli testnet, then I called this contract's function and it worked. But for hardhat forking mainnet, it isn't deployed properly or I am doing something wrong during calling the contract's function. Otherwise it wouldn't work in goerli.

If possible, you can paste the code of the function myTestFunction, and maybe you can use console.log in the contract to see what happened: console.log | Hardhat Network

function myTestFunction() public pure returns(uint256) {
        return 55;
    }

Your function looks like simple enough, maybe you can share the full project, then I can have a test.

I have found a solution for this issue. Explaining is in the stackoverflow here. Thanks..