Skip to content

Commit

Permalink
fetchCurrencies rework ccxt#553
Browse files Browse the repository at this point in the history
  • Loading branch information
kroitor committed Nov 24, 2017
1 parent 8df74ba commit 31ca2cf
Show file tree
Hide file tree
Showing 31 changed files with 129 additions and 75 deletions.
5 changes: 3 additions & 2 deletions js/_1broker.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ module.exports = class _1broker extends Exchange {
let result = {
'info': response,
};
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let c = 0; c < currencies.length; c++) {
let currency = currencies[c];
result[currency] = this.account ();
}
let total = parseFloat (response['balance']);
Expand Down
5 changes: 3 additions & 2 deletions js/_1btcxe.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ module.exports = class _1btcxe extends Exchange {
let response = await this.privatePostBalancesAndInfo ();
let balance = response['balances-and-info'];
let result = { 'info': balance };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
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);
Expand Down
30 changes: 19 additions & 11 deletions js/base/Exchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const { deepExtend
, extend
, sleep
, timeout
, indexBy
, sortBy
, aggregate } = functions

Expand Down Expand Up @@ -445,27 +446,34 @@ module.exports = class Exchange {
'limits': this.limits,
'precision': this.precision,
}, this.fees['trading'], market))
this.markets = deepExtend (this.markets, this.indexBy (values, 'symbol'))
this.marketsById = this.indexBy (markets, 'id')
this.markets = deepExtend (this.markets, indexBy (values, 'symbol'))
this.marketsById = indexBy (markets, 'id')
this.markets_by_id = this.marketsById
this.symbols = Object.keys (this.markets).sort ()
this.ids = Object.keys (this.markets_by_id).sort ()
let base = this.pluck (values.filter (market => 'base' in market), 'base')
let quote = this.pluck (values.filter (market => 'quote' in market), 'quote')
this.currencies = this.unique (base.concat (quote))
const baseCurrencies = values.filter (market => 'base' in market)
.map (market => ({ id: market.baseId || market.base, code: market.base }))
const quoteCurrencies = values.filter (market => 'quote' in market)
.map (market => ({ id: market.quoteId || market.quote, code: market.quote }))
const currencies = baseCurrencies.concat (quoteCurrencies).map (currency =>
extend (currency, this.currencies[currency.code] || {}))
this.currencies = extend (indexBy (currencies, 'code'), this.currencies)
return this.markets
}

