@@ -271,6 +294,7 @@
data() {
return {
charge: JSON.parse('{{charge_data | tojson}}'),
+ pendingFunds: 0,
ws: null,
newProgress: 0.4,
counter: 1,
@@ -313,6 +337,26 @@
LNbits.utils.notifyApiError(error)
}
},
+ checkPendingOnchain: async function () {
+ const {
+ bitcoin: {addresses: addressesAPI}
+ } = mempoolJS()
+
+ try {
+ const fn = async () =>
+ addressesAPI.getAddressTxsUtxo({
+ address: this.charge.onchainaddress
+ })
+
+ const utxos = await retryWithDelay(fn)
+
+ this.pendingFunds = utxos
+ .filter(u => !u.status.confirmed)
+ .reduce((t, u) => t + u.value, 0)
+ } catch (error) {
+ console.error('cannot check pending funds')
+ }
+ },
payInvoice: function () {
this.lnbtc = true
this.onbtc = false
@@ -339,14 +383,13 @@
loopRefresh: function () {
// invoice only
const refreshIntervalId = setInterval(async () => {
- console.log('### 1111')
if (this.charge.paid || this.timetoComplete < 1) {
clearInterval(refreshIntervalId)
}
this.refreshProgres()
if (this.counter % 10 === 0) {
- console.log('### 11110')
await this.checkBalances()
+ await this.checkPendingOnchain()
}
this.counter++
}, 1000)
@@ -360,17 +403,14 @@
this.ws = new WebSocket('wss://mempool.space/api/v1/ws')
this.ws.addEventListener('open', x => {
- console.log('### open ws', x)
- })
- this.ws.addEventListener('close', x => {
- console.log('### close ws', x)
+ if (this.charge.onchainaddress) {
+ this.trackAddress(this.charge.onchainaddress)
+ }
})
this.ws.addEventListener('message', async ({data}) => {
const res = JSON.parse(data.toString())
- console.log('### socker mempool res', res)
if (res['address-transactions']) {
- console.log('### res', res)
await this.checkBalances()
this.$q.notify({
type: 'positive',
@@ -389,7 +429,6 @@
trackAddress: async function (address, retry = 0) {
try {
if (!this.ws || this.ws.readyState !== WebSocket.OPEN) this.initWs()
- console.log('### start to track: ', address)
this.ws.send(JSON.stringify({'track-address': address}))
} catch (error) {
await sleep(1000)
@@ -415,6 +454,7 @@
if (this.charge.onchainaddress) {
this.loopPingWs()
+ this.checkPendingOnchain()
this.trackAddress(this.charge.onchainaddress)
}
}