refactor: remove nested loop
This commit is contained in:
parent
dd3b4116a6
commit
0e39aa78c9
2 changed files with 9 additions and 12 deletions
|
|
@ -128,12 +128,12 @@ function cryptoNetwork (account, cryptoCode) {
|
||||||
|
|
||||||
function fetchRBF (txId) {
|
function fetchRBF (txId) {
|
||||||
return fetch('getmempoolentry', [txId])
|
return fetch('getmempoolentry', [txId])
|
||||||
.then((res) => _.assign(_.pick(['bip125-replaceable'], res)), { tx_hash: txId })
|
.then((res) => {
|
||||||
|
return [txId, res['bip125-replaceable']]
|
||||||
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
if (err.code === -5) console.log(`${err.message}`)
|
if (err.code === -5) console.log(`${err.message}`)
|
||||||
const result = { tx_hash: txId }
|
return [txId, true]
|
||||||
result['bip125-replaceable'] = true
|
|
||||||
return result
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,9 @@ const _ = require('lodash/fp')
|
||||||
const { fetchRBF } = require('../../wallet/bitcoind/bitcoind')
|
const { fetchRBF } = require('../../wallet/bitcoind/bitcoind')
|
||||||
module.exports = { authorize }
|
module.exports = { authorize }
|
||||||
|
|
||||||
function highConfidence (confidence, txref) {
|
function highConfidence (confidence, txref, txRBF) {
|
||||||
if (txref.double_spend) return 0
|
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
|
if (txref.confirmations > 0 || txref.confidence * 100 >= confidence) return txref.value
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
@ -30,22 +30,19 @@ function authorize (account, toAddress, cryptoAtoms, cryptoCode, isBitcoindAvail
|
||||||
return axios.get(url)
|
return axios.get(url)
|
||||||
.then(r => {
|
.then(r => {
|
||||||
const data = r.data
|
const data = r.data
|
||||||
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref), txrefs)
|
|
||||||
if (isBitcoindAvailable && isRBFEnabled && data.unconfirmed_txrefs) {
|
if (isBitcoindAvailable && isRBFEnabled && data.unconfirmed_txrefs) {
|
||||||
_.map(unconfirmedTxref => {
|
_.map(unconfirmedTxref => {
|
||||||
promise.push(new Promise((resolve, reject) => { resolve(fetchRBF(unconfirmedTxref.tx_hash)) }))
|
promise.push(new Promise((resolve, reject) => { resolve(fetchRBF(unconfirmedTxref.tx_hash)) }))
|
||||||
}, data.unconfirmed_txrefs)
|
}, data.unconfirmed_txrefs)
|
||||||
return Promise.all(promise)
|
return Promise.all(promise)
|
||||||
.then(values => {
|
.then(values => {
|
||||||
_.map(rbfInfo => {
|
const unconfirmedTxsRBF = _.fromPairs(values)
|
||||||
_.map(unconfirmedTxref => {
|
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref, unconfirmedTxsRBF[txref.tx_hash]), txrefs)
|
||||||
if (rbfInfo.tx_hash === unconfirmedTxref.tx_hash) unconfirmedTxref.rbf = rbfInfo['bip125-replaceable']
|
|
||||||
}, data.unconfirmed_txrefs)
|
|
||||||
}, values)
|
|
||||||
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 {
|
} else {
|
||||||
|
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref), 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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue