Cleaned up lnurl-withdraw

This commit is contained in:
benarc 2021-11-04 12:41:31 +00:00
parent 1ac84f1445
commit 9b1cadaffc
4 changed files with 37 additions and 79 deletions

View File

@ -56,12 +56,7 @@ async def get_withdraw_link(link_id: str, num=0) -> Optional[WithdrawLink]:
if not row:
return None
# link = []
# for item in row:
# link.append(item)
# link.append(num)
# print("GET_LINK", WithdrawLink.from_row(row))
return WithdrawLink.from_row(row)
return WithdrawLink(**row) if row else None
async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[WithdrawLink]:
@ -70,12 +65,7 @@ async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[Withdra
)
if not row:
return None
# link = []
# for item in row:
# link.append(item)
# link.append(num)
return WithdrawLink.from_row(row)
return WithdrawLink(**row) if row else None
async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]:
@ -86,14 +76,12 @@ async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[Withdraw
rows = await db.fetchall(
f"SELECT * FROM withdraw.withdraw_link WHERE wallet IN ({q})", (*wallet_ids,)
)
return [WithdrawLink.from_row(row) for row in rows]
return [WithdrawLink(**row) for row in rows]
async def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]:
if "is_unique" in kwargs:
kwargs["is_unique"] = int(kwargs["is_unique"])
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
await db.execute(
f"UPDATE withdraw.withdraw_link SET {q} WHERE id = ?",
@ -102,7 +90,7 @@ async def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]
row = await db.fetchone(
"SELECT * FROM withdraw.withdraw_link WHERE id = ?", (link_id,)
)
return WithdrawLink.from_row(row) if row else None
return WithdrawLink(**row) if row else None
async def delete_withdraw_link(link_id: str) -> None:

View File

@ -54,25 +54,22 @@ async def api_lnurl_callback(
):
link = await get_withdraw_link_by_hash(unique_hash)
now = int(datetime.now().timestamp())
print("link")
if not link:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
)
print("link")
if link.is_spent:
raise HTTPException(
# WHAT STATUS_CODE TO USE??
detail="Withdraw is spent."
)
raise HTTPException(status_code=HTTPStatus.OK, detail="Withdraw is spent.")
print("link")
if link.k1 != k1:
raise HTTPException(status_code=HTTPStatus.BAD_REQUEST, detail="Bad request.")
raise HTTPException(status_code=HTTPStatus.OK, detail="Bad request.")
print("link")
if now < link.open_time:
return {"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}
print("link")
try:
usescsv = ""
for x in range(1, link.uses - link.used):
@ -92,7 +89,6 @@ async def api_lnurl_callback(
"used": link.used + 1,
"usescsv": usescsv,
}
await update_withdraw_link(link.id, **changes)
await pay_invoice(
@ -101,18 +97,11 @@ async def api_lnurl_callback(
max_sat=link.max_withdrawable,
extra={"tag": "withdraw"},
)
# should these be "raise" instead of the "return" ??
except ValueError as e:
await update_withdraw_link(link.id, **changesback)
return {"status": "ERROR", "reason": str(e)}
except PermissionError:
await update_withdraw_link(link.id, **changesback)
return {"status": "ERROR", "reason": "Withdraw link is empty."}
except Exception as e:
await update_withdraw_link(link.id, **changesback)
return {"status": "ERROR", "reason": str(e)}
return {"status": "OK"}
return {"status": "OK"}
except Exception as e:
wibble = await update_withdraw_link(link.id, **changesback)
return {"status": "ERROR", "reason": str(e)}
# FOR LNURLs WHICH ARE UNIQUE
@ -128,22 +117,11 @@ async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash
if not link:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
)
# return (
# {"status": "ERROR", "reason": "LNURL-withdraw not found."},
# HTTPStatus.OK,
# )
if link.is_spent:
raise HTTPException(
# WHAT STATUS_CODE TO USE??
detail="Withdraw is spent."
)
# return (
# {"status": "ERROR", "reason": "Withdraw is spent."},
# HTTPStatus.OK,
# )
raise HTTPException(status_code=HTTPStatus.OK, detail="Withdraw is spent.")
useslist = link.usescsv.split(",")
found = False
@ -153,11 +131,7 @@ async def api_lnurl_multi_response(request: Request, unique_hash, id_unique_hash
found = True
if not found:
raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="LNURL-withdraw not found."
status_code=HTTPStatus.OK, detail="LNURL-withdraw not found."
)
# return (
# {"status": "ERROR", "reason": "LNURL-withdraw not found."},
# HTTPStatus.OK,
# )
return link.lnurl_response(request).dict()

View File

@ -19,26 +19,19 @@ class CreateWithdrawData(BaseModel):
class WithdrawLink(BaseModel):
id: str
wallet: str
title: str
min_withdrawable: int
max_withdrawable: int
uses: int
wait_time: int
is_unique: bool
unique_hash: str
k1: str
open_time: int
used: int
usescsv: str
number: int
@classmethod
def from_row(cls, row: Row) -> "WithdrawLink":
data = dict(row)
data["is_unique"] = bool(data["is_unique"])
data["number"] = 0
return cls(**data)
wallet: str = Query(None)
title: str = Query(None)
min_withdrawable: int = Query(0)
max_withdrawable: int = Query(0)
uses: int = Query(0)
wait_time: int = Query(0)
is_unique: bool = Query(False)
unique_hash: str = Query(0)
k1: str = Query(None)
open_time: int = Query(0)
used: int = Query(0)
usescsv: str = Query(None)
number: int = Query(0)
@property
def is_spent(self) -> bool:

View File

@ -150,14 +150,16 @@
dense
v-model.number="formDialog.data.min_withdrawable"
type="number"
label="Min withdrawable (sat) *"
min="10"
label="Min withdrawable (sat, at least 10) *"
></q-input>
<q-input
filled
dense
v-model.number="formDialog.data.max_withdrawable"
type="number"
label="Max withdrawable (sat) *"
min="10"
label="Max withdrawable (sat, at least 10) *"
></q-input>
<q-input
filled
@ -264,7 +266,8 @@
dense
v-model.number="simpleformDialog.data.max_withdrawable"
type="number"
label="Withdraw amount per voucher"
min="10"
label="Withdraw amount per voucher (sat, at least 10)"
></q-input>
<q-input
filled