# eth_feeHistory - Somnia

> Example code for the eth_feeHistory JSON-RPC method. Complete guide on how to use eth_feeHistory JSON-RPC in GetBlock Web3 documentation.

This method returns historical gas information on the Somnia network. This is useful for estimating appropriate gas prices based on recent network activity. Somnia's predictable gas pricing through IceDB makes this data particularly reliable.

## Parameters

| Parameter | Type | Required | Description |
| --- | --- | --- | --- |
| blockCount | string | Yes | Number of blocks to return (hex) |
| newestBlock | string | Yes | Highest block number, or "latest", "pending" |
| rewardPercentiles | array | No | Percentiles for priority fee sampling |

## Request Example

#### cURL

#### curl

```bash
curl -X POST https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/ \
-H "Content-Type: application/json" \
-d '{
  "jsonrpc": "2.0",
  "id": "getblock.io",
  "method": "eth_feeHistory",
  "params": ["0x5", "latest", [25, 50, 75]]
}'
```

#### JavaScript (Axios)

#### example.js

```javascript
const axios = require('axios');

const url = 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/';

const payload = {
  jsonrpc: '2.0',
  id: 'getblock.io',
  method: 'eth_feeHistory',
  params: ['0x5', 'latest', [25, 50, 75]]
};

axios.post(url, payload, {
  headers: { 'Content-Type': 'application/json' }
})
.then(response => console.log(response.data))
.catch(error => console.error(error));
```

#### Python

#### example.py

```python
import requests
import json

url = "https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/"

payload = {
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "method": "eth_feeHistory",
    "params": ["0x5", "latest", [25, 50, 75]]
}

response = requests.post(url, headers={"Content-Type": "application/json"}, json=payload)
print(response.json())
```

#### Rust

#### main.rs

```rust
use reqwest::Client;
use serde_json::json;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    
    let payload = json!({
        "jsonrpc": "2.0",
        "id": "getblock.io",
        "method": "eth_feeHistory",
        "params": ["0x5", "latest", [25, 50, 75]]
    });

    let response = client
        .post("https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/")
        .header("Content-Type", "application/json")
        .json(&payload)
        .send()
        .await?;

    let result: serde_json::Value = response.json().await?;
    println!("{:#?}", result);
    
    Ok(())
}
```

## Response Example

#### response.json

```json
{
  "jsonrpc": "2.0",
  "id": "getblock.io",
  "result": {
    "baseFeePerGas": ["0x3b9aca00", "0x3b9aca00", "0x3b9aca00", "0x3b9aca00", "0x3b9aca00", "0x3b9aca00"],
    "gasUsedRatio": [0.5, 0.3, 0.4, 0.6, 0.2],
    "oldestBlock": "0x1a2b38",
    "reward": [
      ["0x0", "0x0", "0x0"],
      ["0x0", "0x0", "0x0"],
      ["0x0", "0x0", "0x0"],
      ["0x0", "0x0", "0x0"],
      ["0x0", "0x0", "0x0"]
    ]
  }
}
```

## Response Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| baseFeePerGas | array | Base fee for each block (n+1 entries) |
| gasUsedRatio | array | Gas utilization ratio (0.0 to 1.0) |
| oldestBlock | string | First block in the range |
| reward | array | Priority fee at each percentile |

## Use Cases

* Implement dynamic gas pricing
* Analyze network congestion patterns
* Optimize transaction timing
* Build gas estimation algorithms
* Monitor network utilization

## Error Handling

| Error Code | Description |
| --- | --- |
| -32602 | Invalid params - invalid block count or range |
| -32603 | Internal error - node processing issues |

## SDK Integration

#### Ethers.js

#### ethers.js

```javascript
const { ethers } = require('ethers');

const provider = new ethers.JsonRpcProvider('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');

const feeData = await provider.getFeeData();
console.log(feeData);
```

#### Viem

#### viem.js

```javascript
import { createPublicClient, http } from 'viem';
import { somnia } from 'viem/chains';

const client = createPublicClient({
  chain: somnia,
  transport: http('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/')
});

const feeHistory = await client.getFeeHistory({
  blockCount: 5,
  rewardPercentiles: [25, 50, 75]
});
console.log(feeHistory);
```
