eth_estimateGas RPC method returns that estimate by simulating the transaction on a node, without broadcasting it.
Prerequisites
- An EVM-compatible RPC endpoint:
-
The transaction data you are sending:
from,to,dataif you are calling a contract, andvalueif you are sending PEAQ. You do not need the gas amount upfront, that is what you are estimating. -
A JSON-RPC client:
curl,web3.js,ethers.js, or any library that speaks JSON-RPC. The example below usescurl.
Instructions
1. Prepare the JSON-RPC request
Use theeth_estimateGas method. The parameters describe the transaction you intend to send. Addresses and values are hex. Omit value or set it to "0x0" when you are not sending PEAQ.
2. Send the request to the RPC endpoint
Usingcurl from your terminal, with your own RPC URL:
3. Read the response
A successful response looks like this:result field is the estimated gas limit as a hex string. 0x5208 is 21000 in decimal, the cost of a simple transfer.
4. Convert and use the returned value
Parse the hex string into an integer. In JavaScript:5. Use the estimated gas in your transaction
Set thegas field of the final transaction to the estimate plus your buffer. You pay for the gas the transaction actually uses; the gas limit times the gas price (base fee plus priority fee) is the most it can cost.
