-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
v5_ws_public_liquidation.go
115 lines (103 loc) · 3.31 KB
/
v5_ws_public_liquidation.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"
)
// SubscribeLiquidation :
func (s *V5WebsocketPublicService) SubscribeLiquidation(
key V5WebsocketPublicLiquidationParamKey,
f func(V5WebsocketPublicLiquidationResponse) error,
) (func() error, error) {
if err := s.addParamLiquidationFunc(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, []byte(buf)); err != nil {
return err
}
s.removeParamLiquidationFunc(key)
return nil
}, nil
}
// V5WebsocketPublicLiquidationParamKey :
type V5WebsocketPublicLiquidationParamKey struct {
Symbol SymbolV5
}
// Topic :
func (k *V5WebsocketPublicLiquidationParamKey) Topic() string {
return fmt.Sprintf("%s.%s", V5WebsocketPublicTopicLiquidation, k.Symbol)
}
// V5WebsocketPublicLiquidationResponse :
type V5WebsocketPublicLiquidationResponse struct {
Topic string `json:"topic"`
Type string `json:"type"`
TimeStamp int64 `json:"ts"`
Data V5WebsocketPublicLiquidationData `json:"data"`
}
// V5WebsocketPublicLiquidationData :
type V5WebsocketPublicLiquidationData struct {
UpdatedTime uint64 `json:"updatedTime"` // The updated timestamp (ms)
Symbol SymbolV5 `json:"symbol"` // Symbol name
Side Side `json:"side"` // Position side. Buy,Sell. When you receive a Buy update, this means that a long position has been liquidated
Size string `json:"size"` // Executed size
Price string `json:"price"` // Bankruptcy price
}
// Key :
func (r *V5WebsocketPublicLiquidationResponse) Key() V5WebsocketPublicLiquidationParamKey {
topic := r.Topic
arr := strings.Split(topic, ".")
if arr[0] != V5WebsocketPublicTopicLiquidation.String() || len(arr) != 2 {
return V5WebsocketPublicLiquidationParamKey{}
}
return V5WebsocketPublicLiquidationParamKey{
Symbol: SymbolV5(arr[1]),
}
}
// addParamLiquidationFunc :
func (s *V5WebsocketPublicService) addParamLiquidationFunc(key V5WebsocketPublicLiquidationParamKey, f func(V5WebsocketPublicLiquidationResponse) error) error {
if _, exist := s.paramLiquidationMap[key]; exist {
return errors.New("already registered for this key")
}
s.paramLiquidationMap[key] = f
return nil
}
// removeParamLiquidationFunc :
func (s *V5WebsocketPublicService) removeParamLiquidationFunc(key V5WebsocketPublicLiquidationParamKey) {
delete(s.paramLiquidationMap, key)
}
// retrievePositionFunc :
func (s *V5WebsocketPublicService) retrieveLiquidationFunc(key V5WebsocketPublicLiquidationParamKey) (func(V5WebsocketPublicLiquidationResponse) error, error) {
f, exist := s.paramLiquidationMap[key]
if !exist {
return nil, errors.New("func not found")
}
return f, nil
}