forked from ccxt/ccxt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlivecoin.py
More file actions
639 lines (609 loc) · 24 KB
/
livecoin.py
File metadata and controls
639 lines (609 loc) · 24 KB
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# -*- coding: utf-8 -*-
# PLEASE DO NOT EDIT THIS FILE, IT IS GENERATED AND WILL BE OVERWRITTEN:
# https://github.com/ccxt/ccxt/blob/master/CONTRIBUTING.md#how-to-contribute-code
from ccxt.base.exchange import Exchange
# -----------------------------------------------------------------------------
try:
basestring # Python 3
except NameError:
basestring = str # Python 2
import hashlib
import math
import json
from ccxt.base.errors import ExchangeError
from ccxt.base.errors import AuthenticationError
from ccxt.base.errors import InsufficientFunds
from ccxt.base.errors import InvalidOrder
from ccxt.base.errors import OrderNotFound
from ccxt.base.errors import NotSupported
from ccxt.base.errors import DDoSProtection
from ccxt.base.errors import ExchangeNotAvailable
class livecoin (Exchange):
def describe(self):
return self.deep_extend(super(livecoin, self).describe(), {
'id': 'livecoin',
'name': 'LiveCoin',
'countries': ['US', 'UK', 'RU'],
'rateLimit': 1000,
'userAgent': self.userAgents['chrome'],
'has': {
'fetchDepositAddress': True,
'CORS': False,
'fetchTickers': True,
'fetchCurrencies': True,
'fetchTradingFees': True,
'fetchOrders': True,
'fetchOrder': True,
'fetchOpenOrders': True,
'fetchClosedOrders': True,
'withdraw': True,
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/27980768-f22fc424-638a-11e7-89c9-6010a54ff9be.jpg',
'api': 'https://api.livecoin.net',
'www': 'https://www.livecoin.net',
'doc': 'https://www.livecoin.net/api?lang=en',
},
'api': {
'public': {
'get': [
'exchange/all/order_book',
'exchange/last_trades',
'exchange/maxbid_minask',
'exchange/order_book',
'exchange/restrictions',
'exchange/ticker', # omit params to get all tickers at once
'info/coinInfo',
],
},
'private': {
'get': [
'exchange/client_orders',
'exchange/order',
'exchange/trades',
'exchange/commission',
'exchange/commissionCommonInfo',
'payment/balances',
'payment/balance',
'payment/get/address',
'payment/history/size',
'payment/history/transactions',
],
'post': [
'exchange/buylimit',
'exchange/buymarket',
'exchange/cancellimit',
'exchange/selllimit',
'exchange/sellmarket',
'payment/out/capitalist',
'payment/out/card',
'payment/out/coin',
'payment/out/okpay',
'payment/out/payeer',
'payment/out/perfectmoney',
'payment/voucher/amount',
'payment/voucher/make',
'payment/voucher/redeem',
],
},
},
'fees': {
'trading': {
'tierBased': False,
'percentage': True,
'maker': 0.18 / 100,
'taker': 0.18 / 100,
},
},
'commonCurrencies': {
'BTCH': 'Bithash',
'CPC': 'CapriCoin',
'CRC': 'CryCash',
'EDR': 'E-Dinar Coin', # conflicts with EDR for Endor Protocol and EDRCoin
'eETT': 'EETT',
'FirstBlood': '1ST',
'FORTYTWO': '42',
'ORE': 'Orectic',
'RUR': 'RUB',
'SCT': 'SpaceCoin',
'TPI': 'ThaneCoin',
'wETT': 'WETT',
'XBT': 'Bricktox',
},
'exceptions': {
'1': ExchangeError,
'10': AuthenticationError,
'100': ExchangeError, # invalid parameters
'101': AuthenticationError,
'102': AuthenticationError,
'103': InvalidOrder, # invalid currency
'104': InvalidOrder, # invalid amount
'105': InvalidOrder, # unable to block funds
'11': AuthenticationError,
'12': AuthenticationError,
'2': AuthenticationError, # "User not found"
'20': AuthenticationError,
'30': AuthenticationError,
'31': NotSupported,
'32': ExchangeError,
'429': DDoSProtection,
'503': ExchangeNotAvailable,
},
})
def fetch_markets(self):
markets = self.publicGetExchangeTicker()
restrictions = self.publicGetExchangeRestrictions()
restrictionsById = self.index_by(restrictions['restrictions'], 'currencyPair')
result = []
for p in range(0, len(markets)):
market = markets[p]
id = market['symbol']
symbol = id
baseId, quoteId = symbol.split('/')
base = self.common_currency_code(baseId)
quote = self.common_currency_code(quoteId)
coinRestrictions = self.safe_value(restrictionsById, symbol)
precision = {
'price': 5,
'amount': 8,
'cost': 8,
}
limits = {
'amount': {
'min': math.pow(10, -precision['amount']),
'max': math.pow(10, precision['amount']),
},
}
if coinRestrictions:
precision['price'] = self.safe_integer(coinRestrictions, 'priceScale', 5)
limits['amount']['min'] = self.safe_float(coinRestrictions, 'minLimitQuantity', limits['amount']['min'])
limits['price'] = {
'min': math.pow(10, -precision['price']),
'max': math.pow(10, precision['price']),
}
result.append({
'id': id,
'symbol': symbol,
'base': base,
'quote': quote,
'baseId': baseId,
'quoteId': quoteId,
'active': True,
'precision': precision,
'limits': limits,
'info': market,
})
return result
def fetch_currencies(self, params={}):
response = self.publicGetInfoCoinInfo(params)
currencies = response['info']
result = {}
for i in range(0, len(currencies)):
currency = currencies[i]
id = currency['symbol']
# todo: will need to rethink the fees
# to add support for multiple withdrawal/deposit methods and
# differentiated fees for each particular method
code = self.common_currency_code(id)
precision = 8 # default precision, todo: fix "magic constants"
active = (currency['walletStatus'] == 'normal')
result[code] = {
'id': id,
'code': code,
'info': currency,
'name': currency['name'],
'active': active,
'fee': currency['withdrawFee'], # todo: redesign
'precision': precision,
'limits': {
'amount': {
'min': currency['minOrderAmount'],
'max': math.pow(10, precision),
},
'price': {
'min': math.pow(10, -precision),
'max': math.pow(10, precision),
},
'cost': {
'min': currency['minOrderAmount'],
'max': None,
},
'withdraw': {
'min': currency['minWithdrawAmount'],
'max': math.pow(10, precision),
},
'deposit': {
'min': currency['minDepositAmount'],
'max': None,
},
},
}
result = self.append_fiat_currencies(result)
return result
def append_fiat_currencies(self, result):
precision = 8
defaults = {
'info': None,
'active': True,
'fee': None,
'precision': precision,
'limits': {
'withdraw': {'min': None, 'max': None},
'deposit': {'min': None, 'max': None},
'amount': {'min': None, 'max': None},
'cost': {'min': None, 'max': None},
'price': {
'min': math.pow(10, -precision),
'max': math.pow(10, precision),
},
},
}
currencies = [
{'id': 'USD', 'code': 'USD', 'name': 'US Dollar'},
{'id': 'EUR', 'code': 'EUR', 'name': 'Euro'},
# {'id': 'RUR', 'code': 'RUB', 'name': 'Russian ruble'},
]
currencies.append({
'id': 'RUR',
'code': self.common_currency_code('RUR'),
'name': 'Russian ruble',
})
for i in range(0, len(currencies)):
currency = currencies[i]
code = currency['code']
result[code] = self.extend(defaults, currency)
return result
def fetch_balance(self, params={}):
self.load_markets()
balances = self.privateGetPaymentBalances()
result = {'info': balances}
for b in range(0, len(balances)):
balance = balances[b]
currency = balance['currency']
account = None
if currency in result:
account = result[currency]
else:
account = self.account()
if balance['type'] == 'total':
account['total'] = float(balance['value'])
if balance['type'] == 'available':
account['free'] = float(balance['value'])
if balance['type'] == 'trade':
account['used'] = float(balance['value'])
result[currency] = account
return self.parse_balance(result)
def fetch_trading_fees(self, params={}):
self.load_markets()
response = self.privateGetExchangeCommissionCommonInfo(params)
commission = self.safe_float(response, 'commission')
return {
'info': response,
'maker': commission,
'taker': commission,
}
def fetch_order_book(self, symbol, limit=None, params={}):
self.load_markets()
request = {
'currencyPair': self.market_id(symbol),
'groupByPrice': 'false',
}
if limit is not None:
request['depth'] = limit # 100
orderbook = self.publicGetExchangeOrderBook(self.extend(request, params))
timestamp = orderbook['timestamp']
return self.parse_order_book(orderbook, timestamp)
def parse_ticker(self, ticker, market=None):
timestamp = self.milliseconds()
symbol = None
if market:
symbol = market['symbol']
vwap = self.safe_float(ticker, 'vwap')
baseVolume = self.safe_float(ticker, 'volume')
quoteVolume = baseVolume * vwap
last = self.safe_float(ticker, 'last')
return {
'symbol': symbol,
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'high': self.safe_float(ticker, 'high'),
'low': self.safe_float(ticker, 'low'),
'bid': self.safe_float(ticker, 'best_bid'),
'bidVolume': None,
'ask': self.safe_float(ticker, 'best_ask'),
'askVolume': None,
'vwap': self.safe_float(ticker, 'vwap'),
'open': None,
'close': last,
'last': last,
'previousClose': None,
'change': None,
'percentage': None,
'average': None,
'baseVolume': baseVolume,
'quoteVolume': quoteVolume,
'info': ticker,
}
def fetch_tickers(self, symbols=None, params={}):
self.load_markets()
response = self.publicGetExchangeTicker(params)
tickers = self.index_by(response, 'symbol')
ids = list(tickers.keys())
result = {}
for i in range(0, len(ids)):
id = ids[i]
market = self.markets_by_id[id]
symbol = market['symbol']
ticker = tickers[id]
result[symbol] = self.parse_ticker(ticker, market)
return result
def fetch_ticker(self, symbol, params={}):
self.load_markets()
market = self.market(symbol)
ticker = self.publicGetExchangeTicker(self.extend({
'currencyPair': market['id'],
}, params))
return self.parse_ticker(ticker, market)
def parse_trade(self, trade, market):
timestamp = trade['time'] * 1000
return {
'info': trade,
'timestamp': timestamp,
'datetime': self.iso8601(timestamp),
'symbol': market['symbol'],
'id': str(trade['id']),
'order': None,
'type': None,
'side': trade['type'].lower(),
'price': trade['price'],
'amount': trade['quantity'],
}
def fetch_trades(self, symbol, since=None, limit=None, params={}):
self.load_markets()
market = self.market(symbol)
response = self.publicGetExchangeLastTrades(self.extend({
'currencyPair': market['id'],
}, params))
return self.parse_trades(response, market, since, limit)
def fetch_order(self, id, symbol=None, params={}):
self.load_markets()
request = {
'orderId': id,
}
response = self.privateGetExchangeOrder(self.extend(request, params))
return self.parse_order(response)
def parse_order_status(self, status):
statuses = {
'OPEN': 'open',
'PARTIALLY_FILLED': 'open',
'EXECUTED': 'closed',
'CANCELLED': 'canceled',
'PARTIALLY_FILLED_AND_CANCELLED': 'canceled',
}
if status in statuses:
return statuses[status]
return status
def parse_order(self, order, market=None):
timestamp = None
datetime = None
if 'lastModificationTime' in order:
timestamp = self.safe_string(order, 'lastModificationTime')
if timestamp is not None:
if timestamp.find('T') >= 0:
timestamp = self.parse8601(timestamp)
else:
timestamp = self.safe_integer(order, 'lastModificationTime')
if timestamp:
datetime = self.iso8601(timestamp)
# TODO currently not supported by livecoin
# trades = self.parse_trades(order['trades'], market, since, limit)
trades = None
status = self.safe_string(order, 'status')
status = self.safe_string(order, 'orderStatus', status)
status = self.parse_order_status(status)
symbol = None
if market is None:
marketId = self.safe_string(order, 'currencyPair')
marketId = self.safe_string(order, 'symbol', marketId)
if marketId in self.markets_by_id:
market = self.markets_by_id[marketId]
type = None
side = None
if 'type' in order:
lowercaseType = order['type'].lower()
orderType = lowercaseType.split('_')
type = orderType[0]
side = orderType[1]
price = self.safe_float(order, 'price')
# of the next two lines the latter overrides the former, if present in the order structure
remaining = self.safe_float(order, 'remainingQuantity')
remaining = self.safe_float(order, 'remaining_quantity', remaining)
amount = self.safe_float(order, 'quantity', remaining)
filled = None
if remaining is not None:
filled = amount - remaining
cost = None
if filled is not None and price is not None:
cost = filled * price
feeRate = self.safe_float(order, 'commission_rate')
feeCost = None
if cost is not None and feeRate is not None:
feeCost = cost * feeRate
feeCurrency = None
if market is not None:
symbol = market['symbol']
feeCurrency = market['quote']
return {
'info': order,
'id': order['id'],
'timestamp': timestamp,
'datetime': datetime,
'lastTradeTimestamp': None,
'status': status,
'symbol': symbol,
'type': type,
'side': side,
'price': price,
'amount': amount,
'cost': cost,
'filled': filled,
'remaining': remaining,
'trades': trades,
'fee': {
'cost': feeCost,
'currency': feeCurrency,
'rate': feeRate,
},
}
def fetch_orders(self, symbol=None, since=None, limit=None, params={}):
self.load_markets()
market = None
request = {}
if symbol is not None:
market = self.market(symbol)
request['currencyPair'] = market['id']
if since is not None:
request['issuedFrom'] = int(since)
if limit is not None:
request['endRow'] = limit - 1
response = self.privateGetExchangeClientOrders(self.extend(request, params))
result = []
rawOrders = []
if response['data']:
rawOrders = response['data']
for i in range(0, len(rawOrders)):
order = rawOrders[i]
result.append(self.parse_order(order, market))
return result
def fetch_open_orders(self, symbol=None, since=None, limit=None, params={}):
result = self.fetch_orders(symbol, since, limit, self.extend({
'openClosed': 'OPEN',
}, params))
return result
def fetch_closed_orders(self, symbol=None, since=None, limit=None, params={}):
result = self.fetch_orders(symbol, since, limit, self.extend({
'openClosed': 'CLOSED',
}, params))
return result
def create_order(self, symbol, type, side, amount, price=None, params={}):
self.load_markets()
method = 'privatePostExchange' + self.capitalize(side) + type
market = self.market(symbol)
order = {
'quantity': self.amount_to_precision(symbol, amount),
'currencyPair': market['id'],
}
if type == 'limit':
order['price'] = self.price_to_precision(symbol, price)
response = getattr(self, method)(self.extend(order, params))
result = {
'info': response,
'id': str(response['orderId']),
}
success = self.safe_value(response, 'success')
if success:
result['status'] = 'open'
return result
def cancel_order(self, id, symbol=None, params={}):
if symbol is None:
raise ExchangeError(self.id + ' cancelOrder requires a symbol argument')
self.load_markets()
market = self.market(symbol)
currencyPair = market['id']
response = self.privatePostExchangeCancellimit(self.extend({
'orderId': id,
'currencyPair': currencyPair,
}, params))
message = self.safe_string(response, 'message', self.json(response))
if 'success' in response:
if not response['success']:
raise InvalidOrder(message)
elif 'cancelled' in response:
if response['cancelled']:
return {
'status': 'canceled',
'info': response,
}
else:
raise OrderNotFound(message)
raise ExchangeError(self.id + ' cancelOrder() failed: ' + self.json(response))
def withdraw(self, currency, amount, address, tag=None, params={}):
# Sometimes the response with be {key: null} for all keys.
# An example is if you attempt to withdraw more than is allowed when withdrawal fees are considered.
self.load_markets()
self.check_address(address)
wallet = address
if tag is not None:
wallet += '::' + tag
withdrawal = {
'amount': self.truncate(amount, self.currencies[currency]['precision']), # throws an error when amount is too precise
'currency': self.common_currency_code(currency),
'wallet': wallet,
}
response = self.privatePostPaymentOutCoin(self.extend(withdrawal, params))
id = self.safe_integer(response, 'id')
if id is None:
raise InsufficientFunds(self.id + ' insufficient funds to cover requested withdrawal amount post fees ' + self.json(response))
return {
'info': response,
'id': id,
}
def fetch_deposit_address(self, currency, params={}):
request = {
'currency': currency,
}
response = self.privateGetPaymentGetAddress(self.extend(request, params))
address = self.safe_string(response, 'wallet')
tag = None
if address.find(':') >= 0:
parts = address.split(':')
address = parts[0]
tag = parts[2]
self.check_address(address)
return {
'currency': currency,
'address': address,
'tag': tag,
'info': response,
}
def sign(self, path, api='public', method='GET', params={}, headers=None, body=None):
url = self.urls['api'] + '/' + path
query = self.urlencode(self.keysort(params))
if method == 'GET':
if params:
url += '?' + query
if api == 'private':
self.check_required_credentials()
if method == 'POST':
body = query
signature = self.hmac(self.encode(query), self.encode(self.secret), hashlib.sha256)
headers = {
'Api-Key': self.apiKey,
'Sign': signature.upper(),
'Content-Type': 'application/x-www-form-urlencoded',
}
return {'url': url, 'method': method, 'body': body, 'headers': headers}
def handle_errors(self, code, reason, url, method, headers, body):
if not isinstance(body, basestring):
return
if body[0] == '{':
response = json.loads(body)
if code >= 300:
errorCode = self.safe_string(response, 'errorCode')
if errorCode in self.exceptions:
ExceptionClass = self.exceptions[errorCode]
raise ExceptionClass(self.id + ' ' + body)
else:
raise ExchangeError(self.id + ' ' + body)
# returns status code 200 even if success == False
success = self.safe_value(response, 'success', True)
if not success:
message = self.safe_string(response, 'message')
if message is not None:
if message.find('Cannot find order') >= 0:
raise OrderNotFound(self.id + ' ' + body)
exception = self.safe_string(response, 'exception')
if exception is not None:
if exception.find('Minimal amount is') >= 0:
raise InvalidOrder(self.id + ' ' + body)
raise ExchangeError(self.id + ' ' + body)