-
Notifications
You must be signed in to change notification settings - Fork 3
/
factories.go
230 lines (205 loc) · 6.3 KB
/
factories.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package websocket
import (
"encoding/json"
"fmt"
"github.com/bitfinexcom/bitfinex-api-go/v2"
"strings"
"sync"
)
type messageFactory interface {
Build(chanID int64, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error)
BuildSnapshot(chanID int64, raw [][]interface{}, raw_bytes []byte) (interface{}, error)
}
type TickerFactory struct {
*subscriptions
}
func newTickerFactory(subs *subscriptions) *TickerFactory {
return &TickerFactory{
subscriptions: subs,
}
}
func (f *TickerFactory) Build(chanID int64, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error) {
sub, err := f.subscriptions.lookupByChannelID(chanID)
if err == nil {
tick, err := bitfinex.NewTickerFromRaw(sub.Request.Symbol, raw)
return tick, err
}
return nil, err
}
func (f *TickerFactory) BuildSnapshot(chanID int64, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
converted, err := bitfinex.ToFloat64Array(raw)
if err != nil {
return nil, err
}
sub, err := f.subscriptions.lookupByChannelID(chanID)
if err == nil {
return bitfinex.NewTickerSnapshotFromRaw(sub.Request.Symbol, converted)
}
return nil, err
}
type TradeFactory struct {
*subscriptions
}
func newTradeFactory(subs *subscriptions) *TradeFactory {
return &TradeFactory{
subscriptions: subs,
}
}
func (f *TradeFactory) Build(chanID int64, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error) {
sub, err := f.subscriptions.lookupByChannelID(chanID)
if "tu" == objType {
return nil, nil // do not process TradeUpdate messages on public feed, only need to process TradeExecution (first copy seen)
}
if err == nil {
trade, err := bitfinex.NewTradeFromRaw(sub.Request.Symbol, raw)
return trade, err
}
return nil, err
}
func (f *TradeFactory) BuildSnapshot(chanID int64, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
converted, err := bitfinex.ToFloat64Array(raw)
if err != nil {
return nil, err
}
sub, err := f.subscriptions.lookupByChannelID(chanID)
if err == nil {
return bitfinex.NewTradeSnapshotFromRaw(sub.Request.Symbol, converted)
}
return nil, err
}
type BookFactory struct {
*subscriptions
orderbooks map[string]*Orderbook
manageBooks bool
lock sync.Mutex
}
func newBookFactory(subs *subscriptions, obs map[string]*Orderbook, manageBooks bool) *BookFactory {
return &BookFactory{
subscriptions: subs,
orderbooks: obs,
manageBooks: manageBooks,
}
}
func ConvertBytesToJsonNumberArray(raw_bytes []byte) ([]interface{}, error) {
var raw_json_number []interface{}
d := json.NewDecoder(strings.NewReader(string(raw_bytes)))
d.UseNumber()
str_conv_err := d.Decode(&raw_json_number);
if str_conv_err != nil {
return nil, str_conv_err
}
return raw_json_number, nil
}
func (f *BookFactory) Build(chanID int64, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error) {
sub, err := f.subscriptions.lookupByChannelID(chanID)
if err == nil {
// we need ot parse the bytes using json numbers since they store the exact string value
// and not a float64 representation
raw_json_number, str_conv_err := ConvertBytesToJsonNumberArray(raw_bytes)
if str_conv_err != nil {
return nil, str_conv_err
}
update, err := bitfinex.NewBookUpdateFromRaw(sub.Request.Symbol, sub.Request.Precision, raw, raw_json_number[1])
if f.manageBooks {
if orderbook, ok := f.orderbooks[sub.Request.Symbol]; ok {
f.lock.Lock()
defer f.lock.Unlock()
orderbook.UpdateWith(update)
}
}
return update, err
}
return nil, err
}
func (f *BookFactory) BuildSnapshot(chanID int64, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
converted, err := bitfinex.ToFloat64Array(raw)
if err != nil {
return nil, err
}
sub, err := f.subscriptions.lookupByChannelID(chanID)
// parse the bytes using the json number value to store the exact string value
raw_json_number, str_conv_err := ConvertBytesToJsonNumberArray(raw_bytes)
if str_conv_err != nil {
return nil, str_conv_err
}
update, err2 := bitfinex.NewBookUpdateSnapshotFromRaw(sub.Request.Symbol, sub.Request.Precision, converted, raw_json_number[1])
if err2 != nil {
return nil, err2
}
if err == nil {
if f.manageBooks {
f.lock.Lock()
defer f.lock.Unlock()
// create new orderbook
f.orderbooks[sub.Request.Symbol] = &Orderbook{
symbol: sub.Request.Symbol,
bids: make([]*bitfinex.BookUpdate, 0),
asks: make([]*bitfinex.BookUpdate, 0),
}
f.orderbooks[sub.Request.Symbol].SetWithSnapshot(update)
}
return update, err
}
return nil, err
}
type CandlesFactory struct {
*subscriptions
}
func newCandlesFactory(subs *subscriptions) *CandlesFactory {
return &CandlesFactory{
subscriptions: subs,
}
}
func (f *CandlesFactory) Build(chanID int64, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error) {
sub, err := f.subscriptions.lookupByChannelID(chanID)
if err != nil {
return nil, err
}
sym, res, err := extractSymbolResolutionFromKey(sub.Request.Key)
if err != nil {
return nil, err
}
candle, err := bitfinex.NewCandleFromRaw(sym, res, raw)
return candle, err
}
func (f *CandlesFactory) BuildSnapshot(chanID int64, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
converted, err := bitfinex.ToFloat64Array(raw)
if err != nil {
return nil, err
}
sub, err := f.subscriptions.lookupByChannelID(chanID)
if err != nil {
return nil, err
}
sym, res, err := extractSymbolResolutionFromKey(sub.Request.Key)
if err != nil {
return nil, err
}
snap, err := bitfinex.NewCandleSnapshotFromRaw(sym, res, converted)
return snap, err
}
type StatsFactory struct {
*subscriptions
}
func newStatsFactory(subs *subscriptions) *StatsFactory {
return &StatsFactory{
subscriptions: subs,
}
}
func (f *StatsFactory) Build(chanID int64, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error) {
sub, err := f.subscriptions.lookupByChannelID(chanID)
if err != nil {
return nil, err
}
splits := strings.Split(sub.Request.Key, ":")
if len(splits) != 3 {
return nil, fmt.Errorf("unable to parse key to symbol %s", sub.Request.Key)
}
symbol := splits[1] + ":" + splits[2]
candle, err := bitfinex.NewDerivativeStatusFromWsRaw(symbol, raw)
return candle, err
}
func (f *StatsFactory) BuildSnapshot(chanID int64, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
// no snapshots
return nil, nil
}