Hi. 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?
Does you account have DAI? In order to send a DAI from your account to another, you need to have DAI to send.
The next problem in that code example might be using "dai.tokens.ethers.eth" as your token address. I have only configured the mainnet ENS name for these shortcuts. Can you try using the entire DAI address?
I replaced the DAI ENS + the ENS in the transfer() function, and I'm still getting the same error :(
Hi got the same issue!!
--------CODE:---------
const tx = await router.swapExactTokensForTokens(
amountIn,
amountOutMin,
[tokenIn, tokenOut],
addresses.recipient,
Date.now() + 1000 * 60 * 10, //10 minutes
);
const receipt = await tx.wait();
console.log('Transaction receipt');
console.log(receipt);```
-----ERROR Restponse---------
```
bsc-bot_1 | 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","error":{"code":3,"response":"{\"jsonrpc\":\"2.0\",\"id\":10,\"error\":{\"code\":3,\"data\":\"0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000245472616e7366657248656c7065723a205452414e534645525f46524f4d5f4641494c454400000000000000000000000000000000000000000000000000000000\",\"message\":\"execution reverted: TransferHelper: TRANSFER_FROM_FAILED\"}}"},"method":"estimateGas","transaction":{"from":"0x09F574b25F6Ce593B26fFce3058E005c2c7683E9","gasPrice":{"type":"BigNumber","hex":"0x3b9aca00"},"to":"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D","data":"0x38ed1739000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000000000000000000000000000e55936dd6b926c680600000000000000000000000000000000000000000000000000000000000000a000000000000000000000000009f574b25f6ce593b26ffce3058e005c2c7683e9000000000000000000000000000000000000000000000000000001788481eda30000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c778417e063141139fce010982780140aa0cd5ab000000000000000000000000f26c66abbef5ed99eeec161e3fb1a0428d764b47"}}, tx={"data":"0x38ed1739000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000000000000000000000000000e55936dd6b926c680600000000000000000000000000000000000000000000000000000000000000a000000000000000000000000009f574b25f6ce593b26ffce3058e005c2c7683e9000000000000000000000000000000000000000000000000000001788481eda30000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c778417e063141139fce010982780140aa0cd5ab000000000000000000000000f26c66abbef5ed99eeec161e3fb1a0428d764b47","to":{},"from":"0x09F574b25F6Ce593B26fFce3058E005c2c7683E9","gasPrice":{},"nonce":{},"gasLimit":{},"chainId":{}}, code=UNPREDICTABLE_GAS_LIMIT, version=abstract-signer/5.0.14)
bsc-bot_1 | at Logger.makeError (/home/node/app/node_modules/@ethersproject/logger/lib/index.js:180:21)
bsc-bot_1 | at Logger.throwError (/home/node/app/node_modules/@ethersproject/logger/lib/index.js:189:20)
bsc-bot_1 | at /home/node/app/node_modules/@ethersproject/abstract-signer/lib/index.js:263:47
bsc-bot_1 | at processTicksAndRejections (internal/process/task_queues.js:97:5)
bsc-bot_1 | at async Promise.all (index 5) {
bsc-bot_1 | reason: 'cannot estimate gas; transaction may fail or may require manual gas limit',
bsc-bot_1 | code: 'UNPREDICTABLE_GAS_LIMIT',
bsc-bot_1 | error: Error: cannot estimate gas; transaction may fail or may require manual gas limit (error={"code":3,"response":"{\"jsonrpc\":\"2.0\",\"id\":10,\"error\":{\"code\":3,\"data\":\"0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000245472616e7366657248656c7065723a205452414e534645525f46524f4d5f4641494c454400000000000000000000000000000000000000000000000000000000\",\"message\":\"execution reverted: TransferHelper: TRANSFER_FROM_FAILED\"}}"}, method="estimateGas", transaction={"from":"0x09F574b25F6Ce593B26fFce3058E005c2c7683E9","gasPrice":{"type":"BigNumber","hex":"0x3b9aca00"},"to":"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D","data":"0x38ed1739000000000000000000000000000000000000000000000000016345785d8a00000000000000000000000000000000000000000000000000e55936dd6b926c680600000000000000000000000000000000000000000000000000000000000000a000000000000000000000000009f574b25f6ce593b26ffce3058e005c2c7683e9000000000000000000000000000000000000000000000000000001788481eda30000000000000000000000000000000000000000000000000000000000000002000000000000000000000000c778417e063141139fce010982780140aa0cd5ab000000000000000000000000f26c66abbef5ed99eeec161e3fb1a0428d764b47"}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.0.24)
I never did end up figuring out how to fix the issue. I just pivoted to using Web3.js even though ethers.js was much nicer to use :(
Have you tried setting a manual gasLimit? That means the call to estimate gas is failing. It could be because the transaction will fail (e.g. a require(balance > value)), but it could just be the node cant compute it for you (sometimes complex operations confuse it).
Try overriding gasLimit?
While you're trying, I'd also like to suggest including the following line, which may help figure out the problem.
const erc20_rw = new ethers.Contract(daiAddress, daiAbi, wallet)
// add this following both lines
const balance = await erc20_rw.balanceOf(wallet.address);
console.log('balance: ' + ethers.utils.formatUnits(balance, 18));
// // Each DAI has 18 decimal places
const dai = ethers.utils.parseUnits("1.0", 18);
await erc20_rw.transfer('0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', dai);
}
let us know if you can successfully see the balance printed.
Have you tried setting a manual gasLimit? That means the call to estimate gas is failing. It could be because the transaction will fail (e.g. a
require(balance > value)), but it could just be the node cant compute it for you (sometimes complex operations confuse it).Try overriding gasLimit?
how do you manually set gasLimit
await erc20_rw.transfer('0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D', dai, {
gasLimit: 1000000
});
For more docs you can visit here.
Overriding the gasLimit worked for me :) tyvm
I think @zemse answered the OP, so I鈥檒l close this now, but if I鈥檓 mistaken, please re-open.
Thanks! :)