forked from beldur/kraken-go-api-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
krakenapi.go
329 lines (277 loc) · 8.36 KB
/
krakenapi.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
327
328
329
package krakenapi
import (
"crypto/hmac"
"crypto/sha256"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
)
const (
// APIURL is the official Kraken API Endpoint
APIURL = "https://api.kraken.com"
// APIVersion is the official Kraken API Version Number
APIVersion = "0"
// APIUserAgent identifies this library with the Kraken API
APIUserAgent = "Kraken GO API Agent (https://github.com/beldur/kraken-go-api-client)"
)
// List of valid public methods
var publicMethods = []string{
"Time",
"Assets",
"AssetPairs",
"Ticker",
"OHLC",
"Depth",
"Trades",
"Spread",
}
// List of valid private methods
var privateMethods = []string{
"Balance",
"TradeBalance",
"OpenOrders",
"ClosedOrders",
"QueryOrders",
"TradesHistory",
"QueryTrades",
"OpenPositions",
"Ledgers",
"QueryLedgers",
"TradeVolume",
"AddOrder",
"CancelOrder",
}
// KrakenApi represents a Kraken API Client connection
type KrakenApi struct {
key string
secret string
client *http.Client
}
// New creates a new Kraken API client
func New(key, secret string) *KrakenApi {
return NewWithClient(key, secret, http.DefaultClient)
}
func NewWithClient(key, secret string, httpClient *http.Client) *KrakenApi {
return &KrakenApi{key, secret, httpClient}
}
// Time returns the server's time
func (api *KrakenApi) Time() (*TimeResponse, error) {
resp, err := api.queryPublic("Time", nil, &TimeResponse{})
if err != nil {
return nil, err
}
return resp.(*TimeResponse), nil
}
// Assets returns the servers available assets
func (api *KrakenApi) Assets() (*AssetsResponse, error) {
resp, err := api.queryPublic("Assets", nil, &AssetsResponse{})
if err != nil {
return nil, err
}
return resp.(*AssetsResponse), nil
}
// AssetPairs returns the servers available asset pairs
func (api *KrakenApi) AssetPairs() (*AssetPairsResponse, error) {
resp, err := api.queryPublic("AssetPairs", nil, &AssetPairsResponse{})
if err != nil {
return nil, err
}
return resp.(*AssetPairsResponse), nil
}
// Ticker returns the ticker for given comma separated pairs
func (api *KrakenApi) Ticker(pairs ...string) (*TickerResponse, error) {
resp, err := api.queryPublic("Ticker", url.Values{
"pair": {strings.Join(pairs, ",")},
}, &TickerResponse{})
if err != nil {
return nil, err
}
return resp.(*TickerResponse), nil
}
// Trades returns the recent trades for given pair
func (api *KrakenApi) Trades(pair string, since int64) (*TradesResponse, error) {
values := url.Values{"pair": {pair}}
if since > 0 {
values.Set("since", strconv.FormatInt(since, 10))
}
resp, err := api.queryPublic("Trades", values, nil)
if err != nil {
return nil, err
}
v := resp.(map[string]interface{})
last, err := strconv.ParseInt(v["last"].(string), 10, 64)
if err != nil {
return nil, err
}
result := &TradesResponse{
Last: last,
Trades: make([]TradeInfo, 0),
}
trades := v[pair].([]interface{})
for _, v := range trades {
trade := v.([]interface{})
priceString := trade[0].(string)
price, _ := strconv.ParseFloat(priceString, 64)
volume, _ := strconv.ParseFloat(trade[1].(string), 64)
priceParts := strings.Split(priceString, ".")
priceInt, _ := strconv.ParseInt(priceParts[0]+priceParts[1], 10, 64)
tradeInfo := TradeInfo{
Price: trade[0].(string),
PriceFloat: price,
PriceInt: priceInt,
Volume: volume,
Time: trade[2].(float64),
Buy: trade[3].(string) == BUY,
Sell: trade[3].(string) == SELL,
Market: trade[4].(string) == MARKET,
Limit: trade[4].(string) == LIMIT,
Miscellaneous: trade[5].(string),
}
result.Trades = append(result.Trades, tradeInfo)
}
return result, nil
}
// Balance returns all account asset balances
func (api *KrakenApi) Balance() (*BalanceResponse, error) {
resp, err := api.queryPrivate("Balance", url.Values{}, &BalanceResponse{})
if err != nil {
return nil, err
}
return resp.(*BalanceResponse), nil
}
// ClosedOrders returns all closed orders
func (api *KrakenApi) ClosedOrders(args map[string]string) (*ClosedOrdersResponse, error) {
params := url.Values{}
if value, ok := args["trades"]; ok {
params.Add("trades", value)
}
if value, ok := args["userref"]; ok {
params.Add("userref", value)
}
if value, ok := args["start"]; ok {
params.Add("start", value)
}
if value, ok := args["end"]; ok {
params.Add("end", value)
}
if value, ok := args["ofs"]; ok {
params.Add("ofs", value)
}
if value, ok := args["closetime"]; ok {
params.Add("closetime", value)
}
resp, err := api.queryPrivate("ClosedOrders", params, &ClosedOrdersResponse{})
if err != nil {
return nil, err
}
return resp.(*ClosedOrdersResponse), nil
}
// Query sends a query to Kraken api for given method and parameters
func (api *KrakenApi) Query(method string, data map[string]string) (interface{}, error) {
values := url.Values{}
for key, value := range data {
values.Set(key, value)
}
// Check if method is public or private
if isStringInSlice(method, publicMethods) {
return api.queryPublic(method, values, nil)
} else if isStringInSlice(method, privateMethods) {
return api.queryPrivate(method, values, nil)
}
return nil, fmt.Errorf("Method '%s' is not valid", method)
}
// Execute a public method query
func (api *KrakenApi) queryPublic(method string, values url.Values, typ interface{}) (interface{}, error) {
url := fmt.Sprintf("%s/%s/public/%s", APIURL, APIVersion, method)
resp, err := api.doRequest(url, values, nil, typ)
return resp, err
}
// queryPrivate executes a private method query
func (api *KrakenApi) queryPrivate(method string, values url.Values, typ interface{}) (interface{}, error) {
urlPath := fmt.Sprintf("/%s/private/%s", APIVersion, method)
reqURL := fmt.Sprintf("%s%s", APIURL, urlPath)
secret, _ := base64.StdEncoding.DecodeString(api.secret)
values.Set("nonce", fmt.Sprintf("%d", time.Now().UnixNano()))
// Create signature
signature := createSignature(urlPath, values, secret)
// Add Key and signature to request headers
headers := map[string]string{
"API-Key": api.key,
"API-Sign": signature,
}
resp, err := api.doRequest(reqURL, values, headers, typ)
return resp, err
}
// doRequest executes a HTTP Request to the Kraken API and returns the result
func (api *KrakenApi) doRequest(reqURL string, values url.Values, headers map[string]string, typ interface{}) (interface{}, error) {
// Create request
req, err := http.NewRequest("POST", reqURL, strings.NewReader(values.Encode()))
if err != nil {
return nil, fmt.Errorf("Could not execute request! (%s)", err.Error())
}
req.Header.Add("User-Agent", APIUserAgent)
for key, value := range headers {
req.Header.Add(key, value)
}
// Execute request
resp, err := api.client.Do(req)
if err != nil {
return nil, fmt.Errorf("Could not execute request! (%s)", err.Error())
}
defer resp.Body.Close()
// Read request
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("Could not execute request! (%s)", err.Error())
}
// Parse request
var jsonData KrakenResponse
// Set the KrakenResoinse.Result to typ so `json.Unmarshal` will
// unmarshal it into given typ instead of `interface{}`.
if typ != nil {
jsonData.Result = typ
}
err = json.Unmarshal(body, &jsonData)
if err != nil {
return nil, fmt.Errorf("Could not execute request! (%s)", err.Error())
}
// Check for Kraken API error
if len(jsonData.Error) > 0 {
return nil, fmt.Errorf("Could not execute request! (%s)", jsonData.Error)
}
return jsonData.Result, nil
}
// isStringInSlice is a helper function to test if given term is in a list of strings
func isStringInSlice(term string, list []string) bool {
for _, found := range list {
if term == found {
return true
}
}
return false
}
// getSha256 creates a sha256 hash for given []byte
func getSha256(input []byte) []byte {
sha := sha256.New()
sha.Write(input)
return sha.Sum(nil)
}
// getHMacSha512 creates a hmac hash with sha512
func getHMacSha512(message, secret []byte) []byte {
mac := hmac.New(sha512.New, secret)
mac.Write(message)
return mac.Sum(nil)
}
func createSignature(urlPath string, values url.Values, secret []byte) string {
// See https://www.kraken.com/help/api#general-usage for more information
shaSum := getSha256([]byte(values.Get("nonce") + values.Encode()))
macSum := getHMacSha512(append([]byte(urlPath), shaSum...), secret)
return base64.StdEncoding.EncodeToString(macSum)
}