diff --git a/lib/wallet/txdb.js b/lib/wallet/txdb.js index 770be7c6..079b8cf4 100644 --- a/lib/wallet/txdb.js +++ b/lib/wallet/txdb.js @@ -2801,15 +2801,17 @@ function Details(txdb, tx) { this.network = this.db.network; this.wid = txdb.wallet.wid; this.id = txdb.wallet.id; + this.hash = tx.hash('hex'); - this.height = tx.height; + this.tx = tx; + this.block = tx.block; + this.height = tx.height; + this.ts = tx.ts; this.index = tx.index; this.confirmations = tx.getConfirmations(this.db.height); - this.fee = tx.getFee(); - this.ts = tx.ts; + this.ps = tx.ps; - this.tx = tx; this.inputs = []; this.outputs = []; @@ -2878,6 +2880,34 @@ Details.prototype.setOutput = function setOutput(i, path) { } }; +/** + * Calculate fee. Only works if wallet + * owns all inputs. Returns 0 otherwise. + * @returns {Amount} + */ + +Details.prototype.getFee = function getFee() { + var inputValue = 0; + var outputValue = 0; + var i, input, output; + + for (i = 0; i < this.inputs.length; i++) { + input = this.inputs[i]; + + if (!input.path) + return 0; + + inputValue += input.value; + } + + for (i = 0; i < this.outputs.length; i++) { + output = this.outputs[i]; + outputValue += output.value; + } + + return inputValue - outputValue; +}; + /** * Convert details to a more json-friendly object. * @returns {Object} @@ -2894,7 +2924,7 @@ Details.prototype.toJSON = function toJSON() { ts: this.ts, ps: this.ps, index: this.index, - fee: utils.btc(this.fee), + fee: utils.btc(this.getFee()), confirmations: this.confirmations, inputs: this.inputs.map(function(input) { return input.toJSON(self.network);