Huobi: Fix sending trades to the websocket DataHandler (#1817)

* Send trades down the DataHandler

* Send individual trades down the DataHandler

Update the test to send the multiple trades
Remove the ID setting to 0 in the test
Iterate through the trades to send them down individually
Rename the test to match the method

* Fix the timestamp conversion

* Fix the wording

* Update the expected data format

* Update the test name and description

* Fix error case
This commit is contained in:
Bea
2025-03-11 06:21:28 +07:00
committed by GitHub
parent 8c29dd9f28
commit 72a94a4b6c
4 changed files with 54 additions and 7 deletions

View File

@@ -31,6 +31,7 @@ import (
"github.com/thrasher-corp/gocryptotrader/exchanges/stream"
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
"github.com/thrasher-corp/gocryptotrader/exchanges/ticker"
"github.com/thrasher-corp/gocryptotrader/exchanges/trade"
testexch "github.com/thrasher-corp/gocryptotrader/internal/testing/exchange"
testsubs "github.com/thrasher-corp/gocryptotrader/internal/testing/subscriptions"
mockws "github.com/thrasher-corp/gocryptotrader/internal/testing/websocket"
@@ -1356,9 +1357,8 @@ func TestWSOrderbook(t *testing.T) {
assert.Equal(t, 0.56281, liq, "Bid Liquidity should be correct")
}
// TestWSTradeDetail checks we can send a trade detail through
// We can't currently easily see the result with the current DB instance, so we just check it doesn't error
func TestWSTradeDetail(t *testing.T) {
// TestWSHandleAllTradesMsg ensures wsHandleAllTrades sends trade.Data to the ws.DataHandler
func TestWSHandleAllTradesMsg(t *testing.T) {
t.Parallel()
h := new(HUOBI) //nolint:govet // Intentional shadow to avoid future copy/paste mistakes
require.NoError(t, testexch.Setup(h), "Setup Instance must not error")
@@ -1367,6 +1367,40 @@ func TestWSTradeDetail(t *testing.T) {
h.SetSaveTradeDataStatus(true)
testexch.FixtureToDataHandler(t, "testdata/wsAllTrades.json", h.wsHandleData)
close(h.Websocket.DataHandler)
exp := []trade.Data{
{
Exchange: h.Name,
CurrencyPair: btcusdtPair,
Timestamp: time.UnixMilli(1630994963173).UTC(),
Price: 52648.62,
Amount: 0.006754,
Side: order.Buy,
TID: "102523573486",
AssetType: asset.Spot,
},
{
Exchange: h.Name,
CurrencyPair: btcusdtPair,
Timestamp: time.UnixMilli(1630994963184).UTC(),
Price: 52648.73,
Amount: 0.006755,
Side: order.Sell,
TID: "102523573487",
AssetType: asset.Spot,
},
}
require.Len(t, h.Websocket.DataHandler, 2, "Must see correct number of trades")
for resp := range h.Websocket.DataHandler {
switch v := resp.(type) {
case trade.Data:
i := 1 - len(h.Websocket.DataHandler)
require.Equalf(t, exp[i], v, "Trade [%d] must be correct", i)
case error:
t.Error(v)
default:
t.Errorf("Unexpected type in DataHandler: %T(%s)", v, v)
}
}
require.Empty(t, h.Websocket.DataHandler, "Must not see any errors going to datahandler")
}

View File

@@ -229,7 +229,9 @@ func (h *HUOBI) wsHandleCandleMsg(s *subscription.Subscription, respRaw []byte)
}
func (h *HUOBI) wsHandleAllTradesMsg(s *subscription.Subscription, respRaw []byte) error {
if !h.IsSaveTradeDataEnabled() {
saveTradeData := h.IsSaveTradeDataEnabled()
tradeFeed := h.IsTradeFeedEnabled()
if !saveTradeData && !tradeFeed {
return nil
}
if len(s.Pairs) != 1 {
@@ -249,14 +251,22 @@ func (h *HUOBI) wsHandleAllTradesMsg(s *subscription.Subscription, respRaw []byt
Exchange: h.Name,
AssetType: s.Asset,
CurrencyPair: s.Pairs[0],
Timestamp: t.Tick.Data[i].Timestamp.Time(),
Timestamp: t.Tick.Data[i].Timestamp.Time().UTC(),
Amount: t.Tick.Data[i].Amount,
Price: t.Tick.Data[i].Price,
Side: side,
TID: strconv.FormatFloat(t.Tick.Data[i].TradeID, 'f', -1, 64),
})
}
return trade.AddTradesToBuffer(trades...)
if tradeFeed {
for i := range trades {
h.Websocket.DataHandler <- trades[i]
}
}
if saveTradeData {
return trade.AddTradesToBuffer(trades...)
}
return nil
}
func (h *HUOBI) wsHandleTickerMsg(s *subscription.Subscription, respRaw []byte) error {

View File

@@ -1 +1,2 @@
{"ch":"market.btcusdt.trade.detail","ts":1630994963175,"tick":{"id":137005445109,"ts":1630994963173,"data":[{"id":137005445109359290000000000,"ts":1630994963173,"tradeId":102523573486,"amount":0.006754,"price":52648.62,"direction":"buy"}]}}
{"ch":"market.btcusdt.trade.detail","ts":1630994963174,"tick":{"id":137005445110,"ts":1630994963184,"data":[{"id":137005445109359300000000000,"ts":1630994963184,"tradeId":102523573487,"amount":0.006755,"price":52648.73,"direction":"sell"}]}}

View File

@@ -1872,7 +1872,9 @@
},
"enabled": {
"autoPairUpdates": true,
"websocketAPI": true
"websocketAPI": true,
"saveTradeData": false,
"tradeFeed": true
}
},
"bankAccounts": [