I am trying to use a method of a contract on the test network (ropsten), but it fails due to this error:
reason: 'cannot estimate gas; transaction may fail or may require manual gas limit', code: 'UNPREDICTABLE_GAS_LIMIT'
I created an instance of the smart contract and wanted to call it's register method:
const registrationContract = new ethers.Contract(ADDRESS, abi, signer);
const hashedDomain = utils.keccak256(utils.toUtf8Bytes(domain));
const register = await registrationContract.register(hashedDomain, walletAddress);
Does ethers.js provide a function to set the limit on the contract? Or can this be done otherwise? I didn't find one in the documentation.
You can set the gas limit with an object as the last argument, for a simple transfer transaction, you could do something like this:
const tx = {
to: toAddress,
value: ethers.utils.parseEther(value),
gasLimit: 50000,
nonce: nonce || undefined,
};
await signer.sendTransaction(tx);
If you are doing a transaction to a smart contract, the idea is the same, but make sure you set the last parameter in one of your abi methods, example:
const tx = await contract.safeTransferFrom(from, to, tokenId, amount, [], {
gasLimit: 100000,
nonce: nonce || undefined,
});
This can fix the UNPREDICTABLE_GAS_LIMIT error, since informing it manually the ethers skips a rpc method call to the provider requesting the calculated gas_limit.