forked from hirokisan/bybit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v5_ws_public_ticker.go
183 lines (168 loc) · 6.36 KB
/
v5_ws_public_ticker.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package bybit
import (
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/gorilla/websocket"
)
// SubscribeTicker :
func (s *V5WebsocketPublicService) SubscribeTicker(
key V5WebsocketPublicTickerParamKey,
f func(V5WebsocketPublicTickerResponse) error,
) (func() error, error) {
if err := s.addParamTickerFunc(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.removeParamTickerFunc(key)
return nil
}, nil
}
// V5WebsocketPublicTickerParamKey :
type V5WebsocketPublicTickerParamKey struct {
Symbol SymbolV5
}
// Topic :
func (k *V5WebsocketPublicTickerParamKey) Topic() string {
return fmt.Sprintf("%s.%s", V5WebsocketPublicTopicTicker, k.Symbol)
}
// V5WebsocketPublicTickerResponse :
type V5WebsocketPublicTickerResponse struct {
Topic string `json:"topic"`
Type string `json:"type"`
TimeStamp int64 `json:"ts"`
CrossSeq int64 `json:"cs,omitempty"`
ID string `json:"id,omitempty"`
Data V5WebsocketPublicTickerData `json:"data"`
}
// V5WebsocketPublicTickerData :
type V5WebsocketPublicTickerData struct {
category CategoryV5
LinearInverse *V5WebsocketPublicTickerLinearInverseResult
Option *V5WebsocketPublicTickerOptionResult
Spot *V5WebsocketPublicTickerSpotResult
}
// V5WebsocketPublicTickerLinearInverseResult :
type V5WebsocketPublicTickerLinearInverseResult struct {
Symbol SymbolV5 `json:"symbol"`
TickDirection TickDirection `json:"tickDirection"`
Price24hPercent string `json:"price24hPcnt"`
LastPrice string `json:"lastPrice"`
PrevPrice24h string `json:"prevPrice24h"`
HighPrice24h string `json:"highPrice24h"`
LowPrice24h string `json:"lowPrice24h"`
PrevPrice1h string `json:"prevPrice1h"`
MarkPrice string `json:"markPrice"`
IndexPrice string `json:"indexPrice"`
OpenInterest string `json:"openInterest"`
OpenInterestValue string `json:"openInterestValue"`
Turnover24h string `json:"turnover24h"`
Volume24h string `json:"volume24h"`
NextFundingTime string `json:"nextFundingTime"`
FundingRate string `json:"fundingRate"`
Bid1Price string `json:"bid1Price"`
Bid1Size string `json:"bid1Size"`
Ask1Price string `json:"ask1Price"`
Ask1Size string `json:"ask1Size"`
DeliveryTime string `json:"deliveryTime,omitempty"`
BasisRate string `json:"basisRate,omitempty"`
DeliveryFeeRate string `json:"deliveryFeeRate,omitempty"`
PredictedDeliveryPrice string `json:"predictedDeliveryPrice,omitempty"`
}
// V5WebsocketPublicTickerOptionResult :
type V5WebsocketPublicTickerOptionResult struct {
Symbol SymbolV5 `json:"symbol"`
BidPrice string `json:"bidPrice"`
BidSize string `json:"bidSize"`
BidIv string `json:"bidIv"`
AskPrice string `json:"askPrice"`
AskSize string `json:"askSize"`
AskIv string `json:"askIv"`
LastPrice string `json:"lastPrice"`
HighPrice24H string `json:"highPrice24h"`
LowPrice24H string `json:"lowPrice24h"`
MarkPrice string `json:"markPrice"`
IndexPrice string `json:"indexPrice"`
MarkPriceIv string `json:"markPriceIv"`
UnderlyingPrice string `json:"underlyingPrice"`
OpenInterest string `json:"openInterest"`
Turnover24H string `json:"turnover24h"`
Volume24H string `json:"volume24h"`
TotalVolume string `json:"totalVolume"`
TotalTurnover string `json:"totalTurnover"`
Delta string `json:"delta"`
Gamma string `json:"gamma"`
Vega string `json:"vega"`
Theta string `json:"theta"`
PredictedDeliveryPrice string `json:"predictedDeliveryPrice"`
Change24H string `json:"change24h"`
}
// V5WebsocketPublicTickerSpotResult :
type V5WebsocketPublicTickerSpotResult struct {
Symbol SymbolV5 `json:"symbol"`
LastPrice string `json:"lastPrice"`
HighPrice24H string `json:"highPrice24h"`
LowPrice24H string `json:"lowPrice24h"`
PrevPrice24H string `json:"prevPrice24h"`
Volume24H string `json:"volume24h"`
Turnover24H string `json:"turnover24h"`
Price24HPcnt string `json:"price24hPcnt"`
UsdIndexPrice string `json:"usdIndexPrice"`
}
// Key :
func (r *V5WebsocketPublicTickerResponse) Key() V5WebsocketPublicTickerParamKey {
topic := r.Topic
arr := strings.Split(topic, ".")
if arr[0] != V5WebsocketPublicTopicTicker.String() || len(arr) != 2 {
return V5WebsocketPublicTickerParamKey{}
}
return V5WebsocketPublicTickerParamKey{
Symbol: SymbolV5(arr[1]),
}
}
// addParamTickerFunc :
func (s *V5WebsocketPublicService) addParamTickerFunc(key V5WebsocketPublicTickerParamKey, f func(V5WebsocketPublicTickerResponse) error) error {
if _, exist := s.paramTickerMap.Get(key); exist {
return errors.New("already registered for this key")
}
s.paramTickerMap.Add(key, f)
return nil
}
// removeParamTickerFunc :
func (s *V5WebsocketPublicService) removeParamTickerFunc(key V5WebsocketPublicTickerParamKey) {
s.paramTickerMap.Remove(key)
}
// retrieveTickerFunc :
func (s *V5WebsocketPublicService) retrieveTickerFunc(key V5WebsocketPublicTickerParamKey) (func(V5WebsocketPublicTickerResponse) error, bool) {
f, exist := s.paramTickerMap.Get(key)
return f, exist
}