-
-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
267 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"topic": "kline.5.BTCUSDT", | ||
"data": [ | ||
{ | ||
"start": 1672324800000, | ||
"end": 1672325099999, | ||
"interval": "5", | ||
"open": "16649.5", | ||
"close": "16677", | ||
"high": "16677", | ||
"low": "16608", | ||
"volume": "2.081", | ||
"turnover": "34666.4005", | ||
"confirm": false, | ||
"timestamp": 1672324988882 | ||
} | ||
], | ||
"ts": 1672324988882, | ||
"type": "snapshot" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package bybit | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/gorilla/websocket" | ||
) | ||
|
||
// SubscribeKline : | ||
func (s *V5WebsocketPublicService) SubscribeKline( | ||
key V5WebsocketPublicKlineParamKey, | ||
f func(V5WebsocketPublicKlineResponse) error, | ||
) (func() error, error) { | ||
if err := s.addParamKlineFunc(key, f); err != nil { | ||
return nil, err | ||
} | ||
param := struct { | ||
Op string `json:"op"` | ||
Args []interface{} `json:"args"` | ||
}{ | ||
Op: "subscribe", | ||
Args: []interface{}{key.Topic()}, | ||
} | ||
buf, err := json.Marshal(param) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if err := s.connection.WriteMessage(websocket.TextMessage, buf); err != nil { | ||
return nil, err | ||
} | ||
return func() error { | ||
param := struct { | ||
Op string `json:"op"` | ||
Args []interface{} `json:"args"` | ||
}{ | ||
Op: "unsubscribe", | ||
Args: []interface{}{key.Topic()}, | ||
} | ||
buf, err := json.Marshal(param) | ||
if err != nil { | ||
return err | ||
} | ||
if err := s.connection.WriteMessage(websocket.TextMessage, []byte(buf)); err != nil { | ||
return err | ||
} | ||
s.removeParamKlineFunc(key) | ||
return nil | ||
}, nil | ||
} | ||
|
||
// V5WebsocketPublicKlineParamKey : | ||
type V5WebsocketPublicKlineParamKey struct { | ||
Interval Interval | ||
Symbol SymbolV5 | ||
} | ||
|
||
// Topic : | ||
func (k *V5WebsocketPublicKlineParamKey) Topic() string { | ||
return fmt.Sprintf("%s.%s.%s", V5WebsocketPublicTopicKline, k.Interval, k.Symbol) | ||
} | ||
|
||
// V5WebsocketPublicKlineResponse : | ||
type V5WebsocketPublicKlineResponse struct { | ||
Topic string `json:"topic"` | ||
Type string `json:"type"` | ||
TimeStamp int64 `json:"ts"` | ||
Data []V5WebsocketPublicKlineData `json:"data"` | ||
} | ||
|
||
// V5WebsocketPublicKlineData : | ||
type V5WebsocketPublicKlineData struct { | ||
Start int `json:"start"` | ||
End int `json:"end"` | ||
Interval Interval `json:"interval"` | ||
Open string `json:"open"` | ||
Close string `json:"close"` | ||
High string `json:"high"` | ||
Low string `json:"low"` | ||
Volume string `json:"volume"` | ||
Turnover string `json:"turnover"` | ||
Confirm bool `json:"confirm"` | ||
Timestamp int `json:"timestamp"` | ||
} | ||
|
||
// Key : | ||
func (r *V5WebsocketPublicKlineResponse) Key() V5WebsocketPublicKlineParamKey { | ||
topic := r.Topic | ||
arr := strings.Split(topic, ".") | ||
if arr[0] != V5WebsocketPublicTopicKline || len(arr) != 3 { | ||
return V5WebsocketPublicKlineParamKey{} | ||
} | ||
|
||
return V5WebsocketPublicKlineParamKey{ | ||
Interval: Interval(arr[1]), | ||
Symbol: SymbolV5(arr[2]), | ||
} | ||
} | ||
|
||
// addParamKlineFunc : | ||
func (s *V5WebsocketPublicService) addParamKlineFunc(key V5WebsocketPublicKlineParamKey, f func(V5WebsocketPublicKlineResponse) error) error { | ||
if _, exist := s.paramKlineMap[key]; exist { | ||
return errors.New("already registered for this key") | ||
} | ||
s.paramKlineMap[key] = f | ||
return nil | ||
} | ||
|
||
// removeParamTradeFunc : | ||
func (s *V5WebsocketPublicService) removeParamKlineFunc(key V5WebsocketPublicKlineParamKey) { | ||
delete(s.paramKlineMap, key) | ||
} | ||
|
||
// retrievePositionFunc : | ||
func (s *V5WebsocketPublicService) retrieveKlineFunc(key V5WebsocketPublicKlineParamKey) (func(V5WebsocketPublicKlineResponse) error, error) { | ||
f, exist := s.paramKlineMap[key] | ||
if !exist { | ||
return nil, errors.New("func not found") | ||
} | ||
return f, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package bybit | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/hirokisan/bybit/v2/testhelper" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestWebsocketV5Public_Kline(t *testing.T) { | ||
respBody := map[string]interface{}{ | ||
"topic": "kline.5.BTCUSDT", | ||
"type": "snapshot", | ||
"ts": 1672324988882, | ||
"data": []map[string]interface{}{ | ||
{ | ||
"start": 1672324800000, | ||
"end": 1672325099999, | ||
"interval": "5", | ||
"open": "16649.5", | ||
"close": "16677", | ||
"high": "16677", | ||
"low": "16608", | ||
"volume": "2.081", | ||
"turnover": "34666.4005", | ||
"confirm": false, | ||
"timestamp": 1672324988882, | ||
}, | ||
}, | ||
} | ||
bytesBody, err := json.Marshal(respBody) | ||
require.NoError(t, err) | ||
|
||
category := CategoryV5Linear | ||
|
||
server, teardown := testhelper.NewWebsocketServer( | ||
testhelper.WithWebsocketHandlerOption(V5WebsocketPublicPathFor(category), bytesBody), | ||
) | ||
defer teardown() | ||
|
||
wsClient := NewTestWebsocketClient(). | ||
WithBaseURL(server.URL) | ||
|
||
svc, err := wsClient.V5().Public(category) | ||
require.NoError(t, err) | ||
|
||
{ | ||
_, err := svc.SubscribeKline( | ||
V5WebsocketPublicKlineParamKey{ | ||
Interval: Interval5, | ||
Symbol: SymbolV5BTCUSDT, | ||
}, | ||
func(response V5WebsocketPublicKlineResponse) error { | ||
assert.Equal(t, respBody["topic"], response.Topic) | ||
return nil | ||
}, | ||
) | ||
require.NoError(t, err) | ||
} | ||
|
||
assert.NoError(t, svc.Run()) | ||
assert.NoError(t, svc.Ping()) | ||
assert.NoError(t, svc.Close()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters