# Account Frontend State Stream

User state snapshot, mainly used by frontend.

## Update Speed

* Pushed on each block that is at least 0.5 seconds since the last push

## Subscribe Request

* Subscribe Schema

See [`WsUserFrontendStateSubscriptionParams`](https://sodex.com/documentation/rest-v1/schema#wsuserfrontendstatesubscriptionparams) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface SubscribeRequest {
  op: "subscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  params: WsUserFrontendStateSubscriptionParams;
}
```

* Subscribe Ack Schema

See [`WsUserFrontendStateSubscriptionResult`](https://sodex.com/documentation/rest-v1/schema#wsuserfrontendstatesubscriptionresult) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface SubscriptionResult {
  op: "subscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  result: WsUserFrontendStateSubscriptionResult | null;
  success: boolean; // Indicates if the request was successfully processed by the engine.
  connID: string;
  error: string | null; // Condition: If success is false. Error message.
  time_in: number; // The timestamp when the subscription was received on the wire, just prior to parsing data, in milliseconds.
  time_out: number; // The timestamp when the acknowledgement was sent on the wire, just prior to transmitting data, in milliseconds.
}
```

* Example: Subscribe

```json
{
  "op": "subscribe",
  "params": {
    "channel": "accountState",
    "user": "0xed3e79ef16a7890e6eaef2aa6373b6bec5d59d5c"
  }
}
```

* Example: Subscribe Ack

```json
{
  "op": "subscribe",
  "result": {
    "channel": "accountState",
    "user": "0xed3e79ef16a7890e6eaef2aa6373b6bec5d59d5c",
    "accountID": 1001
  },
  "success": true,
  "connID": "0xb0922dfe2b14dadb1195ea4db2b45508",
  "time_in": 1672515782130,
  "time_out": 1672515782136
}
```

## Unsubscribe Request

* Unsubscribe Schema

See [`WsUserFrontendStateSubscriptionParams`](https://sodex.com/documentation/rest-v1/schema#wsuserfrontendstatesubscriptionparams) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface UnsubscribeRequest {
  op: "unsubscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  params: WsUserFrontendStateSubscriptionParams;
}
```

* Unsubscribe Ack Schema

See [`WsUserFrontendStateSubscriptionResult`](https://sodex.com/documentation/rest-v1/schema#wsuserfrontendstatesubscriptionresult) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface UnsubscriptionResult {
  op: "unsubscribe";
  id: number | null; // Optional client originated request identifier sent as acknowledgment in the response.
  result: WsUserFrontendStateSubscriptionResult | null;
  success: boolean; // Indicates if the request was successfully processed by the engine.
  connID: string;
  error: string | null; // Condition: If success is false. Error message.
  time_in: number; // The timestamp when the subscription was received on the wire, just prior to parsing data, in milliseconds.
  time_out: number; // The timestamp when the acknowledgement was sent on the wire, just prior to transmitting data, in milliseconds.
}
```

* Example: Unsubscribe

```json
{
  "op": "unsubscribe",
  "params": {
    "channel": "accountState",
    "user": "0xed3e79ef16a7890e6eaef2aa6373b6bec5d59d5c"
  }
}
```

* Example: Unsubscribe Ack

```json
{
  "op": "unsubscribe",
  "result": {
    "channel": "accountState",
    "user": "0xed3e79ef16a7890e6eaef2aa6373b6bec5d59d5c",
    "accountID": 1001
  },
  "success": true,
  "connID": "0xb0922dfe2b14dadb1195ea4db2b45508",
  "time_in": 1672515782130,
  "time_out": 1672515782136
}
```

## Snapshot Response

* Snapshot Schema

See [`WsSpotState`](https://sodex.com/documentation/rest-v1/schema#wsspotstate), [`WsPerpsState`](https://sodex.com/documentation/rest-v1/schema#wsperpsstate) in [Schema](https://sodex.com/documentation/api/rest-v1/schema).

```typescript
interface UserFrontendStateResponse {
  channel: "accountState";
  type: "snapshot";
  data: WsSpotState | WsPerpsState;
}
```

* Example: Snapshot

```json
{
  "channel": "accountState",
  "type": "snapshot",
  "data": {
    "user": "0xed3e79ef16a7890e6eaef2aa6373b6bec5d59d5c",
    "aid": 1001,
    "uid": 1001,
    "B": [
      {
        "i": 4,
        "a": "WSOSO",
        "t": "1355197181.1898631250846998",
        "l": "572018.57"
      },
      {
        "i": 0,
        "a": "vUSDC",
        "t": "999998002.28716973",
        "l": "1720938.88807991"
      },
      { "i": 1, "a": "vBTC", "t": "10112.98612312", "l": "5.79784" },
      { "i": 2, "a": "vETH", "t": "287323.0277728536949776", "l": "93.1448" },
      {
        "i": 3,
        "a": "vPEPE",
        "t": "163666300460675.6235679214402619",
        "l": "65318205220"
      }
    ],
    "O": [
      {
        "s": "vETH_vUSDC",
        "c": "MAKER-SELL-1-72124681193656",
        "i": 36502,
        "S": "SELL",
        "o": "LIMIT",
        "f": "GTC",
        "p": "3517",
        "q": "16.0315",
        "X": "PARTIALLY_FILLED",
        "z": "0.0083",
        "v": "29.1911",
        "M": "16.0232",
        "ct": 1766846797324,
        "ut": 1766846798323
      },
      {
        "s": "WSOSO_vUSDC",
        "c": "MAKER-BUY-3-12891982191108",
        "i": 36559,
        "S": "BUY",
        "o": "LIMIT",
        "f": "GTC",
        "p": "0.729",
        "q": "18027.43",
        "X": "NEW",
        "z": "0",
        "v": "0",
        "M": "13141.99647",
        "ct": 1766846797324,
        "ut": 1766846797324
      }
    ]
  }
}
```
