0

I wanna call my smart contract function using ethersjs , but when i call function i got this error:

Unhandled Runtime Error Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="execution reverted", method="estimateGas", transaction={"from":"0x1204D7F27702d793260Ad5a406dDEE7660d21B61","to":"0xf3BfC4Ce2c8392fc93D24e74193b1d133320126e","data":"0x15364cd50000000000000000000000001204d7f27702d793260ad5a406ddee7660d21b61","accessList":null}, error={"code":-32000,"message":"execution reverted"}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.7.2)

and here is my function codes:

const _faucetContract = new ethers.Contract(faucetContractAddress , faucetAbi , signer )

const getFaucetToken = async () => {
    await _faucetContract.getFaucet(signerAddress)
  }

and that's my function in .sol contract:


    function getFaucet(address user) external {
        require(msg.sender == user, 'you can not sent token for another address');
        require(msg.sender != address(0),"address zero");
        require(timeLastFaucet[msg.sender] + 1 days < block.timestamp,"once a day");

        _mint(user, 50000000000000000000);
        timeLastFaucet[msg.sender] = block.timestamp ;

    }

i think i should set gas fee and gas limit for my function but i don't know how??

can anyone explain how i can define arguments and details that we need to call contract functions?

Thanks a lot.

2 Answers 2

1

Function calls in ethers.js accept an extra 'overrides' parameter’s than can be used to set gas and gas price.

For example to set the gasLimit to 100k

const getFaucetToken = async () => {
  await _faucetContract.getFaucet(signerAddress, { gasLimit: 100000, })
}

Check the documentation for other fields https://docs.ethers.org/v5/api/contract/contract/#Contract-functionsCall.

0
const gasLimit = 21000; // The gas limit for the transaction
const gasPrice = ethers.utils.parseUnits("20.0", "gwei"); // The gas price, in gwei

You should be able to set the gas limit/price using the above.

2
  • Where i should add this two lines?? how? Can u add it in my code?
    – Alireza
    Dec 12, 2022 at 7:04
  • Before you send the transaction
    – VX3
    Dec 30, 2022 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.