The backend component to interface with the smart contract.
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

43 rader
1.7 KiB

  1. from uuid import uuid4
  2. from stellar_sdk.xdr import SCString, SCVal, SCBytes, SCVec, SCValType
  3. from typing_extensions import TypeVar
  4. from app.contracts.contract import SmartContract
  5. from app.schemas.issue import IssuePost
  6. T = TypeVar("T")
  7. class IssueContract(SmartContract):
  8. def __init__(self, contract_id, user_key):
  9. super().__init__(contract_id, user_key)
  10. async def list_issues(self):
  11. issues = await self._execute_procedure("list_issues")
  12. return [IssuePost(**issue) for issue in issues]
  13. async def add_issue(self, title: str, paragraphs: list[str], telegram_handle: str):
  14. issue_id = uuid4().bytes
  15. print(f' => {telegram_handle}')
  16. await self._execute_procedure("add_issue", [
  17. SCVal(type=SCValType.SCV_BYTES, bytes=SCBytes(issue_id)),
  18. SCVal(type=SCValType.SCV_STRING, str=SCString(title.encode('utf-8'))),
  19. SCVal(type=SCValType.SCV_VEC, vec=SCVec([SCVal(SCValType.SCV_STRING, str=SCString(paragraph.encode('utf-8'))) for paragraph in paragraphs])),
  20. SCVal(type=SCValType.SCV_STRING, str=SCString(telegram_handle.encode('utf-8'))),
  21. ])
  22. async def get_paragraphs(self, issue_id: bytes):
  23. return await self._execute_procedure("get_paragraphs_for_issue", [
  24. SCVal(type=SCValType.SCV_BYTES, bytes=SCBytes(issue_id))
  25. ])
  26. async def increase_vote(self, issue_id: bytes):
  27. await self._execute_procedure("increase_positive_vote", [
  28. SCVal(type=SCValType.SCV_BYTES, bytes=SCBytes(issue_id))
  29. ])
  30. async def decrease_vote(self, issue_id: bytes):
  31. await self._execute_procedure("increase_negative_vote", [
  32. SCVal(type=SCValType.SCV_BYTES, bytes=SCBytes(issue_id))
  33. ])