-
Notifications
You must be signed in to change notification settings - Fork 3
/
models.py
81 lines (62 loc) · 1.8 KB
/
models.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
# Data models for your extension
import time
from sqlite3 import Row
from typing import Any, Dict, List
from pydantic import BaseModel
class NWCKey(BaseModel):
pubkey: str
wallet: str
description: str
expires_at: int
permissions: str
created_at: int
last_used: int
def get_permissions(self) -> List[str]:
try:
return self.permissions.split(" ")
except Exception:
return []
@classmethod
def from_row(cls, row: Dict[str, Any]) -> "NWCKey":
return cls(**row)
class NWCBudget(BaseModel):
id: int
pubkey: str
budget_msats: int
refresh_window: int
created_at: int
used_budget_msats: int = 0
def get_timestamp_range(self) -> tuple[int, int]:
c = int(time.time())
if self.refresh_window <= 0: # never refresh
# return a timestamp in the future
return c, c + 21000000
# calculate the next refresh timestamp
elapsed = c - self.created_at
passed_cycles = elapsed // self.refresh_window
last_cycle = self.created_at + (passed_cycles * self.refresh_window)
next_cycle = last_cycle + self.refresh_window
return last_cycle, next_cycle
@classmethod
def from_row(cls, row: Row) -> "NWCBudget":
return cls(**dict(row))
class NWCLog(BaseModel):
id: int
pubkey: str
payload: str
created_at: int
@classmethod
def from_row(cls, row: Row) -> "NWCLog":
return cls(**dict(row))
class NWCNewBudget(BaseModel):
budget_msats: int
refresh_window: int
created_at: int
class NWCRegistrationRequest(BaseModel):
permissions: List[str]
description: str
expires_at: int
budgets: List[NWCNewBudget]
class NWCGetResponse(BaseModel):
data: NWCKey
budgets: List[NWCBudget]