forked from hirokisan/bybit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v5_ws_private_execution.go
119 lines (110 loc) · 3.7 KB
/
v5_ws_private_execution.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
116
117
118
119
package bybit
import (
"encoding/json"
"errors"
"github.com/gorilla/websocket"
)
// SubscribeExecution :
func (s *V5WebsocketPrivateService) SubscribeExecution(
f func(V5WebsocketPrivateExecutionResponse) error,
) (func() error, error) {
key := V5WebsocketPrivateParamKey{
Topic: V5WebsocketPrivateTopicExecution,
}
if err := s.addParamExecutionFunc(key, f); err != nil {
return nil, err
}
param := struct {
Op string `json:"op"`
Args []interface{} `json:"args"`
}{
Op: "subscribe",
Args: []interface{}{V5WebsocketPrivateTopicExecution},
}
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{}{V5WebsocketPrivateTopicExecution},
}
buf, err := json.Marshal(param)
if err != nil {
return err
}
if err := s.writeMessage(websocket.TextMessage, []byte(buf)); err != nil {
return err
}
s.removeParamExecutionFunc(key)
return nil
}, nil
}
// V5WebsocketPrivateExecutionResponse :
type V5WebsocketPrivateExecutionResponse struct {
ID string `json:"id"`
Topic V5WebsocketPrivateTopic `json:"topic"`
CreationTime int64 `json:"creationTime"`
Data []V5WebsocketPrivateExecutionData `json:"data"`
}
// V5WebsocketPrivateExecutionData :
type V5WebsocketPrivateExecutionData struct {
Category CategoryV5 `json:"category"`
Symbol SymbolV5 `json:"symbol"`
ExecFee string `json:"execFee"`
ExecID string `json:"execId"`
ExecPrice string `json:"execPrice"`
ExecQty string `json:"execQty"`
ExecType ExecTypeV5 `json:"execType"`
ExecValue string `json:"execValue"`
IsMaker bool `json:"isMaker"`
FeeRate string `json:"feeRate"`
TradeIv string `json:"tradeIv"`
MarkIv string `json:"markIv"`
BlockTradeID string `json:"blockTradeId"`
MarkPrice string `json:"markPrice"`
IndexPrice string `json:"indexPrice"`
UnderlyingPrice string `json:"underlyingPrice"`
LeavesQty string `json:"leavesQty"`
OrderID string `json:"orderId"`
OrderLinkID string `json:"orderLinkId"`
OrderPrice string `json:"orderPrice"`
OrderQty string `json:"orderQty"`
OrderType OrderType `json:"orderType"`
StopOrderType OrderType `json:"stopOrderType"`
Side Side `json:"side"`
ExecTime string `json:"execTime"`
IsLeverage string `json:"isLeverage"`
ClosedSize string `json:"closedSize"`
CrossSequence int64 `json:"seq"`
}
// Key :
func (r *V5WebsocketPrivateExecutionResponse) Key() V5WebsocketPrivateParamKey {
return V5WebsocketPrivateParamKey{
Topic: r.Topic,
}
}
// addParamExecutionFunc :
func (s *V5WebsocketPrivateService) addParamExecutionFunc(param V5WebsocketPrivateParamKey, f func(V5WebsocketPrivateExecutionResponse) error) error {
if _, exist := s.paramExecutionMap.Get(param); exist {
return errors.New("already registered for this param")
}
s.paramExecutionMap.Add(param, f)
return nil
}
// removeParamExecutionFunc :
func (s *V5WebsocketPrivateService) removeParamExecutionFunc(key V5WebsocketPrivateParamKey) {
s.paramExecutionMap.Remove(key)
}
// retrieveExecutionFunc :
func (s *V5WebsocketPrivateService) retrieveExecutionFunc(key V5WebsocketPrivateParamKey) (func(V5WebsocketPrivateExecutionResponse) error, bool) {
f, exist := s.paramExecutionMap.Get(key)
return f, exist
}