bbgo_origin/pkg/exchange/kucoin/parse.go

105 lines
2.2 KiB
Go
Raw Normal View History

2021-12-22 18:03:13 +00:00
package kucoin
2021-12-23 03:04:22 +00:00
import (
"encoding/json"
"strings"
2021-12-23 03:04:22 +00:00
"github.com/c9s/bbgo/pkg/types"
2021-12-23 03:04:22 +00:00
)
func parseWebSocketEvent(in []byte) (interface{}, error) {
var resp WebSocketEvent
2021-12-23 03:04:22 +00:00
var err = json.Unmarshal(in, &resp)
if err != nil {
return nil, err
}
switch resp.Type {
case WebSocketMessageTypeAck:
2021-12-23 03:04:22 +00:00
return &resp, nil
case WebSocketMessageTypeError:
resp.Object = string(resp.Data)
return &resp, nil
case WebSocketMessageTypeMessage:
2021-12-23 03:04:22 +00:00
switch resp.Subject {
case WebSocketSubjectOrderChange:
var o WebSocketPrivateOrderEvent
2021-12-23 03:04:22 +00:00
if err := json.Unmarshal(resp.Data, &o); err != nil {
return &resp, err
}
resp.Object = &o
case WebSocketSubjectAccountBalance:
var o WebSocketAccountBalanceEvent
2021-12-23 03:04:22 +00:00
if err := json.Unmarshal(resp.Data, &o); err != nil {
return &resp, err
}
resp.Object = &o
case WebSocketSubjectTradeCandlesUpdate, WebSocketSubjectTradeCandlesAdd:
var o WebSocketCandleEvent
2021-12-23 03:04:22 +00:00
if err := json.Unmarshal(resp.Data, &o); err != nil {
return &resp, err
}
o.Interval = extractIntervalFromTopic(resp.Topic)
o.Add = resp.Subject == WebSocketSubjectTradeCandlesAdd
2021-12-23 03:04:22 +00:00
resp.Object = &o
case WebSocketSubjectTradeL2Update:
var o WebSocketOrderBookL2Event
2021-12-23 03:04:22 +00:00
if err := json.Unmarshal(resp.Data, &o); err != nil {
return &resp, err
}
resp.Object = &o
case WebSocketSubjectTradeTicker:
var o WebSocketTickerEvent
2021-12-23 03:04:22 +00:00
if err := json.Unmarshal(resp.Data, &o); err != nil {
return &resp, err
}
resp.Object = &o
default:
// return nil, fmt.Errorf("kucoin: unsupported subject: %s", resp.Subject)
}
}
return &resp, nil
}
func extractIntervalFromTopic(topic string) types.Interval {
ta := strings.Split(topic, ":")
tb := strings.Split(ta[1], "_")
interval := tb[1]
return toGlobalInterval(interval)
}
func toGlobalInterval(a string) types.Interval {
switch a {
case "1min":
return types.Interval1m
case "5min":
return types.Interval5m
case "15min":
return types.Interval15m
case "30min":
return types.Interval30m
case "1hour":
return types.Interval1h
case "2hour":
return types.Interval2h
case "4hour":
return types.Interval4h
case "6hour":
return types.Interval6h
case "12hour":
return types.Interval12h
}
return ""
}