Skip to content

Instantly share code, notes, and snippets.

@poly-rodr
Last active October 16, 2024 13:42
Show Gist options
  • Save poly-rodr/d320df61353c360e7e6c608b19c91409 to your computer and use it in GitHub Desktop.
Save poly-rodr/d320df61353c360e7e6c608b19c91409 to your computer and use it in GitHub Desktop.
A summary with the changes of the new version of the WebSocket API.

Changes in the WebSocket API

Price change event

Current version

If you are subscribed to the market stream you are receiving price_change events like these:

{
    "event_type": "price_change",
    "asset_id": "52114319501245915516055106046884209969926127482827954674443846427813813222426",
    "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
    "price": "0.6",
    "side": "BUY",
    "size": "3400",
    "timestamp": "1729084877646"
}
{
    "event_type": "price_change",
    "asset_id": "71321045679252212594626385532706912750332728571942532289631379312455583992563",
    "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
    "price": "0.4",
    "side": "SELL",
    "size": "3400",
    "timestamp": "1729084877646"
}

New version

Now, you will receive a single message that includes an array with all the changes in the book

{
    "asset_id": "71321045679252212594626385532706912750332728571942532289631379312455583992563",
    "changes": [
        {
            "price": "0.4",
            "side": "SELL",
            "size": "3300"
        },
        {
            "price": "0.5",
            "side": "SELL",
            "size": "3400"
        },
        {
            "price": "0.3",
            "side": "SELL",
            "size": "3400"
        }
    ],
    "event_type": "price_change",
    "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
    "timestamp": "1729084877448"
}
{
    "asset_id": "52114319501245915516055106046884209969926127482827954674443846427813813222426",
    "changes": [
        {
            "price": "0.6",
            "side": "BUY",
            "size": "3300"
        },
        {
            "price": "0.5",
            "side": "BUY",
            "size": "3400"
        },
        {
            "price": "0.7",
            "side": "BUY",
            "size": "3400"
        }
    ],
    "event_type": "price_change",
    "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
    "timestamp": "1729084877448"
}

Incoming messages format

Initial orderbooks dump

Current

When you open a market stream connection, you receive a dump of the current status of the orderbooks that you are subscribed to. If you are subscribed to 2 orderbooks you receive 2 messages:

{
    "asks": [...],
    "asset_id": "52114319501245915516055106046884209969926127482827954674443846427813813222426",
    "bids": [...],
    "event_type": "book",
    "hash": "ab8c376cbc3da9b514f910254e06694ef16ae502",
    "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
    "timestamp": "1729049876439"
}
{
    "asks": [...],
    "asset_id": "71321045679252212594626385532706912750332728571942532289631379312455583992563",
    "bids": [...],
    "event_type": "book",
    "hash": "58b8cdd1eda8b6333060010208c14a6e0f8738af",
    "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
    "timestamp": "1729049876439"
}

New version

Now, you will receive a single message with an array including all the orderbooks:

[
    {
        "asks": [...],
        "asset_id": "52114319501245915516055106046884209969926127482827954674443846427813813222426",
        "bids": [...],
        "event_type": "book",
        "hash": "101e9c5b95f5505193e38dafe2ddaf1717455052",
        "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
        "timestamp": "1729084877452"
    },
    {
        "asks": [...],
        "asset_id": "71321045679252212594626385532706912750332728571942532289631379312455583992563",
        "bids": [...],
        "event_type": "book",
        "hash": "76d581dedc7779e131504236751293eeb1ae299d",
        "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
        "timestamp": "1729084877452"
    }
]

Continious messages stream

Once you are connected to the market or user stream you will receive messages. There is a change in the way you receive them.

Current

In the current version, if you receive 3 events you receive 3 objects in 3 sepparate messages so you have to process them one by one:

{
    "asks": [...],
    "asset_id": "71321045679252212594626385532706912750332728571942532289631379312455583992563",
    "bids": [...],
    "event_type": "book",
    "hash": "323a06d8723e32c275d52a4730fc05326057d010",
    "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
    "timestamp": "1729085856075"
}
{
    "asks": [...],
    "asset_id": "52114319501245915516055106046884209969926127482827954674443846427813813222426",
    "bids": [...],
    "event_type": "book",
    "hash": "bf4e514b6e5604130c00e1ff0d7ed0792e703981",
    "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
    "timestamp": "1729085856075"
}
{
    "asset_id": "71321045679252212594626385532706912750332728571942532289631379312455583992563",
    "event_type": "last_trade_price",
    "fee_rate_bps": "0",
    "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
    "price": "0.3",
    "side": "BUY",
    "size": "0.333332",
    "timestamp": "1729085856145"
}

New

Now, you will receive a single message with an array including all the events:

[
    {
        "asks": [...],
        "asset_id": "71321045679252212594626385532706912750332728571942532289631379312455583992563",
        "bids": [...],
        "event_type": "book",
        "hash": "323a06d8723e32c275d52a4730fc05326057d010",
        "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
        "timestamp": "1729085856075"
    },
    {
        "asks": [...],
        "asset_id": "52114319501245915516055106046884209969926127482827954674443846427813813222426",
        "bids": [...],
        "event_type": "book",
        "hash": "bf4e514b6e5604130c00e1ff0d7ed0792e703981",
        "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
        "timestamp": "1729085856075"
    },
    {
        "asset_id": "71321045679252212594626385532706912750332728571942532289631379312455583992563",
        "event_type": "last_trade_price",
        "fee_rate_bps": "0",
        "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
        "price": "0.3",
        "side": "BUY",
        "size": "0.333332",
        "timestamp": "1729085856145"
    },
    {
        "asset_id": "71321045679252212594626385532706912750332728571942532289631379312455583992563",
        "changes": [
            {
                "price": "0.4",
                "side": "SELL",
                "size": "5800"
            },
            {
                "price": "0.3",
                "side": "SELL",
                "size": "5799.66"
            },
            {
                "price": "0.5",
                "side": "SELL",
                "size": "5800"
            }
        ],
        "event_type": "price_change",
        "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
        "timestamp": "1729085860552"
    },
    {
        "asset_id": "52114319501245915516055106046884209969926127482827954674443846427813813222426",        
        "changes": [
            {
                "price": "0.6",
                "side": "BUY",
                "size": "5800"
            },
            {
                "price": "0.7",
                "side": "BUY",
                "size": "5799.66"
            },
            {
                "price": "0.5",
                "side": "BUY",
                "size": "5800"
            }
        ],
        "event_type": "price_change",
        "market": "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1",
        "timestamp": "1729085860552"
    }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment