Skip to content

Commit

Permalink
Fix WebSocket data parsing to address omission of category indicators (
Browse files Browse the repository at this point in the history
  • Loading branch information
kazz187 authored Mar 26, 2024
1 parent bf6bfc3 commit 5f697f6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
1 change: 1 addition & 0 deletions v5_client_web_socket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (s *V5WebsocketService) Public(category CategoryV5) (V5WebsocketPublicServi
return &V5WebsocketPublicService{
client: s.client,
connection: c,
category: category,
paramOrderBookMap: make(map[V5WebsocketPublicOrderBookParamKey]func(V5WebsocketPublicOrderBookResponse) error),
paramKlineMap: make(map[V5WebsocketPublicKlineParamKey]func(V5WebsocketPublicKlineResponse) error),
paramTickerMap: make(map[V5WebsocketPublicTickerParamKey]func(V5WebsocketPublicTickerResponse) error),
Expand Down
20 changes: 8 additions & 12 deletions v5_ws_public.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ type V5WebsocketPublicServiceI interface {
type V5WebsocketPublicService struct {
client *WebSocketClient
connection *websocket.Conn
category CategoryV5

mu sync.Mutex

Expand Down Expand Up @@ -119,21 +120,15 @@ func (s *V5WebsocketPublicService) judgeTopic(respBody []byte) (V5WebsocketPubli

// UnmarshalJSON :
func (r *V5WebsocketPublicTickerData) UnmarshalJSON(data []byte) error {
var res struct {
Bid1Price string `json:"bid1Price"`
Gamma string `json:"gamma"`
}
if err := json.Unmarshal(data, &res); err != nil {
return err
}

if res.Bid1Price != "" {
switch r.category {
case CategoryV5Linear, CategoryV5Inverse:
return json.Unmarshal(data, &r.LinearInverse)
}
if res.Gamma != "" {
case CategoryV5Option:
return json.Unmarshal(data, &r.Option)
case CategoryV5Spot:
return json.Unmarshal(data, &r.Spot)
}
return json.Unmarshal(data, &r.Spot)
return errors.New("unsupported format")
}

// parseResponse :
Expand Down Expand Up @@ -238,6 +233,7 @@ func (s *V5WebsocketPublicService) Run() error {
}
case V5WebsocketPublicTopicTicker:
var resp V5WebsocketPublicTickerResponse
resp.Data.category = s.category
if err := s.parseResponse(message, &resp); err != nil {
return err
}
Expand Down
1 change: 1 addition & 0 deletions v5_ws_public_ticker.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type V5WebsocketPublicTickerResponse struct {

// V5WebsocketPublicTickerData :
type V5WebsocketPublicTickerData struct {
category CategoryV5
LinearInverse *V5WebsocketPublicTickerLinearInverseResult
Option *V5WebsocketPublicTickerOptionResult
Spot *V5WebsocketPublicTickerSpotResult
Expand Down

0 comments on commit 5f697f6

Please sign in to comment.