Refactor get walletclass (#1776)

* move `get_wallet_class` to wallets module

* adjust imports, fix type issues

flake8
This commit is contained in:
jackstar12
2023-06-27 16:11:00 +02:00
committed by GitHub
parent 9a0878de19
commit bc55d52ea2
10 changed files with 45 additions and 29 deletions

View File

@ -29,7 +29,8 @@ from lnbits.core.tasks import ( # register_watchdog,; unregister_watchdog,
register_task_listeners, register_task_listeners,
unregister_killswitch, unregister_killswitch,
) )
from lnbits.settings import get_wallet_class, set_wallet_class, settings from lnbits.settings import settings
from lnbits.wallets import get_wallet_class, set_wallet_class
from .commands import db_versions, load_disabled_extension_list, migrate_databases from .commands import db_versions, load_disabled_extension_list, migrate_databases
from .core import ( from .core import (
@ -342,7 +343,6 @@ def register_shutdown(app: FastAPI):
def initialize_server_logger(): def initialize_server_logger():
super_user_hash = sha256(settings.super_user.encode("utf-8")).hexdigest() super_user_hash = sha256(settings.super_user.encode("utf-8")).hexdigest()
serverlog_queue = asyncio.Queue() serverlog_queue = asyncio.Queue()

View File

@ -13,7 +13,8 @@ from pydantic import BaseModel
from lnbits.db import Connection, FilterModel, FromRowModel from lnbits.db import Connection, FilterModel, FromRowModel
from lnbits.helpers import url_for from lnbits.helpers import url_for
from lnbits.settings import get_wallet_class, settings from lnbits.settings import settings
from lnbits.wallets import get_wallet_class
from lnbits.wallets.base import PaymentStatus from lnbits.wallets.base import PaymentStatus
@ -37,7 +38,6 @@ class Wallet(BaseModel):
@property @property
def lnurlwithdraw_full(self) -> str: def lnurlwithdraw_full(self) -> str:
url = url_for("/withdraw", external=True, usr=self.user, wal=self.id) url = url_for("/withdraw", external=True, usr=self.user, wal=self.id)
try: try:
return lnurl_encode(url) return lnurl_encode(url)

View File

@ -15,15 +15,13 @@ from lnbits.db import Connection
from lnbits.decorators import WalletTypeInfo, require_admin_key from lnbits.decorators import WalletTypeInfo, require_admin_key
from lnbits.helpers import url_for from lnbits.helpers import url_for
from lnbits.settings import ( from lnbits.settings import (
FAKE_WALLET,
EditableSettings, EditableSettings,
SuperSettings, SuperSettings,
get_wallet_class,
readonly_variables, readonly_variables,
send_admin_user_to_saas, send_admin_user_to_saas,
set_wallet_class,
settings, settings,
) )
from lnbits.wallets import FAKE_WALLET, get_wallet_class, set_wallet_class
from lnbits.wallets.base import PaymentResponse, PaymentStatus from lnbits.wallets.base import PaymentResponse, PaymentStatus
from . import db from . import db
@ -85,7 +83,7 @@ async def create_invoice(
unhashed_description=unhashed_description, unhashed_description=unhashed_description,
expiry=expiry or settings.lightning_invoice_expiry, expiry=expiry or settings.lightning_invoice_expiry,
) )
if not ok: if not ok or not payment_request or not checking_id:
raise InvoiceFailure(error_message or "unexpected backend error.") raise InvoiceFailure(error_message or "unexpected backend error.")
invoice = bolt11.decode(payment_request) invoice = bolt11.decode(payment_request)

View File

@ -15,7 +15,8 @@ from lnbits.core.helpers import to_valid_user_id
from lnbits.core.models import User from lnbits.core.models import User
from lnbits.decorators import check_admin, check_user_exists from lnbits.decorators import check_admin, check_user_exists
from lnbits.helpers import template_renderer, url_for from lnbits.helpers import template_renderer, url_for
from lnbits.settings import get_wallet_class, settings from lnbits.settings import settings
from lnbits.wallets import get_wallet_class
from ...extension_manager import InstallableExtension, get_valid_extensions from ...extension_manager import InstallableExtension, get_valid_extensions
from ...utils.exchange_rates import currencies from ...utils.exchange_rates import currencies

View File

@ -341,19 +341,6 @@ def set_cli_settings(**kwargs):
setattr(settings, key, value) setattr(settings, key, value)
# set wallet class after settings are loaded
def set_wallet_class(class_name: Optional[str] = None):
backend_wallet_class = class_name or settings.lnbits_backend_wallet_class
wallet_class = getattr(wallets_module, backend_wallet_class)
global WALLET
WALLET = wallet_class()
def get_wallet_class():
# wallet_class = getattr(wallets_module, settings.lnbits_backend_wallet_class)
return WALLET
def send_admin_user_to_saas(): def send_admin_user_to_saas():
if settings.lnbits_saas_callback: if settings.lnbits_saas_callback:
with httpx.Client() as client: with httpx.Client() as client:
@ -399,8 +386,11 @@ except:
settings.version = importlib.metadata.version("lnbits") settings.version = importlib.metadata.version("lnbits")
wallets_module = importlib.import_module("lnbits.wallets")
FAKE_WALLET = getattr(wallets_module, "FakeWallet")()
# initialize as fake wallet def get_wallet_class():
WALLET = FAKE_WALLET """
Backwards compatibility
"""
from lnbits.wallets import get_wallet_class
return get_wallet_class()

View File

@ -15,7 +15,7 @@ from lnbits.core.crud import (
get_standalone_payment, get_standalone_payment,
) )
from lnbits.core.services import redeem_lnurl_withdraw from lnbits.core.services import redeem_lnurl_withdraw
from lnbits.settings import get_wallet_class from lnbits.wallets import get_wallet_class
from .core import db from .core import db

