-
Notifications
You must be signed in to change notification settings - Fork 0
/
crud.py
161 lines (124 loc) · 4.69 KB
/
crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from datetime import datetime
from typing import Optional, Union
from lnbits.db import Database
from lnbits.helpers import urlsafe_short_hash
from .models import (
CreateSatsDiceLink,
CreateSatsDicePayment,
CreateSatsDiceWithdraw,
SatsdiceLink,
SatsdicePayment,
SatsdiceWithdraw,
)
db = Database("ext_satsdice")
async def create_satsdice_pay(data: CreateSatsDiceLink) -> SatsdiceLink:
satsdice = SatsdiceLink(
id=urlsafe_short_hash(),
**data.dict(),
)
await db.insert("satsdice.satsdice_pay", satsdice)
return satsdice
async def get_satsdice_pay(link_id: str) -> Optional[SatsdiceLink]:
return await db.fetchone(
"SELECT * FROM satsdice.satsdice_pay WHERE id = :id",
{"id": link_id},
SatsdiceLink,
)
async def get_satsdice_pays(wallet_ids: Union[str, list[str]]) -> list[SatsdiceLink]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
return await db.fetchall(
f"SELECT * FROM satsdice.satsdice_pay WHERE wallet IN ({q})",
model=SatsdiceLink,
)
async def update_satsdice_pay(link: SatsdiceLink) -> SatsdiceLink:
await db.update("satsdice.satsdice_pay", link)
return link
async def delete_satsdice_pay(link_id: str) -> None:
await db.execute(
"DELETE FROM satsdice.satsdice_pay WHERE id = :id", {"id": link_id}
)
async def create_satsdice_payment(data: CreateSatsDicePayment) -> SatsdicePayment:
payment = SatsdicePayment(**data.dict())
await db.insert("satsdice.satsdice_payment", payment)
return payment
async def get_satsdice_payment(payment_hash: str) -> Optional[SatsdicePayment]:
return await db.fetchone(
"SELECT * FROM satsdice.satsdice_payment WHERE payment_hash = :payment_hash",
{"payment_hash": payment_hash},
SatsdicePayment,
)
async def update_satsdice_payment(payment: SatsdicePayment) -> SatsdicePayment:
await db.update(
"satsdice.satsdice_payment",
payment,
"WHERE payment_hash = :payment_hash",
)
return payment
async def create_satsdice_withdraw(data: CreateSatsDiceWithdraw) -> SatsdiceWithdraw:
withdraw = SatsdiceWithdraw(
unique_hash=urlsafe_short_hash(),
k1=urlsafe_short_hash(),
open_time=int(datetime.now().timestamp()),
id=data.payment_hash,
satsdice_pay=data.satsdice_pay,
value=data.value,
used=data.used,
)
await db.insert("satsdice.satsdice_withdraw", withdraw)
return withdraw
async def get_satsdice_withdraw(withdraw_id: str) -> Optional[SatsdiceWithdraw]:
return await db.fetchone(
"SELECT * FROM satsdice.satsdice_withdraw WHERE id = :id",
{"id": withdraw_id},
SatsdiceWithdraw,
)
async def get_satsdice_withdraw_by_hash(unique_hash: str) -> Optional[SatsdiceWithdraw]:
return await db.fetchone(
"SELECT * FROM satsdice.satsdice_withdraw WHERE unique_hash = :unique_hash",
{"unique_hash": unique_hash},
SatsdiceWithdraw,
)
async def get_satsdice_withdraws(
wallet_ids: Union[str, list[str]]
) -> list[SatsdiceWithdraw]:
if isinstance(wallet_ids, str):
wallet_ids = [wallet_ids]
q = ",".join([f"'{wallet_id}'" for wallet_id in wallet_ids])
return await db.fetchall(
f"SELECT * FROM satsdice.satsdice_withdraw WHERE wallet IN ({q})",
model=SatsdiceWithdraw,
)
async def update_satsdice_withdraw(withdraw: SatsdiceWithdraw) -> SatsdiceWithdraw:
await db.update("satsdice.satsdice_withdraw", withdraw)
return withdraw
async def delete_satsdice_withdraw(withdraw_id: str) -> None:
await db.execute(
"DELETE FROM satsdice.satsdice_withdraw WHERE id = :id", {"id": withdraw_id}
)
async def create_withdraw_hash_check(the_hash: str, lnurl_id: str):
await db.execute(
"""
INSERT INTO satsdice.hash_checkw (id, lnurl_id)
VALUES (:id, :lnurl_id)
""",
{"id": the_hash, "lnurl_id": lnurl_id},
)
hash_check = await get_withdraw_hash_checkw(the_hash, lnurl_id)
return hash_check
async def get_withdraw_hash_checkw(the_hash: str, lnurl_id: str):
result1 = await db.execute(
"SELECT * FROM satsdice.hash_checkw WHERE id = :hash", {"hash": the_hash}
)
rowid = result1.mappings().first()
result2 = await db.execute(
"SELECT * FROM satsdice.hash_checkw WHERE lnurl_id = :lnurl_id",
{"lnurl_id": lnurl_id},
)
rowlnurl = result2.mappings().first()
if not rowlnurl or not rowid:
await create_withdraw_hash_check(the_hash, lnurl_id)
return {"lnurl": True, "hash": False}
else:
return {"lnurl": True, "hash": True}