feat: only refresh balance on the back-end if new funds are detected by the front-end refresh

This commit is contained in:
Vlad Stan 2022-07-11 11:13:26 +03:00
parent 31b6a9410c
commit 057de41678
2 changed files with 22 additions and 1 deletions

View File

@ -329,6 +329,9 @@
)
},
checkBalances: async function () {
console.log('### checkBalances 1', this.charge.hasStaleBalance)
if (!this.charge.hasStaleBalance) return
console.log('### checkBalances 2')
try {
const {data} = await LNbits.api.request(
'GET',
@ -356,10 +359,18 @@
const utxos = await addressesAPI.getAddressTxsUtxo({
address: this.charge.onchainaddress
})
const newBalance = utxos.reduce((t, u) => t + u.value, 0)
this.charge.hasStaleBalance = this.charge.balance === newBalance
this.pendingFunds = utxos
.filter(u => !u.status.confirmed)
.reduce((t, u) => t + u.value, 0)
console.log(
'### charge new funds',
this.charge.balance === newBalance,
this.charge
)
} catch (error) {
console.error('cannot check pending funds')
}

View File

@ -576,9 +576,10 @@
},
refreshActiveChargesBalance: async function () {
const activeLinkIds = this.chargeLinks
.filter(c => !c.paid && !c.time_elapsed)
.filter(c => !c.paid && !c.time_elapsed && !c.hasStaleBalance)
.map(c => c.id)
.join(',')
console.log('### activeLinkIds', activeLinkIds)
await LNbits.api.request(
'GET',
'/satspay/api/v1/charges/balance/' + activeLinkIds,
@ -615,9 +616,18 @@
})
const utxos = await retryWithDelay(fn)
const newBalance = utxos.reduce((t, u) => t + u.value, 0)
charge.pendingBalance = utxos
.filter(u => !u.status.confirmed)
.reduce((t, u) => t + u.value, 0)
charge.hasStaleBalance = charge.balance === newBalance
console.log(
'### charge new funds',
charge.balance === newBalance,
charge
)
}
} catch (error) {
console.error(error)