Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add function for Diff. Depth Stream with an update speed of 100ms #340

Merged
merged 1 commit into from Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions v2/websocket_service.go
Expand Up @@ -211,6 +211,19 @@ func WsCombinedDepthServe(symbols []string, handler WsDepthHandler, errHandler E
endpoint += fmt.Sprintf("%s@depth", strings.ToLower(s)) + "/"
}
endpoint = endpoint[:len(endpoint)-1]
return wsCombinedDepthServe(endpoint, handler, errHandler)
}

func WsCombinedDepthServe100Ms(symbols []string, handler WsDepthHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
endpoint := getCombinedEndpoint()
for _, s := range symbols {
endpoint += fmt.Sprintf("%s@depth@100ms", strings.ToLower(s)) + "/"
}
endpoint = endpoint[:len(endpoint)-1]
return wsCombinedDepthServe(endpoint, handler, errHandler)
}

func wsCombinedDepthServe(endpoint string, handler WsDepthHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
cfg := newWsConfig(endpoint)
wsHandler := func(message []byte) {
j, err := newJSON(message)
Expand Down
52 changes: 52 additions & 0 deletions v2/websocket_service_test.go
Expand Up @@ -436,6 +436,58 @@ func (s *websocketServiceTestSuite) TestCombinedDepthServe() {
<-doneC
}

func (s *websocketServiceTestSuite) TestCombinedDepthServe100Ms() {
data := []byte(`{
"stream":"btcusdt@depth",
"data":{
"e":"depthUpdate",
"E":1629769560797,
"s":"BTCUSDT",
"U":13544035,
"u":13544037,
"b":[["49095.23000000","0.01018500"],["49081.00000000","0.00000000"]],
"a":[["49095.65000000","0.01018500"]]}}
`)
symbols := []string{
"BTCUSDT",
"ETHUSDT",
}
fakeErrMsg := "fake error"
s.mockWsServe(data, errors.New(fakeErrMsg))
defer s.assertWsServe()
doneC, stopC, err := WsCombinedDepthServe100Ms(symbols, func(event *WsDepthEvent) {
e := &WsDepthEvent{
Symbol: "BTCUSDT",
Time: 1629769560797,
LastUpdateID: 13544037,
FirstUpdateID: 13544035,
Bids: []Bid{
{
Price: "49095.23000000",
Quantity: "0.01018500",
},
{
Price: "49081.00000000",
Quantity: "0.00000000",
},
},
Asks: []Ask{
{
Price: "49095.65000000",
Quantity: "0.01018500",
},
},
}
s.assertWsDepthEventEqual(e, event)
},
func(err error) {
s.r().EqualError(err, fakeErrMsg)
})
s.r().NoError(err)
stopC <- struct{}{}
<-doneC
}

func (s *websocketServiceTestSuite) TestKlineServe() {
data := []byte(`{
"e": "kline",
Expand Down