lnbits-legend/lnbits/core/static/js/wallet.js

428 lines
10 KiB
JavaScript
Raw Normal View History

/* globals decode, Vue, VueQrcodeReader, VueQrcode, Quasar, LNbits, _, EventHub, Chart */
2020-05-03 13:55:17 -03:00
Vue.component(VueQrcode.name, VueQrcode)
Vue.use(VueQrcodeReader)
2020-03-05 20:29:27 +01:00
function generateChart(canvas, payments) {
2020-05-03 13:55:17 -03:00
var txs = []
var n = 0
2020-03-05 20:29:27 +01:00
var data = {
labels: [],
income: [],
outcome: [],
2020-03-05 20:29:27 +01:00
cumulative: []
2020-05-03 13:55:17 -03:00
}
2020-03-05 20:29:27 +01:00
2020-05-03 13:55:17 -03:00
_.each(
payments
.filter(p => !p.pending)
.sort(function (a, b) {
return a.time - b.time
}),
function (tx) {
2020-05-03 13:55:17 -03:00
txs.push({
hour: Quasar.utils.date.formatDate(tx.date, 'YYYY-MM-DDTHH:00'),
sat: tx.sat
})
}
)
2020-03-05 20:29:27 +01:00
_.each(_.groupBy(txs, 'hour'), function (value, day) {
2020-05-03 13:55:17 -03:00
var income = _.reduce(
value,
function (memo, tx) {
2020-05-03 13:55:17 -03:00
return tx.sat >= 0 ? memo + tx.sat : memo
},
0
)
var outcome = _.reduce(
value,
function (memo, tx) {
2020-05-03 13:55:17 -03:00
return tx.sat < 0 ? memo + Math.abs(tx.sat) : memo
},
0
)
n = n + income - outcome
data.labels.push(day)
data.income.push(income)
data.outcome.push(outcome)
data.cumulative.push(n)
})
2020-03-05 20:29:27 +01:00
new Chart(canvas.getContext('2d'), {
type: 'bar',
data: {
labels: data.labels,
datasets: [
{
data: data.cumulative,
type: 'line',
label: 'balance',
2020-05-03 13:55:17 -03:00
backgroundColor: '#673ab7', // deep-purple
borderColor: '#673ab7',
2020-03-05 20:29:27 +01:00
borderWidth: 4,
pointRadius: 3,
fill: false
},
{
data: data.income,
2020-03-05 20:29:27 +01:00
type: 'bar',
label: 'in',
barPercentage: 0.75,
backgroundColor: window.Color('rgb(76,175,80)').alpha(0.5).rgbString() // green
},
{
data: data.outcome,
type: 'bar',
label: 'out',
barPercentage: 0.75,
backgroundColor: window.Color('rgb(233,30,99)').alpha(0.5).rgbString() // pink
2020-03-05 20:29:27 +01:00
}
]
},
options: {
title: {
text: 'Chart.js Combo Time Scale'
},
tooltips: {
mode: 'index',
2020-05-03 13:55:17 -03:00
intersect: false
2020-03-05 20:29:27 +01:00
},
scales: {
2020-05-03 13:55:17 -03:00
xAxes: [
{
type: 'time',
display: true,
offset: true,
time: {
minUnit: 'hour',
stepSize: 3
}
2020-03-05 20:29:27 +01:00
}
2020-05-03 13:55:17 -03:00
]
2020-03-05 20:29:27 +01:00
},
// performance tweaks
animation: {
duration: 0
},
elements: {
line: {
tension: 0
}
}
2020-03-05 20:29:27 +01:00
}
2020-05-03 13:55:17 -03:00
})
2020-03-05 20:29:27 +01:00
}
new Vue({
el: '#vue',
mixins: [windowMixin],
data: function () {
return {
user: LNbits.map.user(window.user),
receive: {
show: false,
status: 'pending',
paymentReq: null,
data: {
amount: null,
memo: ''
}
},
send: {
show: false,
invoice: null,
data: {
bolt11: ''
}
},
2020-03-10 23:12:22 +01:00
sendCamera: {
show: false,
camera: 'auto'
},
payments: [],
paymentsTable: {
columns: [
{
name: 'memo',
align: 'left',
label: 'Memo',
field: 'memo'
},
2020-05-03 13:55:17 -03:00
{
name: 'date',
align: 'left',
label: 'Date',
field: 'date',
sortable: true
},
{
name: 'sat',
align: 'right',
label: 'Amount (sat)',
field: 'sat',
sortable: true
}
],
pagination: {
rowsPerPage: 10
},
filter: null
2020-03-05 20:29:27 +01:00
},
paymentsChart: {
2020-03-05 20:29:27 +01:00
show: false
},
disclaimerDialog: {
show: false,
location: window.location
2020-09-29 20:04:02 -03:00
},
balance: 0
2020-05-03 13:55:17 -03:00
}
},
computed: {
2020-09-29 20:04:02 -03:00
formattedBalance: function () {
return LNbits.utils.formatSat(this.balance || this.g.wallet.sat)
},
filteredPayments: function () {
2020-05-03 13:55:17 -03:00
var q = this.paymentsTable.filter
if (!q || q === '') return this.payments
2020-05-03 13:55:17 -03:00
return LNbits.utils.search(this.payments, q)
},
canPay: function () {
2020-05-03 13:55:17 -03:00
if (!this.send.invoice) return false
return this.send.invoice.sat <= this.balance
},
pendingPaymentsExist: function () {
2020-05-03 13:55:17 -03:00
return this.payments
? _.where(this.payments, {pending: 1}).length > 0
2020-05-03 13:55:17 -03:00
: false
}
},
methods: {
closeCamera: function () {
2020-05-03 13:55:17 -03:00
this.sendCamera.show = false
2020-03-10 23:12:22 +01:00
},
showCamera: function () {
2020-05-03 13:55:17 -03:00
this.sendCamera.show = true
2020-03-10 23:12:22 +01:00
},
showChart: function () {
2020-05-03 13:55:17 -03:00
this.paymentsChart.show = true
this.$nextTick(function () {
2020-05-03 13:55:17 -03:00
generateChart(this.$refs.canvas, this.payments)
})
},
showReceiveDialog: function () {
this.receive = {
show: true,
status: 'pending',
paymentReq: null,
data: {
amount: null,
memo: ''
},
paymentChecker: null
2020-05-03 13:55:17 -03:00
}
},
showSendDialog: function () {
this.send = {
show: true,
invoice: null,
data: {
bolt11: ''
2020-03-10 23:12:22 +01:00
},
paymentChecker: null
2020-05-03 13:55:17 -03:00
}
},
closeReceiveDialog: function () {
2020-05-03 13:55:17 -03:00
var checker = this.receive.paymentChecker
setTimeout(function () {
2020-05-03 13:55:17 -03:00
clearInterval(checker)
}, 10000)
2020-03-05 20:29:27 +01:00
},
closeSendDialog: function () {
2020-05-03 13:55:17 -03:00
this.sendCamera.show = false
var checker = this.send.paymentChecker
setTimeout(function () {
2020-05-03 13:55:17 -03:00
clearInterval(checker)
}, 1000)
2020-03-10 23:12:22 +01:00
},
createInvoice: function () {
2020-05-03 13:55:17 -03:00
var self = this
this.receive.status = 'loading'
LNbits.api
.createInvoice(
this.g.wallet,
this.receive.data.amount,
this.receive.data.memo
)
.then(function (response) {
2020-05-03 13:55:17 -03:00
self.receive.status = 'success'
self.receive.paymentReq = response.data.payment_request
self.receive.paymentChecker = setInterval(function () {
2020-05-03 13:55:17 -03:00
LNbits.api
.getPayment(self.g.wallet, response.data.payment_hash)
.then(function (response) {
2020-05-03 13:55:17 -03:00
if (response.data.paid) {
self.fetchPayments()
self.receive.show = false
clearInterval(self.receive.paymentChecker)
}
})
}, 2000)
})
.catch(function (error) {
2020-05-03 13:55:17 -03:00
LNbits.utils.notifyApiError(error)
self.receive.status = 'pending'
})
},
decodeQR: function (res) {
2020-05-03 13:55:17 -03:00
this.send.data.bolt11 = res
this.decodeInvoice()
this.sendCamera.show = false
2020-03-10 23:12:22 +01:00
},
decodeInvoice: function () {
2020-04-27 23:13:42 +02:00
if (this.send.data.bolt11.startsWith('lightning:')) {
2020-05-03 13:55:17 -03:00
this.send.data.bolt11 = this.send.data.bolt11.slice(10)
2020-04-27 23:13:42 +02:00
}
2020-05-03 13:55:17 -03:00
let invoice
try {
2020-05-03 13:55:17 -03:00
invoice = decode(this.send.data.bolt11)
} catch (error) {
this.$q.notify({
timeout: 3000,
type: 'warning',
message: error + '.',
caption: '400 BAD REQUEST',
icon: null
2020-05-03 13:55:17 -03:00
})
return
}
2020-04-27 23:13:42 +02:00
let cleanInvoice = {
msat: invoice.human_readable_part.amount,
sat: invoice.human_readable_part.amount / 1000,
fsat: LNbits.utils.formatSat(invoice.human_readable_part.amount / 1000)
2020-05-03 13:55:17 -03:00
}
_.each(invoice.data.tags, function (tag) {
if (_.isObject(tag) && _.has(tag, 'description')) {
if (tag.description === 'payment_hash') {
2020-05-03 13:55:17 -03:00
cleanInvoice.hash = tag.value
} else if (tag.description === 'description') {
2020-05-03 13:55:17 -03:00
cleanInvoice.description = tag.value
} else if (tag.description === 'expiry') {
2020-05-03 13:55:17 -03:00
var expireDate = new Date(
(invoice.data.time_stamp + tag.value) * 1000
)
cleanInvoice.expireDate = Quasar.utils.date.formatDate(
expireDate,
'YYYY-MM-DDTHH:mm:ss.SSSZ'
)
cleanInvoice.expired = false // TODO
}
}
2020-05-03 13:55:17 -03:00
})
2020-05-03 13:55:17 -03:00
this.send.invoice = Object.freeze(cleanInvoice)
},
payInvoice: function () {
2020-05-03 13:55:17 -03:00
var self = this
let dismissPaymentMsg = this.$q.notify({
timeout: 0,
message: 'Processing payment...',
icon: null
2020-05-03 13:55:17 -03:00
})
2020-05-03 13:55:17 -03:00
LNbits.api
.payInvoice(this.g.wallet, this.send.data.bolt11)
.then(function (response) {
self.send.paymentChecker = setInterval(function () {
2020-05-03 13:55:17 -03:00
LNbits.api
.getPayment(self.g.wallet, response.data.payment_hash)
.then(function (res) {
2020-05-03 13:55:17 -03:00
if (res.data.paid) {
self.send.show = false
clearInterval(self.send.paymentChecker)
dismissPaymentMsg()
self.fetchPayments()
}
})
}, 2000)
})
.catch(function (error) {
2020-05-03 13:55:17 -03:00
dismissPaymentMsg()
LNbits.utils.notifyApiError(error)
})
},
deleteWallet: function (walletId, user) {
2020-05-03 13:55:17 -03:00
LNbits.utils
.confirmDialog('Are you sure you want to delete this wallet?')
.onOk(function () {
2020-05-03 13:55:17 -03:00
LNbits.href.deleteWallet(walletId, user)
})
},
fetchPayments: function (checkPending) {
2020-05-03 13:55:17 -03:00
var self = this
2020-05-03 13:55:17 -03:00
return LNbits.api
.getPayments(this.g.wallet, checkPending)
.then(function (response) {
2020-05-03 13:55:17 -03:00
self.payments = response.data
.map(function (obj) {
2020-05-03 13:55:17 -03:00
return LNbits.map.payment(obj)
})
.sort(function (a, b) {
2020-05-03 13:55:17 -03:00
return b.time - a.time
})
})
},
2020-09-29 20:04:02 -03:00
fetchBalance: function () {
var self = this
LNbits.api.getWallet(self.g.wallet).then(function (response) {
self.balance = Math.round(response.data.balance / 1000)
EventHub.$emit('update-wallet-balance', [
self.g.wallet.id,
self.balance
])
})
},
checkPendingPayments: function () {
var dismissMsg = this.$q.notify({
timeout: 0,
message: 'Checking pending transactions...',
icon: null
2020-05-03 13:55:17 -03:00
})
this.fetchPayments(true).then(function () {
2020-05-03 13:55:17 -03:00
dismissMsg()
})
2020-04-01 22:18:46 +02:00
},
exportCSV: function () {
2020-05-03 13:55:17 -03:00
LNbits.utils.exportCSV(this.paymentsTable.columns, this.payments)
2020-04-01 22:18:46 +02:00
}
},
watch: {
payments: function () {
2020-09-29 20:04:02 -03:00
this.fetchBalance()
}
},
created: function () {
2020-09-29 20:04:02 -03:00
this.fetchBalance()
2020-05-03 13:55:17 -03:00
this.fetchPayments()
setTimeout(this.checkPendingPayments(), 1200)
},
mounted: function () {
2020-05-03 13:55:17 -03:00
if (
this.$refs.disclaimer &&
!this.$q.localStorage.getItem('lnbits.disclaimerShown')
) {
this.disclaimerDialog.show = true
this.$q.localStorage.set('lnbits.disclaimerShown', true)
2020-04-21 23:47:16 +02:00
}
}
2020-05-03 13:55:17 -03:00
})