-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
59 lines (53 loc) · 1.81 KB
/
helpers.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
import random
from datetime import datetime
import httpx
from lnbits.core.services import pay_invoice
from lnbits.core.views.api import api_lnurlscan
from .crud import (
update_satspot,
)
async def get_pr(ln_address, amount):
data = await api_lnurlscan(ln_address)
if data.get("status") == "ERROR":
return
try:
async with httpx.AsyncClient() as client:
response = await client.get(url=f"{data['callback']}?amount={amount* 1000}")
if response.status_code != 200:
return
return response.json()["pr"]
except Exception:
return None
async def calculate_winner(satspot):
if (
datetime.now().timestamp() > satspot.closing_date.timestamp()
and not satspot.completed
):
satspot_players = satspot.players.split(",")
winner = random.choice(satspot_players)
# Calculate the total amount of winnings
total_amount = satspot.buy_in * len(satspot_players)
# Calculate the haircut amount
haircut_amount = total_amount * (satspot.haircut / 100)
# Calculate the winnings minus haircut
max_sat = int(total_amount - haircut_amount)
pr = await get_pr(winner, max_sat)
if not pr:
satspot.completed = False
await update_satspot(satspot)
return
try:
await pay_invoice(
wallet_id=satspot.wallet,
payment_request=pr,
max_sat=max_sat,
description=f"({winner}) won the satspot {satspot.name}!",
)
satspot.players = winner
satspot.completed = True
await update_satspot(satspot)
except Exception:
satspot.completed = False
await update_satspot(satspot)
return
return