-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupbit.go
286 lines (245 loc) · 7.92 KB
/
upbit.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
package crypto_exchange
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/sirupsen/logrus"
)
type UpbitService service
const (
upbitBaseURL = "https://api.upbit.com"
)
func newUpbitService(httpClient *http.Client, accessKey, secretKey string) *UpbitService {
return &UpbitService{
client: httpClient,
baseURL: upbitBaseURL,
accessKey: accessKey,
secretKey: secretKey,
}
}
type UpbitAccount struct {
Currency string `json:"currency,omitempty"`
Balance string `json:"balance,omitempty"`
Locked string `json:"locked,omitempty"`
AvgBuyPrice string `json:"avg_buy_price,omitempty"`
AvgBuyPriceModified bool `json:"avg_buy_price_modified,omitempty"`
UnitCurrency string `json:"unit_currency,omitempty"`
}
type UpbitErrorBody struct {
Message string `json:"message,omitempty"`
Name string `json:"name,omitempty"`
}
type UpbitError struct {
Error UpbitErrorBody `json:"error,omitempty"`
}
func (s *UpbitService) ListAccounts() ([]UpbitAccount, error) {
req, err := http.NewRequest(http.MethodGet, s.baseURL+"/v1/accounts", nil)
if err != nil {
return nil, err
}
authToken, err := generateAuthorizationToken(s.accessKey, s.secretKey, nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", authToken)
resp, err := getResponse(req)
if err != nil {
return nil, err
}
var accounts []UpbitAccount
if err := json.Unmarshal(resp, &accounts); err != nil {
return nil, err
}
return accounts, nil
}
type MarketCode struct {
// ex) KRW-BTC, BTC-ETC
Market string `json:"market,omitempty"`
KoreanName string `json:"korean_name,omitempty"`
EnglishName string `json:"english_name,omitempty"`
// NONE (해당 사항 없음), CAUTION(투자유의)
MarketWarning string `json:"market_warning,omitempty"`
}
func (s *UpbitService) ListMarketCodes() ([]MarketCode, error) {
req, err := http.NewRequest(http.MethodGet, s.baseURL+"/v1/market/all", nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
q := req.URL.Query()
q.Add("isDetails", "true")
req.URL.RawQuery = q.Encode()
resp, err := getResponse(req)
if err != nil {
return nil, err
}
var marketCodes []MarketCode
if err := json.Unmarshal(resp, &marketCodes); err != nil {
return nil, err
}
return marketCodes, nil
}
type MarketCurrentPrice struct {
// ex) KRW-BTC, BTC-ETC
Market string `json:"market,omitempty"`
TradeDate string `json:"trade_date,omitempty"`
TradeTime string `json:"trade_time,omitempty"`
TradeDateKst string `json:"trade_date_kst,omitempty"`
TradeTimeKst string `json:"trade_time_kst,omitempty"`
TradeTimestamp int64 `json:"trade_timestamp,omitempty"`
OpeningPrice float64 `json:"opening_price,omitempty"`
HighPrice float64 `json:"high_price,omitempty"`
LowPrice float64 `json:"low_price,omitempty"`
TradePrice float64 `json:"trade_price,omitempty"`
PrevClosingPrice float64 `json:"prev_closing_price,omitempty"`
Change string `json:"change,omitempty"`
ChangePrice float64 `json:"change_price,omitempty"`
ChangeRate float64 `json:"change_rate,omitempty"`
SignedChangePrice float64 `json:"signed_change_price,omitempty"`
SignedChangeRate float64 `json:"signed_change_rate,omitempty"`
TradeVolume float64 `json:"trade_volume,omitempty"`
AccTradePrice float64 `json:"acc_trade_price,omitempty"`
AccTradePrice24H float64 `json:"acc_trade_price_24h,omitempty"`
AccTradeVolume float64 `json:"acc_trade_volume,omitempty"`
AccTradeVolume24H float64 `json:"acc_trade_volume_24h,omitempty"`
Highest52WeekPrice float64 `json:"highest_52_week_price,omitempty"`
Highest52WeekDate string `json:"highest_52_week_date,omitempty"`
Lowest52WeekPrice float64 `json:"lowest_52_week_price,omitempty"`
Lowest52WeekDate string `json:"lowest_52_week_date,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
}
func (s *UpbitService) ListCurrentPriceByMarketCodes(marketCodes []string) ([]MarketCurrentPrice, error) {
req, err := http.NewRequest(http.MethodGet, s.baseURL+"/v1/ticker", nil)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
q := req.URL.Query()
q.Add("markets", strings.Join(marketCodes, ","))
req.URL.RawQuery = q.Encode()
resp, err := getResponse(req)
if err != nil {
return nil, err
}
var currPrices []MarketCurrentPrice
if err := json.Unmarshal(resp, &currPrices); err != nil {
return nil, err
}
return currPrices, nil
}
type BuyOrSell string
type OrderType string
const (
Buy BuyOrSell = "bid"
Sell BuyOrSell = "ask"
// Limit 지정가 주문
Limit OrderType = "limit"
// MarketPriceBuy 시장가 매수
MarketPriceBuy OrderType = "price"
// MarketPriceSell 시장가 매도
MarketPriceSell OrderType = "market"
)
type OrderResp struct {
Uuid string `json:"uuid,omitempty"`
Side string `json:"side,omitempty"`
OrdType string `json:"ord_type,omitempty"`
Price string `json:"price,omitempty"`
AvgPrice string `json:"avg_price,omitempty"`
State string `json:"state,omitempty"`
Market string `json:"market,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Volume string `json:"volume,omitempty"`
RemainingVolume string `json:"remaining_volume,omitempty"`
ReservedFee string `json:"reserved_fee,omitempty"`
RemainingFee string `json:"remaining_fee,omitempty"`
PaidFee string `json:"paid_fee,omitempty"`
Locked string `json:"locked,omitempty"`
ExecutedVolume string `json:"executed_volume,omitempty"`
TradesCount int `json:"trades_count,omitempty"`
}
func (s *UpbitService) CreateOrder(
marketCode string,
side BuyOrSell,
volume float64,
price float64,
orderType OrderType,
) (*OrderResp, error) {
params := url.Values{}
params.Add("market", marketCode)
params.Add("side", string(side))
params.Add("volume", fmt.Sprintf("%f", volume))
params.Add("price", fmt.Sprintf("%f", price))
params.Add("ord_type", string(orderType))
encodedParams := params.Encode()
// TODO - fix query param inappropriately encoded issue
values := map[string]string{
"market": params.Get("market"),
"side": params.Get("side"),
"volume": params.Get("volume"),
"price": params.Get("price"),
"ord_type": params.Get("ord_type"),
}
jsonPayload, _ := json.Marshal(values)
req, err := http.NewRequest(http.MethodPost, s.baseURL+"/v1/orders", bytes.NewBuffer(jsonPayload))
if err != nil {
return nil, err
}
authHeader, err := generateAuthorizationToken(s.accessKey, s.secretKey, &encodedParams)
if err != nil {
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Authorization", authHeader)
resp, err := getResponse(req)
if err != nil {
return nil, err
}
var orderResp *OrderResp
if err := json.Unmarshal(resp, orderResp); err != nil {
return nil, err
}
return orderResp, nil
}
func unmarshalUpbitError(b []byte, code int) error {
var upbitError UpbitError
if err := json.Unmarshal(b, &upbitError); err != nil {
return err
}
return errors.New("status code " + strconv.Itoa(code) + ": " + upbitError.Error.Message)
}
func closeBody(resp *http.Response) func() {
return func() {
if _, err := io.Copy(ioutil.Discard, resp.Body); err != nil {
logrus.Error(err.Error())
}
if err := resp.Body.Close(); err != nil {
logrus.Error(err.Error())
}
}
}
func getResponse(req *http.Request) ([]byte, error) {
resp, err := http.DefaultClient.Do(req)
if resp != nil {
defer closeBody(resp)
}
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, unmarshalUpbitError(b, resp.StatusCode)
}
return b, nil
}