diff --git a/lnbits/extensions/satspay/crud.py b/lnbits/extensions/satspay/crud.py index 6c0c1cb3..968c9ab0 100644 --- a/lnbits/extensions/satspay/crud.py +++ b/lnbits/extensions/satspay/crud.py @@ -1,7 +1,6 @@ import json from typing import List, Optional -import httpx from loguru import logger from lnbits.core.services import create_invoice @@ -9,9 +8,8 @@ from lnbits.core.views.api import api_payment from lnbits.helpers import urlsafe_short_hash from ..watchonly.crud import get_config, get_fresh_address - -# from lnbits.db import open_ext_db from . import db +from .helpers import fetch_onchain_balance from .models import Charges, CreateCharge ###############CHARGES########################## @@ -111,19 +109,10 @@ async def check_address_balance(charge_id: str) -> Optional[Charges]: if not charge.paid: if charge.onchainaddress: - endpoint = ( - f"{charge.config['mempool_endpoint']}/testnet" - if charge.config["network"] == "Testnet" - else charge.config["mempool_endpoint"] - ) try: - async with httpx.AsyncClient() as client: - r = await client.get( - endpoint + "/api/address/" + charge.onchainaddress - ) - respAmount = r.json()["chain_stats"]["funded_txo_sum"] - if respAmount > charge.balance: - await update_charge(charge_id=charge_id, balance=respAmount) + respAmount = await fetch_onchain_balance(charge) + if respAmount > charge.balance: + await update_charge(charge_id=charge_id, balance=respAmount) except Exception as e: logger.warning(e) if charge.lnbitswallet: diff --git a/lnbits/extensions/satspay/helpers.py b/lnbits/extensions/satspay/helpers.py index 1ebaa062..6862c608 100644 --- a/lnbits/extensions/satspay/helpers.py +++ b/lnbits/extensions/satspay/helpers.py @@ -1,3 +1,6 @@ +import httpx +from loguru import logger + from .models import Charges @@ -22,3 +25,29 @@ def public_charge(charge: Charges): c["completelink"] = charge.completelink return c + + +async def call_webhook(charge: Charges): + async with httpx.AsyncClient() as client: + try: + r = await client.post( + charge.webhook, + json=public_charge(charge), + timeout=40, + ) + except AssertionError: + charge.webhook = None + except Exception as e: + logger.warning(f"Failed to call webhook for charge {charge.id}") + logger.warning(e) + + +async def fetch_onchain_balance(charge: Charges): + endpoint = ( + f"{charge.config['mempool_endpoint']}/testnet" + if charge.config["network"] == "Testnet" + else charge.config["mempool_endpoint"] + ) + async with httpx.AsyncClient() as client: + r = await client.get(endpoint + "/api/address/" + charge.onchainaddress) + return r.json()["chain_stats"]["funded_txo_sum"] diff --git a/lnbits/extensions/satspay/tasks.py b/lnbits/extensions/satspay/tasks.py index 3a77501b..27eb879a 100644 --- a/lnbits/extensions/satspay/tasks.py +++ b/lnbits/extensions/satspay/tasks.py @@ -1,6 +1,5 @@ import asyncio -import httpx from loguru import logger from lnbits.core.models import Payment @@ -8,8 +7,7 @@ from lnbits.extensions.satspay.crud import check_address_balance, get_charge from lnbits.helpers import get_current_extension_name from lnbits.tasks import register_invoice_listener -from .helpers import public_charge -from .models import Charges +from .helpers import call_webhook async def wait_for_paid_invoices(): @@ -36,18 +34,3 @@ async def on_invoice_paid(payment: Payment) -> None: if charge.paid and charge.webhook: await call_webhook(charge) - - -async def call_webhook(charge: Charges): - async with httpx.AsyncClient() as client: - try: - r = await client.post( - charge.webhook, - json=public_charge(charge), - timeout=40, - ) - except AssertionError: - charge.webhook = None - except Exception as e: - logger.warning(f"Failed to call webhook for charge {charge.id}") - logger.warning(e)