refactor: join static and dynamic configs to reuse pollQueries()

This commit is contained in:
André Sá 2022-04-22 16:06:33 +01:00
parent df2213566e
commit d20f4968f6
3 changed files with 104 additions and 93 deletions

View file

@ -27,8 +27,6 @@ const loyalty = require('./loyalty')
const transactionBatching = require('./tx-batching')
const state = require('./middlewares/state')
const VERSION = require('../package.json').version
const { CASSETTE_MAX_CAPACITY, CASH_OUT_DISPENSE_READY, CONFIRMATION_CODE } = require('./constants')
const notifier = require('./notifier')
@ -284,78 +282,6 @@ function plugins (settings, deviceId) {
})
}
function staticConfigQueries () {
const massageCoins = _.map(_.pick([
'batchable',
'cashInCommission',
'cashInFee',
'cashOutCommission',
'cryptoCode',
'cryptoNetwork',
'cryptoUnits',
'display',
'minimumTx'
]))
return pollQueries()
.then(_.flow(
_.pick([
'areThereAvailablePromoCodes',
'coins',
'configVersion',
'timezone'
]),
_.update('coins', massageCoins),
_.set('serverVersion', VERSION)
))
}
function dynamicConfigQueries () {
const massageCassettes = cassettes =>
cassettes ?
_.flow(
cassettes => _.set('physical', _.get('cassettes', cassettes), cassettes),
cassettes => _.set('virtual', _.get('virtualCassettes', cassettes), cassettes),
_.unset('cassettes'),
_.unset('virtualCassettes')
)(cassettes) :
null
return pollQueries()
.then(_.flow(
_.pick(['balances', 'cassettes', 'coins', 'rates']),
_.update('cassettes', massageCassettes),
/* [{ cryptoCode, rates }, ...] => [[cryptoCode, rates], ...] */
_.update('coins', _.map(({ cryptoCode, rates }) => [cryptoCode, rates])),
/* [{ cryptoCode: balance }, ...] => [[cryptoCode, { balance }], ...] */
_.update('balances', _.flow(
_.toPairs,
_.map(([cryptoCode, balance]) => [cryptoCode, { balance }])
)),
/* Group the separate objects by cryptoCode */
/* { balances, coins, rates } => { cryptoCode: { balance, ask, bid, cashIn, cashOut }, ... } */
({ balances, cassettes, coins, rates }) => ({
cassettes,
coins: _.flow(
_.reduce(
(ret, [cryptoCode, obj]) => _.update(cryptoCode, _.assign(obj), ret),
rates
),
/* { cryptoCode: { balance, ask, bid, cashIn, cashOut }, ... } => [[cryptoCode, { balance, ask, bid, cashIn, cashOut }], ...] */
_.toPairs,
/* [[cryptoCode, { balance, ask, bid, cashIn, cashOut }], ...] => [{ cryptoCode, balance, ask, bid, cashIn, cashOut }, ...] */
_.map(([cryptoCode, obj]) => _.set('cryptoCode', cryptoCode, obj))
)(_.concat(balances, coins))
})
))
}
function sendCoins (tx) {
return wallet.supportsBatching(settings, tx.cryptoCode)
.then(supportsBatching => {
@ -929,8 +855,6 @@ function plugins (settings, deviceId) {
getRawRates,
buildRatesNoCommission,
pollQueries,
staticConfigQueries,
dynamicConfigQueries,
sendCoins,
newAddress,
isHd,