forked from bitfinexcom/bitfinex-api-go
/
factories.go
192 lines (167 loc) · 5.47 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
package websocket
import (
"encoding/json"
"fmt"
"github.com/CapTec-Analytics/bitfinex-api-go/v2"
"strings"
"sync"
)
type messageFactory interface {
Build(sub *subscription, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error)
BuildSnapshot(sub *subscription, raw [][]interface{}, raw_bytes []byte) (interface{}, error)
}
type TickerFactory struct {
*subscriptions
}
func newTickerFactory(subs *subscriptions) *TickerFactory {
return &TickerFactory{
subscriptions: subs,
}
}
func (f *TickerFactory) Build(sub *subscription, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error) {
return bitfinex.NewTickerFromRaw(sub.Request.Symbol, raw)
}
func (f *TickerFactory) BuildSnapshot(sub *subscription, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
converted, err := bitfinex.ToFloat64Array(raw)
if err != nil {
return nil, err
}
return bitfinex.NewTickerSnapshotFromRaw(sub.Request.Symbol, converted)
}
type TradeFactory struct {
*subscriptions
}
func newTradeFactory(subs *subscriptions) *TradeFactory {
return &TradeFactory{
subscriptions: subs,
}
}
func (f *TradeFactory) Build(sub *subscription, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error) {
if "tu" == objType {
return nil, nil // do not process TradeUpdate messages on public feed, only need to process TradeExecution (first copy seen)
}
return bitfinex.NewTradeFromRaw(sub.Request.Symbol, raw)
}
func (f *TradeFactory) BuildSnapshot(sub *subscription, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
converted, err := bitfinex.ToFloat64Array(raw)
if err != nil {
return nil, err
}
return bitfinex.NewTradeSnapshotFromRaw(sub.Request.Symbol, converted)
}
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(sub *subscription, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error) {
// 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 {
f.lock.Lock()
defer f.lock.Unlock()
if orderbook, ok := f.orderbooks[sub.Request.Symbol]; ok {
orderbook.UpdateWith(update)
}
}
return update, err
}
func (f *BookFactory) BuildSnapshot(sub *subscription, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
converted, err := bitfinex.ToFloat64Array(raw)
if err != nil {
return nil, err
}
// 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 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
}
type CandlesFactory struct {
*subscriptions
}
func newCandlesFactory(subs *subscriptions) *CandlesFactory {
return &CandlesFactory{
subscriptions: subs,
}
}
func (f *CandlesFactory) Build(sub *subscription, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error) {
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(sub *subscription, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
converted, err := bitfinex.ToFloat64Array(raw)
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(sub *subscription, objType string, raw []interface{}, raw_bytes []byte) (interface{}, error) {
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(sub *subscription, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
// no snapshots
return nil, nil
}