Wallet/src/js/controllers/addresses.js

68 lines
2.3 KiB
JavaScript
Raw Normal View History

2016-11-14 14:52:10 -03:00
'use strict';
2016-11-15 17:40:44 -03:00
angular.module('copayApp.controllers').controller('addressesController', function($scope, $stateParams, $timeout, $ionicScrollDelegate, configService, popupService, gettextCatalog, ongoingProcess, lodash, profileService, walletService) {
2016-11-16 15:23:26 -03:00
var UNUSED_ADDRESS_LIMIT = 5;
var BALANCE_ADDRESS_LIMIT = 5;
2016-11-15 17:40:44 -03:00
var config;
var unitName;
var unitToSatoshi;
var satToUnit;
var unitDecimals;
2016-11-14 14:52:10 -03:00
$scope.wallet = profileService.getWallet($stateParams.walletId);
2016-11-14 16:51:11 -03:00
$scope.showInfo = false;
2016-11-14 14:52:10 -03:00
2016-11-14 16:51:11 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
2016-11-15 17:40:44 -03:00
config = configService.getSync().wallet.settings;
unitToSatoshi = config.unitToSatoshi;
satToUnit = 1 / unitToSatoshi;
unitName = config.unitName;
unitDecimals = config.unitDecimals;
2016-11-16 15:23:26 -03:00
init();
});
2016-11-15 17:40:44 -03:00
2016-11-16 15:23:26 -03:00
function init() {
2016-11-14 16:51:11 -03:00
ongoingProcess.set('extractingWalletInfo', true);
2016-11-16 15:23:26 -03:00
walletService.getMainAddresses($scope.wallet, {}, function(err, addresses) {
2016-11-15 17:40:44 -03:00
if (err) {
2016-11-16 15:23:26 -03:00
ongoingProcess.set('extractingWalletInfo', false);
2016-11-15 17:40:44 -03:00
return popupService.showAlert(gettextCatalog.getString('Error'), err);
}
2016-11-14 16:51:11 -03:00
2016-11-16 15:23:26 -03:00
$scope.allAddresses = addresses;
walletService.getBalance($scope.wallet, {}, function(err, resp) {
ongoingProcess.set('extractingWalletInfo', false);
if (err) {
return popupService.showAlert(gettextCatalog.getString('Error'), err);
}
var withBalance = resp.byAddress;
var idx = lodash.indexBy(withBalance, 'address');
var noBalance = lodash.reject($scope.allAddresses, function(x) {
return idx[x.address];
});
lodash.each(noBalance, function(n) {
n.path = n.path.replace(/^m/g, 'xpub');
});
$scope.unused = lodash.slice(noBalance, 0, UNUSED_ADDRESS_LIMIT);
$scope.withBalance = lodash.slice(withBalance, 0, BALANCE_ADDRESS_LIMIT);
lodash.each($scope.withBalance, function(a) {
a.balanceStr = (a.amount * satToUnit).toFixed(unitDecimals) + ' ' + unitName;
});
$scope.viewAll = {
value: noBalance.length > UNUSED_ADDRESS_LIMIT || withBalance.length > BALANCE_ADDRESS_LIMIT
};
$scope.$digest();
2016-11-15 17:40:44 -03:00
});
2016-11-14 16:51:11 -03:00
});
2016-11-16 15:23:26 -03:00
};
2016-11-14 16:51:11 -03:00
$scope.showInformation = function() {
$timeout(function() {
$scope.showInfo = !$scope.showInfo;
$ionicScrollDelegate.resize();
});
};
2016-11-14 14:52:10 -03:00
});