-
Notifications
You must be signed in to change notification settings - Fork 8
/
client.go
326 lines (255 loc) · 7.38 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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package bitstamp
import (
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"net/url"
"time"
"github.com/crxfoz/webclient"
"github.com/google/uuid"
)
// From API docs: Should you receive the error response 'Order could not be placed' when trying to place an order, please retry order placement.
var (
ErrStatus = errors.New("incorrect status code")
)
type ErrorResult struct {
Status string `json:"status"`
Reason interface{} `json:"reason"`
Code string `json:"code"`
}
func (er ErrorResult) Error() string {
return fmt.Sprintf("api error: %s", er.Reason)
}
type PrivateClient struct {
APIKey string
SecretKey string
client *webclient.Webclient
}
func NewPrivateClient(apiKey string, secretKey string) *PrivateClient {
return &PrivateClient{
APIKey: apiKey,
SecretKey: secretKey,
client: webclient.Config{
Timeout: time.Second * 10,
UseKeepAlive: false,
FollowRedirect: false,
}.New(),
}
}
func (pc *PrivateClient) privateRequest(path string, params map[string]string) (string, error) {
ts := time.Now().Add(time.Second * 10).Unix()
nonce, err := uuid.NewUUID()
if err != nil {
return "", err
}
// line from bitstamp API official docs
// it doesn't work without this line and with UnixNano
ts *= 1000
values := url.Values{}
for k, v := range params {
values.Add(k, v)
}
// if request body is empty, shouldn't pass Content-Type
contentType := ""
if len(params) > 0 {
contentType = "application/x-www-form-urlencoded"
}
// Bitstamp API v2 auth method: https://www.bitstamp.net/api/
msg := fmt.Sprintf("BITSTAMP %s"+
"POST"+
"www.bitstamp.net"+
"%s"+
"%s"+
"%s"+
"%d"+
"v2"+
"%s", pc.APIKey, path, contentType, nonce, ts, values.Encode())
h := hmac.New(sha256.New, []byte(pc.SecretKey))
if _, err := h.Write([]byte(msg)); err != nil {
return "", err
}
sign := h.Sum(nil)
headers := map[string]string{
"X-Auth": fmt.Sprintf("BITSTAMP %s", pc.APIKey),
"X-Auth-Signature": hex.EncodeToString(sign),
"X-Auth-Nonce": nonce.String(),
"X-Auth-Timestamp": fmt.Sprintf("%d", ts),
"X-Auth-Version": "v2",
}
req := pc.client.Post(fmt.Sprintf("https://www.bitstamp.net%s", path)).SetHeaders(headers)
for k, v := range params {
req.SendParam(k, v)
}
_, body, err := req.Do()
if err != nil {
return "", err
}
// parsing errors
var errBody ErrorResult
if err := json.Unmarshal([]byte(body), &errBody); err == nil && errBody.Status == "error" {
return "", errBody
}
return body, nil
}
func (pc *PrivateClient) GetBalances() (BalanceResult, error) {
resp, err := pc.privateRequest("/api/v2/balance/", nil)
if err != nil {
return BalanceResult{}, err
}
var balances BalanceResult
if err := json.Unmarshal([]byte(resp), &balances); err != nil {
return BalanceResult{}, err
}
return balances, nil
}
func (pc *PrivateClient) GetTransactions() ([]TransactionResult, error) {
resp, err := pc.privateRequest("/api/v2/user_transactions/", nil)
if err != nil {
return nil, err
}
var transactions []TransactionResult
if err := json.Unmarshal([]byte(resp), &transactions); err != nil {
return nil, err
}
return transactions, nil
}
func (pc *PrivateClient) GetOpenOrders() ([]OpenOrderResult, error) {
resp, err := pc.privateRequest("/api/v2/open_orders/all/", nil)
if err != nil {
return nil, err
}
var orders []OpenOrderResult
if err := json.Unmarshal([]byte(resp), &orders); err != nil {
return nil, err
}
return orders, nil
}
func (pc *PrivateClient) GetOrderStatus(id string) (OrderStatusResult, error) {
resp, err := pc.privateRequest("/api/v2/order_status/", map[string]string{"id": id})
if err != nil {
return OrderStatusResult{}, err
}
var status OrderStatusResult
if err := json.Unmarshal([]byte(resp), &status); err != nil {
return OrderStatusResult{}, err
}
return status, nil
}
func (pc *PrivateClient) CancelOrder(id string) (OrderCancelResult, error) {
resp, err := pc.privateRequest("/api/v2/cancel_order/", map[string]string{"id": id})
if err != nil {
return OrderCancelResult{}, err
}
var status OrderCancelResult
if err := json.Unmarshal([]byte(resp), &status); err != nil {
return OrderCancelResult{}, err
}
return status, nil
}
// CancelAllOrders отменяет все ордера
// TODO: bitstamp возвращает список отмененных ордеров. Сейчас они не парсятся.
func (pc *PrivateClient) CancelAllOrders() (CancelAllOrdersResult, error) {
resp, err := pc.privateRequest("/api/v2/cancel_all_orders/", nil)
if err != nil {
return CancelAllOrdersResult{}, err
}
var status CancelAllOrdersResult
if err := json.Unmarshal([]byte(resp), &status); err != nil {
return CancelAllOrdersResult{}, err
}
return status, nil
}
func (pc *PrivateClient) limitOrder(order PlaceOrderRequest) (PlaceOrderResult, error) {
path := ""
switch order.Side {
case Buy:
path = fmt.Sprintf("/api/v2/buy/%s/", order.Symbol)
case Sell:
path = fmt.Sprintf("/api/v2/sell/%s/", order.Symbol)
default:
return PlaceOrderResult{}, fmt.Errorf("wrong side")
}
params := make(map[string]string)
// TODO: probably could be simplified
// TODO: I'm not sure that 'True' is valid value but it's been written according to Bitstamp docs
// nolint
switch order.ExecType {
case ExecDefault:
case ExecDaily:
params["daily_order"] = "True"
case ExecFOK:
params["fok_order"] = "True"
case ExecIOC:
params["ioc_order"] = "True"
}
params["price"] = fmt.Sprintf("%f", order.Price)
params["amount"] = fmt.Sprintf("%f", order.Amount)
resp, err := pc.privateRequest(path, params)
if err != nil {
return PlaceOrderResult{}, err
}
var status PlaceOrderResult
if err := json.Unmarshal([]byte(resp), &status); err != nil {
return PlaceOrderResult{}, err
}
return status, nil
}
func (pc *PrivateClient) marketOrder(order PlaceOrderRequest) (PlaceOrderResult, error) {
path := ""
switch order.Side {
case Buy:
path = fmt.Sprintf("/api/v2/buy/market/%s/", order.Symbol)
case Sell:
path = fmt.Sprintf("/api/v2/sell/market/%s/", order.Symbol)
default:
return PlaceOrderResult{}, fmt.Errorf("wrong side")
}
amount := fmt.Sprintf("%f", order.Amount)
resp, err := pc.privateRequest(path, map[string]string{
"amount": amount,
})
if err != nil {
return PlaceOrderResult{}, err
}
var status PlaceOrderResult
if err := json.Unmarshal([]byte(resp), &status); err != nil {
return PlaceOrderResult{}, err
}
return status, nil
}
func (pc *PrivateClient) PlaceOrder(opts PlaceOrderRequest) (PlaceOrderResult, error) {
if opts.Symbol == "" {
return PlaceOrderResult{}, fmt.Errorf("symbol isn't specified")
}
if opts.Amount <= 0 {
return PlaceOrderResult{}, fmt.Errorf("amount isn't specified")
}
if opts.Side == "" {
return PlaceOrderResult{}, fmt.Errorf("side isn't specified")
}
switch opts.Type {
case Limit:
if opts.Price <= 0 {
return PlaceOrderResult{}, fmt.Errorf("price can't be 0 for limit orders")
}
return pc.limitOrder(opts)
case Market:
return pc.marketOrder(opts)
default:
return PlaceOrderResult{}, fmt.Errorf("order type isn't specified")
}
}
func (pc *PrivateClient) GenerateWSToken() (*GenerateWSTokenResult, error) {
resp, err := pc.privateRequest("/api/v2/websockets_token/", nil)
if err != nil {
return nil, err
}
var result GenerateWSTokenResult
if err := json.Unmarshal([]byte(resp), &result); err != nil {
return nil, err
}
return &result, nil
}