allow invoices to be generated using Fiat values

This commit is contained in:
Tiago vasconcelos 2021-06-11 16:48:13 +01:00 committed by fiatjaf
parent cfbd6fca3a
commit d91dbbcac3
4 changed files with 46 additions and 3 deletions

View File

@ -119,6 +119,8 @@ new Vue({
paymentHash: null, paymentHash: null,
minMax: [0, 2100000000000000], minMax: [0, 2100000000000000],
lnurl: null, lnurl: null,
units: ['sat'],
unit: 'sat',
data: { data: {
amount: null, amount: null,
memo: '' memo: ''
@ -233,6 +235,7 @@ new Vue({
this.receive.paymentHash = null this.receive.paymentHash = null
this.receive.data.amount = null this.receive.data.amount = null
this.receive.data.memo = null this.receive.data.memo = null
this.receive.unit = 'sat'
this.receive.paymentChecker = null this.receive.paymentChecker = null
this.receive.minMax = [0, 2100000000000000] this.receive.minMax = [0, 2100000000000000]
this.receive.lnurl = null this.receive.lnurl = null
@ -269,11 +272,13 @@ new Vue({
}, },
createInvoice: function () { createInvoice: function () {
this.receive.status = 'loading' this.receive.status = 'loading'
LNbits.api LNbits.api
.createInvoice( .createInvoice(
this.g.wallet, this.g.wallet,
this.receive.data.amount, this.receive.data.amount,
this.receive.data.memo, this.receive.data.memo,
this.receive.unit,
this.receive.lnurl && this.receive.lnurl.callback this.receive.lnurl && this.receive.lnurl.callback
) )
.then(response => { .then(response => {
@ -619,6 +624,15 @@ new Vue({
created: function () { created: function () {
this.fetchBalance() this.fetchBalance()
this.fetchPayments() this.fetchPayments()
LNbits.api
.request('GET', '/api/v1/currencies')
.then(response => {
this.receive.units = ['sat', ...response.data]
})
.catch(err => {
LNbits.utils.notifyApiError(err)
})
}, },
mounted: function () { mounted: function () {
// show disclaimer // show disclaimer

View File

@ -313,12 +313,21 @@
<b>{{receive.lnurl.domain}}</b> is requesting an invoice: <b>{{receive.lnurl.domain}}</b> is requesting an invoice:
</p> </p>
<q-select
filled
dense
v-model="receive.unit"
type="text"
label="Unit"
:options="receive.units"
></q-select>
<q-input <q-input
filled filled
dense dense
v-model.number="receive.data.amount" v-model.number="receive.data.amount"
type="number" type="number"
label="Amount (sat) *" :label="`Amount (${receive.unit}) *`"
:step="receive.unit != 'sat' ? '0.001' : '1'"
:min="receive.minMax[0]" :min="receive.minMax[0]"
:max="receive.minMax[1]" :max="receive.minMax[1]"
:readonly="receive.lnurl && receive.lnurl.fixed" :readonly="receive.lnurl && receive.lnurl.fixed"

View File

@ -10,6 +10,7 @@ from typing import Dict, Union
from lnbits import bolt11 from lnbits import bolt11
from lnbits.decorators import api_check_wallet_key, api_validate_post_request from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.utils.exchange_rates import currencies, fiat_amount_as_satoshis
from .. import core_app, db from .. import core_app, db
from ..crud import save_balance_check from ..crud import save_balance_check
@ -47,13 +48,14 @@ async def api_payments():
@api_check_wallet_key("invoice") @api_check_wallet_key("invoice")
@api_validate_post_request( @api_validate_post_request(
schema={ schema={
"amount": {"type": "integer", "min": 1, "required": True}, "amount": {"type": "number", "min": 0.001, "required": True},
"memo": { "memo": {
"type": "string", "type": "string",
"empty": False, "empty": False,
"required": True, "required": True,
"excludes": "description_hash", "excludes": "description_hash",
}, },
"unit": {"type": "string", "empty": False, "required": True},
"description_hash": { "description_hash": {
"type": "string", "type": "string",
"empty": False, "empty": False,
@ -74,6 +76,13 @@ async def api_payments_create_invoice():
description_hash = b"" description_hash = b""
memo = g.data["memo"] memo = g.data["memo"]
#convert fiat to satoshis
if g.data["unit"] != 'sat':
print(g.data["amount"])
price_in_sats = await fiat_amount_as_satoshis(g.data["amount"], g.data["unit"])
g.data["amount"] = price_in_sats
print(g.data["amount"], price_in_sats)
async with db.connect() as conn: async with db.connect() as conn:
try: try:
payment_hash, payment_request = await create_invoice( payment_hash, payment_request = await create_invoice(
@ -435,3 +444,7 @@ async def api_perform_lnurlauth():
if err: if err:
return jsonify({"reason": err.reason}), HTTPStatus.SERVICE_UNAVAILABLE return jsonify({"reason": err.reason}), HTTPStatus.SERVICE_UNAVAILABLE
return "", HTTPStatus.OK return "", HTTPStatus.OK
@core_app.route("/api/v1/currencies", methods=["GET"])
async def api_list_currencies_available():
return jsonify(list(currencies.keys()))

View File

@ -14,11 +14,18 @@ window.LNbits = {
data: data data: data
}) })
}, },
createInvoice: function (wallet, amount, memo, lnurlCallback = null) { createInvoice: async function (
wallet,
amount,
memo,
unit = 'sat',
lnurlCallback = null
) {
return this.request('post', '/api/v1/payments', wallet.inkey, { return this.request('post', '/api/v1/payments', wallet.inkey, {
out: false, out: false,
amount: amount, amount: amount,
memo: memo, memo: memo,
unit: unit,
lnurl_callback: lnurlCallback lnurl_callback: lnurlCallback
}) })
}, },