loadMarkets (reload = false) {
async loadMarkets (reload = false) {
if (!reload && this.markets) {
if (!this.marketsById) {
return new Promise ((resolve, reject) => resolve (this.setMarkets (this.markets)))
return this.setMarkets (this.markets)
}
return new Promise ((resolve, reject) => resolve (this.markets))
return this.markets
}
return this.fetchMarkets ().then (markets => {
return this.setMarkets (markets)
})
const markets = await this.fetchMarkets ()
let currencies = undefined
if (this.hasFetchCurrencies) {
currencies = await this.fetchCurrencies ()
}
return this.setMarkets (markets, currencies)
}

fetchTickers (symbols = undefined, params = {}) {
Expand Down
5 changes: 3 additions & 2 deletions js/bit2c.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ module.exports = class bit2c extends Exchange {
async fetchBalance (params = {}) {
let balance = await this.privatePostAccountBalanceV2 ();
let result = { 'info': balance };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
if (currency in balance) {
let available = 'AVAILABLE_' + currency;
Expand Down
5 changes: 3 additions & 2 deletions js/bitbay.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ module.exports = class bitbay extends Exchange {
if ('balances' in response) {
let balance = response['balances'];
let result = { 'info': balance };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
if (currency in balance) {
account['free'] = parseFloat (balance[currency]['available']);
Expand Down
5 changes: 3 additions & 2 deletions js/bitcoincoid.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,9 @@ module.exports = class bitcoincoid extends Exchange {
let response = await this.privatePostGetInfo ();
let balance = response['return'];
let result = { 'info': balance };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let account = this.account ();
account['free'] = this.safeFloat (balance['balance'], lowercase, 0.0);
Expand Down
5 changes: 3 additions & 2 deletions js/bitflyer.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,9 @@ module.exports = class bitflyer extends Exchange {
balances[currency] = account;
}
let result = { 'info': response };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
if (currency in balances) {
account['total'] = balances[currency]['amount'];
Expand Down
5 changes: 3 additions & 2 deletions js/bithumb.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,9 @@ module.exports = class bithumb extends Exchange {
}, params));
let result = { 'info': response };
let balances = response['data'];
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
let lowercase = currency.toLowerCase ();
account['total'] = this.safeFloat (balances, 'total_' + lowercase);
Expand Down
5 changes: 3 additions & 2 deletions js/bitlish.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,9 @@ module.exports = class bitlish extends Exchange {
currency = 'DASH';
balance[currency] = account;
}
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
if (currency in balance) {
account['free'] = parseFloat (balance[currency]['funds']);
Expand Down
5 changes: 3 additions & 2 deletions js/bitmarket.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,9 @@ module.exports = class bitmarket extends Exchange {
let data = response['data'];
let balance = data['balances'];
let result = { 'info': data };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
if (currency in balance['available'])
account['free'] = balance['available'][currency];
Expand Down
5 changes: 3 additions & 2 deletions js/bitstamp.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,8 +179,9 @@ module.exports = class bitstamp extends Exchange {
await this.loadMarkets ();
let balance = await this.privatePostBalance ();
let result = { 'info': balance };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let total = lowercase + '_balance';
let free = lowercase + '_available';
Expand Down
5 changes: 3 additions & 2 deletions js/bitstamp1.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,9 @@ module.exports = class bitstamp1 extends Exchange {
async fetchBalance (params = {}) {
let balance = await this.privatePostBalance ();
let result = { 'info': balance };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let total = lowercase + '_balance';
let free = lowercase + '_available';
Expand Down
5 changes: 3 additions & 2 deletions js/bl3p.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ module.exports = class bl3p extends Exchange {
let data = response['data'];
let balance = data['wallets'];
let result = { 'info': data };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
if (currency in balance) {
if ('available' in balance[currency]) {
Expand Down
5 changes: 3 additions & 2 deletions js/btcbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ module.exports = class btcbox extends Exchange {
await this.loadMarkets ();
let balances = await this.privatePostBalance ();
let result = { 'info': balances };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
if (lowercase == 'dash')
lowercase = 'drk';
Expand Down
5 changes: 3 additions & 2 deletions js/btcchina.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,9 @@ module.exports = class btcchina extends Exchange {
let response = await this.privatePostGetAccountInfo ();
let balances = response['result'];
let result = { 'info': balances };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let account = this.account ();
if (lowercase in balances['balance'])
Expand Down
5 changes: 3 additions & 2 deletions js/bter.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ module.exports = class bter extends Exchange {
await this.loadMarkets ();
let balance = await this.privatePostBalances ();
let result = { 'info': balance };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let code = this.commonCurrencyCode (currency);
let account = this.account ();
if ('available' in balance) {
Expand Down
9 changes: 5 additions & 4 deletions js/cex.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ module.exports = class cex extends Exchange {
await this.loadMarkets ();
let balances = await this.privatePostBalance ();
let result = { 'info': balances };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
if (currency in balances) {
let account = {
'free': parseFloat (balances[currency]['available']),
Expand Down Expand Up @@ -214,9 +215,9 @@ module.exports = class cex extends Exchange {

async fetchTickers (symbols = undefined, params = {}) {
await this.loadMarkets ();
let currencies = this.currencies.join ('/');
let currencies = Object.keys (this.currencies);
let response = await this.publicGetTickersCurrencies (this.extend ({
'currencies': currencies,
'currencies': currencies.join ('/'),
}, params));
let tickers = response['data'];
let result = {};
Expand Down
5 changes: 3 additions & 2 deletions js/coincheck.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ module.exports = class coincheck extends Exchange {
async fetchBalance (params = {}) {
let balances = await this.privateGetAccountsBalance ();
let result = { 'info': balances };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let account = this.account ();
if (lowercase in balances)
Expand Down
11 changes: 6 additions & 5 deletions js/coingi.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ module.exports = class coingi extends Exchange {

async fetchBalance (params = {}) {
await this.loadMarkets ();
let currencies = [];
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c].toLowerCase ();
currencies.push (currency);
let lowercaseCurrencies = [];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
lowercaseCurrencies.push (currency.toLowerCase ());
}
let balances = await this.userPostBalance ({
'currencies': currencies.join (',')
'currencies': lowercaseCurrencies.join (',')
});
let result = { 'info': balances };
for (let b = 0; b < balances.length; b++) {
Expand Down
7 changes: 4 additions & 3 deletions js/coinmarketcap.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ module.exports = class coinmarketcap extends Exchange {
let result = [];
for (let p = 0; p < markets.length; p++) {
let market = markets[p];
for (let c = 0; c < this.currencies.length; c++) {
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let quote = currencies[i];
let quoteId = quote.toLowerCase ();
let base = market['symbol'];
let baseId = market['id'];
let quote = this.currencies[c];
let quoteId = quote.toLowerCase ();
let symbol = base + '/' + quote;
let id = baseId + '/' + quote;
result.push ({
Expand Down
5 changes: 3 additions & 2 deletions js/coinmate.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ module.exports = class coinmate extends Exchange {
let response = await this.privatePostBalances ();
let balances = response['data'];
let result = { 'info': balances };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
if (currency in balances) {
account['free'] = balances[currency]['available'];
Expand Down
5 changes: 3 additions & 2 deletions js/exmo.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ module.exports = class exmo extends Exchange {
await this.loadMarkets ();
let response = await this.privatePostUserInfo ();
let result = { 'info': response };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let account = this.account ();
if (currency in response['balances'])
account['free'] = parseFloat (response['balances'][currency]);
Expand Down
5 changes: 3 additions & 2 deletions js/huobi.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,9 @@ module.exports = class huobi extends Exchange {
async fetchBalance (params = {}) {
let balances = await this.tradePostGetAccountInfo ();
let result = { 'info': balances };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let account = this.account ();
let available = 'available_' + lowercase + '_display';
Expand Down
5 changes: 3 additions & 2 deletions js/mercado.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,9 @@ module.exports = class mercado extends Exchange {
let response = await this.privatePostGetAccountInfo ();
let balances = response['response_data']['balance'];
let result = { 'info': response };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let account = this.account ();
if (lowercase in balances) {
Expand Down
5 changes: 3 additions & 2 deletions js/mixcoins.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ module.exports = class mixcoins extends Exchange {
let response = await this.privatePostInfo ();
let balance = response['result']['wallet'];
let result = { 'info': balance };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let account = this.account ();
if (lowercase in balance) {
Expand Down
5 changes: 3 additions & 2 deletions js/okcoinusd.js
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,9 @@ module.exports = class okcoinusd extends Exchange {
let response = await this.privatePostUserinfo ();
let balances = response['info']['funds'];
let result = { 'info': response };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let account = this.account ();
account['free'] = this.safeFloat (balances['free'], lowercase, 0.0);
Expand Down
5 changes: 3 additions & 2 deletions js/paymium.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,9 @@ module.exports = class paymium extends Exchange {
async fetchBalance (params = {}) {
let balances = await this.privateGetUser ();
let result = { 'info': balances };
for (let c = 0; c < this.currencies.length; c++) {
let currency = this.currencies[c];
let currencies = Object.keys (this.currencies);
for (let i = 0; i < currencies.length; i++) {
let currency = currencies[i];
let lowercase = currency.toLowerCase ();
let account = this.account ();
let balance = 'balance_' + lowercase;
Expand Down
Loading

0 comments on commit 31ca2cf

Please sign in to comment.