Skip to content

Commit

Permalink
fix max websocket subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
c9s committed Feb 22, 2021
1 parent 73cb80e commit 507586b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pkg/exchange/max/maxapi/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ var WebSocketURL = "wss://max-stream.maicoin.com/ws"

var ErrMessageTypeNotSupported = errors.New("message type currently not supported")

type SubscribeOptions struct {
Depth int `json:"depth,omitempty"`
Resolution string `json:"resolution,omitempty"`
}

// Subscription is used for presenting the subscription metadata.
// This is used for sending subscribe and unsubscribe requests
type Subscription struct {
Expand Down Expand Up @@ -212,10 +217,12 @@ func (s *WebSocketService) Reconnect() {

// Subscribe is a helper method for building subscription request from the internal mapping types.
// (Internal public method)
func (s *WebSocketService) Subscribe(channel, market string) {
func (s *WebSocketService) Subscribe(channel, market string, options SubscribeOptions) {
s.AddSubscription(Subscription{
Channel: channel,
Market: market,
Depth: options.Depth,
Resolution: options.Resolution,
})
}

Expand Down
16 changes: 15 additions & 1 deletion pkg/exchange/max/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,21 @@ func (s *Stream) SetPublicOnly() {
}

func (s *Stream) Subscribe(channel types.Channel, symbol string, options types.SubscribeOptions) {
s.websocketService.Subscribe(string(channel), toLocalSymbol(symbol))
opt := max.SubscribeOptions{}

if len(options.Depth) > 0 {
depth, err := strconv.Atoi(options.Depth)
if err != nil {
panic(err)
}
opt.Depth = depth
}

if len(options.Interval) > 0 {
opt.Resolution = options.Interval
}

s.websocketService.Subscribe(string(channel), toLocalSymbol(symbol), opt)
}

func (s *Stream) Connect(ctx context.Context) error {
Expand Down

0 comments on commit 507586b

Please sign in to comment.