forked from bitbandi/go-livecoin
/
livecoin.go
368 lines (342 loc) · 10 KB
/
livecoin.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Package Livecoin is an implementation of the Livecoin API in Golang.
package livecoin
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/shopspring/decimal"
)
const (
API_BASE = "https://api.livecoin.net" // Livecoin API endpoint
)
// New returns an instantiated livecoin struct
func New(apiKey, apiSecret string) *Livecoin {
client := NewClient(apiKey, apiSecret)
return &Livecoin{client}
}
// NewWithCustomHttpClient returns an instantiated livecoin struct with custom http client
func NewWithCustomHttpClient(apiKey, apiSecret string, httpClient *http.Client) *Livecoin {
client := NewClientWithCustomHttpConfig(apiKey, apiSecret, httpClient)
return &Livecoin{client}
}
// NewWithCustomTimeout returns an instantiated livecoin struct with custom timeout
func NewWithCustomTimeout(apiKey, apiSecret string, timeout time.Duration) *Livecoin {
client := NewClientWithCustomTimeout(apiKey, apiSecret, timeout)
return &Livecoin{client}
}
// handleErr gets JSON response from livecoin API en deal with error
func handleErr(r interface{}) error {
switch v := r.(type) {
case map[string]interface{}:
errorMessage := r.(map[string]interface{})["errorMessage"]
if errorMessage != nil && errorMessage.(string) != "" {
return errors.New(errorMessage.(string))
}
case []interface{}:
return nil
default:
return fmt.Errorf("I don't know about type %T!\n", v)
}
return nil
}
// livecoin represent a livecoin client
type Livecoin struct {
client *client
}
// set enable/disable http request/response dump
func (c *Livecoin) SetDebug(enable bool) {
c.client.debug = enable
}
// Account
// GetBalances is used to retrieve all balances from your account
func (b *Livecoin) GetBalances() (balances []Balance, err error) {
r, err := b.client.do("GET", "payment/balances", nil, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &balances)
return
}
// Getbalance is used to retrieve the balance from your account for a specific currency.
// currency: a string literal for the currency (ex: LTC)
func (b *Livecoin) GetBalance(currency string) (balance Balance, err error) {
r, err := b.client.do("GET", "payment/balance", map[string]string{"currency": strings.ToUpper(currency)}, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &balance)
return
}
// GetOrderBook is used to retrieve the orderbook for specified currency pair
func (b *Livecoin) GetOrderBook(currency string) (orderbook Orderbook, err error) {
r, err := b.client.do("GET", "exchange/order_book", map[string]string{"currencyPair": strings.ToUpper(currency)}, false)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &orderbook)
return
}
// GetAllOrderBook is used to retrieve the orderbook for every currency pair.
func (b *Livecoin) GetAllOrderBook() (allOrderbook map[string]Orderbook, err error) {
r, err := b.client.do("GET", "exchange/all/order_book", nil, false)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &allOrderbook)
return
}
// GetTrades used to retrieve your trade history.
// market string literal for the market (ie. BTC/LTC). If set to "all", will return for all market
func (b *Livecoin) GetTrades(currencyPair string) (trades []Trade, err error) {
payload := make(map[string]string)
if currencyPair != "all" {
payload["currencyPair"] = currencyPair
}
r, err := b.client.do("GET", "exchange/trades", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
if res, ok := response.(map[string]interface{}); ok {
if exception, ok := res["exception"]; ok && exception == "Data not found" {
return
}
}
err = json.Unmarshal(r, &trades)
return
}
// GetTransactions is used to retrieve your withdrawal and deposit history
// "Start" and "end" are given in UNIX timestamp format in miliseconds and used to specify the date range for the data returned.
func (b *Livecoin) GetTransactions(start uint64, end uint64) (transactions []Transaction, err error) {
if end == 0 {
end = uint64(time.Now().Unix()) * 1000
}
r, err := b.client.do("GET", "payment/history/transactions", map[string]string{"types": "DEPOSIT,WITHDRAWAL", "start": strconv.FormatUint(uint64(start), 10), "end": strconv.FormatUint(uint64(end), 10)}, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &transactions)
return
}
// SellLimit is used to place a sell order (limit) for a specified currency pair.
func (b *Livecoin) SellLimit(market string, quantity, rate decimal.Decimal) (order NewOrder, err error) {
payload := map[string]string{
"currencyPair": market,
"price": fmt.Sprintf("%s", rate),
"quantity": fmt.Sprintf("%s", quantity),
}
r, err := b.client.do("POST", "exchange/selllimit", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &order)
return
}
// BuyLimit is used to place a buy order (limit) for particular currency pair.
func (b *Livecoin) BuyLimit(market string, quantity, rate decimal.Decimal) (order NewOrder, err error) {
payload := map[string]string{
"currencyPair": market,
"price": fmt.Sprintf("%s", rate),
"quantity": fmt.Sprintf("%s", quantity),
}
r, err := b.client.do("POST", "exchange/buylimit", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &order)
return
}
// SellMarket is used to place a sell order (market) for specified amount of selected currency pair.
func (b *Livecoin) SellMarket(market string, quantity decimal.Decimal) (order NewOrder, err error) {
payload := map[string]string{
"currencyPair": market,
"quantity": fmt.Sprintf("%s", quantity),
}
r, err := b.client.do("POST", "exchange/sellmarket", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &order)
return
}
// BuyMarket is used to place a buy order (market) of specified amount for particular currency pair.
func (b *Livecoin) BuyMarket(market string, quantity decimal.Decimal) (order NewOrder, err error) {
payload := map[string]string{
"currencyPair": market,
"quantity": fmt.Sprintf("%s", quantity),
}
r, err := b.client.do("POST", "exchange/buymarket", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &order)
return
}
// CancelOrder is used to cancel an order. NOTE: market orders CANNOT be cancelled
func (b *Livecoin) CancelOrder(market, orderId string) (cancelledOrder CancelledOrder, err error) {
payload := map[string]string{
"currencyPair": market,
"orderId": orderId,
}
r, err := b.client.do("POST", "exchange/cancellimit", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &cancelledOrder)
return
}
// GetOrder is used to retrieve order information by its ID.
func (b *Livecoin) GetOrder(orderId string) (orderInfo OrderInfo, err error) {
payload := map[string]string{
"orderId": orderId,
}
r, err := b.client.do("GET", "exchange/order", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &orderInfo)
return
}
// GetRestrictions is used to retrieve the restrictions of all currency pairs
func (b *Livecoin) GetRestrictions() (restrictions Restrictions, err error) {
r, err := b.client.do("GET", "exchange/restrictions", nil, false)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &restrictions)
return
}
// GetDepositAddress is used to get the deposit address for a specific currency
func (b *Livecoin) GetDepositAddress(currency string) (address DepositAddress, err error) {
payload := map[string]string{
"currency": currency,
}
r, err := b.client.do("GET", "payment/get/address", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &address)
return
}
// Withdraw is used to place a sell order (limit) for a specified currency pair.
func (b *Livecoin) Withdraw(wallet, currency string, quantity decimal.Decimal) (withdrawal Withdrawal, err error) {
payload := map[string]string{
"currency": currency,
"wallet": wallet,
"amount": fmt.Sprintf("%s", quantity),
}
r, err := b.client.do("POST", "payment/out/coin", payload, true)
if err != nil {
return
}
var response interface{}
if err = json.Unmarshal(r, &response); err != nil {
return
}
if err = handleErr(response); err != nil {
return
}
err = json.Unmarshal(r, &withdrawal)
return
}