diff --git a/exchanges/huobi/huobi_test.go b/exchanges/huobi/huobi_test.go index 6a096d2e..8bb32a2c 100644 --- a/exchanges/huobi/huobi_test.go +++ b/exchanges/huobi/huobi_test.go @@ -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") } diff --git a/exchanges/huobi/huobi_websocket.go b/exchanges/huobi/huobi_websocket.go index ab1dcdfa..a7bc52bb 100644 --- a/exchanges/huobi/huobi_websocket.go +++ b/exchanges/huobi/huobi_websocket.go @@ -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 { diff --git a/exchanges/huobi/testdata/wsAllTrades.json b/exchanges/huobi/testdata/wsAllTrades.json index 916c41ce..e37d1ccd 100644 --- a/exchanges/huobi/testdata/wsAllTrades.json +++ b/exchanges/huobi/testdata/wsAllTrades.json @@ -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"}]}} diff --git a/testdata/configtest.json b/testdata/configtest.json index eaf1073c..06f4a433 100644 --- a/testdata/configtest.json +++ b/testdata/configtest.json @@ -1872,7 +1872,9 @@ }, "enabled": { "autoPairUpdates": true, - "websocketAPI": true + "websocketAPI": true, + "saveTradeData": false, + "tradeFeed": true } }, "bankAccounts": [