FIX: issues and improvements to frontend, add lnurlp/lnurladdress, remove split by tag feature (#4)

* deinitialize task
* rework of frontend
* add lnurl and lightningaddresses
* substract fee_reserve from external split, for potential routing fee, add a warning to ui
This commit is contained in:
dni ⚡ 2023-03-24 21:03:33 +01:00 committed by GitHub
commit 5bb234b797
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 269 additions and 344 deletions

View file

@ -1,63 +1,96 @@
from http import HTTPStatus
from typing import List
from fastapi import Depends, Request
from fastapi import Depends
from loguru import logger
from starlette.exceptions import HTTPException
from lnbits.core.crud import get_wallet, get_wallet_for_key
from lnbits.decorators import WalletTypeInfo, require_admin_key
from lnbits.decorators import WalletTypeInfo, check_admin, require_admin_key
from . import splitpayments_ext
from . import scheduled_tasks, splitpayments_ext
from .crud import get_targets, set_targets
from .models import Target, TargetPut
from .models import Target, TargetPutList
@splitpayments_ext.get("/api/v1/targets")
async def api_targets_get(wallet: WalletTypeInfo = Depends(require_admin_key)):
async def api_targets_get(
wallet: WalletTypeInfo = Depends(require_admin_key),
) -> List[Target]:
targets = await get_targets(wallet.wallet.id)
return [target.dict() for target in targets] or []
return targets or []
@splitpayments_ext.put("/api/v1/targets")
@splitpayments_ext.put("/api/v1/targets", status_code=HTTPStatus.OK)
async def api_targets_set(
req: Request, wal: WalletTypeInfo = Depends(require_admin_key)
):
body = await req.json()
targets = []
data = TargetPut.parse_obj(body["targets"])
for entry in data.__root__:
wallet = await get_wallet(entry.wallet)
if not wallet:
wallet = await get_wallet_for_key(entry.wallet, "invoice")
if not wallet:
target_put: TargetPutList,
source_wallet: WalletTypeInfo = Depends(require_admin_key),
) -> None:
try:
targets: List[Target] = []
for entry in target_put.targets:
if entry.wallet.find("@") < 0 and entry.wallet.find("LNURL") < 0:
wallet = await get_wallet(entry.wallet)
if not wallet:
wallet = await get_wallet_for_key(entry.wallet, "invoice")
if not wallet:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"Invalid wallet '{entry.wallet}'.",
)
if wallet.id == source_wallet.wallet.id:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Can't split to itself."
)
if entry.percent <= 0:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"Invalid wallet '{entry.wallet}'.",
detail=f"Invalid percent '{entry.percent}'.",
)
if wallet.id == wal.wallet.id:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Can't split to itself."
targets.append(
Target(
wallet=entry.wallet,
source=source_wallet.wallet.id,
percent=entry.percent,
alias=entry.alias,
)
)
if entry.percent < 0:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail=f"Invalid percent '{entry.percent}'.",
)
percent_sum = sum([target.percent for target in targets])
if percent_sum > 100:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Splitting over 100%"
)
targets.append(
Target(
wallet=wallet.id,
source=wal.wallet.id,
tag=entry.tag,
percent=entry.percent,
alias=entry.alias,
)
await set_targets(source_wallet.wallet.id, targets)
except Exception as ex:
logger.warning(ex)
raise HTTPException(
status_code=HTTPStatus.INTERNAL_SERVER_ERROR,
detail="Cannot set targets.",
)
percent_sum = sum([target.percent for target in targets])
if percent_sum > 100:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST, detail="Splitting over 100%."
)
await set_targets(wal.wallet.id, targets)
return ""
@splitpayments_ext.delete("/api/v1/targets", status_code=HTTPStatus.OK)
async def api_targets_delete(
source_wallet: WalletTypeInfo = Depends(require_admin_key),
) -> None:
await set_targets(source_wallet.wallet.id, [])
# deinit extension invoice listener
@splitpayments_ext.delete(
"/api/v1", status_code=HTTPStatus.OK, dependencies=[Depends(check_admin)]
)
async def api_stop():
for t in scheduled_tasks:
try:
t.cancel()
except Exception as ex:
logger.warning(ex)
return {"success": True}