Unpredictable gas limit error when sending transaction (in documentation example)
See original GitHub issueHi. I’m following the “connecting to the DAI contract” code on the getting started documentation, and I keep on getting an unpredictable gas limit error.
For reference, here’s my code:
async function interact() {
// https://docs.ethers.io/v5/getting-started/#getting-started--contracts
const daiAddress = "dai.tokens.ethers.eth";
const daiAbi = [
// Some details about the token
"function name() view returns (string)",
"function symbol() view returns (string)",
// Get the account balance
"function balanceOf(address) view returns (uint)",
// Send some of your tokens to someone else
"function transfer(address to, uint amount)",
// An event triggered whenever anyone transfers to someone else
"event Transfer(address indexed from, address indexed to, uint amount)"
];
const provider = ethers.getDefaultProvider(process.env.NETWORK);
const wallet = new ethers.Wallet.fromMnemonic(process.env.METAMASK_PRIVATE_KEY).connect(provider);
const daiContract = new ethers.Contract(daiAddress, daiAbi, provider);
let daiSymbol = await daiContract.symbol();
console.log(daiSymbol);
const erc20_rw = new ethers.Contract(daiAddress, daiAbi, wallet)
// // Each DAI has 18 decimal places
const dai = ethers.utils.parseUnits("1.0", 18);
await erc20_rw.transfer('0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', dai);
}
And every time this runs I get the following error:
Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"reason":"cannot estimate gas; transaction may fail or may require manual gas limit","code":"UNPREDICTABLE_GAS_LIMIT","method":"estimateGas","transaction":{"from":"0xe586C7BE1Ab861B658Ff49849ea27b0aFe0D5782","gasPrice":{"type":"BigNumber","hex":"0x47868c0000"},"to":"0x6B175474E89094C44Da98b954EedeAC495271d0F","data":"0xa9059cbb0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d0000000000000000000000000000000000000000000000000de0b6b3a7640000"}}, tx={"data":"0xa9059cbb0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d0000000000000000000000000000000000000000000000000de0b6b3a7640000","to":{},"from":"0xe586C7BE1Ab861B658Ff49849ea27b0aFe0D5782","gasPrice":{},"nonce":{},"gasLimit":{},"chainId":{}}, code=UNPREDICTABLE_GAS_LIMIT, version=abstract-signer/5.0.14)
at Logger.makeError (/Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/logger/src.ts/index.ts:205:28)
at Logger.throwError (/Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/logger/src.ts/index.ts:217:20)
at /Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/abstract-signer/src.ts/index.ts:213:31
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async Promise.all (index 5) {
reason: 'cannot estimate gas; transaction may fail or may require manual gas limit',
code: 'UNPREDICTABLE_GAS_LIMIT',
error: Error: cannot estimate gas; transaction may fail or may require manual gas limit (method="estimateGas", transaction={"from":"0xe586C7BE1Ab861B658Ff49849ea27b0aFe0D5782","gasPrice":{"type":"BigNumber","hex":"0x47868c0000"},"to":"0x6B175474E89094C44Da98b954EedeAC495271d0F","data":"0xa9059cbb0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d0000000000000000000000000000000000000000000000000de0b6b3a7640000"}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.0.24)
at Logger.makeError (/Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/logger/src.ts/index.ts:205:28)
at Logger.throwError (/Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/logger/src.ts/index.ts:217:20)
at /Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/providers/src.ts/fallback-provider.ts:631:24
at Array.forEach (<anonymous>)
at /Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/providers/src.ts/fallback-provider.ts:613:33
at step (/Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/providers/lib/fallback-provider.js:48:23)
at Object.next (/Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/providers/lib/fallback-provider.js:29:53)
at step (/Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/providers/lib/fallback-provider.js:33:139)
at Object.next (/Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/providers/lib/fallback-provider.js:29:53)
at fulfilled (/Users/alex/Local/Million-Dollar-NFT/NFT-contracts/node_modules/@ethersproject/providers/lib/fallback-provider.js:20:58) {
reason: 'cannot estimate gas; transaction may fail or may require manual gas limit',
code: 'UNPREDICTABLE_GAS_LIMIT',
method: 'estimateGas',
transaction: {
from: '0xe586C7BE1Ab861B658Ff49849ea27b0aFe0D5782',
gasPrice: [BigNumber],
to: '0x6B175474E89094C44Da98b954EedeAC495271d0F',
data: '0xa9059cbb0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d0000000000000000000000000000000000000000000000000de0b6b3a7640000'
}
},
tx: {
data: '0xa9059cbb0000000000000000000000007a250d5630b4cf539739df2c5dacb4c659f2488d0000000000000000000000000000000000000000000000000de0b6b3a7640000',
to: Promise { '0x6B175474E89094C44Da98b954EedeAC495271d0F' },
from: '0xe586C7BE1Ab861B658Ff49849ea27b0aFe0D5782',
gasPrice: Promise { [BigNumber] },
nonce: Promise { 0 },
gasLimit: Promise { <rejected> [Circular] },
chainId: Promise { 1 }
}
How can I fix this?
Issue Analytics
- State:
- Created 3 years ago
- Comments:12 (4 by maintainers)
Top Results From Across the Web
UNPREDICTABLE_GAS_LIMIT · Issue #2248 - GitHub
The ethers library's JsonRpcProvider converts the method to Error: cannot estimate gas; transaction may fail or may require manual gas limit ...
Read more >transaction may fail or may require manual gas limit - Contracts
When i execute this script, i get this following error. { Error: cannot estimate gas; transaction may fail or may require manual gas...
Read more >Why is an 'UNPREDICTABLE_GAS_LIMIT' error thrown when ...
In my case, I got that exact error message if the fund of connected wallet has is zero, The capped gas limit is...
Read more >Error: cannot estimate gas; transaction may fail or may require ...
Try setting a high manual gas limit to see if you can get past the error. To do this you will have to...
Read more >Set gas limit on contract method in ethers.js - Stack Overflow
You can set the gas limit with an object as the last argument, for a simple transfer transaction, you could do something like...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

For more docs you can visit here.
Overriding the gasLimit worked for me 😃 tyvm