# StreamAssetCtxs - Stream Market Context for Every Perp in a DEX

`StreamAssetCtxs` sends the market context of every perp in one or more perp DEXs in a single stream, so a client needs no stream per coin. An empty request selects the default DEX. Each coin's context has the same fields as [StreamActiveAssetCtx](https://www.dwellir.com/docs/hyperliquid/stream_active_asset_ctx).

## Request and opening behavior

The request selects one scope. Set at most one of `dex` and `dexes`:

| Request                 | Contexts in each frame                                                              |
| ----------------------- | ----------------------------------------------------------------------------------- |
| `{}` or `dex: ""`       | Every default-DEX perp                                                              |
| `dex: "xyz"`            | Every perp of the HIP-3 builder DEX `xyz`, keyed as `xyz:` coins such as `xyz:GOLD` |
| `dexes: ["xyz", "flx"]` | Every perp of the listed HIP-3 DEXs                                                 |
| `dexes: ["", "xyz"]`    | Every default-DEX perp and every `xyz` perp                                         |

In `dexes`, the empty string `""` selects the default DEX. A frame lists DEXs sorted by name, with the default DEX first, and each DEX's coins in universe order.

A ready subscription immediately receives the contexts of every coin in the scope, taken from the newest render of each requested DEX. After that, the endpoint renders the contexts about every 500 ms and sends a new frame when any coin in the scope changed. Every frame is complete and supersedes the previous one, so keep only the latest. A coin that leaves a DEX's universe is absent from the next frame. A delisted perp stays in the universe and keeps streaming, with zero funding and open interest.

A frame is sent only when a context changes. A quiet DEX sends its first frame on subscribe and then nothing until one of its coins changes. There is no keepalive frame, so a healthy stream can be silent.

The frame's `time` is the newest render time among the requested DEXs, in Unix milliseconds. Each entry's `time` is the render time of its own DEX, so entries from different DEXs in one frame can carry different times. An entry's `time` says when the endpoint last rendered that DEX, not when the coin's values last changed. Neither is a block or upstream timestamp. For a default-DEX coin, values and `time` can differ slightly from `StreamActiveAssetCtx`, which uses a different source.

## Sources and update rates

The endpoint builds these contexts from its own node data, for every DEX including the default DEX. `mid_px`, `impact_pxs`, and `premium` can change on every render. `mark_px` changes once per mark update, about every 2 seconds. `oracle_px` follows the validators' oracle updates on the default DEX and the deployer's oracle price updates on a HIP-3 DEX. `premium` and `mid_px` are unset, and `impact_pxs` is empty, when a market has no book. `prev_day_px` is `"0.0"` for a perp with no trades and no mark yet, such as a new listing. After its first mark, it carries the mark price until the perp trades.

