FIX: transactions list update limit

This commit is contained in:
Overtorment 2018-07-25 00:27:21 +01:00
parent 4a10782453
commit 99db379131
2 changed files with 99 additions and 37 deletions

View file

@ -298,8 +298,19 @@ describe('Watch only wallet', () => {
}); });
it('can fetch tx', async () => { it('can fetch tx', async () => {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 30 * 1000;
let w = new WatchOnlyWallet(); let w = new WatchOnlyWallet();
w.setSecret('12eQ9m4sgAwTSQoNXkRABKhCXCsjm2jdVG');
w.setSecret('167zK5iZrs1U6piDqubD3FjRqUTM2CZnb8');
await w.fetchTransactions();
assert.equal(w.getTransactions().length, 233);
w = new WatchOnlyWallet();
w.setSecret('1BiJW1jyUaxcJp2JWwbPLPzB1toPNWTFJV');
await w.fetchTransactions();
assert.equal(w.getTransactions().length, 2);
// fetch again and make sure no duplicates
await w.fetchTransactions(); await w.fetchTransactions();
assert.equal(w.getTransactions().length, 2); assert.equal(w.getTransactions().length, 2);
}); });

View file

@ -177,18 +177,40 @@ export class LegacyWallet extends AbstractWallet {
baseURI: 'https://api.blockcypher.com/', baseURI: 'https://api.blockcypher.com/',
}); });
let after = 0;
let before = 100500100;
for (let oldTx of this.getTransactions()) {
if (oldTx.block_height && oldTx.confirmations < 7) {
after = Math.max(after, oldTx.block_height);
}
}
while (1) {
let response = await api.get( let response = await api.get(
'v1/btc/main/addrs/' + this.getAddress() + '/full' + ((useBlockcypherTokens && '?token=' + this.getRandomBlockcypherToken()) || ''), 'v1/btc/main/addrs/' +
this.getAddress() +
'/full?after=' +
after +
'&before=' +
before +
'&limit=50' +
((useBlockcypherTokens && '&token=' + this.getRandomBlockcypherToken()) || ''),
); );
let json = response.body; let json = response.body;
if (typeof json === 'undefined' || !json.txs) { if (typeof json === 'undefined' || !json.txs) {
throw new Error('Could not fetch transactions from API:' + response.err); throw new Error('Could not fetch transactions from API:' + response.err);
} }
let alreadyFetchedTransactions = this.transactions;
this.transactions = json.txs; this.transactions = json.txs;
this._lastTxFetch = +new Date(); this._lastTxFetch = +new Date();
// now, calculating value per each transaction... // now, calculating value per each transaction...
for (let tx of this.transactions) { for (let tx of this.transactions) {
if (tx.block_height) {
before = Math.min(before, tx.block_height); // so next time we fetch older TXs
}
// how much came in... // how much came in...
let value = 0; let value = 0;
for (let out of tx.outputs) { for (let out of tx.outputs) {
@ -204,8 +226,8 @@ export class LegacyWallet extends AbstractWallet {
value = 0; value = 0;
for (let inp of tx.inputs) { for (let inp of tx.inputs) {
if (!inp.addresses) { if (!inp.addresses) {
console.log('inp.addresses empty'); // console.log('inp.addresses empty');
console.log('got witness', inp.witness); // TODO // console.log('got witness', inp.witness); // TODO
inp.addresses = []; inp.addresses = [];
if (inp.witness && inp.witness[1]) { if (inp.witness && inp.witness[1]) {
@ -223,6 +245,35 @@ export class LegacyWallet extends AbstractWallet {
tx.value += value; tx.value += value;
// end // end
} }
this.transactions = alreadyFetchedTransactions.concat(this.transactions);
let txsUnconf = [];
let txs = [];
let hashPresent = {};
// now, rearranging TXs. unconfirmed go first:
for (let tx of this.transactions.reverse()) {
if (hashPresent[tx.hash]) continue;
hashPresent[tx.hash] = 1;
if (tx.block_height && tx.block_height === -1) {
// unconfirmed
txsUnconf.push(tx);
} else {
txs.push(tx);
}
}
this.transactions = txsUnconf.reverse().concat(txs.reverse());
// all reverses needed so freshly fetched TXs replace same old TXs
this.transactions = this.transactions.sort((a, b) => {
return a.received < b.received;
});
if (json.txs.length < 50) {
// final batch, so it has les than max txs
break;
}
}
} catch (err) { } catch (err) {
console.warn(err); console.warn(err);
} }