-
Notifications
You must be signed in to change notification settings - Fork 101
/
blackjack.js
35 lines (26 loc) · 983 Bytes
/
blackjack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
var utils = require('./utils')
// only add inputs if they don't bust the target value (aka, exact match)
// worst-case: O(n)
module.exports = function blackjack (utxos, outputs, feeRate) {
if (!isFinite(utils.uintOrNaN(feeRate))) return {}
var bytesAccum = utils.transactionBytes([], outputs)
var inAccum = 0
var inputs = []
var outAccum = utils.sumOrNaN(outputs)
var threshold = utils.dustThreshold({}, feeRate)
for (var i = 0; i < utxos.length; ++i) {
var input = utxos[i]
var inputBytes = utils.inputBytes(input)
var fee = feeRate * (bytesAccum + inputBytes)
var inputValue = utils.uintOrNaN(input.value)
// would it waste value?
if ((inAccum + inputValue) > (outAccum + fee + threshold)) continue
bytesAccum += inputBytes
inAccum += inputValue
inputs.push(input)
// go again?
if (inAccum < outAccum + fee) continue
return utils.finalize(inputs, outputs, feeRate)
}
return { fee: feeRate * bytesAccum }
}