For the same data as a single request, use [metaAndAssetCtxs](https://www.dwellir.com/docs/hyperliquid/meta-and-asset-ctxs) on the Info endpoint.

## Request Parameters

- `dex` (`string, optional`): One perp DEX: a HIP-3 builder DEX such as "xyz" for its xyz: perps, or "" for the default DEX. Mutually exclusive with dexes.
- `dexes` (`repeated string, optional`): Union of these perp DEXs. The empty string "" selects the default DEX. Mutually exclusive with dex.

## Request Example

```go
package main

import (
    "context"
    "crypto/tls"
    "log"
    "os"

    pb "example.com/hyperliquid-v3-client/gen/v3"
    "google.golang.org/grpc"
    "google.golang.org/grpc/credentials"
    "google.golang.org/grpc/metadata"
)

func main() {
    endpoint, apiKey := os.Getenv("HYPERLIQUID_GRPC_ENDPOINT"), os.Getenv("DWELLIR_API_KEY")
    if endpoint == "" || apiKey == "" {
        log.Fatal("set HYPERLIQUID_GRPC_ENDPOINT and DWELLIR_API_KEY")
    }
    conn, err := grpc.NewClient(endpoint, grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{MinVersion: tls.VersionTLS12})))
    if err != nil {
        log.Fatal(err)
    }
    defer conn.Close()
    ctx := metadata.AppendToOutgoingContext(context.Background(), "x-api-key", apiKey)
    stream, err := pb.NewMarketStreamingClient(conn).StreamAssetCtxs(ctx, &pb.AssetCtxsRequest{Dex: "xyz"})
    if err != nil {
        log.Fatal(err)
    }
    for {
        update, err := stream.Recv()
        if err != nil {
            log.Fatal(err)
        }
        log.Printf("update_time=%d coins=%d", update.GetTime(), len(update.GetCtxs()))
        for _, c := range update.GetCtxs() {
            log.Printf("coin=%s mark=%s oracle=%s funding=%s open_interest=%s", c.GetCoin(), c.GetMarkPx(), c.GetOraclePx(), c.GetFunding(), c.GetOpenInterest())
        }
    }
}
```

## Response Fields

- `time` (`int64, required`): Newest render time among the requested DEXs in Unix milliseconds, not block or upstream time.
- `ctxs` (`repeated ActiveAssetCtxUpdate, required`): Complete context of every perp in the requested DEXs, with the fields of StreamActiveAssetCtx. DEXs are sorted by name with the default DEX first; coins follow universe order.
- `ctxs.coin` (`string, required`): Perp coin identifier, such as "BTC" or "xyz:GOLD".
- `ctxs.time` (`int64, required`): Render time of this coin's DEX in Unix milliseconds: when the endpoint last rendered that DEX, not when this coin's values last changed.
- `ctxs.funding` (`string, required`): Hourly funding rate as a decimal string.
- `ctxs.open_interest` (`string, required`): Open interest as a decimal string.
- `ctxs.prev_day_px` (`string, required`): Price 24 hours before the context snapshot; "0.0" for a perp with no trades and no mark yet, such as a new listing. After its first mark, it carries the mark price until the perp trades.
- `ctxs.day_ntl_vlm` (`string, required`): Rolling 24-hour notional volume.
- `ctxs.premium` (`optional string, optional`): Impact-price premium over the oracle price; unset when impact prices or the oracle are unavailable.
- `ctxs.oracle_px` (`string, required`): Oracle price as a decimal string.
- `ctxs.mark_px` (`string, required`): Mark price as a decimal string.
- `ctxs.mid_px` (`optional string, optional`): Book midpoint, unset when the market has no book.
- `ctxs.impact_pxs` (`repeated string, required`): Bid and ask impact prices; empty when the market has no book.
- `ctxs.day_base_vlm` (`string, required`): Rolling 24-hour base-unit volume.

## Successful Response

```protobuf
time: 1785736814057
ctxs: {
  coin: "xyz:GOLD"
  time: 1785736814057
  funding: "0.0000125"
  open_interest: "1520.4"
  prev_day_px: "4010.5"
  day_ntl_vlm: "18250000.75"
  premium: "0.00012"
  oracle_px: "4021.3"
  mark_px: "4021.8"
  mid_px: "4021.85"
  impact_pxs: "4021.6"
  impact_pxs: "4022.1"
  day_base_vlm: "4540.2"
}
ctxs: {
  coin: "xyz:NVDA"
  time: 1785736814057
  funding: "0.00000625"
  open_interest: "84210.0"
  prev_day_px: "181.2"
  day_ntl_vlm: "9650000.5"
  premium: "0.0001"
  oracle_px: "182.05"
  mark_px: "182.07"
  mid_px: "182.075"
  impact_pxs: "182.06"
  impact_pxs: "182.09"
  day_base_vlm: "53050.0"
}
```

## Error Responses

### Invalid request

- Code: `INVALID_ARGUMENT`
- Description: The request sets both a non-empty dex and a non-empty dexes, or names "*", a coin such as "xyz:GOLD", or a DEX that does not exist.

### Feature unavailable

- Code: `FAILED_PRECONDITION`
- Description: This stream is unavailable on the selected endpoint.

### DEX not ready

- Code: `UNAVAILABLE`
- Description: A requested DEX is not ready yet, or its latest data is stale; retry.

### Stream state reset

- Code: `ABORTED`
- Description: The stream state reset, or the node stopped listing a requested DEX for a sustained period; resubscribe. A resubscribe that names that DEX returns INVALID_ARGUMENT until the node lists it again.

### Slow consumer

- Code: `DEADLINE_EXCEEDED`
- Description: The consumer was too slow; reconnect.

### Unauthenticated

- Code: `UNAUTHENTICATED`
- Description: Usable x-api-key metadata was not supplied.

### Permission denied

- Code: `PERMISSION_DENIED`
- Description: The supplied credential cannot call this service.

## Delivery and recovery

Subscriber lag drops obsolete frames in-band; the next frame supersedes them.

If the endpoint stops serving a DEX for a while, a single-DEX stream goes silent until that DEX recovers. In a multi-DEX stream, the frames sent for the other DEXs keep that DEX's last contexts with their older `time`.

If the node stops listing a requested DEX for a sustained period, at least 70 seconds with the default settings, the stream closes with `ABORTED`. A short gap in the node's DEX list does not close the stream. While the node is not listing the DEX, a new subscription that names it returns `INVALID_ARGUMENT` straight away, until the node lists it again.

`INVALID_ARGUMENT` means the request sets both a non-empty `dex` and a non-empty `dexes`, or names `"*"`, a coin such as `"xyz:GOLD"`, or a DEX that does not exist. `UNAVAILABLE` means a requested DEX is not ready yet or its latest data is stale; retry. `FAILED_PRECONDITION` means the stream is unavailable on this endpoint. `ABORTED` means the stream state reset or the node stopped listing a requested DEX; resubscribe. See the [shared market-stream contract](https://www.dwellir.com/docs/hyperliquid/grpc#v3-market-streaming-service) for common errors.

Related reference: [StreamActiveAssetCtx](https://www.dwellir.com/docs/hyperliquid/stream_active_asset_ctx) and [StreamAllMids](https://www.dwellir.com/docs/hyperliquid/stream_all_mids).
