# eth_estimateGas

> For the complete documentation index, see [llms.txt](/docs/llms.txt).

POST https://scroll-mainnet.g.alchemy.com/v2/{apiKey}

Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.

Reference: https://www.alchemy.com/docs/chains/scroll/scroll-api-endpoints/eth-estimate-gas

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| Transaction | object | Yes | The transaction object for which to estimate gas usage. |
| Block | string or enum | No | The block number or tag at which to estimate gas usage. Defaults to `'latest'` if not provided. |

## Result

**Gas used** (string): The estimated amount of gas required for the transaction, as a hexadecimal string.

## Example

### Request

```json
{
  "jsonrpc": "2.0",
  "method": "eth_estimateGas",
  "params": [
    {
      "from": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
      "to": "0x44aa93095d6749a706051658b970b941c72c1d53",
      "value": "0x1"
    }
  ],
  "id": 1
}
```

### Response

```json
{
  "jsonrpc": "2.0",
  "result": "0x5208",
  "id": 1
}
```

## Code Examples

### cURL

```bash
curl --request POST \
  --url https://scroll-mainnet.g.alchemy.com/v2/docs-demo \
  --header 'Content-Type: application/json' \
  --data '{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "eth_estimateGas",
  "params": [
    {
      "from": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
      "to": "0x44aa93095d6749a706051658b970b941c72c1d53",
      "value": "0x1"
    },
    "string"
  ]
}'
```

### JavaScript

```javascript
const options = {
  method: 'POST',
  headers: {'Content-Type': 'application/json'},
  body: JSON.stringify({
    jsonrpc: '2.0',
    id: 1,
    method: 'eth_estimateGas',
    params: [
      {
        from: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045',
        to: '0x44aa93095d6749a706051658b970b941c72c1d53',
        value: '0x1'
      },
      'string'
    ]
  })
};

fetch('https://scroll-mainnet.g.alchemy.com/v2/docs-demo', options)
  .then(res => res.json())
  .then(res => console.log(res))
  .catch(err => console.error(err));
```

### Python

```python
import requests

url = "https://scroll-mainnet.g.alchemy.com/v2/docs-demo"

payload = {
    "jsonrpc": "2.0",
    "id": 1,
    "method": "eth_estimateGas",
    "params": [
        {
            "from": "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
            "to": "0x44aa93095d6749a706051658b970b941c72c1d53",
            "value": "0x1"
        },
        "string"
    ]
}
headers = {"Content-Type": "application/json"}

response = requests.post(url, json=payload, headers=headers)

print(response.text)
```

### Go

```go
package main

import (
	"fmt"
	"strings"
	"net/http"
	"io"
)

func main() {

	url := "https://scroll-mainnet.g.alchemy.com/v2/docs-demo"

	payload := strings.NewReader("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"eth_estimateGas\",\n  \"params\": [\n    {\n      \"from\": \"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045\",\n      \"to\": \"0x44aa93095d6749a706051658b970b941c72c1d53\",\n      \"value\": \"0x1\"\n    },\n    \"string\"\n  ]\n}")

	req, _ := http.NewRequest("POST", url, payload)

	req.Header.Add("Content-Type", "application/json")

	res, _ := http.DefaultClient.Do(req)

	defer res.Body.Close()
	body, _ := io.ReadAll(res.Body)

	fmt.Println(string(body))

}
```

### Java

```java
HttpResponse<String> response = Unirest.post("https://scroll-mainnet.g.alchemy.com/v2/docs-demo")
  .header("Content-Type", "application/json")
  .body("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"eth_estimateGas\",\n  \"params\": [\n    {\n      \"from\": \"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045\",\n      \"to\": \"0x44aa93095d6749a706051658b970b941c72c1d53\",\n      \"value\": \"0x1\"\n    },\n    \"string\"\n  ]\n}")
  .asString();
```

### C#

```csharp
using RestSharp;


var options = new RestClientOptions("https://scroll-mainnet.g.alchemy.com/v2/docs-demo");
var client = new RestClient(options);
var request = new RestRequest("");
request.AddJsonBody("{\n  \"jsonrpc\": \"2.0\",\n  \"id\": 1,\n  \"method\": \"eth_estimateGas\",\n  \"params\": [\n    {\n      \"from\": \"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045\",\n      \"to\": \"0x44aa93095d6749a706051658b970b941c72c1d53\",\n      \"value\": \"0x1\"\n    },\n    \"string\"\n  ]\n}", false);
var response = await client.PostAsync(request);

Console.WriteLine("{0}", response.Content);

```


## OpenRPC Method Specification

