forked from CoinbaseWallet/coinselect
-
Notifications
You must be signed in to change notification settings - Fork 4
/
break.js
34 lines (26 loc) · 961 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')
var ext = require('./bn-extensions')
// break utxos into the maximum number of 'output' possible
module.exports = function broken (utxos, output, feeRate) {
if (!utils.uintOrNull(feeRate)) return {}
var bytesAccum = utils.transactionBytes(utxos, [])
var value = utils.uintOrNull(output.value)
var inAccum = utils.sumOrNaN(utxos)
if (!value || !inAccum) return { fee: ext.mul(feeRate, bytesAccum) }
var outputBytes = utils.outputBytes(output)
var outAccum = ext.BN_ZERO
var outputs = []
while (true) {
var fee = ext.mul(feeRate, ext.add(bytesAccum, outputBytes))
// did we bust?
if (ext.lt(inAccum, ext.add(outAccum, fee, value))) {
// premature?
if (ext.isZero(outAccum)) return { fee: fee }
break
}
bytesAccum = ext.add(bytesAccum, outputBytes)
outAccum = ext.add(outAccum, value)
outputs.push(output)
}
return utils.finalize(utxos, outputs, feeRate)
}