diff --git a/lib/plugins/zero-conf/blockcypher/blockcypher.js b/lib/plugins/zero-conf/blockcypher/blockcypher.js index 21e71f52..adf1ce58 100644 --- a/lib/plugins/zero-conf/blockcypher/blockcypher.js +++ b/lib/plugins/zero-conf/blockcypher/blockcypher.js @@ -4,6 +4,10 @@ const _ = require('lodash/fp') module.exports = {authorize} +function isHighConfidence (confidence, txref) { + return txref.confirmations > 0 || txref.confidence * 100 >= confidence ? txref.value : 0 +} + function authorize (account, toAddress, cryptoAtoms, cryptoCode) { return Promise.resolve() .then(() => { @@ -11,18 +15,23 @@ function authorize (account, toAddress, cryptoAtoms, cryptoCode) { const query = qs.stringify({ token: account.token, - includeConfidence: true, - confidence: account.confidence + includeConfidence: true }) + const confidence = account.confidence + const url = `https://api.blockcypher.com/v1/btc/main/addrs/${toAddress}?${query}` + console.log(url) return axios.get(url) .then(r => { const data = r.data - const authorizedValue = _.sumBy('value', data.txrefs) + const sumTxRefs = txrefs => _.sumBy(txref => isHighConfidence(confidence, txref), txrefs) + + const authorizedValue = sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs) return cryptoAtoms.lte(authorizedValue) }) }) } + diff --git a/lib/plugins/zero-conf/mock-zero-conf/mock-zero-conf.js b/lib/plugins/zero-conf/mock-zero-conf/mock-zero-conf.js index c930775b..76280f29 100644 --- a/lib/plugins/zero-conf/mock-zero-conf/mock-zero-conf.js +++ b/lib/plugins/zero-conf/mock-zero-conf/mock-zero-conf.js @@ -5,8 +5,7 @@ function authorize (account, toAddress, cryptoAtoms, cryptoCode) { .then(() => { if (cryptoCode !== 'BTC') throw new Error('Unsupported crypto: ' + cryptoCode) - const authorizedValue = 1e5 * 2 - console.log('DEBUG300: %j', cryptoAtoms.lte(authorizedValue)) - return cryptoAtoms.lte(authorizedValue) + const isAuthorized = true + return isAuthorized }) }