From 344fc525e0952be4621aad5777088c18844f3edb Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 27 Jul 2015 08:49:54 -0300 Subject: [PATCH] add fee estimation method to txp --- lib/model/txproposal.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lib/model/txproposal.js b/lib/model/txproposal.js index 1e7d776..15812df 100644 --- a/lib/model/txproposal.js +++ b/lib/model/txproposal.js @@ -169,6 +169,25 @@ TxProposal.prototype.getRawTx = function() { return t.uncheckedSerialize(); }; +TxProposal.prototype.estimateFee = function(walletN) { + // Note: found empirically based on all multisig P2SH inputs and within m & n allowed limits. + var safetyMargin = 0.05; + + var walletM = this.requiredSignatures; + + var overhead = 4 + 4 + 9 + 9; + var inputSize = walletM * 72 + walletN * 36 + 44; + var outputSize = 34; + var nbInputs = this.inputs.length; + var nbOutputs = (_.isArray(this.outputs) ? this.outputs.length : 1) + 1; + + var size = overhead + inputSize * nbInputs + outputSize * nbOutputs; + + var fee = this.feePerKb * (size * (1 + safetyMargin)) / 1000; + + // Round up to nearest bit + this.fee = parseInt((Math.ceil(fee / 100) * 100).toFixed(0)); +}; /** * getTotalAmount