fix: create temporal payments to keep balance updated

This commit is contained in:
Eneko Illarramendi 2020-04-24 13:13:05 +02:00
parent 2c30209cba
commit 97f4fabea1
3 changed files with 15 additions and 9 deletions

View File

@ -1,10 +1,10 @@
from typing import Optional, Tuple
from lnbits.bolt11 import decode as bolt11_decode
from lnbits.helpers import urlsafe_short_hash
from lnbits.settings import WALLET
from .crud import create_payment
from .models import Wallet
from .crud import get_wallet, create_payment, delete_payment
def create_invoice(*, wallet_id: str, amount: int, memo: str) -> Tuple[str, str]:
@ -22,10 +22,11 @@ def create_invoice(*, wallet_id: str, amount: int, memo: str) -> Tuple[str, str]
return checking_id, payment_request
def pay_invoice(*, wallet: Wallet, bolt11: str, max_sat: Optional[int] = None) -> str:
def pay_invoice(*, wallet_id: str, bolt11: str, max_sat: Optional[int] = None) -> str:
temp_id = f"temp_{urlsafe_short_hash()}"
try:
invoice = bolt11_decode(bolt11)
ok, checking_id, fee_msat, error_message = WALLET.pay_invoice(bolt11)
if invoice.amount_msat == 0:
raise ValueError("Amountless invoices not supported.")
@ -33,17 +34,22 @@ def pay_invoice(*, wallet: Wallet, bolt11: str, max_sat: Optional[int] = None) -
if max_sat and invoice.amount_msat > max_sat * 1000:
raise ValueError("Amount in invoice is too high.")
if invoice.amount_msat > wallet.balance_msat:
if invoice.amount_msat > get_wallet(wallet_id).balance_msat:
raise PermissionError("Insufficient balance.")
create_payment(wallet_id=wallet_id, checking_id=temp_id, amount=-invoice.amount_msat, memo=temp_id)
ok, checking_id, fee_msat, error_message = WALLET.pay_invoice(bolt11)
if ok:
create_payment(
wallet_id=wallet.id, checking_id=checking_id, amount=-invoice.amount_msat, memo=invoice.description
wallet_id=wallet_id, checking_id=checking_id, amount=-invoice.amount_msat, memo=invoice.description
)
except Exception as e:
ok, error_message = False, str(e)
delete_payment(temp_id)
if not ok:
raise Exception(error_message or "Unexpected backend error.")

View File

@ -45,7 +45,7 @@ def api_payments_create_invoice():
@api_validate_post_request(schema={"bolt11": {"type": "string", "empty": False, "required": True}})
def api_payments_pay_invoice():
try:
checking_id = pay_invoice(wallet=g.wallet, bolt11=g.data["bolt11"])
checking_id = pay_invoice(wallet_id=g.wallet.id, bolt11=g.data["bolt11"])
except ValueError as e:
return jsonify({"message": str(e)}), Status.BAD_REQUEST
except PermissionError as e:

View File

@ -2,7 +2,7 @@ from datetime import datetime
from flask import g, jsonify, request
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
from lnbits.core.crud import get_user, get_wallet
from lnbits.core.crud import get_user
from lnbits.core.services import pay_invoice
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
from lnbits.helpers import urlsafe_short_hash, Status
@ -136,7 +136,7 @@ def api_lnurl_callback(unique_hash):
return jsonify({"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}), Status.OK
try:
pay_invoice(wallet=get_wallet(link.wallet), bolt11=payment_request, max_sat=link.max_withdrawable)
pay_invoice(wallet_id=link.wallet, bolt11=payment_request, max_sat=link.max_withdrawable)
changes = {
"used": link.used + 1,