eth_estimateGas - Polygon PoS

Generates and returns an estimate of how much gas is necessary to allow the transaction to complete on Polygon. The transaction will not be added to the blockchain.

Parameters

  • Object - The transaction call object
    • from: DATA, 20 Bytes - (optional) The address the transaction is sent from.
    • to: DATA, 20 Bytes - The address the transaction is directed to.
    • gas: QUANTITY - (optional) Integer of the gas provided for the transaction execution. eth_call consumes zero gas, but this parameter may be needed by some executions. NOTE: this parameter has a cap of 550 million Gwei per request. Reach out to us at [email protected] if you want to increase this limit.
    • gasPrice: QUANTITY - (optional) Integer of the gasPrice used for each paid gas. Note: most of our users (95%+) never set the gasPrice on eth_call.
    • value: QUANTITY - (optional) Integer of the value sent with this transaction
    • data: DATA - (optional) Hash of the method signature and encoded parameters. For details see Ethereum Contract ABI
  • QUANTITY|TAG - integer block number, or the string "latest", "earliest" or "pending", see the default block parameter.

🚧

Note

  • eth_estimateGas will check the sender's balance (to ensure that the sender has enough gas to complete the request). This means that even though the call doesn't consume any gas, the from address must have enough gas to execute the transaction.
  • If no gas is specified geth uses the block gas limit from the pending block as an upper bound. As a result, the returned estimate might not be enough to execute the call/transaction when the amount of actual gas needed is higher than the pending block gas limit.

Returns

QUANTITY - the amount of gas used.

Example

The Alchemy Composer allows you to make a no-code example request via your browser. Try it out above!

Request

curl https://polygon-mainnet.g.alchemy.com/v2/your-api-key \
-X POST \
-H "Content-Type: application/json" \
-d '{"jsonrpc":"2.0","method":"eth_estimateGas","params":[{see above}],"id":1}'
URL: https://polygon-mainnet.g.alchemy.com/v2/your-api-key
RequestType: POST
Body: 
{
    "jsonrpc":"2.0",
    "method":"eth_estimateGas",
    "params":[{see above}],
    "id":1
}
// Setup: npm install alchemy-sdk
// Github: https://github.com/alchemyplatform/alchemy-sdk-js
import { Network, Alchemy, parseEther } from "alchemy-sdk";

// Optional config object, but defaults to demo api-key and eth-mainnet.
const settings = {
  apiKey: "demo", // Replace with your Alchemy API Key.
  network: Network.MATIC_MAINNET, // Replace with your network.
};
const alchemy = new Alchemy(settings);

alchemy.core
  .estimateGas({
    // Wrapped ETH address
    to: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
    // `function deposit() payable`
    data: "0xd0e30db0",
    // 1 ether
    value: parseEther("1.0"),
  })
  .then(console.log);

Result

{
  "id":1,
  "jsonrpc": "2.0",
  "result": "0x5208" // 21000
}

❗️

The below parameter inputs do not work, please reference the section above instead.

Language
URL
ReadMe