forked from bitfinexcom/bitfinex-api-go
/
events.go
184 lines (167 loc) · 4.22 KB
/
events.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
184
package websocket
import (
"encoding/json"
)
type eventType struct {
Event string `json:"event"`
}
type InfoEvent struct {
Version float64 `json:"version"`
ServerId string `json:"serverId"`
Platform PlatformInfo `json:"platform"`
Code int `json:"code"`
Msg string `json:"msg"`
}
type PlatformInfo struct {
Status int `json:"status"`
}
type RawEvent struct {
Data interface{}
}
type AuthEvent struct {
Event string `json:"event"`
Status string `json:"status"`
ChanID int64 `json:"chanId,omitempty"`
UserID int64 `json:"userId,omitempty"`
SubID string `json:"subId"`
AuthID string `json:"auth_id,omitempty"`
Message string `json:"msg,omitempty"`
Caps Capabilities `json:"caps"`
}
type Capability struct {
Read int `json:"read"`
Write int `json:"write"`
}
type Capabilities struct {
Orders Capability `json:"orders"`
Account Capability `json:"account"`
Funding Capability `json:"funding"`
History Capability `json:"history"`
Wallets Capability `json:"wallets"`
Withdraw Capability `json:"withdraw"`
Positions Capability `json:"positions"`
}
// error codes pulled from v2 docs & API usage
const (
ErrorCodeUnknownEvent int = 10000
ErrorCodeUnknownPair int = 10001
ErrorCodeUnknownBookPrecision int = 10011
ErrorCodeUnknownBookLength int = 10012
ErrorCodeSubscriptionFailed int = 10300
ErrorCodeAlreadySubscribed int = 10301
ErrorCodeUnknownChannel int = 10302
ErrorCodeUnsubscribeFailed int = 10400
ErrorCodeNotSubscribed int = 10401
)
type ErrorEvent struct {
Code int `json:"code"`
Message string `json:"msg"`
// also contain members related to subscription reject
SubID string `json:"subId"`
Channel string `json:"channel"`
ChanID int64 `json:"chanId"`
Symbol string `json:"symbol"`
Precision string `json:"prec,omitempty"`
Frequency string `json:"freq,omitempty"`
Key string `json:"key,omitempty"`
Len string `json:"len,omitempty"`
Pair string `json:"pair"`
}
type UnsubscribeEvent struct {
Status string `json:"status"`
ChanID int64 `json:"chanId"`
}
type SubscribeEvent struct {
SubID string `json:"subId"`
Channel string `json:"channel"`
ChanID int64 `json:"chanId"`
Symbol string `json:"symbol"`
Precision string `json:"prec,omitempty"`
Frequency string `json:"freq,omitempty"`
Key string `json:"key,omitempty"`
Len string `json:"len,omitempty"`
Pair string `json:"pair"`
}
type ConfEvent struct {
Flags int `json:"flags"`
}
// onEvent handles all the event messages and connects SubID and ChannelID.
func (c *Client) handleEvent(socketId SocketId, msg []byte) error {
event := &eventType{}
err := json.Unmarshal(msg, event)
if err != nil {
return err
}
//var e interface{}
switch event.Event {
case "info":
i := InfoEvent{}
err = json.Unmarshal(msg, &i)
if err != nil {
return err
}
if i.Code == 0 && i.Version != 0 {
err_open := c.handleOpen(socketId)
if err_open != nil {
return err_open
}
}
c.listener <- &i
case "auth":
a := AuthEvent{}
err = json.Unmarshal(msg, &a)
if err != nil {
return err
}
if a.Status != "" && a.Status == "OK" {
c.Authentication = SuccessfulAuthentication
} else {
c.Authentication = RejectedAuthentication
}
c.handleAuthAck(socketId, &a)
c.listener <- &a
return nil
case "subscribed":
s := SubscribeEvent{}
err = json.Unmarshal(msg, &s)
if err != nil {
return err
}
err = c.subscriptions.activate(s.SubID, s.ChanID)
if err != nil {
return err
}
c.listener <- &s
return nil
case "unsubscribed":
s := UnsubscribeEvent{}
err = json.Unmarshal(msg, &s)
if err != nil {
return err
}
err_rem := c.subscriptions.removeByChannelID(s.ChanID)
if err_rem != nil {
return err_rem
}
c.listener <- &s
case "error":
er := ErrorEvent{}
err = json.Unmarshal(msg, &er)
if err != nil {
return err
}
c.listener <- &er
case "conf":
ec := ConfEvent{}
err = json.Unmarshal(msg, &ec)
if err != nil {
return err
}
c.listener <- &ec
default:
c.log.Warningf("unknown event: %s", msg)
}
//err = json.Unmarshal(msg, &e)
//TODO raw message isn't ever published
return err
}