lnbits-legend/lnbits/extensions/satsdice/models.py

137 lines
3.4 KiB
Python
Raw Normal View History

2021-10-13 17:08:48 +01:00
import json
from sqlite3 import Row
from typing import Dict, Optional
2021-11-04 16:41:23 +00:00
from fastapi import Request
2021-10-13 21:51:21 +01:00
from fastapi.param_functions import Query
2021-11-04 16:41:23 +00:00
from lnurl import Lnurl, LnurlWithdrawResponse
from lnurl import encode as lnurl_encode # type: ignore
from lnurl.types import LnurlPayMetadata # type: ignore
2021-10-13 21:51:21 +01:00
from pydantic import BaseModel
2021-11-04 16:41:23 +00:00
from pydantic.main import BaseModel
2021-10-13 17:08:48 +01:00
2021-10-15 14:28:41 +01:00
class satsdiceLink(BaseModel):
id: str
2021-10-13 17:08:48 +01:00
wallet: str
title: str
min_bet: int
max_bet: int
amount: int
served_meta: int
served_pr: int
multiplier: float
haircut: float
chance: float
base_url: str
open_time: int
2021-10-19 20:54:02 +01:00
def lnurl(self, req: Request) -> str:
return lnurl_encode(req.url_for("satsdice.lnurlp_response", link_id=self.id))
2021-10-15 14:28:41 +01:00
2021-10-13 17:08:48 +01:00
@classmethod
def from_row(cls, row: Row) -> "satsdiceLink":
data = dict(row)
return cls(**data)
@property
def lnurlpay_metadata(self) -> LnurlPayMetadata:
2021-10-20 04:52:33 +01:00
return LnurlPayMetadata(
json.dumps(
[
[
"text/plain",
f"{self.title} (Chance: {self.chance}%, Multiplier: {self.multiplier})",
]
]
)
)
2021-10-13 17:08:48 +01:00
2021-10-15 14:28:41 +01:00
def success_action(self, payment_hash: str, req: Request) -> Optional[Dict]:
url = req.url_for(
2021-10-19 23:05:29 +01:00
"satsdice.displaywin", link_id=self.id, payment_hash=payment_hash
2021-10-13 17:08:48 +01:00
)
2021-10-17 18:33:29 +01:00
return {"tag": "url", "description": "Check the attached link", "url": url}
2021-10-13 17:08:48 +01:00
2021-10-15 14:28:41 +01:00
class satsdicePayment(BaseModel):
2021-10-13 17:08:48 +01:00
payment_hash: str
satsdice_pay: str
value: int
paid: bool
lost: bool
2021-10-15 14:28:41 +01:00
class satsdiceWithdraw(BaseModel):
2021-10-13 17:08:48 +01:00
id: str
satsdice_pay: str
value: int
unique_hash: str
k1: str
open_time: int
used: int
2021-10-15 14:28:41 +01:00
def lnurl(self, req: Request) -> Lnurl:
return lnurl_encode(
2021-10-19 23:05:29 +01:00
req.url_for("satsdice.lnurlw_response", unique_hash=self.unique_hash)
2021-10-15 14:28:41 +01:00
)
2021-10-13 17:08:48 +01:00
@property
def is_spent(self) -> bool:
return self.used >= 1
@property
2021-10-15 14:28:41 +01:00
def lnurl_response(self, req: Request) -> LnurlWithdrawResponse:
2021-10-19 23:05:29 +01:00
url = req.url_for("satsdice.api_lnurlw_callback", unique_hash=self.unique_hash)
withdrawResponse = {
"tag": "withdrawRequest",
"callback": url,
"k1": self.k1,
"minWithdrawable": self.value * 1000,
"maxWithdrawable": self.value * 1000,
"defaultDescription": "Satsdice winnings!",
}
return withdrawResponse
2021-10-13 17:08:48 +01:00
2021-10-15 14:28:41 +01:00
class HashCheck(BaseModel):
2021-10-13 17:08:48 +01:00
id: str
lnurl_id: str
@classmethod
def from_row(cls, row: Row) -> "Hash":
return cls(**dict(row))
2021-10-13 21:51:21 +01:00
class CreateSatsDiceLink(BaseModel):
2021-11-04 16:41:23 +00:00
wallet: str = Query(None)
2021-10-13 21:51:21 +01:00
title: str = Query(None)
base_url: str = Query(None)
min_bet: str = Query(None)
max_bet: str = Query(None)
multiplier: float = Query(0)
2021-10-13 21:51:21 +01:00
chance: float = Query(0)
haircut: int = Query(0)
class CreateSatsDicePayment(BaseModel):
satsdice_pay: str = Query(None)
value: int = Query(0)
payment_hash: str = Query(None)
class CreateSatsDiceWithdraw(BaseModel):
payment_hash: str = Query(None)
satsdice_pay: str = Query(None)
value: int = Query(0)
used: int = Query(0)
class CreateSatsDiceWithdraws(BaseModel):
title: str = Query(None)
min_satsdiceable: int = Query(0)
max_satsdiceable: int = Query(0)
uses: int = Query(0)
wait_time: str = Query(None)
is_unique: bool = Query(False)