/
spot.go
186 lines (174 loc) · 5.61 KB
/
spot.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
package huobiapi
import (
"bytes"
"net/http"
"strconv"
"strings"
)
type SpotsResponse struct {
Status string `json:"status"`
Data []struct {
BaseCurrency string `json:"base-currency"`
QuoteCurrency string `json:"quote-currency"`
PricePrecision int `json:"price-precision"`
AmountPrecision int `json:"amount-precision"`
SymbolPartition string `json:"symbol-partition"`
Symbol string `json:"symbol"`
State string `json:"state"`
ValuePrecision int `json:"value-precision"`
MinOrderAmt float64 `json:"min-order-amt"`
MaxOrderAmt float64 `json:"max-order-amt"`
MinOrderValue float64 `json:"min-order-value"`
LimitOrderMinOrderAmt float64 `json:"limit-order-min-order-amt"`
LimitOrderMaxOrderAmt float64 `json:"limit-order-max-order-amt"`
LimitOrderMaxBuyAmt float64 `json:"limit-order-max-buy-amt"`
LimitOrderMaxSellAmt float64 `json:"limit-order-max-sell-amt"`
BuyLimitMustLessThan float64 `json:"buy-limit-must-less-than"`
SellLimitMustGreaterThan float64 `json:"sell-limit-must-greater-than"`
SellMarketMinOrderAmt float64 `json:"sell-market-min-order-amt"`
SellMarketMaxOrderAmt float64 `json:"sell-market-max-order-amt"`
BuyMarketMaxOrderValue float64 `json:"buy-market-max-order-value"`
MarketSellOrderRateMustLessThan float64 `json:"market-sell-order-rate-must-less-than"`
MarketBuyOrderRateMustLessThan float64 `json:"market-buy-order-rate-must-less-than"`
APITrading string `json:"api-trading"`
Tags string `json:"tags"`
} `json:"data"`
}
func (p *Client) Spots() (*SpotsResponse, error) {
res, err := p.sendRequest("spot", http.MethodGet, "/v1/common/symbols", nil, nil, false)
if err != nil {
return nil, err
}
var result SpotsResponse
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
err = json.Unmarshal(buf.Bytes(), &result)
if err != nil {
return nil, err
}
return &result, nil
}
type GetAllAccountsResponse struct {
Data []struct {
ID int `json:"id"`
Type string `json:"type"`
Subtype string `json:"subtype"`
State string `json:"state"`
} `json:"data"`
}
func (p *Client) GetAllAccounts() (*GetAllAccountsResponse, error) {
res, err := p.sendRequest("spot", http.MethodGet, "/v1/account/accounts", nil, nil, true)
if err != nil {
return nil, err
}
var result GetAllAccountsResponse
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
err = json.Unmarshal(buf.Bytes(), &result)
if err != nil {
return nil, err
}
return &result, nil
}
type GetAccountDataResponse struct {
Status string `json:"status"`
Data struct {
ID int `json:"id"`
Type string `json:"type"`
State string `json:"state"`
List []*SpotListData `json:"list"`
} `json:"data"`
}
type SpotListData struct {
Currency string `json:"currency"`
Type string `json:"type"`
Balance string `json:"balance"`
}
func (p *Client) GetAccountData(id int) (*GetAccountDataResponse, error) {
var buffer bytes.Buffer
buffer.WriteString("/v1/account/accounts/")
buffer.WriteString(strconv.Itoa(id))
buffer.WriteString("/balance")
res, err := p.sendRequest("spot", http.MethodGet, buffer.String(), nil, nil, true)
if err != nil {
return nil, err
}
var result GetAccountDataResponse
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
err = json.Unmarshal(buf.Bytes(), &result)
if err != nil {
return nil, err
}
return &result, nil
}
type GetLastTradeResponse struct {
Ch string `json:"ch"`
Status string `json:"status"`
Ts float64 `json:"ts"`
Tick struct {
ID float64 `json:"id"`
Ts float64 `json:"ts"`
Data []struct {
ID float64 `json:"id"`
Ts float64 `json:"ts"`
TradeID float64 `json:"trade-id"`
Amount float64 `json:"amount"`
Price float64 `json:"price"`
Direction string `json:"direction"`
} `json:"data"`
} `json:"tick"`
}
// symbol ex: btcusdt
func (p *Client) GetLastTrade(symbol string) (*GetLastTradeResponse, error) {
url := "/market/trade"
params := make(map[string]string)
params["symbol"] = strings.ToLower(symbol)
res, err := p.sendRequest("spot", http.MethodGet, url, nil, ¶ms, false)
if err != nil {
return nil, err
}
var result GetLastTradeResponse
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
err = json.Unmarshal(buf.Bytes(), &result)
if err != nil {
return nil, err
}
return &result, nil
}
type GetSpotDetailResponse struct {
Ch string `json:"ch"`
Status string `json:"status"`
Ts int64 `json:"ts"`
Tick struct {
ID int64 `json:"id"`
Version int64 `json:"version"`
Open float64 `json:"open"`
Close float64 `json:"close"`
Low float64 `json:"low"`
High float64 `json:"high"`
Amount float64 `json:"amount"`
Vol float64 `json:"vol"`
Count int `json:"count"`
Bid []float64 `json:"bid"`
Ask []float64 `json:"ask"`
} `json:"tick"`
}
func (p *Client) GetSpotDetail(symbol string) (*GetSpotDetailResponse, error) {
url := "/market/detail/merged"
params := make(map[string]string)
params["symbol"] = strings.ToLower(symbol)
res, err := p.sendRequest("spot", http.MethodGet, url, nil, ¶ms, false)
if err != nil {
return nil, err
}
var result GetSpotDetailResponse
buf := new(bytes.Buffer)
buf.ReadFrom(res.Body)
err = json.Unmarshal(buf.Bytes(), &result)
if err != nil {
return nil, err
}
return &result, nil
}