ccxt-bybit
Version:
A JavaScript / Python / PHP cryptocurrency trading library with support for 130+ exchanges
285 lines (267 loc) • 10 kB
JavaScript
'use strict';
// ---------------------------------------------------------------------------
const Exchange = require ('./base/Exchange');
const { ExchangeError } = require ('./base/errors');
// ---------------------------------------------------------------------------
module.exports = class lakebtc extends Exchange {
describe () {
return this.deepExtend (super.describe (), {
'id': 'lakebtc',
'name': 'LakeBTC',
'countries': [ 'US' ],
'version': 'api_v2',
'has': {
'CORS': true,
'createMarketOrder': false,
'fetchTickers': true,
},
'urls': {
'logo': 'https://user-images.githubusercontent.com/1294454/28074120-72b7c38a-6660-11e7-92d9-d9027502281d.jpg',
'api': 'https://api.lakebtc.com',
'www': 'https://www.lakebtc.com',
'doc': [
'https://www.lakebtc.com/s/api_v2',
'https://www.lakebtc.com/s/api',
],
},
'api': {
'public': {
'get': [
'bcorderbook',
'bctrades',
'ticker',
],
},
'private': {
'post': [
'buyOrder',
'cancelOrders',
'getAccountInfo',
'getExternalAccounts',
'getOrders',
'getTrades',
'openOrders',
'sellOrder',
],
},
},
'fees': {
'trading': {
'maker': 0.15 / 100,
'taker': 0.2 / 100,
},
},
});
}
async fetchMarkets (params = {}) {
const response = await this.publicGetTicker (params);
const result = [];
const keys = Object.keys (response);
for (let i = 0; i < keys.length; i++) {
const id = keys[i];
const market = response[id];
const baseId = id.slice (0, 3);
const quoteId = id.slice (3, 6);
const base = baseId.toUpperCase ();
const quote = quoteId.toUpperCase ();
const symbol = base + '/' + quote;
result.push ({
'id': id,
'symbol': symbol,
'base': base,
'quote': quote,
'baseId': baseId,
'quoteId': quoteId,
'info': market,
});
}
return result;
}
async fetchBalance (params = {}) {
await this.loadMarkets ();
const response = await this.privatePostGetAccountInfo (params);
const balances = this.safeValue (response, 'balance', {});
const result = { 'info': response };
const currencyIds = Object.keys (balances);
for (let i = 0; i < currencyIds.length; i++) {
const currencyId = currencyIds[i];
const code = this.safeCurrencyCode (currencyId);
const account = this.account ();
account['total'] = this.safeFloat (balances, currencyId);
result[code] = account;
}
return this.parseBalance (result);
}
async fetchOrderBook (symbol, limit = undefined, params = {}) {
await this.loadMarkets ();
const request = {
'symbol': this.marketId (symbol),
};
const response = await this.publicGetBcorderbook (this.extend (request, params));
return this.parseOrderBook (response);
}
parseTicker (ticker, market = undefined) {
const timestamp = this.milliseconds ();
let symbol = undefined;
if (market !== undefined) {
symbol = market['symbol'];
}
const 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, 'bid'),
'bidVolume': undefined,
'ask': this.safeFloat (ticker, 'ask'),
'askVolume': undefined,
'vwap': undefined,
'open': undefined,
'close': last,
'last': last,
'previousClose': undefined,
'change': undefined,
'percentage': undefined,
'average': undefined,
'baseVolume': this.safeFloat (ticker, 'volume'),
'quoteVolume': undefined,
'info': ticker,
};
}
async fetchTickers (symbols = undefined, params = {}) {
await this.loadMarkets ();
const response = await this.publicGetTicker (params);
const ids = Object.keys (response);
const result = {};
for (let i = 0; i < ids.length; i++) {
let symbol = ids[i];
const ticker = response[symbol];
let market = undefined;
if (symbol in this.markets_by_id) {
market = this.markets_by_id[symbol];
symbol = market['symbol'];
}
result[symbol] = this.parseTicker (ticker, market);
}
return result;
}
async fetchTicker (symbol, params = {}) {
await this.loadMarkets ();
const market = this.market (symbol);
const tickers = await this.publicGetTicker (params);
return this.parseTicker (tickers[market['id']], market);
}
parseTrade (trade, market = undefined) {
const timestamp = this.safeTimestamp (trade, 'date');
const id = this.safeString (trade, 'tid');
const price = this.safeFloat (trade, 'price');
const amount = this.safeFloat (trade, 'amount');
let cost = undefined;
if (price !== undefined) {
if (amount !== undefined) {
cost = price * amount;
}
}
let symbol = undefined;
if (market !== undefined) {
symbol = market['symbol'];
}
return {
'id': id,
'info': trade,
'timestamp': timestamp,
'datetime': this.iso8601 (timestamp),
'symbol': symbol,
'order': undefined,
'type': undefined,
'side': undefined,
'takerOrMaker': undefined,
'price': price,
'amount': amount,
'cost': cost,
'fee': undefined,
};
}
async fetchTrades (symbol, since = undefined, limit = undefined, params = {}) {
await this.loadMarkets ();
const market = this.market (symbol);
const request = {
'symbol': market['id'],
};
const response = await this.publicGetBctrades (this.extend (request, params));
return this.parseTrades (response, market, since, limit);
}
async createOrder (symbol, type, side, amount, price = undefined, params = {}) {
await this.loadMarkets ();
if (type === 'market') {
throw new ExchangeError (this.id + ' allows limit orders only');
}
const method = 'privatePost' + this.capitalize (side) + 'Order';
const market = this.market (symbol);
const order = {
'params': [ price, amount, market['id'] ],
};
const response = await this[method] (this.extend (order, params));
return {
'info': response,
'id': this.safeString (response, 'id'),
};
}
async cancelOrder (id, symbol = undefined, params = {}) {
await this.loadMarkets ();
const request = {
'params': [ id ],
};
return await this.privatePostCancelOrder (this.extend (request, params));
}
nonce () {
return this.microseconds ();
}
sign (path, api = 'public', method = 'GET', params = {}, headers = undefined, body = undefined) {
let url = this.urls['api'] + '/' + this.version;
if (api === 'public') {
url += '/' + path;
if (Object.keys (params).length) {
url += '?' + this.urlencode (params);
}
} else {
this.checkRequiredCredentials ();
const nonce = this.nonce ();
let queryParams = '';
if ('params' in params) {
const paramsList = params['params'];
queryParams = paramsList.join (',');
}
const query = this.urlencode ({
'tonce': nonce,
'accesskey': this.apiKey,
'requestmethod': method.toLowerCase (),
'id': nonce,
'method': path,
'params': queryParams,
});
body = this.json ({
'method': path,
'params': queryParams,
'id': nonce,
});
const signature = this.hmac (this.encode (query), this.encode (this.secret), 'sha1');
const auth = this.encode (this.apiKey + ':' + signature);
headers = {
'Json-Rpc-Tonce': nonce.toString (),
'Authorization': 'Basic ' + this.decode (this.stringToBase64 (auth)),
'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) {
const response = await this.fetch2 (path, api, method, params, headers, body);
if ('error' in response) {
throw new ExchangeError (this.id + ' ' + this.json (response));
}
return response;
}
};