@jmparsons/ccxt
Version:
A JavaScript / Python / PHP cryptocurrency trading library with support for 100+ exchanges
220 lines (206 loc) • 9.17 kB
JavaScript
'use strict';
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError } = require ('./base/errors');
// ---------------------------------------------------------------------------
module.exports = class foxbit extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'foxbit',
'name': 'FoxBit',
'countries': 'BR',
'has': {
'CORS': false,
'createMarketOrder': false,
},
'rateLimit': 1000,
'version': 'v1',
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/27991413-11b40d42-647f-11e7-91ee-78ced874dd09.jpg',
'api': {
'public': 'https://api.blinktrade.com/api',
'private': 'https://api.blinktrade.com/tapi',
},
'www': 'https://foxbit.exchange',
'doc': 'https://blinktrade.com/docs',
},
'comment': 'Blinktrade API',
'api': {
'public': {
'get': [
'{currency}/ticker', // ?crypto_currency=BTC
'{currency}/orderbook', // ?crypto_currency=BTC
'{currency}/trades', // ?crypto_currency=BTC&since=<TIMESTAMP>&limit=<NUMBER>
],
},
'private': {
'post': [
'D', // order
'F', // cancel order
'U2', // balance
'U4', // my orders
'U6', // withdraw
'U18', // deposit
'U24', // confirm withdrawal
'U26', // list withdrawals
'U30', // list deposits
'U34', // ledger
'U70', // cancel withdrawal
],
},
},
'markets': {
'BTC/VEF': { 'id': 'BTCVEF', 'symbol': 'BTC/VEF', 'base': 'BTC', 'quote': 'VEF', 'brokerId': 1, 'broker': 'SurBitcoin' },
'BTC/VND': { 'id': 'BTCVND', 'symbol': 'BTC/VND', 'base': 'BTC', 'quote': 'VND', 'brokerId': 3, 'broker': 'VBTC' },
'BTC/BRL': { 'id': 'BTCBRL', 'symbol': 'BTC/BRL', 'base': 'BTC', 'quote': 'BRL', 'brokerId': 4, 'broker': 'FoxBit' },
'BTC/PKR': { 'id': 'BTCPKR', 'symbol': 'BTC/PKR', 'base': 'BTC', 'quote': 'PKR', 'brokerId': 8, 'broker': 'UrduBit' },
'BTC/CLP': { 'id': 'BTCCLP', 'symbol': 'BTC/CLP', 'base': 'BTC', 'quote': 'CLP', 'brokerId': 9, 'broker': 'ChileBit' },
},
'options': {
'brokerId': '4', // https://blinktrade.com/docs/#brokers
},
});
}
async fetchBalance (params = {}) {
let response = await this.privatePostU2 ({
'BalanceReqID': this.nonce (),
});
let balances = this.safeValue (response['Responses'], this.options['brokerId']);
let result = { 'info': response };
if (typeof balances !== 'undefined') {
let currencyIds = Object.keys (this.currencies_by_id);
for (let i = 0; i < currencyIds.length; i++) {
let currencyId = currencyIds[i];
let currency = this.currencies_by_id[currencyId];
let code = currency['code'];
// we only set the balance for the currency if that currency is present in response
// otherwise we will lose the info if the currency balance has been funded or traded or not
if (currencyId in balances) {
let account = this.account ();
account['used'] = parseFloat (balances[currencyId + '_locked']) * 1e-8;
account['total'] = parseFloat (balances[currencyId]) * 1e-8;
account['free'] = account['total'] - account['used'];
result[code] = account;
}
}
}
return this.parseBalance (result);
}
async fetchOrderBook (symbol, limit = undefined, params = {}) {
let market = this.market (symbol);
let orderbook = await this.publicGetCurrencyOrderbook (this.extend ({
'currency': market['quote'],
'crypto_currency': market['base'],
}, params));
return this.parseOrderBook (orderbook);
}
async fetchTicker (symbol, params = {}) {
let market = this.market (symbol);
let ticker = await this.publicGetCurrencyTicker (this.extend ({
'currency': market['quote'],
'crypto_currency': market['base'],
}, params));
let timestamp = this.milliseconds ();
let lowercaseQuote = market['quote'].toLowerCase ();
let quoteVolume = 'vol_' + lowercaseQuote;
let last = this.safeFloat (ticker, 'last');
return {
'symbol': symbol,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'high': this.safeFloat (ticker, 'high'),
'low': this.safeFloat (ticker, 'low'),
'bid': this.safeFloat (ticker, 'buy'),
'bidVolume': undefined,
'ask': this.safeFloat (ticker, 'sell'),
'askVolume': undefined,
'vwap': undefined,
'open': undefined,
'close': last,
'last': last,
'previousClose': undefined,
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': this.safeFloat (ticker, 'vol'),
'quoteVolume': parseFloat (ticker[quoteVolume]),
'info': ticker,
};
}
parseTrade (trade, market) {
let timestamp = trade['date'] * 1000;
return {
'id': this.safeString (trade, 'tid'),
'info': trade,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'symbol': market['symbol'],
'type': undefined,
'side': trade['side'],
'price': trade['price'],
'amount': trade['amount'],
};
}
async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
let market = this.market (symbol);
let response = await this.publicGetCurrencyTrades (this.extend ({
'currency': market['quote'],
'crypto_currency': market['base'],
}, params));
return this.parseTrades (response, market, since, limit);
}
async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
if (type === 'market')
throw new ExchangeError (this.id + ' allows limit orders only');
let market = this.market (symbol);
let orderSide = (side === 'buy') ? '1' : '2';
let order = {
'ClOrdID': this.nonce (),
'Symbol': market['id'],
'Side': orderSide,
'OrdType': '2',
'Price': price,
'OrderQty': amount,
'BrokerID': market['brokerId'],
};
let response = await this.privatePostD (this.extend (order, params));
let indexed = this.indexBy (response['Responses'], 'MsgType');
let execution = indexed['8'];
return {
'info': response,
'id': execution['OrderID'],
};
}
async cancelOrder (id, symbol = undefined, params = {}) {
return await this.privatePostF (this.extend ({
'ClOrdID': id,
}, params));
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let url = this.urls['api'][api] + '/' + this.version + '/' + this.implodeParams (path, params);
let query = this.omit (params, this.extractParams (path));
if (api === 'public') {
if (Object.keys (query).length)
url += '?' + this.urlencode (query);
} else {
this.checkRequiredCredentials ();
let nonce = this.nonce ().toString ();
let request = this.extend ({ 'MsgType': path }, query);
body = this.json (request);
headers = {
'APIKey': this.apiKey,
'Nonce': nonce,
'Signature': this.hmac (this.encode (nonce), this.encode (this.secret)),
'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 ('Status' in response)
if (response['Status'] !== 200)
throw new ExchangeError (this.id + ' ' + this.json (response));
return response;
}
};