# REST API

**Base URL:** `https://api.megatao.com`

The REST API is read-only and requires no authentication. All amounts are returned as strings in wei (18 decimals). Divide by `10^18` to get TAO-denominated values.

Interactive documentation is available at <https://api.megatao.com/swagger>.

***

## Markets

### `GET /v1/info/markets`

Returns all active markets.

**Response**

```json
[
  {
    "market": "0x0000000000000000000000000000000000000040",
    "symbol": "CHUTES",
    "price": "87707801000000000",
    "fundingRate": "20",
    "longOpenInterest": "9003500000000000000",
    "shortOpenInterest": "911995857793391469",
    "maxLeverage": "500",
    "isActive": true
  }
]
```

### `GET /v1/info/markets/:address`

Returns detailed info for a single market by contract address.

### `GET /v1/info/prices`

Returns the latest oracle price for every market, keyed by market address.

```json
{
  "0x0000000000000000000000000000000000000040": {
    "symbol": "CHUTES",
    "price": "87707850000000000"
  }
}
```

### `GET /v1/info/candles`

Returns OHLCV candle data.

| Parameter    | Type    | Description                              |
| ------------ | ------- | ---------------------------------------- |
| `market`     | string  | Market contract address                  |
| `resolution` | string  | `1m`, `5m`, `15m`, `1h`, `4h`, `1d`      |
| `from`       | integer | Start timestamp (Unix seconds, optional) |
| `to`         | integer | End timestamp (Unix seconds, optional)   |

### `GET /v1/info/orderbook`

Returns the order book for a market.

| Parameter | Type    | Description                                    |
| --------- | ------- | ---------------------------------------------- |
| `market`  | string  | Market contract address                        |
| `levels`  | integer | Number of price levels each side (default: 10) |

```json
{
  "market": "0x...",
  "bids": [{ "price": "87500000000000000", "notional": "5000000000000000000", "orderCount": 3 }],
  "asks": [{ "price": "88000000000000000", "notional": "3000000000000000000", "orderCount": 2 }]
}
```

### `GET /v1/info/funding`

Returns current funding rates for all markets.

```json
[
  {
    "market": "0x...",
    "symbol": "CHUTES",
    "currentRate": "20",
    "lastUpdate": "1774565748"
  }
]
```

Funding rates are in basis points per 8-hour interval. See [Funding](https://docs.megatao.com/trading/funding) for the full rate mechanics.

***

## Account

### `GET /v1/info/margin/:address`

Returns margin balances for a trader address.

```json
{
  "depositedBalance": "10000000000000000000",
  "accountEquity": "10500000000000000000",
  "unrealizedPnl": "500000000000000000",
  "lockedMargin": "5000000000000000000",
  "availableMargin": "5500000000000000000"
}
```

### `GET /v1/info/positions/:address`

Returns open positions for a trader.

```json
[
  {
    "positionId": "0x984aa620aba1ac7ae2d1edbb8ba9c3ff",
    "market": "0x...",
    "symbol": "CHUTES",
    "isLong": true,
    "notionalValue": "238000000000000000",
    "margin": "114000000000000000",
    "entryPrice": "100651205495176469",
    "markPrice": "87707850000000000",
    "pricePnl": "-30605878913190245",
    "fundingPayment": "-15472261000158660",
    "liquidationPrice": "62326323402782351",
    "liquidatable": false
  }
]
```

### `GET /v1/info/orders/:address`

Returns open limit orders for a trader.

### `GET /v1/info/trades/:address`

Returns trade history for a trader. Response is split into `opens` and `closes`:

```json
{
  "opens": [...],
  "closes": [...]
}
```

***

## Platform

### `GET /v1/info/vault`

Returns the current vault state.

```json
{
  "totalReserves": "508113125671638784802",
  "availableReserves": "431255395240438378217",
  "insuranceFund": "51166942313050694271",
  "utilizationRate": "1512"
}
```

`utilizationRate` is in basis points (1512 = 15.12%).

***

## Market addresses

| Market | Address                                      | Subnet |
| ------ | -------------------------------------------- | ------ |
| CHUTES | `0x0000000000000000000000000000000000000040` | SN64   |
| LIUM   | `0x0000000000000000000000000000000000000033` | SN51   |
| RIDGES | `0x000000000000000000000000000000000000003e` | SN62   |
| TARGON | `0x0000000000000000000000000000000000000004` | SN4    |
| MTSOS  | `0x000000000000000000000000000000000000FFFE` | Index  |

See [Contract Specifications](https://docs.megatao.com/trading/contract-specifications) for the full list.
