0

I am attempting to implement a basic token swap in which someone sends Ether to the smart contract and receives Governor Token (GT) in return. The contract is able to send GT however, when sending ETH back to the contract I get the following error message:

Error: cannot estimate gas; transaction may fail or may require manual gas limit

Can someone please tell me how to solve this?

Here is the function from which the error occurs:

    function swapETHForGT(uint256 amount) external payable {
        uint256 tokenAmount = amount * rate * 1000000000000000000;
        require(GT.balanceOf(Governor) >= tokenAmount);

        GT.transferFrom(payable(Governor), payable(msg.sender), tokenAmount);

        (bool sent, bytes memory data) = payable(address(this)).call{value: amount * 1000000000000000000}("");
        require(sent, "tx failed");
    }

The function fails when calling ETH. However, the transfer from function works perfectly.

Here is the entirety of the error message:

Uncaught (in promise) Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="Error: VM Exception while processing transaction: reverted with reason string 'tx failed'", method="estimateGas", transaction={"from":"0xdD2FD4581271e230360230F9337D5c0430Bf44C0","to":"0x3Aa5ebB10DC797CAC828524e59A333d0A371443c","data":"0xdbc151bc0000000000000000000000000000000000000000000000000000000000000005","accessList":null}, error={"code":-32603,"message":"Internal JSON-RPC error.","data":{"code":-32603,"message":"Error: VM Exception while processing transaction: reverted with reason string 'tx failed'","data":{"message":"Error: VM Exception while processing transaction: reverted with reason string 'tx 

etc

Terminal output is:

 Error: VM Exception while processing transaction: reverted with reason string 'tx failed'
      at DAO.swapETHForGT (contracts/DAO.sol:170)

1 Answer 1

1

You can try the following:

  • use the correct contract address
  • add the {from: <myAddress>} in order of it to working inside the .call()
  • check and change the network you account is connected to on the metamask (if used).
  • manually set the gasLimit higher than your gas
2
  • How can I set the gasLimit higher for an individual function, I thought setting gasLimit in the hardhat config sets it for contract deployment, am I wrong? Commented Oct 12, 2022 at 3:30
  • Yes, setting gasLimit in the hardhat config sets it for contract deployment, but if a single function expends more gas resulting in over the gasLimit, the transaction will still fail. Commented Oct 12, 2022 at 4:43

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.