Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CPFP endpoint #508

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions app/controllers/transactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,44 @@ exports.showRaw = function(req, res) {
}
};

/**
* Get needed fee (BTC) for given fee rate (BTC/kB)
*/
exports.getCpfpFee = function(req, res) {

if (req.transaction) {
if (req.transaction.confirmations > 0) {
return res.jsonp({extraFeeNeeded: 0});
} else {
bitcoreRpc.getRawMemPool(true, function(err, rawMemPool) {
if (err || !rawMemPool) {
console.log(err);
return res.status(500).send('Internal Server Error');
}

if (rawMemPool.hasOwnProperty(req.transaction.txid)) {
var size = rawMemPool[req.transaction.txid].ancestorsize
var fees = rawMemPool[req.transaction.txid].ancestorfees

var targetFeeRate = parseFloat(req.query.feeRate) // BTC/kB

var targetFee = targetFeeRate * (size / 1000) // BTC

var missingFee = targetFee - (fees / 1e8) // BTC

if (missingFee < (1 / 1e8)) { // if missing fee less than one satoshi
return res.jsonp({extraFeeNeeded: 0});
} else {
return res.jsonp({extraFeeNeeded: missingFee});
}
} else { // txid was not in rawMemPool even though confirmations == 0
return res.status(500).send('Internal Server Error'); // should we just return 0?
}
})
}
}
};


var getTransaction = function(txid, cb) {

Expand Down
1 change: 1 addition & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = function(app) {
// Transaction routes
var transactions = require('../app/controllers/transactions');
app.get(apiPrefix + '/tx/:txid', transactions.show);
app.get(apiPrefix + '/tx/:txid/getcpfpfee', transactions.getCpfpFee);
app.param('txid', transactions.transaction);
app.get(apiPrefix + '/txs', transactions.list);
app.post(apiPrefix + '/tx/send', transactions.send);
Expand Down