forked from Alkise/bitfinex-api-go
/
factories.go
183 lines (152 loc) · 4.87 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
package websocket
import (
"encoding/json"
"fmt"
"strings"
"sync"
"github.com/bitfinexcom/bitfinex-api-go/pkg/models/book"
"github.com/bitfinexcom/bitfinex-api-go/pkg/models/candle"
"github.com/bitfinexcom/bitfinex-api-go/pkg/models/derivatives"
"github.com/bitfinexcom/bitfinex-api-go/pkg/models/ticker"
"github.com/bitfinexcom/bitfinex-api-go/pkg/models/trade"
)
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 ticker.FromRaw(sub.Request.Symbol, raw)
}
func (f *TickerFactory) BuildSnapshot(sub *subscription, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
return ticker.SnapshotFromRaw(sub.Request.Symbol, raw)
}
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 trade.FromRaw(sub.Request.Symbol, raw)
}
func (f *TradeFactory) BuildSnapshot(sub *subscription, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
return trade.SnapshotFromRaw(sub.Request.Symbol, raw)
}
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(b []byte) ([]interface{}, error) {
var rawJSONNumbers []interface{}
d := json.NewDecoder(strings.NewReader(string(b)))
d.UseNumber()
err := d.Decode(&rawJSONNumbers)
if err != nil {
return nil, err
}
return rawJSONNumbers, nil
}
func (f *BookFactory) Build(sub *subscription, objType string, raw []interface{}, b []byte) (interface{}, error) {
rawJSONNumbers, err := ConvertBytesToJsonNumberArray(b)
if err != nil {
return nil, err
}
update, err := book.FromRaw(sub.Request.Symbol, sub.Request.Precision, raw, rawJSONNumbers[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{}, b []byte) (interface{}, error) {
rawJSONNumbers, err := ConvertBytesToJsonNumberArray(b)
if err != nil {
return nil, err
}
update, err := book.SnapshotFromRaw(sub.Request.Symbol, sub.Request.Precision, raw, rawJSONNumbers[1])
if err != nil {
return nil, err
}
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([]*book.Book, 0),
asks: make([]*book.Book, 0),
}
f.orderbooks[sub.Request.Symbol].SetWithSnapshot(update)
}
return update, nil
}
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 := candle.FromRaw(sym, res, raw)
return candle, err
}
func (f *CandlesFactory) BuildSnapshot(sub *subscription, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
sym, res, err := extractSymbolResolutionFromKey(sub.Request.Key)
if err != nil {
return nil, err
}
snap, err := candle.SnapshotFromRaw(sym, res, raw)
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]
d, err := derivatives.FromWsRaw(symbol, raw)
return d, err
}
func (f *StatsFactory) BuildSnapshot(sub *subscription, raw [][]interface{}, raw_bytes []byte) (interface{}, error) {
// no snapshots
return nil, nil
}