# Etherscan Gas API v2 Integration

## Overview

The deployment cost estimation system now uses the correct Etherscan Gas API v2 endpoint for accurate gas price retrieval.

## API Endpoint

**Correct Endpoint**: `https://api.etherscan.io/v2/api?chainid=1&module=gastracker&action=gasoracle&apikey=YourApiKeyToken`

### Response Format

```json
{
  "status": "1",
  "message": "OK",
  "result": {
    "LastBlock": "23788262",
    "SafeGasPrice": "0.111829336",
    "ProposeGasPrice": "0.11182936",
    "FastGasPrice": "0.123012296",
    "suggestBaseFee": "0.111829336",
    "gasUsedRatio": "..."
  }
}
```

## Integration

### Gas Price Sources (Priority Order)

1. **MetaMask** (via `ETHEREUM_MAINNET_RPC`)
2. **Etherscan Gas API v2** (via `ETHERSCAN_API_KEY`)
3. **Default RPC** (fallback)

### Methodology

```
Conservative Gas Price = MIN(MAX(MetaMask Gas, Etherscan Gas), 2.5 gwei)
```

- Fetches from both MetaMask and Etherscan
- Selects the **highest** price
- Caps at **2.5 gwei maximum**

## Usage

### Get Conservative Gas Price

```bash
./scripts/deployment/get-conservative-gas-price.sh
```

### Calculate Costs

```bash
./scripts/deployment/calculate-conservative-costs.sh
```

### Test API

```bash
./scripts/deployment/test-etherscan-gas-api.sh
```

## Configuration

Add to `.env`:

```env
ETHERSCAN_API_KEY=your_api_key_here
ETHEREUM_MAINNET_RPC=https://...
```

## Current Status

- ✅ API endpoint corrected to v2 format
- ✅ Response parsing handles both v1 and v2 formats
- ✅ Gas price extraction working correctly
- ✅ Display formatting improved
- ✅ Conservative calculation using 2.5 gwei cap

## Notes

- Current gas prices (~0.1 gwei) are extremely low and not normal
- System uses actual prices when below 2.5 gwei cap
- For budgeting, consider normal market conditions (30-50 gwei)
