-
Notifications
You must be signed in to change notification settings - Fork 2
/
exmo.go
333 lines (279 loc) · 9.64 KB
/
exmo.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
330
331
332
333
/*
Copyright 2019 Vadim Inshakov
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package exmo
import (
"bytes"
"crypto/hmac"
"crypto/sha512"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strconv"
"time"
)
// ApiResponse is a map for API responses
type ApiResponse map[string]interface{}
// ApiParams is a map for API calls' params
type ApiParams map[string]string
// Exmo holds client-specific info
type Exmo struct {
key string // public key
secret string // secret key
client *http.Client
}
// CheckErr checks error object
func CheckErr(err error) {
if err != nil {
log.Printf("api error: %s\n", err.Error())
}
}
// Api creates Exmo instance with specified credentials
func Api(key string, secret string) Exmo {
var netTransport = &http.Transport{
MaxIdleConns: 30,
MaxConnsPerHost: 1,
IdleConnTimeout: 30 * time.Second,
TLSHandshakeTimeout: 5 * time.Second,
}
var client = &http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
}
return Exmo{key, secret, client}
}
// Api_query is a general query method for API calls
func (ex *Exmo) Api_query(mode string, method string, params ApiParams) (ApiResponse, error) {
post_params := url.Values{}
if mode == "authenticated" {
post_params.Add("nonce", nonce())
}
if params != nil {
for key, value := range params {
post_params.Add(key, value)
}
}
post_content := post_params.Encode()
sign := ex.Do_sign(post_content)
req, _ := http.NewRequest("POST", "https://api.exmo.com/v1/"+method, bytes.NewBuffer([]byte(post_content)))
req.Header.Set("Key", ex.key)
req.Header.Set("Sign", sign)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(post_content)))
resp, err := ex.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.Status != "200 OK" {
return nil, errors.New("http status: " + resp.Status)
}
body, err1 := ioutil.ReadAll(resp.Body)
if err1 != nil {
return nil, err1
}
var dat map[string]interface{}
err2 := json.Unmarshal([]byte(body), &dat)
if err2 != nil {
return nil, err2
}
if result, ok := dat["result"]; ok && result.(bool) != true {
return nil, errors.New(dat["error"].(string))
}
return dat, nil
}
// nonce generates request parameter ‘nonce’ with incremental numerical value (>0). The incremental numerical value should never reiterate or decrease.
func nonce() string {
return fmt.Sprintf("%d", time.Now().UnixNano())
}
// Do_sign encrypts POST data (param=val¶m1=val1) with method HMAC-SHA512 using secret key; the secret key also can be found in user’s profile settings
func (ex *Exmo) Do_sign(message string) string {
mac := hmac.New(sha512.New, []byte(ex.secret))
mac.Write([]byte(message))
return fmt.Sprintf("%x", mac.Sum(nil))
}
/*
Public API
*/
// GetTrades return list of the deals on currency pairs
func (ex *Exmo) GetTrades(pair string) (response ApiResponse, err error) {
response, err = ex.Api_query("public", "trades", ApiParams{"pair": pair})
CheckErr(err)
return
}
// GetOrderBook return the book of current orders on the currency pair
func (ex *Exmo) GetOrderBook(pair string, limit int) (response ApiResponse, err error) {
if limit < 100 || limit > 1000 {
fmt.Printf("limit param must be in range of 100-1000")
response = nil
err = errors.New("limit param must be in range of 100-1000")
} else {
response, err = ex.Api_query("public", "order_book", ApiParams{"pair": pair, "limit": string(limit)})
CheckErr(err)
}
return
}
// Ticker return statistics on prices and volume of trades by currency pairs
func (ex *Exmo) Ticker() (response ApiResponse, err error) {
response, err = ex.Api_query("public", "ticker", ApiParams{})
if err != nil {
fmt.Printf("api error: %s\n", err.Error())
}
return
}
// GetPairSettings return currency pairs settings
func (ex *Exmo) GetPairSettings() (response ApiResponse, err error) {
response, err = ex.Api_query("public", "pair_settings", ApiParams{})
CheckErr(err)
return
}
// GetCurrency return currencies list
func (ex *Exmo) GetCurrency() (response []string, err error) {
resp, err := http.Get("https://api.exmo.com/v1/currency")
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.Status != "200 OK" {
return nil, errors.New("http status: " + resp.Status)
}
body, err1 := ioutil.ReadAll(resp.Body)
if err1 != nil {
return nil, err1
}
var dat []string
err2 := json.Unmarshal([]byte(body), &dat)
if err2 != nil {
return nil, err2
}
return dat, nil
}
/*
Authenticated API
*/
// GetUserInfo return information about user's account
func (ex *Exmo) GetUserInfo() (response ApiResponse, err error) {
response, err = ex.Api_query("authenticated", "user_info", nil)
CheckErr(err)
return
}
// GetUserTrades return the list of user’s deals
func (ex *Exmo) GetUserTrades(pair string, offset, limit int) (response ApiResponse, err error) {
response, err = ex.Api_query("authenticated", "user_trades", ApiParams{"pair": pair, "limit": string(limit), "offset": string(offset)})
CheckErr(err)
return
}
// OrderCreate creates order
func (ex *Exmo) OrderCreate(pair string, quantity string, price string, typeOrder string) (response ApiResponse, err error) {
response, err = ex.Api_query("authenticated", "order_create", ApiParams{"pair": pair, "quantity": quantity, "price": price, "type": typeOrder})
CheckErr(err)
return
}
// Buy creates buy order
func (ex *Exmo) Buy(pair string, quantity string, price string) (response ApiResponse, err error) {
response, err = ex.OrderCreate(pair, quantity, price, "buy")
CheckErr(err)
return
}
// Buy creates sell order
func (ex *Exmo) Sell(pair string, quantity string, price string) (response ApiResponse, err error) {
response, err = ex.OrderCreate(pair, quantity, price, "sell")
CheckErr(err)
return
}
// MarketBuy creates market buy-order
func (ex *Exmo) MarketBuy(pair string, quantity string) (response ApiResponse, err error) {
response, err = ex.OrderCreate(pair, quantity, "0", "market_buy")
CheckErr(err)
return
}
// MarketBuyTotal creates market buy-order for a certain amount (quantity parameter)
func (ex *Exmo) MarketBuyTotal(pair string, quantity string) (response ApiResponse, err error) {
response, err = ex.OrderCreate(pair, quantity, "0", "market_buy_total")
CheckErr(err)
return
}
// MarketSell creates market sell-order
func (ex *Exmo) MarketSell(pair string, quantity string) (response ApiResponse, err error) {
response, err = ex.OrderCreate(pair, quantity, "0", "market_sell")
CheckErr(err)
return
}
// MarketSellTotal creates market sell-order for a certain amount (quantity parameter)
func (ex *Exmo) MarketSellTotal(pair string, quantity string) (response ApiResponse, err error) {
response, err = ex.OrderCreate(pair, quantity, "0", "market_sell_total")
CheckErr(err)
return
}
// OrderCancel cancels order
func (ex *Exmo) OrderCancel(orderId string) (response ApiResponse, err error) {
response, err = ex.Api_query("authenticated", "order_cancel", ApiParams{"order_id": orderId})
CheckErr(err)
return
}
// GetUserOpenOrders returns the list of user’s active orders
func (ex *Exmo) GetUserOpenOrders() (response ApiResponse, err error) {
response, err = ex.Api_query("authenticated", "user_open_orders", ApiParams{})
CheckErr(err)
return
}
// GetUserCancelledOrders returns the list of user’s deals
// This method almost completely copies Api_query method, but it returns array of interfaces, not map
func (ex *Exmo) GetUserCancelledOrders(offset uint, limit uint) (response ApiResponse, err error) {
if limit < 100 || limit > 1000 {
fmt.Printf("limit param must be in range of 100-1000")
response = nil
err = errors.New("limit param must be in range of 100-1000")
} else {
response, err = ex.Api_query("authenticated", "order_cancel", ApiParams{"offset": string(offset), "limit": string(limit)})
CheckErr(err)
return
}
return
}
// GetOrderTrades returns the list of user’s cancelled orders
func (ex *Exmo) GetOrderTrades(orderId string) (response ApiResponse, err error) {
response, err = ex.Api_query("authenticated", "order_trades", ApiParams{"order_id": orderId})
CheckErr(err)
return
}
// GetRequiredAmount calculating and returns the sum of buying a certain amount of currency for the particular currency pair
func (ex *Exmo) GetRequiredAmount(pair string, quantity string) (response ApiResponse, err error) {
response, err = ex.Api_query("authenticated", "required_amount", ApiParams{"pair": pair, "quantity": quantity})
CheckErr(err)
return
}
// GetDepositAddress returns the list of addresses for cryptocurrency deposit
func (ex *Exmo) GetDepositAddress() (response ApiResponse, err error) {
response, err = ex.Api_query("authenticated", "deposit_address", ApiParams{})
CheckErr(err)
return
}
/*
Wallet API
*/
// GetWalletHistory returns history of wallet
func (ex *Exmo) GetWalletHistory(date time.Time) (response ApiResponse, err error) {
dateUnix := date.Unix()
dateConverted := strconv.Itoa(int(dateUnix))
if date.IsZero() {
response, err = ex.Api_query("authenticated", "wallet_history", ApiParams{})
} else {
response, err = ex.Api_query("authenticated", "wallet_history", ApiParams{"date": dateConverted})
}
CheckErr(err)
return
}