Skip to main content
On peaq and other EVM-compatible networks, transaction execution costs are measured in gas. Estimate the gas a transaction needs before you send it, so you neither overpay nor run out of gas mid-execution. The eth_estimateGas RPC method returns that estimate by simulating the transaction on a node, without broadcasting it.

Prerequisites

  1. An EVM-compatible RPC endpoint:
  2. The transaction data you are sending: from, to, data if you are calling a contract, and value if you are sending PEAQ. You do not need the gas amount upfront, that is what you are estimating.
  3. A JSON-RPC client: curl, web3.js, ethers.js, or any library that speaks JSON-RPC. The example below uses curl.

Instructions

1. Prepare the JSON-RPC request

Use the eth_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

Using curl from your terminal, with your own RPC URL:

3. Read the response

A successful response looks like this:
The 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:
Add a buffer of 10 to 20 percent on top of the estimate. Contract state can change between the estimate and the moment the transaction is mined, and the real cost can then be higher.

5. Use the estimated gas in your transaction

Set the gas 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.