from uuid import uuid4 from stellar_sdk.xdr import SCString, SCVal, SCBytes, SCVec, SCValType from typing_extensions import TypeVar from app.contracts.contract import SmartContract from app.schemas.issue import IssuePost T = TypeVar("T") class IssueContract(SmartContract): def __init__(self, contract_id, user_key): super().__init__(contract_id, user_key) async def list_issues(self): issues = await self._execute_procedure("list_issues") return [IssuePost(**issue) for issue in issues] async def add_issue(self, title: str, paragraphs: list[str], telegram_handle: str): issue_id = uuid4().bytes print(f' => {telegram_handle}') await self._execute_procedure("add_issue", [ SCVal(type=SCValType.SCV_BYTES, bytes=SCBytes(issue_id)), SCVal(type=SCValType.SCV_STRING, str=SCString(title.encode('utf-8'))), SCVal(type=SCValType.SCV_VEC, vec=SCVec([SCVal(SCValType.SCV_STRING, str=SCString(paragraph.encode('utf-8'))) for paragraph in paragraphs])), SCVal(type=SCValType.SCV_STRING, str=SCString(telegram_handle.encode('utf-8'))), ]) async def get_paragraphs(self, issue_id: bytes): return await self._execute_procedure("get_paragraphs_for_issue", [ SCVal(type=SCValType.SCV_BYTES, bytes=SCBytes(issue_id)) ]) async def increase_vote(self, issue_id: bytes): await self._execute_procedure("increase_positive_vote", [ SCVal(type=SCValType.SCV_BYTES, bytes=SCBytes(issue_id)) ]) async def decrease_vote(self, issue_id: bytes): await self._execute_procedure("increase_negative_vote", [ SCVal(type=SCValType.SCV_BYTES, bytes=SCBytes(issue_id)) ])