Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update to lnbits 1.0.0 #7

Merged
merged 7 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"short_description": "Connect a Bleskomat ATM to an lnbits",
"tile": "/bleskomat/static/image/bleskomat.png",
"contributors": ["chill117"],
"min_lnbits_version": "0.12.6"
"min_lnbits_version": "1.0.0"
}
112 changes: 43 additions & 69 deletions crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,70 +13,53 @@ async def create_bleskomat(data: CreateBleskomat, wallet_id: str) -> Bleskomat:
api_key_id = secrets.token_hex(8)
api_key_secret = secrets.token_hex(32)
api_key_encoding = "hex"
await db.execute(
"""
INSERT INTO bleskomat.bleskomats
(
id, wallet, api_key_id, api_key_secret, api_key_encoding,
name, fiat_currency, exchange_rate_provider, fee
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
bleskomat_id,
wallet_id,
api_key_id,
api_key_secret,
api_key_encoding,
data.name,
data.fiat_currency,
data.exchange_rate_provider,
data.fee,
),
bleskomat = Bleskomat(
id=bleskomat_id,
wallet=wallet_id,
api_key_id=api_key_id,
api_key_secret=api_key_secret,
api_key_encoding=api_key_encoding,
**data.dict(),
)
bleskomat = await get_bleskomat(bleskomat_id)
assert bleskomat, "Newly created bleskomat couldn't be retrieved"
await db.insert("bleskomat.bleskomats", bleskomat)
return bleskomat


async def get_bleskomat(bleskomat_id: str) -> Optional[Bleskomat]:
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE id = ?", (bleskomat_id,)
return await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE id = :id",
{"id": bleskomat_id},
Bleskomat,
)
return Bleskomat(**row) if row else None


async def get_bleskomat_by_api_key_id(api_key_id: str) -> Optional[Bleskomat]:
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE api_key_id = ?", (api_key_id,)
return await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE api_key_id = :id",
{"id": api_key_id},
Bleskomat,
)
return Bleskomat(**row) if row else None


async def get_bleskomats(wallet_ids: Union[str, List[str]]) -> List[Bleskomat]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"SELECT * FROM bleskomat.bleskomats WHERE wallet IN ({q})", (*wallet_ids,)
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
return await db.fetchall(
f"SELECT * FROM bleskomat.bleskomats WHERE wallet IN ({q})",
model=Bleskomat,
)
return [Bleskomat(**row) for row in rows]


async def update_bleskomat(bleskomat_id: str, **kwargs) -> Optional[Bleskomat]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE bleskomat.bleskomats SET {q} WHERE id = ?",
(*kwargs.values(), bleskomat_id),
)
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomats WHERE id = ?", (bleskomat_id,)
)
return Bleskomat(**row) if row else None
async def update_bleskomat(bleskomat: Bleskomat) -> Bleskomat:
await db.update("bleskomat.bleskomats", bleskomat)
return bleskomat


async def delete_bleskomat(bleskomat_id: str) -> None:
await db.execute("DELETE FROM bleskomat.bleskomats WHERE id = ?", (bleskomat_id,))
await db.execute(
"DELETE FROM bleskomat.bleskomats WHERE id = :id", {"id": bleskomat_id}
)


async def create_bleskomat_lnurl(
Expand All @@ -85,36 +68,27 @@ async def create_bleskomat_lnurl(
bleskomat_lnurl_id = uuid4().hex
lnurl_hash = generate_bleskomat_lnurl_hash(secret)
now = int(time.time())
await db.execute(
"""
INSERT INTO bleskomat.bleskomat_lnurls (
id, bleskomat, wallet, hash, tag, params, api_key_id,
initial_uses, remaining_uses, created_time, updated_time
)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(
bleskomat_lnurl_id,
bleskomat.id,
bleskomat.wallet,
lnurl_hash,
tag,
params,
bleskomat.api_key_id,
uses,
uses,
now,
now,
),
bleskomat_lnurl = BleskomatLnurl(
id=bleskomat_lnurl_id,
bleskomat=bleskomat.id,
wallet=bleskomat.wallet,
hash=lnurl_hash,
tag=tag,
params=params,
api_key_id=bleskomat.api_key_id,
initial_uses=uses,
remaining_uses=uses,
created_time=now,
updated_time=now,
)
bleskomat_lnurl = await get_bleskomat_lnurl(secret)
assert bleskomat_lnurl, "Newly created bleskomat LNURL couldn't be retrieved"
await db.insert("bleskomat.bleskomat_lnurls", bleskomat_lnurl)
return bleskomat_lnurl


async def get_bleskomat_lnurl(secret: str) -> Optional[BleskomatLnurl]:
lnurl_hash = generate_bleskomat_lnurl_hash(secret)
row = await db.fetchone(
"SELECT * FROM bleskomat.bleskomat_lnurls WHERE hash = ?", (lnurl_hash,)
return await db.fetchone(
"SELECT * FROM bleskomat.bleskomat_lnurls WHERE hash = :hash",
{"hash": lnurl_hash},
BleskomatLnurl,
)
return BleskomatLnurl(**row) if row else None
3 changes: 2 additions & 1 deletion models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

import bolt11
from fastapi import Query, Request
from lnbits.core.services import PaymentError, pay_invoice
from lnbits.core.services import pay_invoice
from lnbits.exceptions import PaymentError
from loguru import logger
from pydantic import BaseModel, validator

Expand Down
Loading