fix: trade errors out when no exchange is set

This commit is contained in:
Rafael Taranto 2025-04-18 12:22:33 +01:00
parent 8b1cbce86f
commit 1a109465ea
2 changed files with 24 additions and 28 deletions

View file

@ -35,7 +35,6 @@ function getDiscountRate (discount, commission) {
} }
module.exports = { module.exports = {
truncateCrypto,
fiatToCrypto, fiatToCrypto,
getDiscountRate getDiscountRate
} }

View file

@ -467,6 +467,15 @@ function plugins (settings, deviceId) {
* Trader functions * Trader functions
*/ */
function toMarketString (fiatCode, cryptoCode) {
return [fiatCode, cryptoCode].join('-')
}
function fromMarketString (market) {
const [fiatCode, cryptoCode] = market.split('-')
return { fiatCode, cryptoCode }
}
function buy (rec, tx) { function buy (rec, tx) {
return buyAndSell(rec, true, tx) return buyAndSell(rec, true, tx)
} }
@ -477,14 +486,14 @@ function plugins (settings, deviceId) {
function buyAndSell (rec, doBuy, tx) { function buyAndSell (rec, doBuy, tx) {
const cryptoCode = rec.cryptoCode const cryptoCode = rec.cryptoCode
if (!exchange.active(settings, cryptoCode)) return
return exchange.fetchExchange(settings, cryptoCode) return exchange.fetchExchange(settings, cryptoCode)
.then(_exchange => { .then(_exchange => {
const fiatCode = _exchange.account.currencyMarket const fiatCode = _exchange.account.currencyMarket
const cryptoAtoms = doBuy ? commissionMath.fiatToCrypto(tx, rec, deviceId, settings.config) : rec.cryptoAtoms.negated() const cryptoAtoms = doBuy ? commissionMath.fiatToCrypto(tx, rec, deviceId, settings.config) : rec.cryptoAtoms.negated()
const market = [fiatCode, cryptoCode].join('') const market = toMarketString(fiatCode, cryptoCode)
if (!exchange.active(settings, cryptoCode)) return
const direction = doBuy ? 'cashIn' : 'cashOut' const direction = doBuy ? 'cashIn' : 'cashOut'
const internalTxId = tx ? tx.id : rec.id const internalTxId = tx ? tx.id : rec.id
@ -502,7 +511,7 @@ function plugins (settings, deviceId) {
} }
function consolidateTrades (cryptoCode, fiatCode) { function consolidateTrades (cryptoCode, fiatCode) {
const market = [fiatCode, cryptoCode].join('') const market = toMarketString(fiatCode, cryptoCode)
const marketTradesQueues = tradesQueues[market] const marketTradesQueues = tradesQueues[market]
if (!marketTradesQueues || marketTradesQueues.length === 0) return null if (!marketTradesQueues || marketTradesQueues.length === 0) return null
@ -550,35 +559,23 @@ function plugins (settings, deviceId) {
} }
function executeTrades () { function executeTrades () {
return machineLoader.getMachines() const pairs = _.map(fromMarketString)(_.keys(tradesQueues))
.then(devices => { pairs.forEach(({ fiatCode, cryptoCode }) => {
const deviceIds = devices.map(device => device.deviceId) try {
const lists = deviceIds.map(deviceId => { executeTradesForMarket(settings, fiatCode, cryptoCode)
const localeConfig = configManager.getLocale(deviceId, settings.config) } catch (err) {
const cryptoCodes = localeConfig.cryptoCurrencies logger.error(err)
}
})
return Promise.all(cryptoCodes.map(cryptoCode => { // Poller expects a promise
return exchange.fetchExchange(settings, cryptoCode) return Promise.resolve()
.then(exchange => ({
fiatCode: exchange.account.currencyMarket,
cryptoCode
}))
}))
})
return Promise.all(lists)
})
.then(lists => {
return Promise.all(_.uniq(_.flatten(lists))
.map(r => executeTradesForMarket(settings, r.fiatCode, r.cryptoCode)))
})
.catch(logger.error)
} }
function executeTradesForMarket (settings, fiatCode, cryptoCode) { function executeTradesForMarket (settings, fiatCode, cryptoCode) {
if (!exchange.active(settings, cryptoCode)) return if (!exchange.active(settings, cryptoCode)) return
const market = [fiatCode, cryptoCode].join('') const market = toMarketString(fiatCode, cryptoCode)
const tradeEntry = consolidateTrades(cryptoCode, fiatCode) const tradeEntry = consolidateTrades(cryptoCode, fiatCode)
if (tradeEntry === null || tradeEntry.cryptoAtoms.eq(0)) return if (tradeEntry === null || tradeEntry.cryptoAtoms.eq(0)) return