0

I have a Hardhat project with a sample storage contract. There are 2 functions:

  • Public view function
  • Public write function (creates a transaction)

My Hardhat project forks an Ethereum mainnet public node from Quicknode in order to simulate the usage in Ethereum.

I follow these steps:

  • I run my project, forking the node: npx hardhat node
  • I run a simple web server with index.html (with Ethers.js code connecting to the local network, forking mainnet, and to my contract).
  • I open my browser, with Metamask installed, in ```localhost:8080``
  • I can execute the write function storeNum() with Metamask, generating a transaction. OK
  • When I try to execute the public view function retrieveNum() (only read), I got the following ERROR:
ethers-5.2.umd.min.js:1 Uncaught (in promise) Error: call revert
exception (method="retrieveNum()", errorArgs=null, errorName=null,
errorSignature=null, reason=null, code=CALL_EXCEPTION,
version=abi/5.2.0)

In the terminal where hardhat node runs, I can see the following error:

eth_call
  WARNING: Calling an account which is not a contract
  From:            0x...
  To:              0x...

Something strange:

The interesting point is, that everything works fine if I run automated tests, connecting perfectly to the contract and reading the view function with no problem.

I have created this repo where you can see all the code: https://github.com/Alexander-Herranz/hardhat-network-fork/

2 Answers 2

0

you need to show hardhat.config.js but I think you aren't using the correct network.

try: npx hardhat --network <network_name> node

where network_name is from hardhat.config.js

3
  • You can see the hardhat.config file in the github repo: github.com/Alexander-Herranz/hardhat-network-fork/blob/main/… I'm using hardhat node as you can see, so there is no need to specify hardhat network. Commented Sep 27, 2023 at 21:02
  • The point is: If it works for writing transactions, why not for reading view functions? Cannot be a problem with the node. Commented Sep 27, 2023 at 21:05
  • that's what it says in the docs - hardhat.org/hardhat-network/docs/guides/forking-other-networks but perhaps in the config is enough. but it says the address is not a contract, meaning there is no code there. so either the address is wrong, wrong chain or it didn't fork properly. (note the storeNum() could succeed because there is no code and is treated just like value transfer)
    – high_byte
    Commented Sep 27, 2023 at 23:31
0

You are trying to use a signer to make the view call. It won't work. Signer is an abstraction of an account and it could be used to sign transactions and to send signed transactions for execution. In order to make view calls, use the provider, not the signer.

In your code you could do this: const storageContractForView = new ethers.Contract(storageAddr, abi_storage, provider); and use that contract instance to make the view call. That should work if your wallet is connected.

Or you can use JsonRpcProvider of some kind and make view calls without a connected wallet.

1

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.