fix WALLET initialisation

This commit is contained in:
dni ⚡ 2022-10-05 09:46:59 +02:00
parent 4e8374ed6d
commit e670b53412
8 changed files with 23 additions and 11 deletions

View file

@ -16,7 +16,7 @@ from fastapi.staticfiles import StaticFiles
from loguru import logger
from lnbits.core.tasks import register_task_listeners
from lnbits.settings import WALLET, check_admin_settings, settings
from lnbits.settings import check_admin_settings, get_wallet_class, settings
from .commands import migrate_databases
from .core import core_app
@ -78,12 +78,15 @@ def create_app() -> FastAPI:
async def check_funding_source() -> None:
# original_sigint_handler = signal.getsignal(signal.SIGINT)
# def signal_handler(signal, frame):
# logger.debug(f"SIGINT received, terminating LNbits.")
# sys.exit(1)
# signal.signal(signal.SIGINT, signal_handler)
WALLET = get_wallet_class()
while True:
try:
error_message, balance = await WALLET.status()

View file

@ -11,7 +11,7 @@ from pydantic import BaseModel
from lnbits.db import Connection
from lnbits.helpers import url_for
from lnbits.settings import WALLET
from lnbits.settings import get_wallet_class
from lnbits.wallets.base import PaymentStatus
@ -163,6 +163,7 @@ class Payment(BaseModel):
f"Checking {'outgoing' if self.is_out else 'incoming'} pending payment {self.checking_id}"
)
WALLET = get_wallet_class()
if self.is_out:
status = await WALLET.get_payment_status(self.checking_id)
else:

View file

@ -21,7 +21,7 @@ from lnbits.decorators import (
)
from lnbits.helpers import url_for, urlsafe_short_hash
from lnbits.requestvars import g
from lnbits.settings import FAKE_WALLET, WALLET, settings
from lnbits.settings import FAKE_WALLET, get_wallet_class, settings
from lnbits.wallets.base import PaymentResponse, PaymentStatus
from . import db
@ -65,7 +65,7 @@ async def create_invoice(
invoice_memo = None if description_hash else memo
# use the fake wallet if the invoice is for internal use only
wallet = FAKE_WALLET if internal else WALLET
wallet = FAKE_WALLET if internal else get_wallet_class()
ok, checking_id, payment_request, error_message = await wallet.create_invoice(
amount=amount,
@ -193,6 +193,7 @@ async def pay_invoice(
else:
logger.debug(f"backend: sending payment {temp_id}")
# actually pay the external invoice
WALLET = get_wallet_class()
payment: PaymentResponse = await WALLET.pay_invoice(
payment_request, fee_reserve_msat
)

View file

@ -30,7 +30,7 @@ from lnbits.decorators import (
require_invoice_key,
)
from lnbits.helpers import url_for, urlsafe_short_hash
from lnbits.settings import WALLET, settings
from lnbits.settings import get_wallet_class, settings
from lnbits.utils.exchange_rates import (
currencies,
fiat_amount_as_satoshis,
@ -682,7 +682,7 @@ async def api_auditor(wallet: WalletTypeInfo = Depends(get_key_type)):
raise HTTPException(
status_code=HTTPStatus.FORBIDDEN, detail="Not an admin user"
)
WALLET = get_wallet_class()
total_balance = await get_total_balance()
error_message, node_balance = await WALLET.status()

View file

@ -9,7 +9,7 @@ from starlette.responses import HTMLResponse
from lnbits.core.models import User
from lnbits.decorators import check_admin
from lnbits.requestvars import g
from lnbits.settings import WALLET, settings
from lnbits.settings import get_wallet_class, settings
from . import admin_ext, admin_renderer
@ -18,6 +18,7 @@ templates = Jinja2Templates(directory="templates")
@admin_ext.get("/", response_class=HTMLResponse)
async def index(request: Request, user: User = Depends(check_admin)):
WALLET = get_wallet_class()
error, balance = await WALLET.status()
return admin_renderer().TemplateResponse(

View file

@ -12,7 +12,7 @@ from lnbits import bolt11
from lnbits.core.crud import delete_expired_invoices, get_payments
from lnbits.core.services import create_invoice, pay_invoice
from lnbits.decorators import WalletTypeInfo
from lnbits.settings import WALLET, settings
from lnbits.settings import get_wallet_class, settings
from . import lndhub_ext
from .decorators import check_wallet, require_admin_key
@ -175,6 +175,7 @@ async def lndhub_getuserinvoices(
offset=offset,
exclude_uncheckable=True,
):
WALLET = get_wallet_class()
await invoice.set_pending(
(await WALLET.get_invoice_status(invoice.checking_id)).pending
)

View file

@ -229,6 +229,9 @@ async def check_admin_settings():
wallets_module = importlib.import_module("lnbits.wallets")
wallet_class = getattr(wallets_module, settings.lnbits_backend_wallet_class)
WALLET = wallet_class()
FAKE_WALLET = getattr(wallets_module, "FakeWallet")()
def get_wallet_class():
wallet_class = getattr(wallets_module, settings.lnbits_backend_wallet_class)
return wallet_class()

View file

@ -15,7 +15,7 @@ from lnbits.core.crud import (
get_standalone_payment,
)
from lnbits.core.services import redeem_lnurl_withdraw
from lnbits.settings import WALLET
from lnbits.settings import get_wallet_class
from .core import db
@ -79,6 +79,7 @@ async def webhook_handler():
"""
Returns the webhook_handler for the selected wallet if present. Used by API.
"""
WALLET = get_wallet_class()
handler = getattr(WALLET, "webhook_listener", None)
if handler:
return await handler()
@ -108,6 +109,7 @@ async def invoice_listener():
Called by the app startup sequence.
"""
WALLET = get_wallet_class()
async for checking_id in WALLET.paid_invoices_stream():
logger.info("> got a payment notification", checking_id)
asyncio.create_task(invoice_callback_dispatcher(checking_id))