UNPKG

@jmparsons/ccxt

Version:

A JavaScript / Python / PHP cryptocurrency trading library with support for 100+ exchanges

261 lines (243 loc) 11 kB
'use strict'; // --------------------------------------------------------------------------- const Exchange = require ('./base/Exchange'); const { ExchangeError } = require ('./base/errors'); // --------------------------------------------------------------------------- module.exports = class _1btcxe extends Exchange { describe () { return this.deepExtend (super.describe (), { 'id': '_1btcxe', 'name': '1BTCXE', 'countries': 'PA', // Panama 'comment': 'Crypto Capital API', 'has': { 'CORS': true, 'withdraw': true, }, 'timeframes': { '1d': '1year', }, 'urls': { 'logo': 'https://user-images.githubusercontent.com/1294454/27766049-2b294408-5ecc-11e7-85cc-adaff013dc1a.jpg', 'api': 'https://1btcxe.com/api', 'www': 'https://1btcxe.com', 'doc': 'https://1btcxe.com/api-docs.php', }, 'api': { 'public': { 'get': [ 'stats', 'historical-prices', 'order-book', 'transactions', ], }, 'private': { 'post': [ 'balances-and-info', 'open-orders', 'user-transactions', 'btc-deposit-address/get', 'btc-deposit-address/new', 'deposits/get', 'withdrawals/get', 'orders/new', 'orders/edit', 'orders/cancel', 'orders/status', 'withdrawals/new', ], }, }, }); } async fetchMarkets () { return [ { 'id': 'USD', 'symbol': 'BTC/USD', 'base': 'BTC', 'quote': 'USD' }, { 'id': 'EUR', 'symbol': 'BTC/EUR', 'base': 'BTC', 'quote': 'EUR' }, { 'id': 'CNY', 'symbol': 'BTC/CNY', 'base': 'BTC', 'quote': 'CNY' }, { 'id': 'RUB', 'symbol': 'BTC/RUB', 'base': 'BTC', 'quote': 'RUB' }, { 'id': 'CHF', 'symbol': 'BTC/CHF', 'base': 'BTC', 'quote': 'CHF' }, { 'id': 'JPY', 'symbol': 'BTC/JPY', 'base': 'BTC', 'quote': 'JPY' }, { 'id': 'GBP', 'symbol': 'BTC/GBP', 'base': 'BTC', 'quote': 'GBP' }, { 'id': 'CAD', 'symbol': 'BTC/CAD', 'base': 'BTC', 'quote': 'CAD' }, { 'id': 'AUD', 'symbol': 'BTC/AUD', 'base': 'BTC', 'quote': 'AUD' }, { 'id': 'AED', 'symbol': 'BTC/AED', 'base': 'BTC', 'quote': 'AED' }, { 'id': 'BGN', 'symbol': 'BTC/BGN', 'base': 'BTC', 'quote': 'BGN' }, { 'id': 'CZK', 'symbol': 'BTC/CZK', 'base': 'BTC', 'quote': 'CZK' }, { 'id': 'DKK', 'symbol': 'BTC/DKK', 'base': 'BTC', 'quote': 'DKK' }, { 'id': 'HKD', 'symbol': 'BTC/HKD', 'base': 'BTC', 'quote': 'HKD' }, { 'id': 'HRK', 'symbol': 'BTC/HRK', 'base': 'BTC', 'quote': 'HRK' }, { 'id': 'HUF', 'symbol': 'BTC/HUF', 'base': 'BTC', 'quote': 'HUF' }, { 'id': 'ILS', 'symbol': 'BTC/ILS', 'base': 'BTC', 'quote': 'ILS' }, { 'id': 'INR', 'symbol': 'BTC/INR', 'base': 'BTC', 'quote': 'INR' }, { 'id': 'MUR', 'symbol': 'BTC/MUR', 'base': 'BTC', 'quote': 'MUR' }, { 'id': 'MXN', 'symbol': 'BTC/MXN', 'base': 'BTC', 'quote': 'MXN' }, { 'id': 'NOK', 'symbol': 'BTC/NOK', 'base': 'BTC', 'quote': 'NOK' }, { 'id': 'NZD', 'symbol': 'BTC/NZD', 'base': 'BTC', 'quote': 'NZD' }, { 'id': 'PLN', 'symbol': 'BTC/PLN', 'base': 'BTC', 'quote': 'PLN' }, { 'id': 'RON', 'symbol': 'BTC/RON', 'base': 'BTC', 'quote': 'RON' }, { 'id': 'SEK', 'symbol': 'BTC/SEK', 'base': 'BTC', 'quote': 'SEK' }, { 'id': 'SGD', 'symbol': 'BTC/SGD', 'base': 'BTC', 'quote': 'SGD' }, { 'id': 'THB', 'symbol': 'BTC/THB', 'base': 'BTC', 'quote': 'THB' }, { 'id': 'TRY', 'symbol': 'BTC/TRY', 'base': 'BTC', 'quote': 'TRY' }, { 'id': 'ZAR', 'symbol': 'BTC/ZAR', 'base': 'BTC', 'quote': 'ZAR' }, ]; } async fetchBalance (params = {}) { let response = await this.privatePostBalancesAndInfo (); let balance = response['balances-and-info']; let result = { 'info': balance }; let currencies = Object.keys (this.currencies); for (let i = 0; i < currencies.length; i++) { let currency = currencies[i]; let account = this.account (); account['free'] = this.safeFloat (balance['available'], currency, 0.0); account['used'] = this.safeFloat (balance['on_hold'], currency, 0.0); account['total'] = this.sum (account['free'], account['used']); result[currency] = account; } return this.parseBalance (result); } async fetchOrderBook (symbol, limit = undefined, params = {}) { let response = await this.publicGetOrderBook (this.extend ({ 'currency': this.marketId (symbol), }, params)); return this.parseOrderBook (response['order-book'], undefined, 'bid', 'ask', 'price', 'order_amount'); } async fetchTicker (symbol, params = {}) { let response = await this.publicGetStats (this.extend ({ 'currency': this.marketId (symbol), }, params)); let ticker = response['stats']; let last = this.safeFloat (ticker, 'last_price'); return { 'symbol': symbol, 'timestamp': undefined, 'datetime': undefined, 'high': this.safeFloat (ticker, 'max'), 'low': this.safeFloat (ticker, 'min'), 'bid': this.safeFloat (ticker, 'bid'), 'bidVolume': undefined, 'ask': this.safeFloat (ticker, 'ask'), 'askVolume': undefined, 'vwap': undefined, 'open': this.safeFloat (ticker, 'open'), 'close': last, 'last': last, 'previousClose': undefined, 'change': this.safeFloat (ticker, 'daily_change'), 'percentage': undefined, 'average': undefined, 'baseVolume': undefined, 'quoteVolume': this.safeFloat (ticker, 'total_btc_traded'), 'info': ticker, }; } parseOHLCV (ohlcv, market = undefined, timeframe = '1d', since = undefined, limit = undefined) { return [ this.parse8601 (ohlcv['date'] + ' 00:00:00'), undefined, undefined, undefined, parseFloat (ohlcv['price']), undefined, ]; } async fetchOHLCV (symbol, timeframe = '1d', since = undefined, limit = undefined, params = {}) { let market = this.market (symbol); let response = await this.publicGetHistoricalPrices (this.extend ({ 'currency': market['id'], 'timeframe': this.timeframes[timeframe], }, params)); let ohlcvs = this.omit (response['historical-prices'], 'request_currency'); return this.parseOHLCVs (ohlcvs, market, timeframe, since, limit); } parseTrade (trade, market) { let timestamp = parseInt (trade['timestamp']) * 1000; return { 'id': trade['id'], 'info': trade, 'timestamp': timestamp, 'datetime': this.iso8601 (timestamp), 'symbol': market['symbol'], 'order': undefined, 'type': undefined, 'side': trade['maker_type'], 'price': this.safeFloat (trade, 'price'), 'amount': this.safeFloat (trade, 'amount'), }; } async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) { let market = this.market (symbol); let response = await this.publicGetTransactions (this.extend ({ 'currency': market['id'], }, params)); let trades = this.omit (response['transactions'], 'request_currency'); return this.parseTrades (trades, market, since, limit); } async createOrder (symbol, type, side, amount, price = undefined, params = {}) { let order = { 'side': side, 'type': type, 'currency': this.marketId (symbol), 'amount': amount, }; if (type === 'limit') order['limit_price'] = price; let result = await this.privatePostOrdersNew (this.extend (order, params)); return { 'info': result, 'id': result, }; } async cancelOrder (id, symbol = undefined, params = {}) { return await this.privatePostOrdersCancel ({ 'id': id }); } async withdraw (currency, amount, address, tag = undefined, params = {}) { this.checkAddress (address); await this.loadMarkets (); let response = await this.privatePostWithdrawalsNew (this.extend ({ 'currency': currency, 'amount': parseFloat (amount), 'address': address, }, params)); return { 'info': response, 'id': response['result']['uuid'], }; } sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) { if (this.id === 'cryptocapital') throw new ExchangeError (this.id + ' is an abstract base API for _1btcxe'); let url = this.urls['api'] + '/' + path; if (api === 'public') { if (Object.keys (params).length) url += '?' + this.urlencode (params); } else { this.checkRequiredCredentials (); let query = this.extend ({ 'api_key': this.apiKey, 'nonce': this.nonce (), }, params); let request = this.json (query); query['signature'] = this.hmac (this.encode (request), this.encode (this.secret)); body = this.json (query); headers = { 'Content-Type': 'application/json' }; } return { 'url': url, 'method': method, 'body': body, 'headers': headers }; } async request (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) { let response = await this.fetch2 (path, api, method, params, headers, body); if ('errors' in response) { let errors = []; for (let e = 0; e < response['errors'].length; e++) { let error = response['errors'][e]; errors.push (error['code'] + ': ' + error['message']); } errors = errors.join (' '); throw new ExchangeError (this.id + ' ' + errors); } return response; } };