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,
minMax: [0, 2100000000000000],
lnurl: null,
units: ['sat'],
unit: 'sat',
data: {
amount: null,
memo: ''
@ -233,6 +235,7 @@ new Vue({
this.receive.paymentHash = null
this.receive.data.amount = null
this.receive.data.memo = null
this.receive.unit = 'sat'
this.receive.paymentChecker = null
this.receive.minMax = [0, 2100000000000000]
this.receive.lnurl = null
@ -269,11 +272,13 @@ new Vue({
},
createInvoice: function () {
this.receive.status = 'loading'
LNbits.api
.createInvoice(
this.g.wallet,
this.receive.data.amount,
this.receive.data.memo,
this.receive.unit,
this.receive.lnurl && this.receive.lnurl.callback
)
.then(response => {
@ -619,6 +624,15 @@ new Vue({
created: function () {
this.fetchBalance()
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 () {
// show disclaimer

View File

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

View File

@ -10,6 +10,7 @@ from typing import Dict, Union
from lnbits import bolt11
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 ..crud import save_balance_check
@ -47,13 +48,14 @@ async def api_payments():
@api_check_wallet_key("invoice")
@api_validate_post_request(
schema={
"amount": {"type": "integer", "min": 1, "required": True},
"amount": {"type": "number", "min": 0.001, "required": True},
"memo": {
"type": "string",
"empty": False,
"required": True,
"excludes": "description_hash",
},
"unit": {"type": "string", "empty": False, "required": True},
"description_hash": {
"type": "string",
"empty": False,
@ -74,6 +76,13 @@ async def api_payments_create_invoice():
description_hash = b""
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:
try:
payment_hash, payment_request = await create_invoice(
@ -435,3 +444,7 @@ async def api_perform_lnurlauth():
if err:
return jsonify({"reason": err.reason}), HTTPStatus.SERVICE_UNAVAILABLE
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
})
},
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, {
out: false,
amount: amount,
memo: memo,
unit: unit,
lnurl_callback: lnurlCallback
})
},