You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm working with the AdonisJS framework and have embedded the following two routers into it. The AdonisJS application generates a transaction object for swapping coins:
{
to: '0x13f4EA83D0bd40E75C8222255bc855a974568Dd4',
value: 0,
data: '0x42712a670000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000e54883aa311800000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000fc0fbc1b573cf7c815906129c47fe4db515334000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000e09fabb73bd3ade0a17ecc321fd13a19e81ce82000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000bc7d6b50616989655afd682fb42743507003056d' // Ensure this is a valid hex string
}
to is the address of smart router on BSC (As mentioned here: https://developer.pancakeswap.finance/contracts/v3/addresses#smart-router), data is the execution of the contract's function.
I think data field is generated due to call the router contract (Pancake's router that I embedded). Subsequently, I tried to estimate the gas of that transaction using the following code:
const ethers = require('ethers');
// Connect to BSC node (you can use a public RPC or your own)
const provider = new ethers.providers.JsonRpcProvider('https://bsc-dataseed.binance.org/');
// Replace with your private key
const privateKey = 'fe78e112e8a94f339cf7669e6b83322365fd2e2ea71327404509fc5e3484693e';
const wallet = new ethers.Wallet(privateKey, provider);
async function estimateGas(transaction) {
try {
// Get current gas price
const gasPrice = await provider.getGasPrice();
console.log(`Current Gas Price: ${ethers.utils.formatUnits(gasPrice, 'gwei')} Gwei`);
// Estimate gas limit for the transaction
const gasLimit = await provider.estimateGas(transaction);
console.log(`Estimated Gas Limit: ${gasLimit.toString()}`);
// Calculate total transaction fee in BNB
const transactionFee = gasPrice.mul(gasLimit);
console.log(`Transaction Fee in Wei: ${transactionFee.toString()}`);
console.log(`Transaction Fee in BNB: ${ethers.utils.formatEther(transactionFee)} BNB`);
} catch (error) {
console.error('Error estimating gas:', error);
}
}
// Example transaction object (for sending BNB)
const transactionObject = {
to: '0x13f4EA83D0bd40E75C8222255bc855a974568Dd4', // Replace with recipient address
value: 0, // Amount of BNB to send
data: '0x42712a670000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000e54883aa311800000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000fc0fbc1b573cf7c815906129c47fe4db515334000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000e09fabb73bd3ade0a17ecc321fd13a19e81ce82000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000bc7d6b50616989655afd682fb42743507003056d' // Ensure this is a valid hex string
};
// Check if data is valid
if (!/^0x[0-9A-Fa-f]*$/.test(transactionObject.data)) {
throw new Error('Invalid hex string for data');
}
estimateGas(transactionObject);
That is using the same transaction as input for estimateGas() function. But this error thrown:
Current Gas Price: 1.0 Gwei
Error estimating gas: 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={"to":"0x13f4EA83D0bd40E75C8222255bc855a974568Dd4","data":"0x42712a670000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000e54883aa311800000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000fc0fbc1b573cf7c815906129c47fe4db515334000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000e09fabb73bd3ade0a17ecc321fd13a19e81ce82000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000bc7d6b50616989655afd682fb42743507003056d","accessList":null}, error={"reason":"processing response error","code":"SERVER_ERROR","body":"{\"jsonrpc\":\"2.0\",\"id\":46,\"error\":{\"code\":-32000,\"message\":\"execution reverted\"}}","error":{"code":-32000},"requestBody":"{\"method\":\"eth_estimateGas\",\"params\":[{\"to\":\"0x13f4ea83d0bd40e75c8222255bc855a974568dd4\",\"data\":\"0x42712a670000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000e54883aa311800000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000fc0fbc1b573cf7c815906129c47fe4db515334000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000e09fabb73bd3ade0a17ecc321fd13a19e81ce82000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000bc7d6b50616989655afd682fb42743507003056d\"}],\"id\":46,\"jsonrpc\":\"2.0\"}","requestMethod":"POST","url":"https://bsc-dataseed.binance.org/"}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.7.2)
at Logger.makeError (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\logger\lib\index.js:238:21)
at checkError (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:122:20)
at JsonRpcProvider.<anonymous> (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:751:47)
at step (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:48:23)
at Object.throw (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:29:53)
at rejected (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:21:65)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
reason: 'execution reverted',
code: 'UNPREDICTABLE_GAS_LIMIT',
transaction: {
to: '0x13f4EA83D0bd40E75C8222255bc855a974568Dd4',
data: '0x42712a670000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000e54883aa311800000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000fc0fbc1b573cf7c815906129c47fe4db515334000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000e09fabb73bd3ade0a17ecc321fd13a19e81ce82000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000bc7d6b50616989655afd682fb42743507003056d',
accessList: null
},
error: Error: processing response error (body="{\"jsonrpc\":\"2.0\",\"id\":46,\"error\":{\"code\":-32000,\"message\":\"execution reverted\"}}", error={"code":-32000}, requestBody="{\"method\":\"eth_estimateG883aa311800000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000fc0fbc1b573cf7c815906129c47fe4db515334000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000e09fabb73bd3ade0a17ecc321fd13a19e81ce82000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000bc7d6b50616989655afd682fb42743507003056d\"}],\"id\":46,\"jsonrpc\":\"2.0\"}", requestMethod="POST", url="https://bsc-dataseed.binance.org/", code=SERVER_ERROR, version=web/5.7.1)
at Logger.makeError (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\logger\lib\index.js:238:21)
at Logger.throwError (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\logger\lib\index.js:247:20)
at C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\web\lib\index.js:313:32
at step (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\web\lib\index.js:33:23)
at Object.next (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\web\lib\index.js:14:53)
at fulfilled (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\web\lib\index.js:5:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
reason: 'processing response error',
code: 'SERVER_ERROR',
body: '{"jsonrpc":"2.0","id":46,"error":{"code":-32000,"message":"execution reverted"}}',
error: Error: execution reverted
at getResult (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\providers\lib\json-rpc-provider.js:191:21)
at processJsonFunc (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\web\lib\index.js:356:22)
at C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\web\lib\index.js:288:46
at step (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\web\lib\index.js:33:23)
at Object.next (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\web\lib\index.js:14:53)
at fulfilled (C:\adonisjs\backapp.dex\back-master\node_modules\@ethersproject\web\lib\index.js:5:58)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: -32000,
data: undefined
},
requestBody: '{"method":"eth_estimateGas","params":[{"to":"0x13f4ea83d0bd40e75c8222255bc855a974568dd4","data":"0x42712a670000000000000000000000000000000000000000000000000de0b6b3a76400000000000000000000000000000000000000000000000000000e54883aa311800000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000fc0fbc1b573cf7c815906129c47fe4db515334000000000000000000000000000000000000000000000000000000000000000030000000000000000000000000e09fabb73bd3ade0a17ecc321fd13a19e81ce82000000000000000000000000bb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c000000000000000000000000bc7d6b50616989655afd682fb42743507003056d"}],"id":46,"jsonrpc":"2.0"}',
requestMethod: 'POST',
url: 'https://bsc-dataseed.binance.org/'
}
}
I tried that for Ethereum mainnet and the same error thrown!
As I researched, method provider.estimateGas() can't calculate the gas, because the transaction (call contract function) is too complex. I don't know why it is complex, that is just a guess!
What is the problem? Where did I make a mistake and how can I solve it?
I'm working with the AdonisJS framework and have embedded the following two routers into it. The AdonisJS application generates a transaction object for swapping coins:
to
is the address of smart router on BSC (As mentioned here: https://developer.pancakeswap.finance/contracts/v3/addresses#smart-router),data
is the execution of the contract's function.I think
data
field is generated due to call the router contract (Pancake's router that I embedded). Subsequently, I tried to estimate the gas of that transaction using the following code:That is using the same transaction as input for
estimateGas()
function. But this error thrown:I tried that for Ethereum mainnet and the same error thrown!
As I researched, method
provider.estimateGas()
can't calculate the gas, because the transaction (call contract function) is too complex. I don't know why it is complex, that is just a guess!What is the problem? Where did I make a mistake and how can I solve it?
The two embedded routers:
Router ABI:
Router V2 ABI:
The text was updated successfully, but these errors were encountered: