feat: rbf checking in blockcypher plugin

This commit is contained in:
José Oliveira 2021-04-04 21:45:09 +01:00 committed by Josh Harvey
parent 434d30b079
commit d338d0adca
3 changed files with 43 additions and 14 deletions

View file

@ -126,11 +126,23 @@ function cryptoNetwork (account, cryptoCode) {
.then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main') .then(() => parseInt(rpcConfig.port, 10) === 18332 ? 'test' : 'main')
} }
function fetchRBF (txId) {
return fetch('getmempoolentry', [txId])
.then((res) => _.assign(_.pick(['bip125-replaceable'], res)), { tx_hash: txId })
.catch(err => {
if (err.code === -5) console.log(`${err.message}`)
const result = { tx_hash: txId }
result['bip125-replaceable'] = true
return result
})
}
module.exports = { module.exports = {
balance, balance,
sendCoins, sendCoins,
newAddress, newAddress,
getStatus, getStatus,
newFunding, newFunding,
cryptoNetwork cryptoNetwork,
fetchRBF
} }

View file

@ -2,36 +2,51 @@ const qs = require('querystring')
const axios = require('axios') const axios = require('axios')
const _ = require('lodash/fp') const _ = require('lodash/fp')
module.exports = {authorize} const { fetchRBF } = require('../../wallet/bitcoind/bitcoind')
module.exports = { authorize }
function highConfidence (confidence, txref) { function highConfidence (confidence, txref) {
if (txref.double_spend) return 0 if (txref.double_spend) return 0
if (txref.rbf) 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
} }
function authorize (account, toAddress, cryptoAtoms, cryptoCode) { function authorize (account, toAddress, cryptoAtoms, cryptoCode, isBitcoindAvailable) {
var 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)
const query = qs.stringify({ const query = qs.stringify({
token: account.token, token: account.token,
includeConfidence: true includeConfidence: true
}) })
const confidence = account.confidenceFactor const confidence = account.confidenceFactor
const verifyRBF = account.rbf
const url = `https://api.blockcypher.com/v1/btc/main/addrs/${toAddress}?${query}` const url = `https://api.blockcypher.com/v1/btc/main/addrs/${toAddress}?${query}`
return axios.get(url) return axios.get(url)
.then(r => { .then(r => {
const data = r.data const data = r.data
if (isBitcoindAvailable && verifyRBF && 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 sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref), txrefs) 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)
}) })
} else {
const sumTxRefs = txrefs => _.sumBy(txref => highConfidence(confidence, txref), txrefs)
const authorizedValue = sumTxRefs(data.txrefs) + sumTxRefs(data.unconfirmed_txrefs)
return cryptoAtoms.lte(authorizedValue)
}
})
}) })
} }

View file

@ -132,6 +132,8 @@ function getWalletStatus (settings, tx) {
function authorizeZeroConf (settings, tx, machineId) { function authorizeZeroConf (settings, tx, machineId) {
const plugin = configManager.getWalletSettings(tx.cryptoCode, settings.config).zeroConf const plugin = configManager.getWalletSettings(tx.cryptoCode, settings.config).zeroConf
const isBitcoindAvailable =
configManager.getWalletSettings(tx.cryptoCode, settings.config).wallet === 'bitcoind'
const cashOutConfig = configManager.getCashOut(machineId, settings.config) const cashOutConfig = configManager.getCashOut(machineId, settings.config)
const zeroConfLimit = cashOutConfig.zeroConfLimit const zeroConfLimit = cashOutConfig.zeroConfLimit
@ -148,7 +150,7 @@ function authorizeZeroConf (settings, tx, machineId) {
const zeroConf = ph.load(ph.ZERO_CONF, plugin) const zeroConf = ph.load(ph.ZERO_CONF, plugin)
const account = settings.accounts[plugin] const account = settings.accounts[plugin]
return zeroConf.authorize(account, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode) return zeroConf.authorize(account, tx.toAddress, tx.cryptoAtoms, tx.cryptoCode, isBitcoindAvailable)
} }
function getStatus (settings, tx, machineId) { function getStatus (settings, tx, machineId) {