From 97f4fabea109dea46dc3d69799c130c0dfdc11e4 Mon Sep 17 00:00:00 2001 From: Eneko Illarramendi Date: Fri, 24 Apr 2020 13:13:05 +0200 Subject: [PATCH] fix: create temporal payments to keep balance updated --- lnbits/core/services.py | 18 ++++++++++++------ lnbits/core/views/api.py | 2 +- lnbits/extensions/withdraw/views_api.py | 4 ++-- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lnbits/core/services.py b/lnbits/core/services.py index 7caa5b3..12183aa 100644 --- a/lnbits/core/services.py +++ b/lnbits/core/services.py @@ -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.") diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index 876d54a..f2728b2 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -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: diff --git a/lnbits/extensions/withdraw/views_api.py b/lnbits/extensions/withdraw/views_api.py index 780f222..219976a 100644 --- a/lnbits/extensions/withdraw/views_api.py +++ b/lnbits/extensions/withdraw/views_api.py @@ -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,