Displaying transactions from cache when first entering wallet details.
This commit is contained in:
parent
33d780f2e5
commit
03fa87adbc
6 changed files with 239 additions and 36 deletions
129
src/js/services/wallet-history.service.js
Normal file
129
src/js/services/wallet-history.service.js
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
'use strict';
|
||||
|
||||
(function(){
|
||||
|
||||
angular
|
||||
.module('bitcoincom.services')
|
||||
.factory('walletHistoryService', walletHistoryService);
|
||||
|
||||
function walletHistoryService(configService, storageService, lodash, $log, txFormatService) {
|
||||
// 8 Transactions fit on an iPhone Plus screen when in Wallet Details.
|
||||
// Make it a little bit bigger so it doesn't have to load more immediately
|
||||
var PAGE_SIZE = 50 / 1.5;
|
||||
// How much to overlap on each end of the page, for mitigating inconsistent sort order.
|
||||
var PAGE_OVERLAP_FRACTION = 0.5;
|
||||
|
||||
var SAFE_CONFIRMATIONS = 6;
|
||||
|
||||
var service = {
|
||||
getCachedTxHistory: getCachedTxHistory,
|
||||
updateTxHistoryByPage: updateTxHistoryByPage
|
||||
};
|
||||
return service;
|
||||
|
||||
/**
|
||||
* @param {string} walletId
|
||||
* @param {function(error, txs)} cb
|
||||
*/
|
||||
function getCachedTxHistory(walletId, cb) {
|
||||
storageService.getTxHistory(walletId, function onGetTxHistory(err, txHistoryString){
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
if (!txHistoryString) {
|
||||
return cb(null, txHistoryString);
|
||||
}
|
||||
|
||||
try {
|
||||
var txHistory = JSON.parse(txHistoryString);
|
||||
return cb(null, txHistory);
|
||||
} catch (e) {
|
||||
$log.error('Failed to parse tx history.', e);
|
||||
return cb(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function processNewTxs(wallet, txs) {
|
||||
var now = Math.floor(Date.now() / 1000);
|
||||
var txHistoryUnique = {};
|
||||
var processedTxs = [];
|
||||
wallet.hasUnsafeConfirmed = false;
|
||||
|
||||
lodash.each(txs, function(tx) {
|
||||
tx = txFormatService.processTx(wallet.coin, tx);
|
||||
|
||||
// no future transactions...
|
||||
if (tx.time > now)
|
||||
tx.time = now;
|
||||
|
||||
if (tx.confirmations >= SAFE_CONFIRMATIONS) {
|
||||
tx.safeConfirmed = SAFE_CONFIRMATIONS + '+';
|
||||
} else {
|
||||
tx.safeConfirmed = false;
|
||||
wallet.hasUnsafeConfirmed = true;
|
||||
}
|
||||
|
||||
if (tx.note) {
|
||||
delete tx.note.encryptedEditedByName;
|
||||
delete tx.note.encryptedBody;
|
||||
}
|
||||
|
||||
if (!txHistoryUnique[tx.txid]) {
|
||||
processedTxs.push(tx);
|
||||
txHistoryUnique[tx.txid] = true;
|
||||
} else {
|
||||
$log.debug('Ignoring duplicate TX in history: ' + tx.txid)
|
||||
}
|
||||
});
|
||||
|
||||
// Update notes?
|
||||
|
||||
return processedTxs;
|
||||
};
|
||||
|
||||
/**
|
||||
* A small cache of up to the 50 most recent transactions
|
||||
*/
|
||||
function saveTxHistory(processedTxs, walletId) {
|
||||
storageService.setTxHistory(processedTxs, walletId, function onSetTxHistory(error){
|
||||
// Looks like callback only gets called on error
|
||||
$log.error('pagination Failed to save tx history.', error);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
// Only clear the cache once we have received new transactions from the server.
|
||||
function updateTxHistoryByPage(wallet, getLatest, flushCacheOnNew, cb) {
|
||||
var skip = 0;
|
||||
var limit = Math.floor(PAGE_SIZE * (1 + PAGE_OVERLAP_FRACTION));
|
||||
|
||||
var opts = {
|
||||
skip: skip,
|
||||
limit: limit
|
||||
};
|
||||
wallet.getTxHistory(opts, function onTxHistory(err, txsFromServer) {
|
||||
if (err) {
|
||||
return cb(err);
|
||||
}
|
||||
|
||||
if (!txsFromServer.length) {
|
||||
return cb(null, []);
|
||||
}
|
||||
|
||||
var processedTxs = processNewTxs(wallet, txsFromServer);
|
||||
|
||||
if (getLatest) {
|
||||
console.log('pagination Saving retrieved txs.');
|
||||
saveTxHistory(wallet, processedTxs);
|
||||
}
|
||||
|
||||
return cb(null, processedTxs);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
})();
|
||||
|
|
@ -398,6 +398,21 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
|
|||
|
||||
var skipped = 0;
|
||||
|
||||
function fixTxsUnit(txs) {
|
||||
if (!txs || !txs[0] || !txs[0].amountStr) return;
|
||||
|
||||
var cacheCoin = txs[0].amountStr.split(' ')[1];
|
||||
|
||||
if (cacheCoin == 'bits') {
|
||||
|
||||
$log.debug('Fixing Tx Cache Unit to: ' + wallet.coin)
|
||||
lodash.each(txs, function(tx) {
|
||||
tx.amountStr = txFormatService.formatAmountStr(wallet.coin, tx.amount);
|
||||
tx.feeStr = txFormatService.formatAmountStr(wallet.coin, tx.fees);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
var updateLocalTxHistory = function(wallet, opts, cb) {
|
||||
var FIRST_LIMIT = 5;
|
||||
var LIMIT = 50;
|
||||
|
|
@ -408,25 +423,10 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
|
|||
var progressFn = opts.progressFn || function() {};
|
||||
var foundLimitTx = false;
|
||||
|
||||
|
||||
if (opts.feeLevels) {
|
||||
opts.lowAmount = root.getLowAmount(wallet, opts.feeLevels);
|
||||
}
|
||||
|
||||
var fixTxsUnit = function(txs) {
|
||||
if (!txs || !txs[0] || !txs[0].amountStr) return;
|
||||
|
||||
var cacheCoin = txs[0].amountStr.split(' ')[1];
|
||||
|
||||
if (cacheCoin == 'bits') {
|
||||
|
||||
$log.debug('Fixing Tx Cache Unit to: ' + wallet.coin)
|
||||
lodash.each(txs, function(tx) {
|
||||
tx.amountStr = txFormatService.formatAmountStr(wallet.coin, tx.amount);
|
||||
tx.feeStr = txFormatService.formatAmountStr(wallet.coin, tx.fees);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
getSavedTxs(walletId, function(err, txsFromLocal) {
|
||||
if (err) return cb(err);
|
||||
|
|
@ -437,13 +437,14 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
|
|||
var endingTxid = confirmedTxs[0] ? confirmedTxs[0].txid : null;
|
||||
var endingTs = confirmedTxs[0] ? confirmedTxs[0].time : null;
|
||||
|
||||
$log.debug('Confirmed TXs. Got:' + confirmedTxs.length + '/' + txsFromLocal.length);
|
||||
console.log('pagination Hard confirmed TXs. Got:' + confirmedTxs.length + '/' + txsFromLocal.length);
|
||||
|
||||
// First update
|
||||
progressFn(txsFromLocal, 0);
|
||||
wallet.completeHistory = txsFromLocal;
|
||||
|
||||
function getNewTxs(newTxs, skip, next) {
|
||||
console.log('pagination getNewTxs skip: ' + skip);
|
||||
getTxsFromServer(wallet, skip, endingTxid, requestLimit, function(err, res) {
|
||||
if (err) {
|
||||
$log.warn(bwcError.msg(err, 'Server Error')); //TODO
|
||||
|
|
@ -456,6 +457,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
|
|||
return next(err);
|
||||
}
|
||||
|
||||
console.log('pagination Result count: ' + res.length);
|
||||
// Check if new txs are founds, if yes, lets investigate in the 50 next
|
||||
// To be sure we are not missing txs by sorting (maybe a new tx is after the "endingTxid"
|
||||
var newDiscoveredTxs = res.filter(function (x) {
|
||||
|
|
@ -464,7 +466,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
|
|||
}).length == 0;
|
||||
});
|
||||
|
||||
$log.debug('Discovering TXs. Got:' + newDiscoveredTxs.length);
|
||||
console.log('pagination Discovering new TXs. Got:' + newDiscoveredTxs.length);
|
||||
|
||||
var shouldContinue = newDiscoveredTxs.length > 0;
|
||||
|
||||
|
|
@ -477,7 +479,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
|
|||
|
||||
skip = skip + requestLimit;
|
||||
|
||||
$log.debug('Syncing TXs. Got:' + newTxs.length + ' Skip:' + skip, ' EndingTxid:', endingTxid, ' Continue:', shouldContinue);
|
||||
console.log('pagination Syncing TXs. Got:' + newTxs.length + ' Skip:' + skip, ' EndingTxid:', endingTxid, ' Continue:', shouldContinue);
|
||||
|
||||
// TODO Dirty <HACK>
|
||||
// do not sync all history, just looking for a single TX.
|
||||
|
|
@ -499,7 +501,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
|
|||
skipped = skip;
|
||||
|
||||
if (!shouldContinue) {
|
||||
$log.debug('Finished Sync: New / soft confirmed Txs: ' + newTxs.length);
|
||||
console.log('pagination Finished Sync: New / soft confirmed Txs: ' + newTxs.length);
|
||||
return next(null, newTxs);
|
||||
}
|
||||
|
||||
|
|
@ -545,6 +547,7 @@ angular.module('copayApp.services').factory('walletService', function($log, $tim
|
|||
getNewTxs([], skipped, function(err, txs) {
|
||||
if (err) return cb(err);
|
||||
|
||||
console.log();
|
||||
createReceivedEvents(txs);
|
||||
|
||||
var newHistory = lodash.uniq(lodash.compact(txs.concat(confirmedTxs)), function(x) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue