github.com/bitfinexcom/bitfinex-api-go@v0.0.0-20210608095005-9e0b26f200fb/v2/websocket/events.go (about) 1 package websocket 2 3 import ( 4 "encoding/json" 5 ) 6 7 type eventType struct { 8 Event string `json:"event"` 9 } 10 11 type InfoEvent struct { 12 Version float64 `json:"version"` 13 ServerId string `json:"serverId"` 14 Platform PlatformInfo `json:"platform"` 15 Code int `json:"code"` 16 Msg string `json:"msg"` 17 } 18 19 type PlatformInfo struct { 20 Status int `json:"status"` 21 } 22 23 type RawEvent struct { 24 Data interface{} 25 } 26 27 type AuthEvent struct { 28 Event string `json:"event"` 29 Status string `json:"status"` 30 ChanID int64 `json:"chanId,omitempty"` 31 UserID int64 `json:"userId,omitempty"` 32 SubID string `json:"subId"` 33 AuthID string `json:"auth_id,omitempty"` 34 Message string `json:"msg,omitempty"` 35 Caps Capabilities `json:"caps"` 36 } 37 38 type Capability struct { 39 Read int `json:"read"` 40 Write int `json:"write"` 41 } 42 43 type Capabilities struct { 44 Orders Capability `json:"orders"` 45 Account Capability `json:"account"` 46 Funding Capability `json:"funding"` 47 History Capability `json:"history"` 48 Wallets Capability `json:"wallets"` 49 Withdraw Capability `json:"withdraw"` 50 Positions Capability `json:"positions"` 51 } 52 53 // error codes pulled from v2 docs & API usage 54 const ( 55 ErrorCodeUnknownEvent int = 10000 56 ErrorCodeUnknownPair int = 10001 57 ErrorCodeUnknownBookPrecision int = 10011 58 ErrorCodeUnknownBookLength int = 10012 59 ErrorCodeSubscriptionFailed int = 10300 60 ErrorCodeAlreadySubscribed int = 10301 61 ErrorCodeUnknownChannel int = 10302 62 ErrorCodeUnsubscribeFailed int = 10400 63 ErrorCodeNotSubscribed int = 10401 64 ) 65 66 type ErrorEvent struct { 67 Code int `json:"code"` 68 Message string `json:"msg"` 69 70 // also contain members related to subscription reject 71 SubID string `json:"subId"` 72 Channel string `json:"channel"` 73 ChanID int64 `json:"chanId"` 74 Symbol string `json:"symbol"` 75 Precision string `json:"prec,omitempty"` 76 Frequency string `json:"freq,omitempty"` 77 Key string `json:"key,omitempty"` 78 Len string `json:"len,omitempty"` 79 Pair string `json:"pair"` 80 } 81 82 type UnsubscribeEvent struct { 83 Status string `json:"status"` 84 ChanID int64 `json:"chanId"` 85 } 86 87 type SubscribeEvent struct { 88 SubID string `json:"subId"` 89 Channel string `json:"channel"` 90 ChanID int64 `json:"chanId"` 91 Symbol string `json:"symbol"` 92 Precision string `json:"prec,omitempty"` 93 Frequency string `json:"freq,omitempty"` 94 Key string `json:"key,omitempty"` 95 Len string `json:"len,omitempty"` 96 Pair string `json:"pair"` 97 } 98 99 type ConfEvent struct { 100 Flags int `json:"flags"` 101 } 102 103 // onEvent handles all the event messages and connects SubID and ChannelID. 104 func (c *Client) handleEvent(socketId SocketId, msg []byte) error { 105 event := &eventType{} 106 err := json.Unmarshal(msg, event) 107 if err != nil { 108 return err 109 } 110 //var e interface{} 111 switch event.Event { 112 case "info": 113 i := InfoEvent{} 114 err = json.Unmarshal(msg, &i) 115 if err != nil { 116 return err 117 } 118 if i.Code == 0 && i.Version != 0 { 119 err_open := c.handleOpen(socketId) 120 if err_open != nil { 121 return err_open 122 } 123 } 124 c.listener <- &i 125 case "auth": 126 a := AuthEvent{} 127 err = json.Unmarshal(msg, &a) 128 if err != nil { 129 return err 130 } 131 if a.Status != "" && a.Status == "OK" { 132 c.Authentication = SuccessfulAuthentication 133 } else { 134 c.Authentication = RejectedAuthentication 135 } 136 c.handleAuthAck(socketId, &a) 137 c.listener <- &a 138 return nil 139 case "subscribed": 140 s := SubscribeEvent{} 141 err = json.Unmarshal(msg, &s) 142 if err != nil { 143 return err 144 } 145 err = c.subscriptions.activate(s.SubID, s.ChanID) 146 if err != nil { 147 return err 148 } 149 c.listener <- &s 150 return nil 151 case "unsubscribed": 152 s := UnsubscribeEvent{} 153 err = json.Unmarshal(msg, &s) 154 if err != nil { 155 return err 156 } 157 err_rem := c.subscriptions.removeByChannelID(s.ChanID) 158 if err_rem != nil { 159 return err_rem 160 } 161 c.listener <- &s 162 case "error": 163 er := ErrorEvent{} 164 err = json.Unmarshal(msg, &er) 165 if err != nil { 166 return err 167 } 168 c.listener <- &er 169 case "conf": 170 ec := ConfEvent{} 171 err = json.Unmarshal(msg, &ec) 172 if err != nil { 173 return err 174 } 175 c.listener <- &ec 176 default: 177 c.log.Warningf("unknown event: %s", msg) 178 } 179 180 //err = json.Unmarshal(msg, &e) 181 //TODO raw message isn't ever published 182 183 return err 184 }