This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
coincheck.go
295 lines (268 loc) · 7.8 KB
/
coincheck.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
package coincheck
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"time"
)
// URL is a Coincheck API base URL
const URL = "https://coincheck.jp"
// APIClient struct represents Coincheck API client
type APIClient struct {
key string
secret string
client *http.Client
}
// Ticker represents Coincheck ticker
type Ticker struct {
Ask float64 `json:"ask"`
Bid float64 `json:"bid"`
High float64 `json:"high"`
Last float64 `json:"last"`
Low float64 `json:"low"`
Timestamp float64 `json:"timestamp"`
Volume string `json:"volume"`
}
// Trade represents Coincheck trade
type Trade struct {
Amount string `json:"amount"`
CreatedAt string `json:"created_at"`
ID int `json:"id"`
OrderType string `json:"order_type"`
Rate float64 `json:"rate"`
}
// OrderBook represents account Coincheck order book
type OrderBook struct {
Asks [][]string `json:"asks"`
Bids [][]string `json:"bids"`
}
// Balance represents account balance
type Balance struct {
BTC string `json:"btc"`
BTCDebt string `json:"btc_debt"`
BTCLendInUse string `json:"btc_lend_in_use"`
BTCLent string `json:"btc_lent"`
BTCReserved string `json:"btc_reserved"`
JPY string `json:"jpy"`
JPYDebt string `json:"jpy_debt"`
JPYLendInUse string `json:"jpy_lend_in_use"`
JPYLent string `json:"jpy_lent"`
JPYReserved string `json:"jpy_reserved"`
Success bool `json:"success"`
Error string `json:"error"`
}
// LeverageBalance represents account leverage balance
type LeverageBalance struct {
Margin struct {
JPY float64 `json:"jpy"`
} `json:"margin"`
MarginAvailable struct {
JPY string `json:"jpy"`
} `json:"margin_available"`
MarginLevel string `json:"margin_level"`
Success bool `json:"success"`
Error string `json:"error"`
}
// Accounts represents Coincheck accounts
type Accounts struct {
BitcoinAddress string `json:"bitcoin_address"`
Email string `json:"email"`
ID int `json:"id"`
IdentityStatus string `json:"identity_status"`
LendingLeverage string `json:"lending_leverage"`
MakerFee string `json:"maker_fee"`
Success bool `json:"success"`
TakerFee string `json:"taker_fee"`
Error string `json:"error"`
}
// Order represents a new order.
type Order struct {
ID int `json:"id"`
Rate string `json:"rate"`
Amount string `json:"amount"`
OrderType string `json:"order_type"`
Pair string `json:"pair"`
PendingAmount string `json:"pending_amount"`
PendingMarketBuyAmount string `json:"pending_marker_buy_amount"`
StopLossRate string `json:"stop_loss_rate"`
CreatedAt string `json:"created_at"`
Success bool `json:"success"`
Error string `json:"error"`
}
// OpenOrders represents list of open orders.
type OpenOrders struct {
Success bool `json:"success"`
Error string `json:"error"`
Orders []Order
}
// New creates a new Kraken API struct
func New(key, secret string) (client *APIClient) {
client = new(APIClient)
client.key = key
client.secret = secret
client.client = new(http.Client)
return client
}
// GetTicker returns Coincheck ticker
func (api APIClient) GetTicker() (ticker Ticker, err error) {
err = api.doGetRequest("/api/ticker", []byte(""), &ticker)
if err != nil {
return ticker, err
}
return ticker, nil
}
// GetTrades returns Coincheck trades
func (api APIClient) GetTrades() (trades []Trade, err error) {
err = api.doGetRequest("/api/trades", []byte(""), &trades)
if err != nil {
return trades, err
}
return trades, nil
}
// GetOrderBook returns Coincheck order book
func (api APIClient) GetOrderBook() (orderBook OrderBook, err error) {
err = api.doGetRequest("/api/order_books", []byte(""), &orderBook)
if err != nil {
return orderBook, err
}
return orderBook, nil
}
// GetBalance returns account balance
func (api APIClient) GetBalance() (balance Balance, err error) {
err = api.doGetRequest("/api/accounts/balance", []byte(""), &balance)
if err != nil {
return balance, err
}
if !balance.Success {
return balance, errors.New(balance.Error)
}
return balance, nil
}
// GetLeverageBalance returns account leverage balance
func (api APIClient) GetLeverageBalance() (leverageBalance LeverageBalance, err error) {
err = api.doGetRequest("/api/accounts/leverage_balance", []byte(""), &leverageBalance)
if err != nil {
return leverageBalance, err
}
if !leverageBalance.Success {
return leverageBalance, errors.New(leverageBalance.Error)
}
return leverageBalance, nil
}
// GetAccounts returns accounts
func (api APIClient) GetAccounts() (accounts Accounts, err error) {
err = api.doGetRequest("/api/accounts", []byte(""), &accounts)
if err != nil {
return accounts, err
}
if !accounts.Success {
return accounts, errors.New(accounts.Error)
}
return accounts, nil
}
// GetOpenOrders returns all open orders.
func (api APIClient) GetOpenOrders() (orders OpenOrders, err error) {
err = api.doGetRequest("/api/exchange/orders/opens", []byte(""), &orders)
if err != nil {
return orders, err
}
if !orders.Success {
return orders, errors.New(orders.Error)
}
return orders, nil
}
// NewOrder sends a new order.
func (api APIClient) NewOrder(order Order) (newOrder Order, err error) {
newOrder = order
data, err := json.Marshal(newOrder)
if err != nil {
return newOrder, err
}
err = api.doPostRequest("/api/exchange/orders", data, &newOrder)
if err != nil {
return newOrder, err
}
if newOrder.Error != "" {
return newOrder, errors.New(newOrder.Error)
}
return newOrder, nil
}
func (api *APIClient) doGetRequest(endpoint string, body []byte, data interface{}) (err error) {
headers := headers(api.key, api.secret, URL+endpoint, string(body))
resp, err := api.doRequest("GET", URL+endpoint, body, headers)
if err != nil {
return err
}
err = json.Unmarshal(resp, data)
if err != nil {
return err
}
return nil
}
func (api *APIClient) doPostRequest(endpoint string, body []byte, data interface{}) (err error) {
headers := headers(api.key, api.secret, URL+endpoint, string(body))
resp, err := api.doRequest("POST", URL+endpoint, body, headers)
if err != nil {
return err
}
err = json.Unmarshal(resp, data)
if err != nil {
return err
}
return nil
}
func (api *APIClient) doRequest(method, endpoint string, data []byte, headers map[string]string) ([]byte, error) {
req, err := http.NewRequest(method, endpoint, bytes.NewBuffer(data))
if err != nil {
return nil, requestError(err.Error())
}
setHeaders(req, headers)
resp, err := api.client.Do(req)
if err != nil {
return nil, requestError(err.Error())
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, requestError(err.Error())
}
return body, nil
}
// headers
func headers(key, secret, uri, body string) map[string]string {
currentTime := time.Now().UTC().Unix()
nonce := strconv.Itoa(int(currentTime))
message := nonce + uri + body
signature := computeHmac256(message, secret)
headers := map[string]string{
"Content-Type": "application/json",
"ACCESS-KEY": key,
"ACCESS-NONCE": nonce,
"ACCESS-SIGNATURE": signature,
}
return headers
}
// computeHmac256 calculate hash of message usign HMAC SHA256
func computeHmac256(message string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
return hex.EncodeToString(h.Sum(nil))
}
// requestError formats request error
func requestError(err interface{}) error {
return fmt.Errorf("Could not execute request! (%s)", err)
}
// setHeaders sets request headers
func setHeaders(req *http.Request, headers map[string]string) {
for key, value := range headers {
req.Header.Add(key, value)
}
}