Browse Source

Admin top up complete

adminvar
benarc 4 years ago
parent
commit
7ba506923e
  1. 14
      lnbits/core/services.py
  2. 28
      lnbits/core/static/js/wallet.js
  3. 34
      lnbits/core/templates/core/wallet.html
  4. 18
      lnbits/core/views/api.py
  5. 5
      lnbits/static/js/base.js

14
lnbits/core/services.py

@ -125,3 +125,17 @@ def check_invoice_status(wallet_id: str, payment_hash: str) -> PaymentStatus:
return PaymentStatus(None)
return WALLET.get_invoice_status(payment.checking_id)
def update_wallet_balance(wallet_id: str, amount: int) -> str:
temp_id = f"temp_{urlsafe_short_hash()}"
internal_id = f"internal_{urlsafe_short_hash()}"
create_payment(
wallet_id=wallet_id,
checking_id=internal_id,
payment_request="admin_internal",
payment_hash="admin_internal",
amount=amount*1000,
memo="admin_internal",
pending=False,
)
return "admin_internal"

28
lnbits/core/static/js/wallet.js

@ -127,6 +127,12 @@ new Vue({
memo: ''
}
},
update: {
show: false,
data: {
amount: null
}
},
send: {
show: false,
invoice: null,
@ -241,6 +247,14 @@ new Vue({
paymentChecker: null
}
},
showUpdateDialog: function () {
this.update = {
show: true,
data: {
amount: null
}
}
},
closeReceiveDialog: function () {
var checker = this.receive.paymentChecker
setTimeout(function () {
@ -284,6 +298,20 @@ new Vue({
self.receive.status = 'pending'
})
},
updateBalance: function () {
var self = this
self.receive.status = 'loading'
LNbits.api
.updateBalance(self.g.wallet, self.update.data.amount)
.then(function (response) {
self.fetchPayments()
self.update.show = false
})
.catch(function (error) {
LNbits.utils.notifyApiError(error)
self.receive.status = 'pending'
})
},
decodeQR: function (res) {
this.send.data.bolt11 = res
this.decodeInvoice()

34
lnbits/core/templates/core/wallet.html

@ -25,8 +25,14 @@
<h3 class="q-my-none">
<strong>{% raw %}{{ fbalance }}{% endraw %}</strong> sat {% if
admin %}
<q-btn round size="10px" color="purple" icon="add">
<q-tooltip> Update balance </q-tooltip>
<q-btn
round
size="10px"
color="purple"
icon="add"
@click="showUpdateDialog"
>
<q-tooltip> Admin top up </q-tooltip>
</q-btn>
{% endif %}
</h3>
@ -251,6 +257,30 @@
</div>
</div>
<q-dialog v-model="update.show" position="top">
<q-card class="q-pa-lg q-pt-xl lnbits__dialog-card">
<q-form @submit="updateBalance" class="q-gutter-md">
<q-input
filled
dense
v-model.number="update.data.amount"
type="number"
label="Amount (sat) *"
></q-input>
<div class="row q-mt-lg">
<q-btn
unelevated
color="deep-purple"
:disable="update.data.amount == null || update.data.amount <= 0"
type="submit"
>Top up</q-btn
>
<q-btn v-close-popup flat color="grey" class="q-ml-auto">Cancel</q-btn>
</div>
</q-form>
</q-card>
</q-dialog>
<q-dialog v-model="receive.show" position="top" @hide="closeReceiveDialog">
<q-card
v-if="!receive.paymentReq"

18
lnbits/core/views/api.py

@ -4,11 +4,10 @@ from binascii import unhexlify
from lnbits import bolt11
from lnbits.core import core_app
from lnbits.core.services import create_invoice, pay_invoice
from lnbits.core.services import create_invoice, pay_invoice, update_wallet_balance
from lnbits.core.crud import delete_expired_invoices
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.settings import WALLET
from lnbits.settings import WALLET, LNBITS_ADMIN_USERS
@core_app.route("/api/v1/payments", methods=["GET"])
@api_check_wallet_key("invoice")
@ -122,3 +121,16 @@ def api_payment(payment_hash):
return jsonify({"paid": True}), HTTPStatus.OK
return jsonify({"paid": False}), HTTPStatus.OK
@core_app.route("/api/v1/balance", methods=["POST"])
@api_check_wallet_key("admin")
@api_validate_post_request(
schema={
"amount": {"type": "integer", "min": 1, "required": True}
}
)
def api_update_balance():
print(g.wallet.user)
if g.wallet.user in LNBITS_ADMIN_USERS:
return update_wallet_balance(g.wallet.id, g.data["amount"])
return jsonify({"message": "Not an admin wallet"}), HTTPStatus.INTERNAL_SERVER_ERROR

5
lnbits/static/js/base.js

@ -43,6 +43,11 @@ var LNbits = {
'/api/v1/payments/' + paymentHash,
wallet.inkey
)
},
updateBalance: function (wallet, amount) {
return this.request('post', '/api/v1/balance', wallet.adminkey, {
amount: amount
})
}
},
href: {

Loading…
Cancel
Save