```yaml
name: eth_estimateGas
description: Generates and returns an estimate of how much gas is necessary to allow the transaction to complete.
params:
  - name: Transaction
    required: true
    description: The transaction object for which to estimate gas usage.
    schema:
      type: object
      title: Transaction object generic to all types
      description: Parameters describing a transaction/message call.
      additionalProperties: false
      properties:
        type:
          title: type
          description: Transaction type byte (per EIP-2718), e.g., 0x0 (legacy), 0x1 (EIP-2930), 0x2 (EIP-1559), 0x3 (EIP-4844).
          type: string
          pattern: ^0x([0-9a-fA-F]?){1,2}$
        nonce:
          title: nonce
          description: Number of prior transactions sent from the sender account.
          type: string
          pattern: ^0x([1-9a-f]+[0-9a-f]*|0)$
        to:
          title: to address
          description: Recipient of the call. Use null to indicate contract creation.
          oneOf:
            - title: Contract Creation (null)
              description: Null to create a contract; the address is derived from sender and nonce.
              type: 'null'
            - title: Address
              type: string
              pattern: ^0x[0-9a-fA-F]{40}$
              description: 20-byte address of the recipient account or contract.
        from:
          title: from address
          type: string
          pattern: ^0x[0-9a-fA-F]{40}$
          description: 20-byte address of the sender (msg.sender).
        gas:
          title: gas limit
          description: Maximum gas provided for execution.
          type: string
          pattern: ^0x([1-9a-f]+[0-9a-f]*|0)$
        value:
          title: value
          description: Amount of Ether to transfer with the call, in wei.
          type: string
          pattern: ^0x([1-9a-f]+[0-9a-f]*|0)$
        input:
          title: input data
          description: Calldata for the call (hex-encoded bytes).
          type: string
          pattern: ^0x[0-9a-f]*$
        gasPrice:
          title: gas price
          description: The gas price willing to be paid by the sender in wei
          type: string
          pattern: ^0x([1-9a-f]+[0-9a-f]*|0)$
        maxPriorityFeePerGas:
          title: max priority fee per gas
          description: Maximum fee per gas the sender is willing to pay to miners in wei
          type: string
          pattern: ^0x([1-9a-f]+[0-9a-f]*|0)$
        maxFeePerGas:
          title: max fee per gas
          description: The maximum total fee per gas the sender is willing to pay (includes the network / base fee and miner / priority fee) in wei
          type: string
          pattern: ^0x([1-9a-f]+[0-9a-f]*|0)$
        maxFeePerBlobGas:
          title: max fee per blob gas
          description: The maximum total fee per gas the sender is willing to pay for blob gas in wei
          type: string
          pattern: ^0x([1-9a-f]+[0-9a-f]*|0)$
        accessList:
          title: accessList
          description: EIP-2930 access list of pre-touched addresses and storage keys.
          type: array
          items:
            title: Access list entry
            type: object
            additionalProperties: false
            properties:
              address:
                title: hex encoded address
                type: string
                pattern: ^0x[0-9a-fA-F]{40}$
              storageKeys:
                type: array
                items:
                  title: 32 byte hex value
                  type: string
                  pattern: ^0x[0-9a-f]{64}$
        blobVersionedHashes:
          title: blobVersionedHashes
          description: List of versioned blob hashes associated with the transaction's EIP-4844 data blobs.
          type: array
          items:
            title: 32 byte hex value
            type: string
            pattern: ^0x[0-9a-f]{64}$
        blobs:
          title: blobs
          description: Raw EIP-4844 blob data (each item is a binary-encoded blob).
          type: array
          items:
            title: hex encoded bytes
            type: string
            pattern: ^0x[0-9a-f]*$
        chainId:
          title: chainId
          description: Chain ID that this transaction is valid on.
          type: string
          pattern: ^0x([1-9a-f]+[0-9a-f]*|0)$
  - name: Block
    required: false
    description: The block number or tag at which to estimate gas usage. Defaults to `'latest'` if not provided.
    schema:
      title: Block number or tag
      oneOf:
        - title: Block number
          type: string
          pattern: ^0x([1-9a-f]+[0-9a-f]*|0)$
        - title: Block tag
          type: string
          enum:
            - earliest
            - finalized
            - safe
            - latest
            - pending
          description: '`earliest`: The lowest numbered block the client has available; `finalized`: The most recent crypto-economically secure block, cannot be re-orged outside of manual intervention driven by community coordination; `safe`: The most recent block that is safe from re-orgs under honest majority and certain synchronicity assumptions; `latest`: The most recent block in the canonical chain observed by the client, this block may be re-orged out of the canonical chain even under healthy/normal conditions; `pending`: A sample next block built by the client on top of `latest` and containing the set of transactions usually taken from local mempool. Before the merge transition is finalized, any call querying for `finalized` or `safe` block MUST be responded to with `-39001: Unknown block` error'
result:
  name: Gas used
  description: The estimated amount of gas required for the transaction, as a hexadecimal string.
  schema:
    title: hex encoded unsigned integer
    type: string
    pattern: ^0x([1-9a-f]+[0-9a-f]*|0)$
examples:
  - name: eth_estimateGas example
    params:
      - name: Transaction
        value:
          from: '0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045'
          to: '0x44aa93095d6749a706051658b970b941c72c1d53'
          value: '0x1'
    result:
      name: Gas used
      value: '0x5208'
```
