1

I have some bulk calculations in a contract, so I used a function that compare the gas left with a constant value and save the current state. At the next time it continues the calculations until it will be finished or will pause again.

I used such a code:

function calculate(...) public {
    // Restore the previous state
    ...
    while (...)
        if (gasleft() < minimumGasAmount) {
            // Save the current state
            ...
            break;
        }
        // Do calculations
        ...
    }
}

For a better execution more I select the gas limit for a transaction, less times I need to run transactions.

So I need to set transaction gaslimit to its maximum value.

Unfortunately in Metamask wallet we cannot set gaslimit greater than 7,900,000. What is this limitation and how does it come here?

Is there any maximum for transaction gas limit except the block limit?

(Block gas limit for Ethereum and Polygon is 30,000,000)

1 Answer 1

1

The maximum gas limit for a transaction on Ethereum is ultimately determined by the block gas limit (i.e., currently 30 million gas units as you've mentioned), as transactions must fit within the block gas limit to be included in a block. However, within the constraints of the block gas limit, there is technically no specific maximum gas limit for an individual transaction set by the protocol itself.

The Ethereum protocol allows users to specify any gas limit for their transactions, as long as it does not exceed the block gas limit. Therefore, users can set a gas limit based on their specific transaction requirements, such as the complexity of the transaction or the amount of computation it requires.

However, it's important to note that setting excessively high gas limits can lead to increased transaction costs and may also increase the likelihood of encountering network congestion issues. Additionally, transactions with very high gas limits may face challenges in getting included in blocks during periods of high network activity.

Wallets and interfaces like MetaMask often impose their own limits on the gas limit that users can set for transactions, as a safety measure to prevent users from accidentally setting gas limits that are too high. These limits are typically well below the block gas limit to reduce the risk of failed transactions or out-of-gas errors.

If you would like to set the transaction gasLimit to its maximum value, then you can use any library like ethers.js, web3.js, web3.py, etc. to achieve that.

Here's one example that I did using ethers.js, in which it's transferring 1 ETH (can be 1 MATIC, 1 BNB, etc. depending on the network) from one account to another, having the gasLimit specified as 30000000:

const { ethers } = require("ethers");

// Connect to an Ethereum provider
const provider = new ethers.providers.JsonRpcProvider("YOUR_RPC_URL");

// Define your wallet and connect it to the provider
const privateKey = "YOUR_PRIVATE_KEY";
const wallet = new ethers.Wallet(privateKey, provider);

// Specify the recipient address and amount
const recipientAddress = "RECIPIENT_ADDRESS";
const amount = ethers.utils.parseEther("1"); // Example: sending 1 Ether

// Define the transaction parameters
const txParams = {
    to: recipientAddress,
    value: amount,
    gasLimit: 30000000, // Set gas limit to 30 million gas units
};

// Create and sign the transaction
const signedTx = await wallet.sendTransaction(txParams);

// Send the signed transaction
const txReceipt = await signedTx.wait();
console.log("Transaction hash:", txReceipt.transactionHash);

Here's the generated transaction hash for the same on Polygon-Mumbai testnet:

0x653943773852c2a91e4290b2fe9f4b610b99ece0db2ee4b32ba4d590faface65

You can see the gas limit being passed as 30,000,000 (i.e., 30 million gas units):

enter image description here

PS, If I'm passing 1 gas unit above that (i.e., 30,000,001), the transaction is getting failed (not included in block) with this error:

{"jsonrpc":"2.0","id":51,"error":{"code":-32000,"message":"exceeds block gas limit"}}

Similarly, you can call the calculate() function of your contract with the maximum gasLimit like this:

const { ethers } = require("ethers");

// Connect to an Ethereum provider
const provider = new ethers.providers.JsonRpcProvider("YOUR_RPC_URL");

// Define your wallet and connect it to the provider
const privateKey = "YOUR_PRIVATE_KEY";
const wallet = new ethers.Wallet(privateKey, provider);

// Address of the deployed contract
const contractAddress = "CONTRACT_ADDRESS";

// ABI (Application Binary Interface) of the contract
const contractABI = [
    // Include the ABI of the calculate function if needed
];

// Create a contract instance
const contract = new ethers.Contract(contractAddress, contractABI, wallet);

// Specify the gas limit
const gasLimit = 30000000; // 30 million gas units

// Call the calculate() function of the contract
const txResponse = await contract.calculate({ gasLimit: gasLimit });

// Wait for the transaction to be mined
const txReceipt = await txResponse.wait();
console.log("Transaction hash:", txReceipt.transactionHash);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.