txdb: fee.

This commit is contained in:
Christopher Jeffrey 2016-10-19 16:44:55 -07:00
parent 9049053a1e
commit 3aee9861a3
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -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);