From 8f0413da987dfb12cc3eb45133828fab9dde585a Mon Sep 17 00:00:00 2001 From: Wei Lu Date: Mon, 24 Mar 2014 01:01:50 +0800 Subject: [PATCH] always assume change output exists when estimating fee --- src/wallet.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/wallet.js b/src/wallet.js index 5c153cc..e3180cc 100644 --- a/src/wallet.js +++ b/src/wallet.js @@ -191,14 +191,12 @@ var Wallet = function (seed, options) { totalInValue += output.value if(totalInValue < value) continue; - var fee = fixedFee || tx.estimateFee() + var fee = fixedFee || estimateFeePadChangeOutput(tx) if(totalInValue < value + fee) continue; var change = totalInValue - value - fee - var changeAddress = getChangeAddress() if(change > 0) { - tx.addOutput(changeAddress, change) - // TODO: recalculate fee + tx.addOutput(getChangeAddress(), change) } break; } @@ -207,6 +205,12 @@ var Wallet = function (seed, options) { return tx } + function estimateFeePadChangeOutput(tx){ + var tmpTx = tx.clone() + tmpTx.addOutput(getChangeAddress(), 0) + return tmpTx.estimateFee() + } + function getChangeAddress() { if(me.changeAddresses.length === 0) me.generateChangeAddress() return me.changeAddresses[me.changeAddresses.length - 1]