Update: Rewrites balance checking with a promise-based function to avoid asynchronous issues

This commit is contained in:
Sam Cheng Hung 2018-04-11 15:24:29 +08:00
commit 92e593ab12
2 changed files with 41 additions and 27 deletions

View file

@ -1037,28 +1037,24 @@ angular.module('copayApp.services')
return cb(null, txps, n); return cb(null, txps, n);
}; };
// Displays Bitcoin Core Wallets if BTC balance is more than 0
root.initBitcoinCoreDisplay = function() { root.initBitcoinCoreDisplay = function() {
console.log('Init Bitcoin Core Display...'); storageService.checkIfFlagIsSet('displayBitcoinCoreFlag')
storageService.checkIfFlagIsSet('displayBitcoinCoreFlag').then(function(result) { .then(function(result) {
if (!result) { // Perform checks for flags which are even set to true once more, set the new flag value to 1
var walletsBtc = root.getWallets({coin: 'btc'}); if (result === false || result === true) {
var totalBtc = 0; root.checkBtcBalanceAndInitDisplay(1);
var errorBalance = false; }
});
};
if (walletsBtc.length > 0) { root.checkBtcBalanceAndInitDisplay = function(flagValue) {
walletsBtc.forEach(function(value, key, index) { var walletsBtc = root.getWallets({coin: 'btc'});
// Do not trust cachedBalance as it is added asynchronously. Manually call getLastKnownBalance for each wallet ID if (walletsBtc.length > 0) {
root.getLastKnownBalance(value.id, function(err, data) { // Do not trust cachedBalance as it is added asynchronously. Using a new promise-based function.
if (data) { root.getWalletsBalance(walletsBtc)
var balanceData = JSON.parse(data); .then(function(totalBalance) {
totalBtc += parseFloat(balanceData.balance); var enableDisplayBitcoinCore = totalBalance > 0 ? true : false;
} else {
errorBalance = true;
}
});
});
var enableDisplayBitcoinCore = (totalBtc > 0) && !errorBalance ? true : false;
var opts = { var opts = {
displayBitcoinCore: { displayBitcoinCore: {
@ -1069,11 +1065,29 @@ angular.module('copayApp.services')
if (err) $log.debug(err); if (err) $log.debug(err);
}); });
if (!errorBalance) storageService.activateDisplayBitcoinCoreFlag(); storageService.activateDisplayBitcoinCoreFlag(flagValue);
} });
} }
}
// Calculate wallets total balance (Promise). Attempts to fix asynchronous issue with cachedBalance not being available when it's needed
root.getWalletsBalance = function(wallets) {
return new Promise((resolve, reject) => {
var totalBalance = 0;
// Manually call getLastKnownBalance for each wallet ID
wallets.forEach(function(value, index, array) {
root.getLastKnownBalance(value.id, function(err, data) {
if (data) {
var balanceData = JSON.parse(data);
totalBalance += parseFloat(balanceData.balance);
}
// Resolves promise with the total balance on the final iteration
if (index === array.length - 1) resolve(totalBalance);
});
});
}); });
}; }
return root; return root;
}); });

View file

@ -640,15 +640,15 @@ angular.module('copayApp.services')
if (value == null) { if (value == null) {
resolve(false); resolve(false);
} else { } else {
resolve(true); resolve(JSON.parse(value).initialized);
} }
}); });
}); });
} }
root.activateDisplayBitcoinCoreFlag = function() { root.activateDisplayBitcoinCoreFlag = function(value) {
var flag = { var flag = {
initialized: true initialized: value
}; };
storage.set('displayBitcoinCoreFlag', flag, function() { }); storage.set('displayBitcoinCoreFlag', flag, function() { });
} }