1

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)
const { ethers } = require('ethers');

const provider2 = new ethers.providers.getDefaultProvider('http://127.0.0.1:8545/')
const hrdhatAccountPrivate = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"//owner
const hrdhatAccountPublic = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"//owner

const ERC_20_ABI = require("./erc20_abi.json")
const FORK_MAIN_ABI = [
    "function myTestFunction() pure returns (uint256)",
]

const walletFork = new ethers.Wallet(hrdhatAccountPrivate, provider2)


async function main() {

    const fundxPoolAddr = '0xBbc18b580256A82dC0F9A86152b8B22E7C1C8005'//forking

    const fundxPoolContract = new ethers.Contract(fundxPoolAddr, FORK_MAIN_ABI, provider2)
    const fundxPoolContractWithWallet = fundxPoolContract.connect(hrdhatAccountPublic)//walletFork

    const tx = await fundxPoolContractWithWallet.myTestFunction()//{gasLimit:300000}
    console.log(tx);

}

main()
  .catch((error) => {
    console.error(error);
    process.exitCode = 1;
  });

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.

1 Answers1

1

When I comment/remove the networks parameters in hardhat.config.js and then start the hardhat forking sw with command line just like this: npx hardhat node --fork https://eth-mainnet.alchemyapi.io/v2/<key>, it works well. The problem was about network. I had started the forking network and deployed the contract into it. But in my script I was trying to interact with this contract in the local network.

I very thank nick.tran in KyberNetwork Team for his help.

  • Thanks for sharing -- this was super helpful! I was facing the same issue and 2 things helped. Using chain ID 31337 for the fork and doing the fork like how you mentioned above. – Bob Bob May 29 '23 at 22:10