Skip to content

Commit

Permalink
add v5 public SubscribeTickers (#195)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sagleft authored Nov 15, 2024
1 parent 0067576 commit 035189a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions v5_ws_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type V5WebsocketPublicServiceI interface {
func(V5WebsocketPublicTickerResponse) error,
) (func() error, error)

SubscribeTickers(
[]V5WebsocketPublicTickerParamKey,
func(V5WebsocketPublicTickerResponse) error,
) (func() error, error)

SubscribeTrade(
V5WebsocketPublicTradeParamKey,
func(V5WebsocketPublicTradeResponse) error,
Expand Down
34 changes: 29 additions & 5 deletions v5_ws_public_ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,36 @@ func (s *V5WebsocketPublicService) SubscribeTicker(
key V5WebsocketPublicTickerParamKey,
f func(V5WebsocketPublicTickerResponse) error,
) (func() error, error) {
if err := s.addParamTickerFunc(key, f); err != nil {
return nil, err
return s.subscribeTickers([]V5WebsocketPublicTickerParamKey{key}, f)
}

// SubscribeTickers :
func (s *V5WebsocketPublicService) SubscribeTickers(
keys []V5WebsocketPublicTickerParamKey,
f func(V5WebsocketPublicTickerResponse) error,
) (func() error, error) {
return s.subscribeTickers(keys, f)
}

func (s *V5WebsocketPublicService) subscribeTickers(
keys []V5WebsocketPublicTickerParamKey,
f func(V5WebsocketPublicTickerResponse) error,
) (func() error, error) {
var args []interface{}
for _, key := range keys {
if err := s.addParamTickerFunc(key, f); err != nil {
return nil, err
}

args = append(args, key.Topic())
}

param := struct {
Op string `json:"op"`
Args []interface{} `json:"args"`
}{
Op: "subscribe",
Args: []interface{}{key.Topic()},
Args: args,
}
buf, err := json.Marshal(param)
if err != nil {
Expand All @@ -37,7 +58,7 @@ func (s *V5WebsocketPublicService) SubscribeTicker(
Args []interface{} `json:"args"`
}{
Op: "unsubscribe",
Args: []interface{}{key.Topic()},
Args: args,
}
buf, err := json.Marshal(param)
if err != nil {
Expand All @@ -46,7 +67,10 @@ func (s *V5WebsocketPublicService) SubscribeTicker(
if err := s.writeMessage(websocket.TextMessage, []byte(buf)); err != nil {
return err
}
s.removeParamTickerFunc(key)

for _, key := range keys {
s.removeParamTickerFunc(key)
}
return nil
}, nil
}
Expand Down

0 comments on commit 035189a

Please sign in to comment.