diff --git a/lib/exchange.js b/lib/exchange.js index 94083882..f9811bb8 100644 --- a/lib/exchange.js +++ b/lib/exchange.js @@ -66,6 +66,7 @@ function getMarkets () { } module.exports = { + fetchExchange, buy, sell, active, diff --git a/lib/plugins.js b/lib/plugins.js index d5bfcb4f..157c67ed 100644 --- a/lib/plugins.js +++ b/lib/plugins.js @@ -475,25 +475,28 @@ function plugins (settings, deviceId) { function buyAndSell (rec, doBuy, tx) { const cryptoCode = rec.cryptoCode - const fiatCode = rec.fiatCode - const cryptoAtoms = doBuy ? commissionMath.fiatToCrypto(tx, rec, deviceId, settings.config) : rec.cryptoAtoms.negated() + return exchange.fetchExchange(settings, cryptoCode) + .then(_exchange => { + const fiatCode = _exchange.account.currencyMarket + const cryptoAtoms = doBuy ? commissionMath.fiatToCrypto(tx, rec, deviceId, settings.config) : rec.cryptoAtoms.negated() - const market = [fiatCode, cryptoCode].join('') + const market = [fiatCode, cryptoCode].join('') - if (!exchange.active(settings, cryptoCode)) return + if (!exchange.active(settings, cryptoCode)) return - const direction = doBuy ? 'cashIn' : 'cashOut' - const internalTxId = tx ? tx.id : rec.id - logger.debug('[%s] Pushing trade: %d', market, cryptoAtoms) - if (!tradesQueues[market]) tradesQueues[market] = [] - tradesQueues[market].push({ - direction, - internalTxId, - fiatCode, - cryptoAtoms, - cryptoCode, - timestamp: Date.now() - }) + const direction = doBuy ? 'cashIn' : 'cashOut' + const internalTxId = tx ? tx.id : rec.id + logger.debug('[%s] Pushing trade: %d', market, cryptoAtoms) + if (!tradesQueues[market]) tradesQueues[market] = [] + tradesQueues[market].push({ + direction, + internalTxId, + fiatCode, + cryptoAtoms, + cryptoCode, + timestamp: Date.now() + }) + }) } function consolidateTrades (cryptoCode, fiatCode) { @@ -550,19 +553,22 @@ function plugins (settings, deviceId) { const deviceIds = devices.map(device => device.deviceId) const lists = deviceIds.map(deviceId => { const localeConfig = configManager.getLocale(deviceId, settings.config) - const fiatCode = localeConfig.fiatCurrency const cryptoCodes = localeConfig.cryptoCurrencies - return cryptoCodes.map(cryptoCode => ({ - fiatCode, - cryptoCode + return Promise.all(cryptoCodes.map(cryptoCode => { + return exchange.fetchExchange(settings, cryptoCode) + .then(exchange => ({ + fiatCode: exchange.account.currencyMarket, + cryptoCode + })) })) }) - - const tradesPromises = _.uniq(_.flatten(lists)) - .map(r => executeTradesForMarket(settings, r.fiatCode, r.cryptoCode)) - - return Promise.all(tradesPromises) + + return Promise.all(lists) + }) + .then(lists => { + return Promise.all(_.uniq(_.flatten(lists)) + .map(r => executeTradesForMarket(settings, r.fiatCode, r.cryptoCode))) }) .catch(logger.error) }