Skip to content

Commit 1b109ee

Browse files
authored
Add cpfp endpoint
1 parent 038eb40 commit 1b109ee

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

app/controllers/transactions.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,44 @@ exports.showRaw = function(req, res) {
9292
}
9393
};
9494

95+
/**
96+
* Get needed fee (BTC) for given fee rate (BTC/kB)
97+
*/
98+
exports.getCpfpFee = function(req, res) {
99+
100+
if (req.transaction) {
101+
if (req.transaction.confirmations > 0) {
102+
return res.jsonp({extraFeeNeeded: 0});
103+
} else {
104+
bitcoreRpc.getRawMemPool(true, function(err, rawMemPool) {
105+
if (err || !rawMemPool) {
106+
console.log(err);
107+
return res.status(500).send('Internal Server Error');
108+
}
109+
110+
if (rawMemPool.hasOwnProperty(req.transaction.txid)) {
111+
var size = rawMemPool[req.transaction.txid].ancestorsize
112+
var fees = rawMemPool[req.transaction.txid].ancestorfees
113+
114+
var targetFeeRate = parseFloat(req.query.feeRate) // BTC/kB
115+
116+
var targetFee = targetFeeRate * (size / 1000) // BTC
117+
118+
var missingFee = targetFee - (fees / 1e8) // BTC
119+
120+
if (missingFee < (1 / 1e8)) { // if missing fee less than one satoshi
121+
return res.jsonp({extraFeeNeeded: 0});
122+
} else {
123+
return res.jsonp({extraFeeNeeded: missingFee});
124+
}
125+
} else { // txid was not in rawMemPool even though confirmations == 0
126+
return res.status(500).send('Internal Server Error'); // should we just return 0?
127+
}
128+
})
129+
}
130+
}
131+
};
132+
95133

96134
var getTransaction = function(txid, cb) {
97135

0 commit comments

Comments
 (0)