forked from hirokisan/bybit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v5_ws_public_trade.go
115 lines (103 loc) · 3.12 KB
/
v5_ws_public_trade.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package bybit
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/gorilla/websocket"
)
// SubscribeTrade :
func (s *V5WebsocketPublicService) SubscribeTrade(
key V5WebsocketPublicTradeParamKey,
f func(V5WebsocketPublicTradeResponse) error,
) (func() error, error) {
if err := s.addParamTradeFunc(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.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.writeMessage(websocket.TextMessage, buf); err != nil {
return err
}
s.removeParamTradeFunc(key)
return nil
}, nil
}
// V5WebsocketPublicTradeParamKey :
type V5WebsocketPublicTradeParamKey struct {
Symbol SymbolV5
}
// Topic :
func (k *V5WebsocketPublicTradeParamKey) Topic() string {
return fmt.Sprintf("%s.%s", V5WebsocketPublicTopicTrade, k.Symbol)
}
// V5WebsocketPublicTradeResponse :
type V5WebsocketPublicTradeResponse struct {
Topic string `json:"topic"`
Type string `json:"type"`
TimeStamp int64 `json:"ts"`
Data []V5WebsocketPublicTradeData `json:"data"`
}
// V5WebsocketPublicTradeData :
type V5WebsocketPublicTradeData struct {
Timestamp uint64 `json:"T"` // The timestamp (ms) that the order is filled
Symbol SymbolV5 `json:"s"` // Symbol name
Side Side `json:"S"` // Side of taker. Buy,Sell
Value string `json:"v"` // Trade size
Trade string `json:"p"` // Trade price
Direction string `json:"L"` // Direction of price change. Unique field for future
ID string `json:"i"` // Trade ID
BlockTrade bool `json:"BT"` // Whether it is a block trade order or not
}
// Key :
func (r *V5WebsocketPublicTradeResponse) Key() V5WebsocketPublicTradeParamKey {
topic := r.Topic
arr := strings.Split(topic, ".")
if arr[0] != V5WebsocketPublicTopicTrade.String() || len(arr) != 2 {
return V5WebsocketPublicTradeParamKey{}
}
return V5WebsocketPublicTradeParamKey{
Symbol: SymbolV5(arr[1]),
}
}
// addParamTradeFunc :
func (s *V5WebsocketPublicService) addParamTradeFunc(key V5WebsocketPublicTradeParamKey, f func(V5WebsocketPublicTradeResponse) error) error {
if _, exist := s.paramTradeMap.Get(key); exist {
return errors.New("already registered for this key")
}
s.paramTradeMap.Add(key, f)
return nil
}
// removeParamTradeFunc :
func (s *V5WebsocketPublicService) removeParamTradeFunc(key V5WebsocketPublicTradeParamKey) {
s.paramTradeMap.Remove(key)
}
// retrievePositionFunc :
func (s *V5WebsocketPublicService) retrieveTradeFunc(key V5WebsocketPublicTradeParamKey) (func(V5WebsocketPublicTradeResponse) error, bool) {
f, exist := s.paramTradeMap.Get(key)
return f, exist
}