|
| 1 | +package bybit |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/gorilla/websocket" |
| 10 | +) |
| 11 | + |
| 12 | +// SubscribeAllLiquidation : |
| 13 | +func (s *V5WebsocketPublicService) SubscribeAllLiquidation( |
| 14 | + key V5WebsocketPublicAllLiquidationParamKey, |
| 15 | + f func(V5WebsocketPublicAllLiquidationResponse) error, |
| 16 | +) (func() error, error) { |
| 17 | + if err := s.addParamAllLiquidationFunc(key, f); err != nil { |
| 18 | + return nil, err |
| 19 | + } |
| 20 | + param := struct { |
| 21 | + Op string `json:"op"` |
| 22 | + Args []interface{} `json:"args"` |
| 23 | + }{ |
| 24 | + Op: "subscribe", |
| 25 | + Args: []interface{}{key.Topic()}, |
| 26 | + } |
| 27 | + buf, err := json.Marshal(param) |
| 28 | + if err != nil { |
| 29 | + return nil, err |
| 30 | + } |
| 31 | + if err := s.writeMessage(websocket.TextMessage, buf); err != nil { |
| 32 | + return nil, err |
| 33 | + } |
| 34 | + return func() error { |
| 35 | + param := struct { |
| 36 | + Op string `json:"op"` |
| 37 | + Args []interface{} `json:"args"` |
| 38 | + }{ |
| 39 | + Op: "unsubscribe", |
| 40 | + Args: []interface{}{key.Topic()}, |
| 41 | + } |
| 42 | + buf, err := json.Marshal(param) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + if err := s.writeMessage(websocket.TextMessage, []byte(buf)); err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + s.removeParamAllLiquidationFunc(key) |
| 50 | + return nil |
| 51 | + }, nil |
| 52 | +} |
| 53 | + |
| 54 | +// V5WebsocketPublicAllLiquidationParamKey : |
| 55 | +type V5WebsocketPublicAllLiquidationParamKey struct { |
| 56 | + Symbol SymbolV5 |
| 57 | +} |
| 58 | + |
| 59 | +// Topic : |
| 60 | +func (k *V5WebsocketPublicAllLiquidationParamKey) Topic() string { |
| 61 | + return fmt.Sprintf("%s.%s", V5WebsocketPublicTopicAllLiquidation, k.Symbol) |
| 62 | +} |
| 63 | + |
| 64 | +// V5WebsocketPublicAllLiquidationResponse : |
| 65 | +type V5WebsocketPublicAllLiquidationResponse struct { |
| 66 | + Topic string `json:"topic"` |
| 67 | + Type string `json:"type"` |
| 68 | + TimeStamp int64 `json:"ts"` |
| 69 | + Data []V5WebsocketPublicAllLiquidationData `json:"data"` |
| 70 | +} |
| 71 | + |
| 72 | +// V5WebsocketPublicAllLiquidationData : |
| 73 | +type V5WebsocketPublicAllLiquidationData struct { |
| 74 | + UpdatedTime uint64 `json:"T"` // The updated timestamp (ms) |
| 75 | + Symbol SymbolV5 `json:"s"` // Symbol name |
| 76 | + Side Side `json:"S"` // Position side. Buy,Sell. When you receive a Buy update, this means that a long position has been liquidated |
| 77 | + Size string `json:"v"` // Executed size |
| 78 | + Price string `json:"p"` // Bankruptcy price |
| 79 | +} |
| 80 | + |
| 81 | +// Key : |
| 82 | +func (r *V5WebsocketPublicAllLiquidationResponse) Key() V5WebsocketPublicAllLiquidationParamKey { |
| 83 | + topic := r.Topic |
| 84 | + arr := strings.Split(topic, ".") |
| 85 | + if arr[0] != V5WebsocketPublicTopicAllLiquidation.String() || len(arr) != 2 { |
| 86 | + return V5WebsocketPublicAllLiquidationParamKey{} |
| 87 | + } |
| 88 | + |
| 89 | + return V5WebsocketPublicAllLiquidationParamKey{ |
| 90 | + Symbol: SymbolV5(arr[1]), |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// addParamAllLiquidationFunc : |
| 95 | +func (s *V5WebsocketPublicService) addParamAllLiquidationFunc(key V5WebsocketPublicAllLiquidationParamKey, f func(V5WebsocketPublicAllLiquidationResponse) error) error { |
| 96 | + if _, exist := s.paramAllLiquidationMap[key]; exist { |
| 97 | + return errors.New("already registered for this key") |
| 98 | + } |
| 99 | + s.paramAllLiquidationMap[key] = f |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +// removeParamAllLiquidationFunc : |
| 104 | +func (s *V5WebsocketPublicService) removeParamAllLiquidationFunc(key V5WebsocketPublicAllLiquidationParamKey) { |
| 105 | + delete(s.paramAllLiquidationMap, key) |
| 106 | +} |
| 107 | + |
| 108 | +// retrieveAllLiquidationFunc : |
| 109 | +func (s *V5WebsocketPublicService) retrieveAllLiquidationFunc(key V5WebsocketPublicAllLiquidationParamKey) (func(V5WebsocketPublicAllLiquidationResponse) error, error) { |
| 110 | + f, exist := s.paramAllLiquidationMap[key] |
| 111 | + if !exist { |
| 112 | + return nil, errors.New("func not found") |
| 113 | + } |
| 114 | + return f, nil |
| 115 | +} |
0 commit comments