refactor: fetch rbf status of unconfirmed txs

This commit is contained in:
José Oliveira 2021-05-11 19:26:40 +01:00 committed by Josh Harvey
parent 0e39aa78c9
commit e6b1446616

View file

@ -13,7 +13,6 @@ function highConfidence (confidence, txref, txRBF) {
} }
function authorize (account, toAddress, cryptoAtoms, cryptoCode, isBitcoindAvailable) { function authorize (account, toAddress, cryptoAtoms, cryptoCode, isBitcoindAvailable) {
let promise = []
return Promise.resolve() return Promise.resolve()
.then(() => { .then(() => {
if (cryptoCode !== 'BTC') throw new Error('Unsupported crypto: ' + cryptoCode) if (cryptoCode !== 'BTC') throw new Error('Unsupported crypto: ' + cryptoCode)
@ -31,21 +30,19 @@ function authorize (account, toAddress, cryptoAtoms, cryptoCode, isBitcoindAvail
.then(r => { .then(r => {
const data = r.data const data = r.data
if (isBitcoindAvailable && isRBFEnabled && data.unconfirmed_txrefs) { if (isBitcoindAvailable && isRBFEnabled && data.unconfirmed_txrefs) {
_.map(unconfirmedTxref => { const promises = _.map(unconfirmedTxref => fetchRBF(unconfirmedTxref.tx_hash), data.unconfirmed_txrefs)
promise.push(new Promise((resolve, reject) => { resolve(fetchRBF(unconfirmedTxref.tx_hash)) })) return Promise.all(promises)
}, data.unconfirmed_txrefs)
return Promise.all(promise)
.then(values => { .then(values => {
const unconfirmedTxsRBF = _.fromPairs(values) const unconfirmedTxsRBF = _.fromPairs(values)
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref, unconfirmedTxsRBF[txref.tx_hash]), txrefs) const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref, unconfirmedTxsRBF[txref.tx_hash]), txrefs)
const authorizedValue = sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs) const authorizedValue = sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return cryptoAtoms.lte(authorizedValue) 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)
} }
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref), txrefs)
const authorizedValue = sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return cryptoAtoms.lte(authorizedValue)
}) })
}) })
} }