4

I am trying to call a pure function via ethers js and store the data in a variable. Now I supposed that variable to contain the return value from the contract, but instead, that variable contains transaction data, also when I call the function using the frontend button metamask pops up with gas fees. Possible fixes I tried:

  1. Checked the abi file if it stateMutability is pure or not.
  2. Used callStatic but it makes the contract's function name undefined.

Sharing the exact line link https://github.com/Zartaj0/BasicSignature/blob/2b020c3f2901c43340abf250cbe0d1101965692b/Frontend/src/VerifyMessage.js#L29

I am using local hardhat node for deployment. The contract is fine and everything is fine. The problem is, the function call should return the data without transaction from contract function but it creates a transaction. What can be the wrong thing I am doing?

1 Answer 1

1

I think the issue might be that you are creating the smart contract instance using the signer instead of the provider.

contract = new ethers.Contract(contractAddress, abi.abi, signer)

try this:

contract = new ethers.Contract(contractAddress, abi.abi, provider)

In this case you want to use the eth_call method since it's a pure function. You can find more examples in the Chainstack docs | eth_call.

1
  • I tried using the first approach, but the error was: sending a transaction require a signer And the eth_call method is not available in ethers, there is something different that needs the contract address and function data to call the function, which is not very good in my opinion Commented Apr 14, 2023 at 3:39

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.