forked from KyberNetwork/deribit-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
251 lines (210 loc) · 5.22 KB
/
client.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package deribit
import (
"context"
"encoding/json"
"errors"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/KyberNetwork/deribit-api/models"
"github.com/chuckpreslar/emission"
ws "github.com/gorilla/websocket"
"github.com/sourcegraph/jsonrpc2"
sws "github.com/sourcegraph/jsonrpc2/websocket"
)
const (
RealBaseURL = "wss://www.deribit.com/ws/api/v2/"
TestBaseURL = "wss://test.deribit.com/ws/api/v2/"
)
const (
MaxTryTimes = 10
)
var (
ErrAuthenticationIsRequired = errors.New("authentication is required")
ErrNotConnected = errors.New("not connected")
)
// Event is wrapper of received event
type Event struct {
Channel string `json:"channel"`
Data json.RawMessage `json:"data"`
}
type Configuration struct {
Addr string `json:"addr"`
ApiKey string `json:"api_key"`
SecretKey string `json:"secret_key"`
AutoReconnect bool `json:"auto_reconnect"`
DebugMode bool `json:"debug_mode"`
}
type Client struct {
addr string
apiKey string
secretKey string
autoReconnect bool
debugMode bool
conn *ws.Conn
rpcConn *jsonrpc2.Conn
mu sync.RWMutex
heartCancel chan struct{}
isConnected bool
subscriptions []string
subscriptionsMap map[string]struct{}
emitter *emission.Emitter
}
func New(cfg *Configuration) (*Client, error) {
client := &Client{
addr: cfg.Addr,
apiKey: cfg.ApiKey,
secretKey: cfg.SecretKey,
autoReconnect: cfg.AutoReconnect,
debugMode: cfg.DebugMode,
subscriptionsMap: make(map[string]struct{}),
emitter: emission.NewEmitter(),
}
err := client.start()
if err != nil {
return nil, err
}
return client, nil
}
// setIsConnected sets state for isConnected
func (c *Client) setIsConnected(state bool) {
c.mu.Lock()
defer c.mu.Unlock()
c.isConnected = state
}
// IsConnected returns the WebSocket connection state
func (c *Client) IsConnected() bool {
c.mu.RLock()
defer c.mu.RUnlock()
return c.isConnected
}
func (c *Client) Subscribe(channels []string) {
c.subscriptions = append(c.subscriptions, channels...)
c.subscribe(channels)
}
func (c *Client) subscribe(channels []string) {
var publicChannels []string
var privateChannels []string
for _, v := range c.subscriptions {
if _, ok := c.subscriptionsMap[v]; ok {
continue
}
if strings.HasPrefix(v, "user.") {
privateChannels = append(privateChannels, v)
} else {
publicChannels = append(publicChannels, v)
}
}
if len(publicChannels) > 0 {
if _, err := c.PublicSubscribe(context.Background(), &models.SubscribeParams{
Channels: publicChannels,
}); err != nil {
log.Printf("error subscribe public err = %s", err)
}
}
if len(privateChannels) > 0 {
if _, err := c.PrivateSubscribe(context.Background(), &models.SubscribeParams{
Channels: privateChannels,
}); err != nil {
log.Printf("error subscribe private err = %s", err)
}
}
allChannels := append(publicChannels, privateChannels...)
for _, v := range allChannels {
c.subscriptionsMap[v] = struct{}{}
}
}
func (c *Client) start() error {
c.setIsConnected(false)
c.subscriptionsMap = make(map[string]struct{})
c.conn = nil
c.rpcConn = nil
c.heartCancel = make(chan struct{})
var (
err error
conn *ws.Conn
)
for i := 0; i < MaxTryTimes; i++ {
conn, _, err = ws.DefaultDialer.Dial(c.addr, nil)
if err != nil {
time.Sleep(time.Duration(i+1) * time.Second)
continue
}
c.conn = conn
break
}
if err != nil {
return err
}
c.rpcConn = jsonrpc2.NewConn(context.Background(), sws.NewObjectStream(c.conn), c)
c.setIsConnected(true)
// auth
if c.apiKey != "" && c.secretKey != "" {
if _, err := c.Auth(context.Background()); err != nil {
log.Printf("failed to auth, err = %s", err)
return err
}
}
// subscribe
c.subscribe(c.subscriptions)
if _, err := c.SetHeartbeat(context.Background(), &models.SetHeartbeatParams{Interval: 30}); err != nil {
return err
}
if c.autoReconnect {
go c.reconnect()
}
go c.heartbeat()
return nil
}
// Call issues JSONRPC v2 calls
func (c *Client) Call(ctx context.Context, method string, params interface{}, result interface{}) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("%v", r)
}
}()
if !c.IsConnected() {
return ErrNotConnected
}
if params == nil {
params = json.RawMessage("{}")
}
return c.rpcConn.Call(ctx, method, params, result)
}
// Handle implements jsonrpc2.Handler
func (c *Client) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
if req.Method == "subscription" {
if req.Params != nil && len(*req.Params) > 0 {
var event Event
if err := json.Unmarshal(*req.Params, &event); err != nil {
return
}
c.subscriptionsProcess(&event)
}
}
}
func (c *Client) heartbeat() {
t := time.NewTicker(5 * time.Second)
for {
select {
case <-t.C:
if _, err := c.Test(context.Background()); err != nil {
log.Printf("error test server, err = %s", err)
_ = c.conn.Close() // close server
}
case <-c.heartCancel:
return
}
}
}
func (c *Client) reconnect() {
notify := c.rpcConn.DisconnectNotify()
<-notify
c.setIsConnected(false)
log.Println("disconnect, reconnect...")
close(c.heartCancel)
time.Sleep(1 * time.Second)
_ = c.start()
}