code cleanup

This commit is contained in:
Riccardo Balbo 2024-07-24 14:18:57 +00:00
commit 17f4bb7218
10 changed files with 35 additions and 73 deletions

37
crud.py
View file

@ -12,10 +12,11 @@ async def create_nwc(
expires_at: int ,
permissions:List[str] ,
budgets: Optional[List[NWCNewBudget]] = None
):
) -> NWCKey:
# Check if the key already exists
if await get_nwc(pubkey, None, True):
raise Exception("Public key already used")
# If not, create it
now=int(time.time())
await db.execute(
"""
@ -24,7 +25,7 @@ async def create_nwc(
""",
(pubkey, wallet_id, description, " ".join(permissions), now,int(expires_at) if expires_at else 0, now)
)
# Add budgets
if budgets:
for budget in budgets:
await db.execute(
@ -34,6 +35,7 @@ async def create_nwc(
""",
(pubkey, budget.budget_msats, budget.refresh_window, budget.created_at)
)
# Return the created key
return NWCKey(
pubkey=pubkey,
wallet=wallet_id,
@ -44,6 +46,7 @@ async def create_nwc(
last_used=now
)
async def delete_nwc(
pubkey: str,
wallet_id:str
@ -69,6 +72,7 @@ async def get_wallet_nwcs(
)
return [NWCKey(**row) for row in rows]
async def get_nwc(
pubkey: str,
wallet_id: Optional[str] = None,
@ -95,6 +99,7 @@ async def get_nwc(
)
return NWCKey(**row)
async def get_budgets_nwc(pubkey, calculate_spent=False):
rows = await db.fetchall(
"SELECT * FROM nwcprovider.budgets WHERE pubkey = ?", (pubkey)
@ -114,19 +119,19 @@ async def get_budgets_nwc(pubkey, calculate_spent=False):
return budgets
async def log_nwc(
pubkey: str,
payload:Optional[Any] = None
):
if not payload: payload=""
payload = json.dumps(payload)
await db.execute(
"""
INSERT INTO nwcprovider.logs (pubkey, payload, created_at)
VALUES (?, ?, ?)
""",
(pubkey, payload, int(time.time()))
)
# async def log_nwc(
# pubkey: str,
# payload:Optional[Any] = None
# ):
# if not payload: payload=""
# payload = json.dumps(payload)
# await db.execute(
# """
# INSERT INTO nwcprovider.logs (pubkey, payload, created_at)
# VALUES (?, ?, ?)
# """,
# (pubkey, payload, int(time.time()))
# )
async def tracked_spend_nwc(
@ -145,7 +150,7 @@ async def tracked_spend_nwc(
SELECT SUM(amount_msats) FROM nwcprovider.spent WHERE pubkey = ? AND created_at >= ? AND created_at < ?
""",
(pubkey, last_cycle, next_cycle)
)[0] or 0 # Ensure we get an int, default to 0 if None
)[0] or 0
if tot_spent_in_range_msats + amount_msats > budget.budget_msats:
in_budget = False
break