added get transaction history method

This commit is contained in:
Ivan Socolsky 2014-10-22 13:10:41 -03:00 committed by Matias Alejo Garcia
commit 797da92d90
3 changed files with 77 additions and 135 deletions

View file

@ -39,72 +39,6 @@ angular.module('copayApp.controllers').controller('TransactionsController',
}, 10);
};
var _aggregateItems = function(items) {
var w = $rootScope.wallet;
if (!items) return [];
var l = items.length;
var ret = [];
var tmp = {};
var u = 0;
for (var i = 0; i < l; i++) {
var notAddr = false;
// non standard input
if (items[i].scriptSig && !items[i].addr) {
items[i].addr = 'Unparsed address [' + u+++']';
items[i].notAddr = true;
notAddr = true;
}
// non standard output
if (items[i].scriptPubKey && !items[i].scriptPubKey.addresses) {
items[i].scriptPubKey.addresses = ['Unparsed address [' + u+++']'];
items[i].notAddr = true;
notAddr = true;
}
// multiple addr at output
if (items[i].scriptPubKey && items[i].scriptPubKey.addresses.length > 1) {
items[i].addr = items[i].scriptPubKey.addresses.join(',');
ret.push(items[i]);
continue;
}
var addr = items[i].addr || (items[i].scriptPubKey && items[i].scriptPubKey.addresses[0]);
if (!tmp[addr]) {
tmp[addr] = {};
tmp[addr].valueSat = 0;
tmp[addr].count = 0;
tmp[addr].addr = addr;
tmp[addr].items = [];
}
tmp[addr].isSpent = items[i].spentTxId;
tmp[addr].doubleSpentTxID = tmp[addr].doubleSpentTxID || items[i].doubleSpentTxID;
tmp[addr].doubleSpentIndex = tmp[addr].doubleSpentIndex || items[i].doubleSpentIndex;
tmp[addr].unconfirmedInput += items[i].unconfirmedInput;
tmp[addr].dbError = tmp[addr].dbError || items[i].dbError;
tmp[addr].valueSat += parseInt((items[i].value * bitcore.util.COIN).toFixed(0));
tmp[addr].items.push(items[i]);
tmp[addr].notAddr = notAddr;
tmp[addr].count++;
}
angular.forEach(tmp, function(v) {
v.value = (parseInt(v.valueSat || 0).toFixed(0)) * satToUnit;
rateService.whenAvailable(function() {
var valueSat = v.value * w.settings.unitToSatoshi;
v.valueAlt = rateService.toFiat(valueSat, w.settings.alternativeIsoCode);
});
ret.push(v);
});
return ret;
};
$scope.toogleLast = function() {
$scope.lastShowed = !$scope.lastShowed;
if ($scope.lastShowed) {
@ -113,37 +47,26 @@ angular.module('copayApp.controllers').controller('TransactionsController',
};
$scope.getTransactions = function() {
var self = this;
var w = $rootScope.wallet;
$scope.loading = true;
if (w) {
var addresses = w.getAddressesStr();
if (addresses.length > 0) {
$scope.blockchain_txs = $scope.wallet.txCache || [];
w.blockchain.getTransactions(addresses, function(err, txs) {
if (err) throw err;
if (!w) return;
$timeout(function() {
$scope.blockchain_txs = [];
for (var i = 0; i < txs.length; i++) {
txs[i].vinSimple = _aggregateItems(txs[i].vin);
txs[i].voutSimple = _aggregateItems(txs[i].vout);
txs[i].valueOut = ((txs[i].valueOut * bitcore.util.COIN).toFixed(0)) * satToUnit;
txs[i].fees = ((txs[i].fees * bitcore.util.COIN).toFixed(0)) * satToUnit;
$scope.blockchain_txs.push(txs[i]);
}
$scope.wallet.txCache = $scope.blockchain_txs;
$scope.loading = false;
}, 10);
});
} else {
$timeout(function() {
$scope.loading = false;
$scope.lastShowed = false;
}, 1);
$scope.loading = true;
w.getTransactionHistory(function(err, res) {
if (err) throw err;
if (!res) {
$scope.loading = false;
$scope.lastShowed = false;
return;
}
}
$scope.blockchain_txs = res;
$scope.loading = false;
});
};
$scope.hasAction = function(actions, action) {
return actions.hasOwnProperty('create');
}
@ -158,7 +81,7 @@ angular.module('copayApp.controllers').controller('TransactionsController',
$scope.getTransactions();
}
$scope.amountAlternative = function (amount, txIndex, cb) {
$scope.amountAlternative = function(amount, txIndex, cb) {
var w = $rootScope.wallet;
rateService.whenAvailable(function() {
var valueSat = amount * w.settings.unitToSatoshi;

View file

@ -2913,5 +2913,61 @@ Wallet.prototype.read_Old = function(walletId, skipFields, cb) {
});
};
Wallet.prototype.getTransactionHistory = function(cb) {
var self = this;
var satToUnit = 1 / self.settings.unitToSatoshi;
var addresses = _.pluck(self.getAddressesInfo(), 'addressStr');
if (addresses.length == 0) return cb();
function computeAmountIn(items) {
var mine = _.filter(items, function(item) {
return _.contains(addresses, item.addr);
});
return _.reduce(mine, function(memo, item) {
return memo + item.valueSat;
}, 0);
};
function computeAmountOut(items) {
var mine = _.filter(items, function(item) {
if (!item.scriptPubKey) return false;
// If classic multisig, ignore
if (item.scriptPubKey.addresses.length > 1) return false;
return _.contains(addresses, item.scriptPubKey.addresses[0]);
});
return _.reduce(mine, function(memo, item) {
return memo + parseInt((item.value * bitcore.util.COIN).toFixed(0));
}, 0);
};
function decorateTx(tx) {
var amountIn = computeAmountIn(tx.vin);
var amountOut = computeAmountOut(tx.vout);
var fees = parseInt((tx.fees * bitcore.util.COIN).toFixed(0));
var amount = amountIn - amountOut - (amountIn > 0 ? fees : 0);
if (amount == 0) {
tx.action = 'moved';
} else if (amount > 0) {
tx.action = 'sent';
} else {
tx.action = 'received';
}
tx.amountSat = Math.abs(amount);
tx.amount = tx.amountSat * satToUnit;
};
if (addresses.length > 0) {
self.blockchain.getTransactions(addresses, function(err, txs) {
if (err) return cb(err);
var history = _.map(txs, function(tx) {
decorateTx(tx);
return tx;
});
return cb(null, history);
});
}
};
module.exports = Wallet;

View file

@ -52,54 +52,17 @@
</div>
</div>
<div class="last-transactions-content">
<div class="large-5 medium-5 small-12 columns">
<div ng-repeat="vin in btx.vinSimple" class="m5b">
<small class="right"
ng-class="{'has-tip': vin.valueAlt}"
data-options="disable_for_touch:true"
tooltip-popup-delay='500'
tooltip="{{vin.valueAlt|noFractionNumber}} {{alternativeIsoCode}}"
tooltip-trigger="mouseenter"
tooltip-placement="right"
>
{{vin.value| noFractionNumber}} {{$root.wallet.settings.unitName}}
</small>
<div class="ellipsis text-gray size-12">
<contact address="{{vin.addr}}" tooltip-popup-delay="500" tooltip tooltip-placement="right" />
</div>
</div>
</div>
<div class="large-1 medium-1 hide-for-small-only columns text-center">
<i class="fi-arrow-right"></i>
</div>
<div class="show-for-small-only small-12 columns text-center">
<i class="fi-arrow-down"></i>
</div>
<div class="large-6 medium-6 small-12 columns">
<div ng-repeat="vout in btx.voutSimple" class="m5b">
<small class="right"
ng-class="{'has-tip': vout.valueAlt}"
data-options="disable_for_touch:true"
tooltip-popup-delay='500'
tooltip="{{vout.valueAlt|noFractionNumber}} {{alternativeIsoCode}}"
tooltip-trigger="mouseenter"
tooltip-placement="left"
>
{{vout.value| noFractionNumber}} {{$root.wallet.settings.unitName}}</small>
<div class="ellipsis text-gray size-12">
<contact address="{{vout.addr}}" tooltip-popup-delay="500" tooltip tooltip-placement="right" />
</div>
</div>
</div>
{{btx.action}}
</div>
<div class="last-transactions-footer">
<div class="row collapse">
<div class="large-6 medium-6 small-6 columns">
<div class="size-12"><span translate>Fee</span>: {{btx.fees | noFractionNumber}} {{$root.wallet.settings.unitName}}</div>
<div class="size-12"><span translate>Confirmations</span>: {{btx.confirmations || 0}}</div>
<div class="size-12" ng-show="!btx.confirmations || btx.confirmations == 0"><span translate>Unconfirmed</span></div>
<div class="size-12" ng-show="btx.confirmations > 0 && btx.confirmations <= 6">{{btx.confirmations || 0}} <span translate>Confirmations</span></div>
<div class="size-12" ng-show="btx.confirmations > 6"><span translate>Confirmed</span></div>
</div>
<div class="large-6 medium-6 small-6 columns text-right">
<p class="label size-14"><span translate>Total</span>: {{btx.valueOut| noFractionNumber}} {{$root.wallet.settings.unitName}}</p>
<p class="label size-14"><span translate>Amount</span>: {{btx.amount| noFractionNumber}} {{$root.wallet.settings.unitName}}</p>
</div>
</div>
</div>