refactor: remove nested loop

This commit is contained in:
José Oliveira 2021-04-06 10:40:35 +01:00 committed by Josh Harvey
parent dd3b4116a6
commit 0e39aa78c9
2 changed files with 9 additions and 12 deletions

View file

@ -5,9 +5,9 @@ const _ = require('lodash/fp')
const { fetchRBF } = require('../../wallet/bitcoind/bitcoind')
module.exports = { authorize }
function highConfidence (confidence, txref) {
function highConfidence (confidence, txref, txRBF) {
if (txref.double_spend) return 0
if (txref.rbf) return 0
if (txRBF) return 0
if (txref.confirmations > 0 || txref.confidence * 100 >= confidence) return txref.value
return 0
}
@ -30,22 +30,19 @@ function authorize (account, toAddress, cryptoAtoms, cryptoCode, isBitcoindAvail
return axios.get(url)
.then(r => {
const data = r.data
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref), txrefs)
if (isBitcoindAvailable && isRBFEnabled && data.unconfirmed_txrefs) {
_.map(unconfirmedTxref => {
promise.push(new Promise((resolve, reject) => { resolve(fetchRBF(unconfirmedTxref.tx_hash)) }))
}, data.unconfirmed_txrefs)
return Promise.all(promise)
.then(values => {
_.map(rbfInfo => {
_.map(unconfirmedTxref => {
if (rbfInfo.tx_hash === unconfirmedTxref.tx_hash) unconfirmedTxref.rbf = rbfInfo['bip125-replaceable']
}, data.unconfirmed_txrefs)
}, values)
const unconfirmedTxsRBF = _.fromPairs(values)
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref, unconfirmedTxsRBF[txref.tx_hash]), txrefs)
const authorizedValue = sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return cryptoAtoms.lte(authorizedValue)
})
} else {
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref), txrefs)
const authorizedValue = sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return cryptoAtoms.lte(authorizedValue)
}