In zkEVM, I calculated the gas fee based on gas price (3 GWEI) and gas limit (3M), but the transaction still failed with insufficient funds. What can I do?

Created by Sourajyoti Gupta, Modified on Fri, 28 Jul 2023 at 07:21 PM by Sourajyoti Gupta

The discrepancy between the calculated gas fee and the insufficient funds error could be influenced by factors like network congestion or higher gas prices. To address this, consider increasing the gas price to a higher value when deploying your contracts, especially during periods of high network activity. This ensures your transaction is prioritized and executed successfully.


Additionally, double-check that your address is correctly funded with the expected balance, and confirm that you are specifying both the gas limit and gas price appropriately during contract deployment.


To help you estimate the gas limit and gas price more accurately, here's a code snippet using ethers.js:


// Initialize provider and signer

const provider = new ethers.providers.InfuraProvider(networkName, projectID);

const signer = new ethers.Wallet(pKey, provider);



// Fetch the latest gas price data for Polygon zkEVM Mainnet

const gasFee = await fetchGasPrice();



// Estimate the gas limit for your contract deployment

const estimatedGasLimit = await provider.estimateGas({

    type: 2,

    to: contractAddress,

    from: userAddress,

    nonce: nonce,

    gasLimit: 14_999_999,

    gasFee: gasFee,

    data: iface.encodeFunctionData("function_name", [functionArgs]),

});



// Set the gas price and gas limit during contract deployment

const txn_response = await contract.functionName(functionArgs, {

    gasLimit: estimatedGasLimit,

    nonce: nonce,

    gasFee: gasFee,

});



Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article