# Estimate Gas

Estimate the gas limit of an EVM transaction on a given chain, with a 20% safety buffer applied.

## Overview

The server calls `eth_estimateGas` on the RPC node configured for `chainSlug` and returns `gasLimit` as a hex string equal to the node's estimate × 1.2, rounded up.

- Only `to`, `data` and `value` are sent to the node — **no `from` address**. Calls whose outcome depends on the sender (balances, allowances, access control) can revert during estimation and return `422`.
- `data` and `value` are passed to the node unchanged, so use JSON-RPC hex encoding (for example `value=0x2386f26fc10000`).
- No gas price or fee fields are returned.
- `chainSlug` must be a chain registered with an RPC URL; otherwise the call returns `400`.

## Endpoint

### `GET /v1/dapps/estimate-gas`

Authentication: bearer token required.

Estimates gas with eth_estimateGas and returns the estimate plus 20% as a hex gasLimit.

**Query parameters**

| Name | Type | Required | Description |
| --- | --- | --- | --- |
| `chainSlug` | string | yes | Chain identifier, e.g. `gnosis`. |
| `to` | string | yes | Destination or contract address. |
| `data` | string | no | Hex-encoded calldata. |
| `value` | string | no | Value in wei as a JSON-RPC hex quantity. |

**Responses**

`200` OK

```json
{ "gasLimit": "0x6270" }
```

`400` No RPC

```json
{
  "error": "NO_RPC_URL",
  "message": "No RPC URL configured for chain 'unknownchain'"
}
```

`422` RPC Error

```json
{
  "error": "RPC_ERROR",
  "message": "error message returned by the RPC node"
}
```

**Example request**

```bash
curl "https://api.aureahub.com/v1/dapps/estimate-gas?chainSlug=gnosis&to=0x71C7656EC7ab88b098defB751B7401B5f6d8976F&value=0x2386f26fc10000" \
  -H "Authorization: Bearer YOUR_TOKEN"
```

In the example above a plain transfer estimated at 21,000 gas (`0x5208`) is returned as `0x6270` (25,200). If the RPC node does not answer within 15 seconds the call returns `504` with `{ "error": "RPC_TIMEOUT", "message": "The RPC node did not respond in time" }`.

## Implementation

```javascript
async function estimateGas(token, chainSlug, tx) {
  const params = new URLSearchParams({ chainSlug, to: tx.to });
  if (tx.data)  params.set('data', tx.data);     // 0x-prefixed calldata
  if (tx.value) params.set('value', tx.value);   // hex quantity, e.g. '0x0'

  const res = await fetch(`https://api.aureahub.com/v1/dapps/estimate-gas?${params}`, {
    headers: { Authorization: `Bearer ${token}` }
  });
  if (!res.ok) throw new Error((await res.json()).message);
  const { gasLimit } = await res.json();
  return gasLimit; // hex string, already includes the 20% buffer
}
```

---

Web version: https://docs.aureahub.com/#dapps-gas
