Skip to content

Commit

Permalink
fixup! Huobi: Add V2 websocket support
Browse files Browse the repository at this point in the history
  • Loading branch information
gbjk committed Oct 31, 2024
1 parent ad6930b commit 287d1bf
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 37 deletions.
91 changes: 54 additions & 37 deletions exchanges/huobi/huobi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1349,48 +1349,65 @@ func TestWsOrderbook(t *testing.T) {
}

func TestWsTradeDetail(t *testing.T) {
h := new(HUOBI) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
require.NoError(t, testexch.Setup(h), "Setup Instance must not error")
err := h.Websocket.AddSubscriptions(h.Websocket.Conn, &subscription.Subscription{Key: "market.btcusdt.trade.detail", Asset: asset.Spot, Pairs: currency.Pairs{btcusdtPair}, Channel: subscription.AllTradesChannel})
require.NoError(t, err, "AddSubscriptions must not error")
pressXToJSON := []byte(`{
"ch": "market.btcusdt.trade.detail",
"ts": 1489474082831,
"tick": {
"id": 14650745135,
"ts": 1533265950234,
"data": [
{
"amount": 0.0099,
"ts": 1533265950234,
"id": 146507451359183894799,
"tradeId": 102043495674,
"price": 401.74,
"direction": "buy"
}
]
}
}`)
err = h.wsHandleData(pressXToJSON)
require.NoError(t, err)
testexch.FixtureToDataHandler(t, "testdata/wsOrderbook.json", h.wsHandleData)
close(h.Websocket.DataHandler)
require.Len(t, h.Websocket.DataHandler, 1, "Should not see anything on the datahandler")
dAny := <-h.Websocket.DataHandler
d, ok := dAny.(*orderbook.Depth)
require.True(t, ok, "Must get the correct type from DataHandler")
require.NotNil(t, d)
/*
"ts": 1489474082831,
"tick": {
"id": 14650745135,
"ts": 1533265950234,
"data": [
{
"amount": 0.0099,
"ts": 1533265950234,
"id": 146507451359183894799,
"tradeId": 102043495674,
"price": 401.74,
"direction": "buy"
}
]
}
}`)
err = h.wsHandleData(pressXToJSON)
require.NoError(t, err)
*/
}

func TestWsTicker(t *testing.T) {
pressXToJSON := []byte(`{
"rep": "market.btcusdt.detail",
"id": "id11",
"data":{
"amount": 12224.2922,
"open": 9790.52,
"close": 10195.00,
"high": 10300.00,
"ts": 1494496390000,
"id": 1494496390,
"count": 15195,
"low": 9657.00,
"vol": 121906001.754751
}
}`)
err := h.wsHandleData(pressXToJSON)
require.NoError(t, err)
h := new(HUOBI) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
require.NoError(t, testexch.Setup(h), "Setup Instance must not error")
err := h.Websocket.AddSubscriptions(h.Websocket.Conn, &subscription.Subscription{Key: "market.btcusdt.detail", Asset: asset.Spot, Pairs: currency.Pairs{btcusdtPair}, Channel: subscription.TickerChannel})
require.NoError(t, err, "AddSubscriptions must not error")
testexch.FixtureToDataHandler(t, "testdata/wsTicker.json", h.wsHandleData)
close(h.Websocket.DataHandler)
require.Len(t, h.Websocket.DataHandler, 1, "Should not see anything on the datahandler")
tickAny := <-h.Websocket.DataHandler
tick, ok := tickAny.(*ticker.Price)
require.True(t, ok, "Must get the correct type from DataHandler")
require.NotNil(t, tick)
exp := &ticker.Price{
High: 52924.14,
Low: 51000,
Bid: 0,
Volume: 13991.028076056185,
QuoteVolume: 7.27676440200527e+08,
Open: 51823.62,
Close: 52379.99,
Pair: btcusdtPair,
ExchangeName: h.Name,
AssetType: asset.Spot,
LastUpdated: time.UnixMilli(1630998026649),
}
assert.Equal(t, exp, tick)
}

func TestWsAccountUpdate(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions exchanges/huobi/testdata/wsTicker.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"ch":"market.btcusdt.detail","ts":1630998026649,"tick":{"id":273956868110,"low":51000,"high":52924.14,"open":51823.62,"close":52379.99,"vol":727676440.200527,"amount":13991.028076056185,"version":273956868110,"count":471348}}

0 comments on commit 287d1bf

Please sign in to comment.