FIX: limit number of userinvoices feetched per polling request

This commit is contained in:
Overtorment 2019-01-10 17:50:33 +00:00
parent 497178be12
commit 45ed240b50
3 changed files with 42 additions and 6 deletions

View file

@ -263,6 +263,14 @@ describe('LightningCustodianWallet', () => {
await lOld.fetchTransactions();
assert.equal(txLen, lOld.transactions_raw.length, 'tx count should not be changed');
assert.equal(invLen, (await lNew.getUserInvoices()).length, 'invoices count should not be changed');
// testing how limiting works:
assert.equal(lNew.user_invoices_raw.length, 1);
await lNew.addInvoice(666, 'test memo 2');
invoices = await lNew.getUserInvoices(1);
assert.equal(invoices.length, 2);
assert.equal(invoices[0].amt, 1);
assert.equal(invoices[1].amt, 666);
});
it('can pay free amount (tip) invoice', async function() {

View file

@ -17,6 +17,7 @@ export class LightningCustodianWallet extends LegacyWallet {
this._access_token_created_ts = 0;
this.refill_addressess = [];
this.pending_transactions_raw = [];
this.user_invoices_raw = [];
this.info_raw = false;
this.preferredBalanceUnit = BitcoinUnit.SATS;
}
@ -128,8 +129,10 @@ export class LightningCustodianWallet extends LegacyWallet {
*
* @return {Promise.<Array>}
*/
async getUserInvoices() {
let response = await this._api.get('/getuserinvoices', {
async getUserInvoices(limit = false) {
let limitString = '';
if (limit) limitString = '?limit=' + parseInt(limit);
let response = await this._api.get('/getuserinvoices' + limitString, {
headers: {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'application/json',
@ -144,9 +147,31 @@ export class LightningCustodianWallet extends LegacyWallet {
if (json && json.error) {
throw new Error('API error: ' + json.message + ' (code ' + json.code + ')');
}
this.user_invoices_raw = json;
return json;
if (limit) {
// need to merge existing invoices with the ones that arrived
// but the ones received later should overwrite older ones
for (let oldInvoice of this.user_invoices_raw) {
// iterate all OLD invoices
let found = false;
for (let newInvoice of json) {
// iterate all NEW invoices
if (newInvoice.payment_request === oldInvoice.payment_request) found = true;
}
if (!found) {
// if old invoice is not found in NEW array, we simply add it:
json.push(oldInvoice);
}
}
}
this.user_invoices_raw = json.sort(function(a, b) {
return a.timestamp - b.timestamp;
});
return this.user_invoices_raw;
}
/**

View file

@ -38,8 +38,11 @@ export default class LNDViewInvoice extends Component {
this.fetchInvoiceInterval = setInterval(async () => {
if (this.state.isFetchingInvoices) {
try {
const userInvoices = JSON.stringify(await this.state.fromWallet.getUserInvoices());
const updatedUserInvoice = JSON.parse(userInvoices).filter(invoice =>
const userInvoices = await this.state.fromWallet.getUserInvoices(20);
// fetching only last 20 invoices
// for invoice that was created just now - that should be enough (it is basically the last one, so limit=1 would be sufficient)
// but that might not work as intended IF user creates 21 invoices, and then tries to check the status of invoice #0, it just wont be updated
const updatedUserInvoice = userInvoices.filter(invoice =>
typeof this.state.invoice === 'object'
? invoice.payment_request === this.state.invoice.payment_request
: invoice.payment_request === this.state.invoice,