lnbits-legend/lnbits/extensions/lnurlpayout/crud.py

63 lines
1.8 KiB
Python
Raw Normal View History

2021-11-28 18:11:35 +00:00
from typing import List, Optional, Union
from lnbits.helpers import urlsafe_short_hash
from . import db
2022-07-16 14:23:03 +02:00
from .models import CreateLnurlPayoutData, lnurlpayout
2021-11-28 18:11:35 +00:00
2022-01-30 19:43:30 +00:00
async def create_lnurlpayout(
wallet_id: str, admin_key: str, data: CreateLnurlPayoutData
) -> lnurlpayout:
2021-11-28 18:11:35 +00:00
lnurlpayout_id = urlsafe_short_hash()
await db.execute(
"""
2021-12-17 12:15:33 +00:00
INSERT INTO lnurlpayout.lnurlpayouts (id, title, wallet, admin_key, lnurlpay, threshold)
2021-12-19 22:28:19 +00:00
VALUES (?, ?, ?, ?, ?, ?)
2021-11-28 18:11:35 +00:00
""",
2022-01-30 19:43:30 +00:00
(
lnurlpayout_id,
data.title,
wallet_id,
admin_key,
data.lnurlpay,
data.threshold,
),
2021-11-28 18:11:35 +00:00
)
lnurlpayout = await get_lnurlpayout(lnurlpayout_id)
assert lnurlpayout, "Newly created lnurlpayout couldn't be retrieved"
return lnurlpayout
async def get_lnurlpayout(lnurlpayout_id: str) -> Optional[lnurlpayout]:
2022-01-30 19:43:30 +00:00
row = await db.fetchone(
"SELECT * FROM lnurlpayout.lnurlpayouts WHERE id = ?", (lnurlpayout_id,)
)
2021-12-02 22:56:31 +00:00
return lnurlpayout(**row) if row else None
2021-11-28 18:11:35 +00:00
2022-01-30 19:43:30 +00:00
2021-12-08 11:43:13 +00:00
async def get_lnurlpayout_from_wallet(wallet_id: str) -> Optional[lnurlpayout]:
2022-01-30 19:43:30 +00:00
row = await db.fetchone(
"SELECT * FROM lnurlpayout.lnurlpayouts WHERE wallet = ?", (wallet_id,)
)
2021-12-08 11:43:13 +00:00
return lnurlpayout(**row) if row else None
2022-01-30 19:43:30 +00:00
2021-11-28 18:11:35 +00:00
async def get_lnurlpayouts(wallet_ids: Union[str, List[str]]) -> List[lnurlpayout]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"SELECT * FROM lnurlpayout.lnurlpayouts WHERE wallet IN ({q})", (*wallet_ids,)
)
2021-12-02 22:56:31 +00:00
return [lnurlpayout(**row) if row else None for row in rows]
2021-11-28 18:11:35 +00:00
async def delete_lnurlpayout(lnurlpayout_id: str) -> None:
2022-01-30 19:43:30 +00:00
await db.execute(
"DELETE FROM lnurlpayout.lnurlpayouts WHERE id = ?", (lnurlpayout_id,)
)