-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
140 lines (125 loc) · 3.35 KB
/
Copy pathapi.go
File metadata and controls
140 lines (125 loc) · 3.35 KB
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
package main
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"sort"
"strconv"
"time"
"github.com/shopspring/decimal"
)
const (
ROOT_URL = "api.huobi.pro"
MARKET_URL = "/market"
PUBLIC_URL = "/v1/common"
TRADING_URL = "/v1"
)
func encodeSortQueryString(req *http.Request, params map[string]string) string {
// add the query params, sort by byte order
var keys []string
q := req.URL.Query()
for k, _ := range params {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
q.Set(k, params[k])
}
return q.Encode()
}
// https://github.com/huobiapi/API_Docs_en/wiki
func (h *Huobi) Do(method, path string, params map[string]string, auth bool) (*bytes.Buffer, error) {
endpoint := "https://" + ROOT_URL + path
var (
req *http.Request
err error
)
switch method {
case "GET":
req, err = http.NewRequest(method, endpoint, nil)
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
case "POST":
b, _ := json.Marshal(params)
req, err = http.NewRequest(method, endpoint, bytes.NewBuffer(b))
req.Header.Set("Content-Type", "application/json")
default:
return nil, fmt.Errorf("Unhandled HTTP Method: %v", method)
}
if err != nil {
return nil, err
}
req.URL.RawQuery = encodeSortQueryString(req, params)
if auth {
params["AccessKeyId"] = h.APIKey
params["SignatureMethod"] = "HmacSHA256"
params["SignatureVersion"] = "2"
params["Timestamp"] = time.Now().UTC().Format("2006-01-02T15:04:05")
req.URL.RawQuery = encodeSortQueryString(req, params)
signStr := method + "\n" + ROOT_URL + "\n" + path + "\n" + req.URL.RawQuery
hashed := hmac.New(sha256.New, []byte(h.APISecret))
_, err = hashed.Write([]byte(signStr))
if err != nil {
return nil, err
}
signature := base64.StdEncoding.EncodeToString(hashed.Sum(nil))
req.URL.RawQuery = req.URL.RawQuery + "&Signature=" + url.QueryEscape(signature)
}
resp, err := h.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBytes, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return bytes.NewBuffer(respBytes), err
}
// GET /v1/account/accounts
func (h *Huobi) GetAccounts() (*Accounts, error) {
url := TRADING_URL + "/account/accounts"
params := map[string]string{}
resp, err := h.Do("GET", url, params, true)
if err != nil {
return nil, err
}
accounts := Accounts{}
json.NewDecoder(resp).Decode(&accounts)
if accounts.ErrMsg != "" {
return &accounts, errors.New(accounts.ErrMsg)
}
return &accounts, nil
}
// POST /v1/order/orders/place Make an order in huobi.pro
func (h *Huobi) PlaceLimitOrder(symbol string, isBuy bool, amount, price decimal.Decimal) (*NewOrder, error) {
url := TRADING_URL + "/order/orders/place"
orderTypes := map[bool]string{
false: "sell-limit",
true: "buy-limit",
}
params := map[string]string{
"account-id": strconv.Itoa(int(h.TradingAccount.ID)),
"amount": amount.String(),
"price": price.String(),
"source": "api", // 'api' for spot trade and 'margin-api' for margin trade
"symbol": symbol,
"type": orderTypes[isBuy],
}
resp, err := h.Do("POST", url, params, true)
if err != nil {
return nil, err
}
no := NewOrder{}
json.NewDecoder(resp).Decode(&no)
if no.ErrMsg != "" {
return &no, errors.New(no.ErrMsg)
}
return &no, nil
}