forked from bitcoinjs/coinselect
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathindex.js
More file actions
22 lines (18 loc) · 740 Bytes
/
index.js
File metadata and controls
22 lines (18 loc) · 740 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
var accumulative = require('./accumulative')
var blackjack = require('./blackjack')
var utils = require('./utils')
var ext = require('./bn-extensions')
// order by descending value, minus the inputs approximate fee
function utxoScore (x, feeRate) {
return ext.sub(x.value, ext.mul(feeRate, utils.inputBytes(x)))
}
module.exports = function coinSelect (utxos, outputs, feeRate) {
utxos = utxos.concat().sort(function (a, b) {
return ext.sub(utxoScore(b, feeRate), utxoScore(a, feeRate))
})
// attempt to use the blackjack strategy first (no change output)
var base = blackjack(utxos, outputs, feeRate)
if (base.inputs) return base
// else, try the accumulative strategy
return accumulative(utxos, outputs, feeRate)
}