Skip to content

Instantly share code, notes, and snippets.

@wjkoh
Created December 12, 2021 09:19
Show Gist options
  • Save wjkoh/badf1d327ebf94f3d66d1cdbeef09892 to your computer and use it in GitHub Desktop.
Save wjkoh/badf1d327ebf94f3d66d1cdbeef09892 to your computer and use it in GitHub Desktop.
Error: cannot estimate gas; transaction may fail or may require manual gas limit
const hre = require("hardhat");
const BATON_CONTRACT_ADDR = "YOUR_BATON_CONTRACT_ADDRESSS";
async function main() {
const Batons = await hre.ethers.getContractFactory("Batons");
const batons = await Batons.attach(BATON_CONTRACT_ADDR);
console.log("Connected to batons at :", BATON_CONTRACT_ADDR);
// Calling read functions consecutively is fine.
await batons.getBatonDonation(3);
await batons.getEvolutionLevel(3);
// Calling WRITE functions consecutively without tx.wait() is NOT okay. You may
// see "Error: cannot estimate gas; transaction may fail or may require
// manual gas limit".
if (await batons.saleState() != 0) {
await batons.stopSale();
}
await batons.startPresale();
await batons.startSale();
// You should add tx.wait() after every write function call. Note that you
// need to wait the wait() call, i.e., `await tx.wait();`.
let tx;
if (await batons.saleState() != 0) {
tx = await batons.stopSale();
await tx.wait();
}
tx = await batons.startPresale();
await tx.wait();
tx = await batons.startSale();
await tx.wait();
}
// We recommend this pattern to be able to use async/await everywhere
// and properly handle errors.
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment