2014-03-26 05:18:42 -07:00
|
|
|
'use strict';
|
2014-06-12 13:42:26 -07:00
|
|
|
var bitcore = require('bitcore');
|
2014-03-26 05:18:42 -07:00
|
|
|
|
2014-06-03 13:42:36 -07:00
|
|
|
angular.module('copayApp.controllers').controller('TransactionsController',
|
2014-06-26 14:51:25 -07:00
|
|
|
function($scope, $rootScope, $timeout, controllerUtils, notification) {
|
2014-04-23 08:59:21 -07:00
|
|
|
|
2014-03-26 05:18:42 -07:00
|
|
|
$scope.title = 'Transactions';
|
2014-04-24 18:43:19 -07:00
|
|
|
$scope.loading = false;
|
2014-05-16 21:19:52 -07:00
|
|
|
$scope.lastShowed = false;
|
2014-04-25 13:53:19 -07:00
|
|
|
|
2014-05-20 11:14:52 -07:00
|
|
|
$scope.txpCurrentPage = 1;
|
|
|
|
$scope.txpItemsPerPage = 4;
|
2014-06-03 12:54:09 -07:00
|
|
|
$scope.blockchain_txs = [];
|
2014-05-20 11:14:52 -07:00
|
|
|
|
2014-06-16 08:44:18 -07:00
|
|
|
var satToUnit = 1 / config.unitToSatoshi;
|
|
|
|
|
|
|
|
$scope.update = function() {
|
2014-05-16 14:33:06 -07:00
|
|
|
$scope.loading = false;
|
2014-06-16 08:44:18 -07:00
|
|
|
var from = ($scope.txpCurrentPage - 1) * $scope.txpItemsPerPage;
|
2014-05-20 14:34:55 -07:00
|
|
|
var opts = {
|
2014-08-06 09:59:33 -07:00
|
|
|
pending: false,
|
|
|
|
skip: [from, from + $scope.txpItemsPerPage]
|
2014-05-20 14:34:55 -07:00
|
|
|
};
|
|
|
|
controllerUtils.updateTxs(opts);
|
2014-08-06 09:59:33 -07:00
|
|
|
setTimeout(function() {
|
|
|
|
$rootScope.$digest();
|
|
|
|
}, 0);
|
2014-05-16 14:33:06 -07:00
|
|
|
};
|
|
|
|
|
2014-08-06 09:59:33 -07:00
|
|
|
$scope.show = function() {
|
2014-06-16 08:44:18 -07:00
|
|
|
$scope.loading = true;
|
|
|
|
setTimeout(function() {
|
2014-05-16 21:19:52 -07:00
|
|
|
$scope.update();
|
|
|
|
}, 10);
|
|
|
|
};
|
|
|
|
|
2014-06-03 12:54:09 -07:00
|
|
|
var _aggregateItems = function(items) {
|
|
|
|
if (!items) return [];
|
|
|
|
|
|
|
|
var l = items.length;
|
|
|
|
|
|
|
|
var ret = [];
|
|
|
|
var tmp = {};
|
|
|
|
var u = 0;
|
|
|
|
|
2014-06-16 08:44:18 -07:00
|
|
|
for (var i = 0; i < l; i++) {
|
2014-06-03 12:54:09 -07:00
|
|
|
|
|
|
|
var notAddr = false;
|
|
|
|
// non standard input
|
|
|
|
if (items[i].scriptSig && !items[i].addr) {
|
2014-06-16 08:44:18 -07:00
|
|
|
items[i].addr = 'Unparsed address [' + u+++']';
|
2014-06-03 12:54:09 -07:00
|
|
|
items[i].notAddr = true;
|
|
|
|
notAddr = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// non standard output
|
|
|
|
if (items[i].scriptPubKey && !items[i].scriptPubKey.addresses) {
|
2014-06-16 08:44:18 -07:00
|
|
|
items[i].scriptPubKey.addresses = ['Unparsed address [' + u+++']'];
|
2014-06-03 12:54:09 -07:00
|
|
|
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;
|
|
|
|
|
2014-06-16 08:44:18 -07:00
|
|
|
tmp[addr].doubleSpentTxID = tmp[addr].doubleSpentTxID || items[i].doubleSpentTxID;
|
2014-06-03 12:54:09 -07:00
|
|
|
tmp[addr].doubleSpentIndex = tmp[addr].doubleSpentIndex || items[i].doubleSpentIndex;
|
|
|
|
tmp[addr].unconfirmedInput += items[i].unconfirmedInput;
|
|
|
|
tmp[addr].dbError = tmp[addr].dbError || items[i].dbError;
|
2014-06-16 08:44:18 -07:00
|
|
|
tmp[addr].valueSat += parseInt((items[i].value * bitcore.util.COIN).toFixed(0));
|
2014-06-03 12:54:09 -07:00
|
|
|
tmp[addr].items.push(items[i]);
|
|
|
|
tmp[addr].notAddr = notAddr;
|
|
|
|
tmp[addr].count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
angular.forEach(tmp, function(v) {
|
2014-06-16 08:44:18 -07:00
|
|
|
v.value = (parseInt(v.valueSat || 0).toFixed(0)) * satToUnit;
|
2014-06-03 12:54:09 -07:00
|
|
|
ret.push(v);
|
|
|
|
});
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
|
2014-06-16 08:44:18 -07:00
|
|
|
$scope.toogleLast = function() {
|
2014-05-16 21:19:52 -07:00
|
|
|
$scope.lastShowed = !$scope.lastShowed;
|
|
|
|
if ($scope.lastShowed) {
|
2014-06-03 12:54:09 -07:00
|
|
|
$scope.getTransactions();
|
2014-05-16 21:19:52 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-06-03 12:54:09 -07:00
|
|
|
$scope.getTransactions = function() {
|
|
|
|
var w = $rootScope.wallet;
|
|
|
|
$scope.loading = true;
|
2014-04-24 19:13:55 -07:00
|
|
|
if (w) {
|
2014-05-16 21:19:52 -07:00
|
|
|
var addresses = w.getAddressesStr();
|
2014-04-24 19:13:55 -07:00
|
|
|
if (addresses.length > 0) {
|
2014-06-24 11:46:07 -07:00
|
|
|
$scope.blockchain_txs = $scope.wallet.txCache || [];
|
2014-06-16 08:44:18 -07:00
|
|
|
w.blockchain.getTransactions(addresses, function(txs) {
|
2014-06-03 12:54:09 -07:00
|
|
|
$timeout(function() {
|
2014-06-24 11:46:07 -07:00
|
|
|
$scope.blockchain_txs = [];
|
2014-06-16 08:44:18 -07:00
|
|
|
for (var i = 0; i < txs.length; i++) {
|
2014-06-03 12:54:09 -07:00
|
|
|
txs[i].vinSimple = _aggregateItems(txs[i].vin);
|
|
|
|
txs[i].voutSimple = _aggregateItems(txs[i].vout);
|
2014-06-16 08:44:18 -07:00
|
|
|
txs[i].valueOut = ((txs[i].valueOut * bitcore.util.COIN).toFixed(0)) * satToUnit;
|
|
|
|
txs[i].fees = ((txs[i].fees * bitcore.util.COIN).toFixed(0)) * satToUnit;
|
2014-06-03 12:54:09 -07:00
|
|
|
$scope.blockchain_txs.push(txs[i]);
|
|
|
|
}
|
2014-06-24 11:46:07 -07:00
|
|
|
$scope.wallet.txCache = $scope.blockchain_txs;
|
2014-06-03 12:54:09 -07:00
|
|
|
$scope.loading = false;
|
|
|
|
}, 10);
|
|
|
|
});
|
2014-06-16 08:44:18 -07:00
|
|
|
} else {
|
2014-06-04 06:57:15 -07:00
|
|
|
$timeout(function() {
|
|
|
|
$scope.loading = false;
|
|
|
|
$scope.lastShowed = false;
|
|
|
|
}, 1);
|
2014-04-24 19:13:55 -07:00
|
|
|
}
|
2014-04-22 23:37:17 -07:00
|
|
|
}
|
2014-04-22 21:55:00 -07:00
|
|
|
};
|
|
|
|
|
2014-07-31 16:34:21 -07:00
|
|
|
$scope.hasAction = function(actions, action) {
|
|
|
|
return actions.hasOwnProperty('create');
|
|
|
|
}
|
|
|
|
|
2014-05-08 14:14:49 -07:00
|
|
|
$scope.getShortNetworkName = function() {
|
2014-06-16 08:44:18 -07:00
|
|
|
return config.networkName.substring(0, 4);
|
2014-05-08 14:14:49 -07:00
|
|
|
};
|
|
|
|
|
2014-06-24 11:46:07 -07:00
|
|
|
// Autoload transactions on 1-of-1
|
|
|
|
if ($rootScope.wallet && $rootScope.wallet.totalCopayers == 1) {
|
|
|
|
$scope.lastShowed = true;
|
|
|
|
$scope.getTransactions();
|
|
|
|
}
|
|
|
|
|
2014-03-26 05:18:42 -07:00
|
|
|
});
|