feat: do client side rescan for onchain address

This commit is contained in:
Vlad Stan 2022-07-07 10:50:23 +03:00
parent 6c21bf359c
commit 91c6d32852
2 changed files with 50 additions and 0 deletions

View file

@ -280,6 +280,8 @@
<!-- lnbits/static/vendor
<script src="/vendor/vue-qrcode@1.0.2/vue-qrcode.min.js"></script> -->
<style></style>
<!-- todo: use config mempool -->
<script src="https://mempool.space/mempool.js"></script>
<script>
Vue.component(VueQrcode.name, VueQrcode)
@ -300,6 +302,21 @@
return obj
}
const sleep = ms => new Promise(r => setTimeout(r, ms))
const retryWithDelay = async function (fn, retryCount = 0) {
try {
await sleep(25)
// Do not return the call directly, use result.
// Otherwise the error will not be cought in this try-catch block.
const result = await fn()
return result
} catch (err) {
if (retryCount > 100) throw err
await sleep((retryCount + 1) * 1000)
return retryWithDelay(fn, retryCount + 1)
}
}
new Vue({
el: '#vue',
mixins: [windowMixin],
@ -423,6 +440,19 @@
}
},
getWalletConfig: async function () {
try {
const {data} = await LNbits.api.request(
'GET',
'/watchonly/api/v1/config',
this.g.user.wallets[0].inkey
)
this.mempool.endpoint = data.mempool_endpoint
} catch (error) {
LNbits.utils.notifyApiError(error)
}
},
getCharges: async function () {
try {
const {data} = await LNbits.api.request(
@ -460,6 +490,22 @@
await this.getCharges()
}, 20000)
},
rescanOnchainAddresses: async function () {
const {
bitcoin: {addresses: addressesAPI}
} = mempoolJS()
const onchainCharges = this.chargeLinks.filter(c => c.onchainaddress)
for (const charge of onchainCharges) {
const fn = async () =>
addressesAPI.getAddressTxsUtxo({
address: charge.onchainaddress
})
const utxos = await retryWithDelay(fn)
charge.balance = utxos.reduce((t, u) => t + u.value, 0)
}
},
createCharge: async function (wallet, data) {
try {
const resp = await LNbits.api.request(
@ -511,7 +557,10 @@
console.log(this.g.user)
await this.getCharges()
await this.getWalletLinks()
await this.getWalletConfig()
this.timerCount()
this.rescanOnchainAddresses()
setInterval(() => this.rescanOnchainAddresses(), 30 * 1000)
}
})
</script>

View file

@ -1198,6 +1198,7 @@
</div>
{% endblock %} {% block scripts %} {{ window_vars(user) }}
<!-- todo: use endpoint here -->
<script type="text/javascript" src="https://mempool.space/mempool.js"></script>
<script src="{{ url_for('watchonly_static', path='js/tables.js') }}"></script>