forked from bitfinexcom/bitfinex-api-go
/
api.go
222 lines (200 loc) · 6.52 KB
/
api.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
package websocket
import (
"context"
"fmt"
"github.com/openware/bitfinex-api-go/v2"
)
type FlagRequest struct {
Event string `json:"event"`
Flags int `json:"flags"`
}
// API for end-users to interact with Bitfinex.
// Send publishes a generic message to the Bitfinex API.
func (c *Client) Send(ctx context.Context, msg interface{}) error {
socket, err := c.getSocket()
if err != nil {
return err
}
return socket.Asynchronous.Send(ctx, msg)
}
// Submit a request to enable the given flag
func (c *Client) EnableFlag(ctx context.Context, flag int) (string, error) {
req := &FlagRequest{
Event: "conf",
Flags: flag,
}
// TODO enable flag on reconnect?
// create sublist to stop concurrent map read
socks := make([]*Socket, len(c.sockets))
c.mtx.RLock()
for i, socket := range c.sockets {
socks[i] = socket
}
c.mtx.RUnlock()
for _, socket := range socks {
err := socket.Asynchronous.Send(ctx, req)
if err != nil {
return "", err
}
}
return "", nil
}
// Gen the count of currently active websocket connections
func (c *Client) ConnectionCount() int {
c.mtx.RLock()
defer c.mtx.RUnlock()
return len(c.sockets)
}
// Get the available capacity of the current
// websocket connections
func (c *Client) AvailableCapacity() int {
return c.getTotalAvailableSocketCapacity()
}
// Start a new websocket connection. This function is only exposed in case you want to
// implicitly add new connections otherwise connection management is already handled for you.
func (c *Client) StartNewConnection() error {
return c.connectSocket(SocketId(c.ConnectionCount()))
}
func (c *Client) subscribeBySocket(ctx context.Context, socket *Socket, req *SubscriptionRequest) (string, error) {
c.subscriptions.add(socket.Id, req)
err := socket.Asynchronous.Send(ctx, req)
if err != nil {
// propagate send error
return "", err
}
return req.SubID, nil
}
// Submit a request to subscribe to the given SubscriptionRequuest
func (c *Client) Subscribe(ctx context.Context, req *SubscriptionRequest) (string, error) {
if c.getTotalAvailableSocketCapacity() <= 1 {
err := c.StartNewConnection()
if err != nil {
return "", err
}
}
// get socket with the highest available capacity
socket, err := c.getMostAvailableSocket()
if err != nil {
return "", err
}
return c.subscribeBySocket(ctx, socket, req)
}
// Submit a request to receive ticker updates
func (c *Client) SubscribeTicker(ctx context.Context, symbol string) (string, error) {
req := &SubscriptionRequest{
SubID: c.nonce.GetNonce(),
Event: EventSubscribe,
Channel: ChanTicker,
Symbol: symbol,
}
return c.Subscribe(ctx, req)
}
// Submit a request to receive trade updates
func (c *Client) SubscribeTrades(ctx context.Context, symbol string) (string, error) {
req := &SubscriptionRequest{
SubID: c.nonce.GetNonce(),
Event: EventSubscribe,
Channel: ChanTrades,
Symbol: symbol,
}
return c.Subscribe(ctx, req)
}
// Submit a subscription request for market data for the given symbol, at the given frequency, with the given precision, returning no more than priceLevels price entries.
// Default values are Precision0, Frequency0, and priceLevels=25.
func (c *Client) SubscribeBook(ctx context.Context, symbol string, precision bitfinex.BookPrecision, frequency bitfinex.BookFrequency, priceLevel int) (string, error) {
if priceLevel < 0 {
return "", fmt.Errorf("negative price levels not supported: %d", priceLevel)
}
req := &SubscriptionRequest{
SubID: c.nonce.GetNonce(),
Event: EventSubscribe,
Channel: ChanBook,
Symbol: symbol,
Precision: string(precision),
Len: fmt.Sprintf("%d", priceLevel), // needed for R0?
}
if !bitfinex.IsRawBook(string(precision)) {
req.Frequency = string(frequency)
}
return c.Subscribe(ctx, req)
}
// Submit a subscription request to receive candle updates
func (c *Client) SubscribeCandles(ctx context.Context, symbol string, resolution bitfinex.CandleResolution) (string, error) {
req := &SubscriptionRequest{
SubID: c.nonce.GetNonce(),
Event: EventSubscribe,
Channel: ChanCandles,
Key: fmt.Sprintf("trade:%s:%s", resolution, symbol),
}
return c.Subscribe(ctx, req)
}
// Submit a subscription request for status updates
func (c *Client) SubscribeStatus(ctx context.Context, symbol string, sType bitfinex.StatusType) (string, error) {
req := &SubscriptionRequest{
SubID: c.nonce.GetNonce(),
Event: EventSubscribe,
Channel: ChanStatus,
Key: fmt.Sprintf("%s:%s", string(sType), symbol),
}
return c.Subscribe(ctx, req)
}
// Retrieve the Orderbook for the given symbol which is managed locally.
// This requires ManageOrderbook=True and an active chanel subscribed to the given
// symbols orderbook
func (c *Client) GetOrderbook(symbol string) (*Orderbook, error) {
c.mtx.RLock()
defer c.mtx.RUnlock()
if val, ok := c.orderbooks[symbol]; ok {
// take dereferenced copy of orderbook
return val, nil
}
return nil, fmt.Errorf("Orderbook %s does not exist", symbol)
}
// Submit a request to create a new order
func (c *Client) SubmitOrder(ctx context.Context, order *bitfinex.OrderNewRequest) error {
socket, err := c.GetAuthenticatedSocket()
if err != nil {
return err
}
return socket.Asynchronous.Send(ctx, order)
}
// Submit and update request to change an existing orders values
func (c *Client) SubmitUpdateOrder(ctx context.Context, orderUpdate *bitfinex.OrderUpdateRequest) error {
socket, err := c.GetAuthenticatedSocket()
if err != nil {
return err
}
return socket.Asynchronous.Send(ctx, orderUpdate)
}
// Submit a cancel request for an existing order
func (c *Client) SubmitCancel(ctx context.Context, cancel *bitfinex.OrderCancelRequest) error {
socket, err := c.GetAuthenticatedSocket()
if err != nil {
return err
}
return socket.Asynchronous.Send(ctx, cancel)
}
// Get a subscription request using a subscription ID
func (c *Client) LookupSubscription(subID string) (*SubscriptionRequest, error) {
s, err := c.subscriptions.lookupBySubscriptionID(subID)
if err != nil {
return nil, err
}
return s.Request, nil
}
// Submit a new funding offer request
func (c *Client) SubmitFundingOffer(ctx context.Context, fundingOffer *bitfinex.FundingOfferRequest) error {
socket, err := c.GetAuthenticatedSocket()
if err != nil {
return err
}
return socket.Asynchronous.Send(ctx, fundingOffer)
}
// Submit a request to cancel and existing funding offer
func (c *Client) SubmitFundingCancel(ctx context.Context, fundingOffer *bitfinex.FundingOfferCancelRequest) error {
socket, err := c.GetAuthenticatedSocket()
if err != nil {
return err
}
return socket.Asynchronous.Send(ctx, fundingOffer)
}