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

284 lines
7.8 KiB
Python
Raw Normal View History

2021-10-13 17:08:48 +01:00
from datetime import datetime
from typing import List, Optional, Union
2021-10-25 11:54:58 +01:00
2021-10-13 17:08:48 +01:00
from lnbits.helpers import urlsafe_short_hash
2021-10-25 11:54:58 +01:00
2021-10-13 17:08:48 +01:00
from . import db
2021-10-13 21:51:21 +01:00
from .models import (
CreateSatsDiceLink,
2021-10-14 23:08:09 +01:00
CreateSatsDicePayment,
CreateSatsDiceWithdraw,
2021-10-25 11:54:58 +01:00
HashCheck,
satsdiceLink,
satsdicePayment,
satsdiceWithdraw,
2021-10-13 21:51:21 +01:00
)
2021-10-13 17:08:48 +01:00
2021-10-19 20:54:02 +01:00
async def create_satsdice_pay(
2021-10-25 11:54:58 +01:00
wallet_id: str,
2021-10-19 20:54:02 +01:00
data: CreateSatsDiceLink,
) -> satsdiceLink:
2021-10-13 17:08:48 +01:00
satsdice_id = urlsafe_short_hash()
await db.execute(
"""
INSERT INTO satsdice.satsdice_pay (
id,
wallet,
title,
base_url,
min_bet,
max_bet,
amount,
served_meta,
served_pr,
multiplier,
chance,
haircut,
open_time
)
VALUES (?, ?, ?, ?, ?, ?, 0, 0, 0, ?, ?, ?, ?)
""",
(
satsdice_id,
2021-10-25 11:54:58 +01:00
wallet_id,
2021-10-13 21:51:21 +01:00
data.title,
data.base_url,
data.min_bet,
data.max_bet,
data.multiplier,
data.chance,
data.haircut,
2021-10-13 17:08:48 +01:00
int(datetime.now().timestamp()),
),
)
link = await get_satsdice_pay(satsdice_id)
assert link, "Newly created link couldn't be retrieved"
return link
async def get_satsdice_pay(link_id: str) -> Optional[satsdiceLink]:
row = await db.fetchone(
"SELECT * FROM satsdice.satsdice_pay WHERE id = ?", (link_id,)
)
return satsdiceLink(**row) if row else None
2021-10-13 17:08:48 +01:00
async def get_satsdice_pays(wallet_ids: Union[str, List[str]]) -> List[satsdiceLink]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"""
SELECT * FROM satsdice.satsdice_pay WHERE wallet IN ({q})
ORDER BY id
""",
(*wallet_ids,),
)
2021-10-15 14:28:41 +01:00
return [satsdiceLink(**row) for row in rows]
2021-10-13 17:08:48 +01:00
async def update_satsdice_pay(link_id: int, **kwargs) -> Optional[satsdiceLink]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE satsdice.satsdice_pay SET {q} WHERE id = ?",
(*kwargs.values(), link_id),
)
row = await db.fetchone(
"SELECT * FROM satsdice.satsdice_pay WHERE id = ?", (link_id,)
)
2021-10-15 14:28:41 +01:00
return satsdiceLink(**row) if row else None
2021-10-13 17:08:48 +01:00
async def increment_satsdice_pay(link_id: int, **kwargs) -> Optional[satsdiceLink]:
q = ", ".join([f"{field[0]} = {field[0]} + ?" for field in kwargs.items()])
await db.execute(
f"UPDATE satsdice.satsdice_pay SET {q} WHERE id = ?",
(*kwargs.values(), link_id),
)
row = await db.fetchone(
"SELECT * FROM satsdice.satsdice_pay WHERE id = ?", (link_id,)
)
return satsdiceLink(**row) if row else None
2021-10-13 17:08:48 +01:00
async def delete_satsdice_pay(link_id: int) -> None:
await db.execute("DELETE FROM satsdice.satsdice_pay WHERE id = ?", (link_id,))
##################SATSDICE PAYMENT LINKS
2021-10-13 21:51:21 +01:00
async def create_satsdice_payment(data: CreateSatsDicePayment) -> satsdicePayment:
2021-10-13 17:08:48 +01:00
await db.execute(
"""
INSERT INTO satsdice.satsdice_payment (
payment_hash,
satsdice_pay,
value,
paid,
lost
)
VALUES (?, ?, ?, ?, ?)
""",
(data["payment_hash"], data["satsdice_pay"], data["value"], False, False),
2021-10-13 17:08:48 +01:00
)
payment = await get_satsdice_payment(data["payment_hash"])
2021-10-13 17:08:48 +01:00
assert payment, "Newly created withdraw couldn't be retrieved"
return payment
async def get_satsdice_payment(payment_hash: str) -> Optional[satsdicePayment]:
row = await db.fetchone(
"SELECT * FROM satsdice.satsdice_payment WHERE payment_hash = ?",
(payment_hash,),
)
return satsdicePayment(**row) if row else None
2021-10-13 17:08:48 +01:00
async def update_satsdice_payment(
payment_hash: int, **kwargs
) -> Optional[satsdicePayment]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE satsdice.satsdice_payment SET {q} WHERE payment_hash = ?",
(bool(*kwargs.values()), payment_hash),
)
row = await db.fetchone(
"SELECT * FROM satsdice.satsdice_payment WHERE payment_hash = ?",
(payment_hash,),
)
return satsdicePayment(**row) if row else None
2021-10-13 17:08:48 +01:00
##################SATSDICE WITHDRAW LINKS
2021-10-13 21:51:21 +01:00
async def create_satsdice_withdraw(data: CreateSatsDiceWithdraw) -> satsdiceWithdraw:
2021-10-13 17:08:48 +01:00
await db.execute(
"""
INSERT INTO satsdice.satsdice_withdraw (
id,
satsdice_pay,
value,
unique_hash,
k1,
open_time,
used
)
VALUES (?, ?, ?, ?, ?, ?, ?)
""",
(
data["payment_hash"],
data["satsdice_pay"],
data["value"],
2021-10-13 17:08:48 +01:00
urlsafe_short_hash(),
urlsafe_short_hash(),
int(datetime.now().timestamp()),
data["used"],
2021-10-13 17:08:48 +01:00
),
)
withdraw = await get_satsdice_withdraw(data["payment_hash"], 0)
2021-10-13 17:08:48 +01:00
assert withdraw, "Newly created withdraw couldn't be retrieved"
return withdraw
async def get_satsdice_withdraw(withdraw_id: str, num=0) -> Optional[satsdiceWithdraw]:
row = await db.fetchone(
"SELECT * FROM satsdice.satsdice_withdraw WHERE id = ?", (withdraw_id,)
)
if not row:
return None
withdraw = []
for item in row:
withdraw.append(item)
withdraw.append(num)
return satsdiceWithdraw(**row)
2021-10-13 17:08:48 +01:00
async def get_satsdice_withdraw_by_hash(
unique_hash: str, num=0
) -> Optional[satsdiceWithdraw]:
row = await db.fetchone(
2021-10-17 18:33:29 +01:00
"SELECT * FROM satsdice.satsdice_withdraw WHERE unique_hash = ?", (unique_hash,)
2021-10-13 17:08:48 +01:00
)
if not row:
return None
withdraw = []
for item in row:
withdraw.append(item)
withdraw.append(num)
return satsdiceWithdraw(**row)
2021-10-13 17:08:48 +01:00
async def get_satsdice_withdraws(
wallet_ids: Union[str, List[str]]
) -> List[satsdiceWithdraw]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join(["?"] * len(wallet_ids))
rows = await db.fetchall(
f"SELECT * FROM satsdice.satsdice_withdraw WHERE wallet IN ({q})",
(*wallet_ids,),
)
return [satsdiceWithdraw(**row) for row in rows]
2021-10-13 17:08:48 +01:00
async def update_satsdice_withdraw(
withdraw_id: str, **kwargs
) -> Optional[satsdiceWithdraw]:
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE satsdice.satsdice_withdraw SET {q} WHERE id = ?",
(*kwargs.values(), withdraw_id),
)
row = await db.fetchone(
"SELECT * FROM satsdice.satsdice_withdraw WHERE id = ?", (withdraw_id,)
)
return satsdiceWithdraw(**row) if row else None
2021-10-13 17:08:48 +01:00
async def delete_satsdice_withdraw(withdraw_id: str) -> None:
await db.execute(
"DELETE FROM satsdice.satsdice_withdraw WHERE id = ?", (withdraw_id,)
)
2021-10-17 18:33:29 +01:00
async def create_withdraw_hash_check(the_hash: str, lnurl_id: str) -> HashCheck:
2021-10-13 17:08:48 +01:00
await db.execute(
"""
INSERT INTO satsdice.hash_checkw (
id,
lnurl_id
)
VALUES (?, ?)
""",
2021-10-17 18:33:29 +01:00
(the_hash, lnurl_id),
2021-10-13 17:08:48 +01:00
)
hashCheck = await get_withdraw_hash_checkw(the_hash, lnurl_id)
return hashCheck
async def get_withdraw_hash_checkw(the_hash: str, lnurl_id: str) -> Optional[HashCheck]:
rowid = await db.fetchone(
"SELECT * FROM satsdice.hash_checkw WHERE id = ?", (the_hash,)
)
rowlnurl = await db.fetchone(
"SELECT * FROM satsdice.hash_checkw WHERE lnurl_id = ?", (lnurl_id,)
)
if not rowlnurl:
await create_withdraw_hash_check(the_hash, lnurl_id)
return {"lnurl": True, "hash": False}
else:
if not rowid:
await create_withdraw_hash_check(the_hash, lnurl_id)
return {"lnurl": True, "hash": False}
else:
return {"lnurl": True, "hash": True}