feat: separate invoice check from onchain check

This commit is contained in:
Vlad Stan 2022-07-07 11:38:25 +03:00
parent 1dd51a2752
commit 615520bcb3

View file

@ -218,6 +218,8 @@
font-size: 25px; font-size: 25px;
} }
</style> </style>
<!-- todo: use config mempool -->
<script src="https://mempool.space/mempool.js"></script>
<script> <script>
Vue.component(VueQrcode.name, VueQrcode) Vue.component(VueQrcode.name, VueQrcode)
@ -246,30 +248,40 @@
this.cancelListener = LNbits.events.onInvoicePaid( this.cancelListener = LNbits.events.onInvoicePaid(
this.wallet, this.wallet,
payment => { payment => {
this.checkBalance() this.checkInvoiceBalance()
} }
) )
}, },
checkBalance: async function () { checkInvoiceBalance: async function () {
try { try {
const {data} = await LNbits.api const {data} = await LNbits.api.request(
.request(
'GET', 'GET',
`/satspay/api/v1/charges/balance/${this.charge.id}`, `/satspay/api/v1/charges/balance/${this.charge.id}`,
'filla' 'filla'
) )
this.charge.time_elapsed = data.time_elapsed this.charge.time_elapsed = data.time_elapsed
this.charge.amount = data.amount this.charge.amount = data.amount
this.charge.balance = data.balance this.charge.balance = data.balance
if (this.charge.balance >= this.charge.amount) { if (this.charge.balance >= this.charge.amount) {
this.charge.paid = true this.charge.paid = true
} }
} catch (error) { } catch (error) {
LNbits.utils.notifyApiError(error) LNbits.utils.notifyApiError(error)
} }
},
checkOnchainAddressBalance: async function () {
const {
bitcoin: {addresses: addressesAPI}
} = mempoolJS()
const fn = async () =>
addressesAPI.getAddressTxsUtxo({
address: charge.onchainaddress
})
const utxos = await retryWithDelay(fn)
charge.balance = utxos.reduce((t, u) => t + u.value, 0)
}, },
payLN: function () { payLN: function () {
@ -280,39 +292,35 @@
this.lnbtc = false this.lnbtc = false
this.onbtc = true this.onbtc = true
}, },
getTheTime: function () { refreshExpirationTime: function () {
var timeToComplete = this.timetoComplete =
parseInt('{{ charge.time }}') * 60 - parseInt('{{ charge.time }}') * 60 -
(Date.now() / 1000 - parseInt('{{ charge.timestamp }}')) (Date.now() / 1000 - parseInt('{{ charge.timestamp }}'))
this.timetoComplete = timeToComplete
var timeLeft = Quasar.utils.date.formatDate( this.newTimeLeft = Quasar.utils.date.formatDate(
new Date((timeToComplete - 3600) * 1000), new Date((this.timeToComplete - 3600) * 1000),
'HH:mm:ss' 'HH:mm:ss'
) )
this.newTimeLeft = timeLeft
}, },
getThePercentage: function () { refreshProgres: function () {
var timeToComplete = this.refreshExpirationTime()
parseInt('{{ charge.time }}') * 60 -
(Date.now() / 1000 - parseInt('{{ charge.timestamp }}'))
this.newProgress = this.newProgress =
1 - timeToComplete / (parseInt('{{ charge.time }}') * 60) 1 - this.timeToComplete / (parseInt('{{ charge.time }}') * 60)
}, },
loopRefresh: function () { // invoice only
timerCount: function () { const refreshIntervalId = setInterval(() => {
self = this console.log('### 1111')
var refreshIntervalId = setInterval(function () { if (this.charge.paid || this.timetoComplete < 1) {
if (self.charge.paid|| self.timetoComplete < 1) {
clearInterval(refreshIntervalId) clearInterval(refreshIntervalId)
} }
self.getTheTime() this.refreshProgres()
self.getThePercentage() this.counter++
self.counter++ if (this.counter % 10 === 0) {
if (self.counter % 10 === 0) { this.checkInvoiceBalance()
self.checkBalance()
} }
}, 1000) }, 1000)
} },
}, },
created: function () { created: function () {
console.log('### charge ', this.charge) console.log('### charge ', this.charge)
@ -322,11 +330,9 @@
// empty for onchain // empty for onchain
this.wallet.inkey = '{{ wallet_inkey }}' this.wallet.inkey = '{{ wallet_inkey }}'
this.getTheTime() this.refreshProgres()
this.getThePercentage()
var timerCount = this.timerCount
if (!this.charge.paid) { if (!this.charge.paid) {
timerCount() this.loopRefresh()
} }
this.startPaymentNotifier() this.startPaymentNotifier()
} }