View File

@ -1,4 +1,12 @@
from __future__ import annotations
# flake8: noqa: F401 # flake8: noqa: F401
import importlib
from typing import Optional
from lnbits.settings import settings
from lnbits.wallets.base import Wallet
from .cliche import ClicheWallet from .cliche import ClicheWallet
from .cln import CoreLightningWallet from .cln import CoreLightningWallet
from .cln import CoreLightningWallet as CLightningWallet from .cln import CoreLightningWallet as CLightningWallet
@ -12,3 +20,21 @@ from .lntips import LnTipsWallet
from .opennode import OpenNodeWallet from .opennode import OpenNodeWallet
from .spark import SparkWallet from .spark import SparkWallet
from .void import VoidWallet from .void import VoidWallet
def set_wallet_class(class_name: Optional[str] = None):
backend_wallet_class = class_name or settings.lnbits_backend_wallet_class
wallet_class = getattr(wallets_module, backend_wallet_class)
global WALLET
WALLET = wallet_class()
def get_wallet_class() -> Wallet:
return WALLET
wallets_module = importlib.import_module("lnbits.wallets")
FAKE_WALLET: Wallet = FakeWallet()
# initialize as fake wallet
WALLET: Wallet = FAKE_WALLET

View File

@ -61,6 +61,7 @@ class Wallet(ABC):
amount: int, amount: int,
memo: Optional[str] = None, memo: Optional[str] = None,
description_hash: Optional[bytes] = None, description_hash: Optional[bytes] = None,
**kwargs,
) -> Coroutine[None, None, InvoiceResponse]: ) -> Coroutine[None, None, InvoiceResponse]:
pass pass

View File

@ -9,7 +9,7 @@ from lnbits.core.models import Payment
from lnbits.core.views.admin_api import api_auditor from lnbits.core.views.admin_api import api_auditor
from lnbits.core.views.api import api_payment from lnbits.core.views.api import api_payment
from lnbits.db import DB_TYPE, SQLITE from lnbits.db import DB_TYPE, SQLITE
from lnbits.settings import get_wallet_class from lnbits.wallets import get_wallet_class
from tests.conftest import CreateInvoiceData, api_payments_create_invoice from tests.conftest import CreateInvoiceData, api_payments_create_invoice
from ...helpers import get_random_invoice_data, is_fake, pay_real_invoice from ...helpers import get_random_invoice_data, is_fake, pay_real_invoice

View File

@ -6,7 +6,7 @@ import string
from subprocess import PIPE, Popen, run from subprocess import PIPE, Popen, run
from lnbits.core.crud import create_payment from lnbits.core.crud import create_payment
from lnbits.settings import get_wallet_class, set_wallet_class from lnbits.wallets import get_wallet_class, set_wallet_class
async def credit_wallet(wallet_id: str, amount: int): async def credit_wallet(wallet_id: str, amount: int):