forked from bitcoinjs/coinselect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
break.js
34 lines (26 loc) · 870 Bytes
/
break.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
var utils = require('./utils')
// break utxos into the maximum number of 'output' possible
module.exports = function broken (utxos, output, feeRate) {
if (!isFinite(utils.uintOrNaN(feeRate))) return {}
var bytesAccum = utils.transactionBytes(utxos, [])
var value = utils.uintOrNaN(output.value)
var inAccum = utils.sumOrNaN(utxos)
if (!isFinite(value) ||
!isFinite(inAccum)) return { fee: feeRate * bytesAccum }
var outputBytes = utils.outputBytes(output)
var outAccum = 0
var outputs = []
while (true) {
var fee = feeRate * (bytesAccum + outputBytes)
// did we bust?
if (inAccum < (outAccum + fee + value)) {
// premature?
if (outAccum === 0) return { fee: fee }
break
}
bytesAccum += outputBytes
outAccum += value
outputs.push(output)
}
return utils.finalize(utxos, outputs, feeRate)
}