-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodels.py
361 lines (283 loc) · 10.3 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
from __future__ import annotations
from datetime import datetime
from typing import Optional
from lnbits.db import FilterModel
from lnbits.utils.exchange_rates import fiat_amount_as_satoshis
from pydantic import BaseModel, Field
from .helpers import format_amount, is_ws_url, normalize_identifier, validate_pub_key
class CustomCost(BaseModel):
bracket: int
amount: float
def validate_data(self):
assert self.bracket >= 0, "Bracket must be positive."
assert self.amount >= 0, "Custom cost must be positive."
class PriceData(BaseModel):
currency: str
price: float
discount: float = 0
referer_bonus: float = 0
reason: str
async def price_sats(self) -> float:
if self.price == 0:
return 0
if self.currency == "sats":
return self.price
return await fiat_amount_as_satoshis(self.price, self.currency)
async def discount_sats(self) -> float:
if self.discount == 0:
return 0
if self.currency == "sats":
return self.discount
return await fiat_amount_as_satoshis(self.discount, self.currency)
async def referer_bonus_sats(self) -> float:
if self.referer_bonus == 0:
return 0
if self.currency == "sats":
return self.referer_bonus
return await fiat_amount_as_satoshis(self.referer_bonus, self.currency)
class Promotion(BaseModel):
code: str = ""
buyer_discount_percent: float
referer_bonus_percent: float
selected_referer: Optional[str] = None
def validate_data(self):
assert (
0 <= self.buyer_discount_percent <= 100
), f"Discount percent for '{self.code}' must be between 0 and 100."
assert (
0 <= self.referer_bonus_percent <= 100
), f"Referer percent for '{self.code}' must be between 0 and 100."
assert self.buyer_discount_percent + self.referer_bonus_percent <= 100, (
f"Discount and Referer for '{self.code}'" " must be less than 100%."
)
class PromoCodeStatus(BaseModel):
buyer_discount: Optional[float] = None
allow_referer: bool = False
referer: Optional[str] = None
class RotateAddressData(BaseModel):
secret: str
pubkey: str
class UpdateAddressData(BaseModel):
pubkey: Optional[str] = None
relays: Optional[list[str]] = None
def validate_data(self):
self.validate_relays_urls()
self.validate_pubkey()
def validate_relays_urls(self):
if not self.relays:
return
for r in self.relays:
if not is_ws_url(r):
raise ValueError(f"Relay '{r}' is not valid!")
def validate_pubkey(self):
if self.pubkey and self.pubkey != "":
self.pubkey = validate_pub_key(self.pubkey)
class CreateAddressData(BaseModel):
domain_id: str
local_part: str
pubkey: str = ""
years: int = 1
relays: Optional[list[str]] = None
promo_code: Optional[str] = None
referer: Optional[str] = None
create_invoice: bool = False
def normalize(self):
self.local_part = self.local_part.strip()
self.pubkey = self.pubkey.strip()
if self.relays:
self.relays = [r.strip() for r in self.relays]
if self.promo_code:
self.promo_code = self.promo_code.strip()
if "@" in self.promo_code:
elements = self.promo_code.rsplit("@")
self.promo_code = elements[0]
self.referer = elements[1]
if self.referer:
self.referer = self.referer.strip()
class DomainCostConfig(BaseModel):
max_years: int = 1
char_count_cost: list[CustomCost] = []
rank_cost: list[CustomCost] = []
promotions: list[Promotion] = []
def apply_promo_code(
self, amount: float, promo_code: Optional[str] = None
) -> tuple[float, float]:
if promo_code is None:
return 0, 0
promotion = next((p for p in self.promotions if p.code == promo_code), None)
if not promotion:
return 0, 0
discount = amount * (promotion.buyer_discount_percent / 100)
referer_bonus = amount * (promotion.referer_bonus_percent / 100)
return round(discount, 2), round(referer_bonus, 2)
def get_promotion(self, promo_code: Optional[str] = None) -> Optional[Promotion]:
if promo_code is None:
return None
return next((p for p in self.promotions if p.code == promo_code), None)
def promo_code_buyer_discount(self, promo_code: Optional[str] = None) -> float:
promotion = self.get_promotion(promo_code)
if not promotion:
return 0
return promotion.buyer_discount_percent
def promo_code_referer(
self, promo_code: Optional[str] = None, default_referer: Optional[str] = None
) -> Optional[str]:
promotion = self.get_promotion(promo_code)
if not promotion:
return None
if promotion.referer_bonus_percent == 0:
return None
if promotion.selected_referer:
return promotion.selected_referer
return default_referer
def promo_code_allows_referer(self, promo_code: Optional[str] = None) -> bool:
promotion = self.get_promotion(promo_code)
if not promotion:
return False
return promotion.referer_bonus_percent > 0 and not promotion.selected_referer
def promo_code_status(self, promo_code: Optional[str] = None) -> PromoCodeStatus:
return PromoCodeStatus(
buyer_discount=self.promo_code_buyer_discount(promo_code),
allow_referer=self.promo_code_allows_referer(promo_code),
referer=self.promo_code_referer(promo_code),
)
def validate_data(self):
for cost in self.char_count_cost:
cost.validate_data()
for cost in self.rank_cost:
cost.validate_data()
assert (
1 < self.max_years < 100
), "Maximum allowed years must be between 1 and 100."
promo_codes = []
for promo in self.promotions:
promo.validate_data()
assert (
promo.code not in promo_codes
), f"Duplicate promo code: '{promo.code}'."
promo_codes.append(promo.code)
class CreateDomainData(BaseModel):
wallet: str
currency: str
cost: float
domain: str
cost_extra: Optional[DomainCostConfig] = None
def validate_data(self):
assert self.cost >= 0, "Domain cost must be positive."
if self.cost_extra:
self.cost_extra.validate_data()
class EditDomainData(BaseModel):
id: str
currency: str
cost: float
cost_extra: Optional[DomainCostConfig] = None
def validate_data(self):
assert self.cost >= 0, "Domain cost must be positive."
if self.cost_extra:
self.cost_extra.validate_data()
class IdentifierRanking(BaseModel):
name: str
rank: int
class PublicDomain(BaseModel):
id: str
currency: str
cost: float
domain: str
class Domain(PublicDomain):
wallet: str
cost_extra: DomainCostConfig
time: datetime
async def price_for_identifier(
self,
identifier: str,
years: int,
rank: Optional[int] = None,
promo_code: Optional[str] = None,
) -> PriceData:
assert (
1 <= years <= self.cost_extra.max_years
), f"Number of years must be between '1' and '{self.cost_extra.max_years}'."
identifier = normalize_identifier(identifier)
max_amount, reason = self.cost, ""
for char_cost in self.cost_extra.char_count_cost:
if len(identifier) <= char_cost.bracket and max_amount < char_cost.amount:
max_amount = char_cost.amount
reason = f"{len(identifier)} characters"
if rank:
for rank_cost in self.cost_extra.rank_cost:
if rank <= rank_cost.bracket and max_amount < rank_cost.amount:
max_amount = rank_cost.amount
reason = f"Top {rank_cost.bracket} identifier"
full_price = max_amount * years
discount, referer_bonus = self.cost_extra.apply_promo_code(
full_price, promo_code
)
return PriceData(
currency=self.currency,
price=full_price - discount,
discount=discount,
referer_bonus=referer_bonus,
reason=reason,
)
def public_data(self):
data = dict(PublicDomain(**dict(self)))
data["max_years"] = self.cost_extra.max_years
return data
class LnAddressConfig(BaseModel):
wallet: str
min: int = 1
max: int = 10_000_000
pay_link_id: Optional[str] = ""
class AddressExtra(BaseModel):
currency: Optional[str] = None
price: Optional[float] = None
price_in_sats: Optional[float] = None
payment_hash: Optional[str] = None
reimburse_payment_hash: Optional[str] = None
promo_code: Optional[str] = None
referer: Optional[str] = None
activated_by_owner: bool = False
years: int = 1
max_years: int = 1
relays: list[str] = []
ln_address: LnAddressConfig = LnAddressConfig(wallet="")
class Address(BaseModel):
id: str
owner_id: Optional[str] = None
domain_id: str
local_part: str
active: bool
time: datetime
expires_at: datetime
pubkey: Optional[str] = None
reimburse_amount: int = 0
promo_code_status: PromoCodeStatus = Field(
default=PromoCodeStatus(), no_database=True
)
extra: AddressExtra = AddressExtra()
class AddressStatus(BaseModel):
identifier: str
available: bool = False
price: Optional[float] = None
price_in_sats: Optional[float] = None
price_reason: Optional[str] = None
currency: Optional[str] = None
@property
def price_formatted(self) -> str:
if self.available and self.price and self.currency:
return format_amount(self.price, self.currency)
return ""
class AddressFilters(FilterModel):
domain_id: str
local_part: str
reimburse_amount: str
pubkey: str
active: bool
time: datetime
class Nip5Settings(BaseModel):
cloudflare_access_token: Optional[str] = None
lnaddress_api_admin_key: Optional[str] = ""
lnaddress_api_endpoint: Optional[str] = "https://nostr.com"
class UserSetting(BaseModel):
owner_id: str
settings: Nip5Settings