updateStatus -> getStatus

This commit is contained in:
Ivan Socolsky 2016-08-17 18:02:47 -03:00
commit 5656caaa5a
No known key found for this signature in database
GPG key ID: FAECE6A05FAA4F56
4 changed files with 1428 additions and 124 deletions

View file

@ -226,7 +226,7 @@
</div> </div>
<ion-infinite-scroll <ion-infinite-scroll
ng-if="historyShowMore" ng-if="wallet.historyShowMore"
on-infinite="showMore()" on-infinite="showMore()"
distance="1%"> distance="1%">
</ion-infinite-scroll> </ion-infinite-scroll>

1334
src/js/controllers/index.js Normal file

File diff suppressed because it is too large Load diff

View file

@ -85,9 +85,7 @@ angular.module('copayApp.controllers').controller('walletDetailsController', fun
$scope.wallet = wallet; $scope.wallet = wallet;
if (wallet) { if (wallet) {
walletService.updateStatus(wallet, { walletService.updateStatus(wallet, {}, function(err, status) {
triggerTxUpdate: true
}, function(err, status) {
console.log(status); console.log(status);
if (err) {} // TODO if (err) {} // TODO
}); });

View file

@ -9,6 +9,8 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
var root = {}; var root = {};
root.WALLET_STATUS_MAX_TRIES = 7;
root.WALLET_STATUS_DELAY_BETWEEN_TRIES = 1.4 * 1000;
root.SOFT_CONFIRMATION_LIMIT = 12; root.SOFT_CONFIRMATION_LIMIT = 12;
root.HISTORY_SHOW_LIMIT = 10; root.HISTORY_SHOW_LIMIT = 10;
@ -107,12 +109,24 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
}; };
root.handleError = lodash.debounce(_handleError, 1000); root.handleError = lodash.debounce(_handleError, 1000);
root.getStatus = function(wallet, opts, cb) {
opts = opts || {};
root.setBalance = function(wallet, balance) { function get(cb) {
wallet.getStatus({
twoStep: true
}, function(err, ret) {
if (err) {
return cb(bwcError.msg(err, gettext('Could not update Wallet')));
}
return cb(null, ret);
});
};
function cacheBalance(wallet, balance) {
if (!balance) return; if (!balance) return;
var config = configService.getSync().wallet.settings; var config = configService.getSync().wallet.settings;
var COIN = 1e8;
// Address with Balance // Address with Balance
wallet.balanceByAddress = balance.byAddress; wallet.balanceByAddress = balance.byAddress;
@ -138,12 +152,12 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
wallet.unitName = config.unitName; wallet.unitName = config.unitName;
//STR //STR
wallet.totalBalanceStr = txFormatService.formatAmount(wallet.totalBalanceSat) + ' ' + wallet.unitName; wallet.totalBalanceStr = root.formatAmount(wallet.totalBalanceSat) + ' ' + wallet.unitName;
wallet.lockedBalanceStr = txFormatService.formatAmount(wallet.lockedBalanceSat) + ' ' + wallet.unitName; wallet.lockedBalanceStr = root.formatAmount(wallet.lockedBalanceSat) + ' ' + wallet.unitName;
wallet.availableBalanceStr = txFormatService.formatAmount(wallet.availableBalanceSat) + ' ' + wallet.unitName; wallet.availableBalanceStr = root.formatAmount(wallet.availableBalanceSat) + ' ' + wallet.unitName;
if (wallet.pendingAmount) { if (wallet.pendingAmount) {
wallet.pendingAmountStr = txFormatService.formatAmount(wallet.pendingAmount) + ' ' + wallet.unitName; wallet.pendingAmountStr = root.formatAmount(wallet.pendingAmount) + ' ' + wallet.unitName;
} else { } else {
wallet.pendingAmountStr = null; wallet.pendingAmountStr = null;
} }
@ -166,96 +180,55 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
}); });
}; };
root.setStatus = function(wallet, status) { function cacheStatus = (status) {
wallet.status = status; wallet.status = status;
wallet.statusUpdatedOn = Date.now(); wallet.statusUpdatedOn = Date.now();
wallet.isValid = true; wallet.isValid = true;
root.setBalance(wallet, status.balance); cacheBalance(wallet, status.balance);
wallet.email = status.preferences.email; wallet.email = status.preferences.email;
wallet.copayers = status.wallet.copayers;
}; };
root.updateStatus = function(wallet, opts, cb) { function walletStatusHash(status) {
opts = opts || {}; return status ? status.balance.totalAmount : wallet.totalBalanceSat;
function updateBalance(cb) {
if (wallet.isValid && !opts.force)
return cb();
}; };
function updateHistory() { function _getStatus(initStatusHash, tries, cb) {
if (!opts.triggerTxUpdate) return; if (wallet.isValid && !opts.force) return cb(null, wallet.status);
$timeout(function() {
root.updateHistory(wallet);
});
};
function getStatus(cb) {
wallet.getStatus({
twoStep: true
}, function(err, ret) {
if (err) {
return cb(bwcError.msg(err, gettext('Could not update Wallet')));
}
return cb(null, ret);
});
};
function walletStatusHash(walletStatus) {
var bal;
if (walletStatus) {
bal = walletStatus.balance.totalAmount;
} else {
bal = wallet.totalBalanceSat;
}
return bal;
};
function doUpdate(initStatusHash, tries) {
if (wallet.isValid && !opts.force) return cb();
tries = tries || 0; tries = tries || 0;
$timeout(function() {
$log.debug('Updating Status:', wallet.credentials.walletName, tries); $log.debug('Updating Status:', wallet.credentials.walletName, tries);
getStatus(function(err, walletStatus) { get(function(err, status) {
if (err) return cb(err); if (err) return cb(err);
var currentStatusHash = walletStatusHash(walletStatus); var currentStatusHash = walletStatusHash(status);
$log.debug('Status update. hash:' + currentStatusHash + ' Try:' + tries); $log.debug('Status update. hash:' + currentStatusHash + ' Try:' + tries);
if (opts.untilItChanges && if (opts.untilItChanges &&
initStatusHash == currentStatusHash && initStatusHash == currentStatusHash &&
tries < 7 && tries < root.WALLET_STATUS_MAX_TRIES &&
walletId == wallet.credentials.walletId) { walletId == wallet.credentials.walletId) {
return $timeout(function() { return $timeout(function() {
$log.debug('Retrying update... ' + walletId + ' Try:' + tries) $log.debug('Retrying update... ' + walletId + ' Try:' + tries)
return root.doUpdate(initStatusHash, ++tries, cb); return _getStatus(initStatusHash, ++tries, cb);
}, 1400 * tries); }, root.WALLET_STATUS_DELAY_BETWEEN_TRIES * tries);
} }
$log.debug('Got Wallet Status for:' + wallet.credentials.walletName); $log.debug('Got Wallet Status for:' + wallet.credentials.walletName);
root.setStatus(wallet, walletStatus); root.cacheStatus(wallet, status);
// wallet.setPendingTxps(walletStatus.pendingTxps); // wallet.setPendingTxps(status.pendingTxps);
return cb(); return cb(null, status);
});
}); });
}; };
// trigger history update now? _getStatus(walletStatusHash(), 0, function(err, status) {
if (!opts.untilItChanges) updateHistory();
doUpdate(walletStatusHash(), 0, function(err) {
if (err) { if (err) {
root.handleError(err); root.handleError(err);
return cb(err); return cb(err);
} }
return cb(null, status);
if (opts.untilItChanges) updateHistory();
return cb();
}) })
}; };
var getSavedTxs = function(walletId, cb) { var getSavedTxs = function(walletId, cb) {
@ -479,11 +452,10 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
}; };
root.updateHistory = function(wallet) { root.updateHistory = function(wallet, cb) {
var walletId = wallet.credentials.walletId; var walletId = wallet.credentials.walletId;
if (!wallet.isComplete()) return; if (!wallet.isComplete()) return cb();
$log.debug('Updating Transaction History'); $log.debug('Updating Transaction History');
wallet.txHistoryError = false; wallet.txHistoryError = false;
@ -499,7 +471,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
$timeout(function() { $timeout(function() {
wallet.newTx = false wallet.newTx = false
}, 1000); });
}); });
}); });