# activeAssetCtx

This subscription mirrors Hyperliquid activeAssetCtx exactly.

Subscribe

```json
{
    "method": "subscribe",
    "subscription": {
        "type": "activeAssetCtx",
        "coin": "<coin_symbol>"
    }
}
```

### Unsubscribe

```json
{
    "method": "unsubscribe",
    "subscription": {
        "type": "activeAssetCtx",
        "coin": "<coin_symbol>"
    }
}
```

### format

```json
{
    "channel": "activeAssetCtx",
    "seq": 1,
    "cursor": "500:1704067200000:3",
    "data": {
        "coin": "ETH",
        "ctx": {
            "oraclePx": "3230.1",
            "markPx": "3227.4",
            "midPx": "3228.25",
            "impactPxs": ["3228.2", "3228.3"],
            "openInterest": "446072.075"
        }
    }
}
```

**Note:** `activeAssetCtx` uses `"channel"` instead of `"type"` as the message identifier for Hyperliquid compatibility. `impactPxs` contains the average execution price to trade impact notional (20k$ for BTC/ETH, 6k$ for others) on bid and ask.

Example:

{% tabs %}
{% tab title="Javascript" %}

```javascript
const WebSocket = require('ws');

const ws = new WebSocket(`wss://api-testnet.hydromancer.xyz/ws?token=${process.env.HYDROMANCER_API_KEY}`);
// const ws = new WebSocket(`wss://api.hyperliquid-testnet.xyz/ws`);

ws.on('open', () => {
    console.log('WebSocket connection opened');
    console.log('Subscribing to oracleUpdates...');
    ws.send(JSON.stringify({
        "method": "subscribe",
        "subscription": {
            "type": "activeAssetCtx",
            "coin": "ETH"
        }
    }));
});

ws.on('error', (error) => {
    console.error('WebSocket error:', error);
});

ws.on('close', () => {
    console.log('WebSocket connection closed');
});

ws.on('message', (data) => {
    const msg = JSON.parse(data);
    if (msg.channel === 'activeAssetCtx') {
        console.log(msg.data)
    }
});
```

{% endtab %}

{% tab title="Python" %}

```python
import websocket
import json
import os

# Get API key from environment variable
api_key = os.environ.get('HYDROMANCER_API_KEY')
url = f'wss://api-testnet.hydromancer.xyz/ws?token={api_key}'
# url = 'wss://api.hyperliquid-testnet.xyz/ws'

def on_open(ws):
    print('WebSocket connection opened')
    print('Subscribing to oracleUpdates...')
    ws.send(json.dumps({
        "method": "subscribe",
        "subscription": {
            "type": "activeAssetCtx",
            "coin": "ETH"
        }
    }))

def on_error(ws, error):
    print(f'WebSocket error: {error}')

def on_close(ws, close_status_code, close_msg):
    print('WebSocket connection closed')

def on_message(ws, message):
    msg = json.loads(message)
    if msg.get('channel') == 'activeAssetCtx':
        print(msg.get('data'))

# Create WebSocket connection
ws = websocket.WebSocketApp(url,
                            on_open=on_open,
                            on_error=on_error,
                            on_close=on_close,
                            on_message=on_message)

# Run the WebSocket connection
ws.run_forever()
```

{% endtab %}
{% endtabs %}

#### Error messages:

```
{
    "type": "error",
    "message": "Invalid API key"
}
```

#### Common errors

1. ```
   Connection timeout - Respond to ping messages
   ```
2. ```
   Invalid coin - Use a valid coin symbol
   ```
3. ```
   Invalid API key
   ```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hydromancer.xyz/readme/websocket/activeassetctx.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
