Verified Commit 24764855 authored by Jakob Moser's avatar Jakob Moser
Browse files

Work on Messages

parent 56da4172
Loading
Loading
Loading
Loading
+14 −0
Changes for poolpay/model/messages/Message.py: 14 added lines, 0 removed lines.
Original line number Diff line number Diff line
from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Self
from uuid import UUID

from poolpay.wire.Packet import Packet


@dataclass(frozen=True)
class Message(ABC):
    uuid: UUID

    @abstractmethod
    def to_packet(self) -> Packet:
        pass

    @classmethod
    @abstractmethod
    def from_packet(cls, packet: Packet) -> Self:
        """
        Try to decode the given packet into a message.

        :raises PacketDecodeError: If the given packet can not be decoded as such a message.
        """
        pass
+16 −1
Changes for poolpay/model/messages/UpdateBalanceMessage.py: 16 added lines, 1 removed line.
Original line number Diff line number Diff line
import json
from dataclasses import dataclass
from typing import Self
from uuid import UUID

from poolpay.model.messages.Message import Message
@@ -8,7 +9,6 @@ from poolpay.wire.Packet import Packet

@dataclass(frozen=True)
class UpdateBalanceMessage(Message):
    uuid: UUID
    by_amount_cents: int
    person_full_name: str

@@ -24,3 +24,18 @@ class UpdateBalanceMessage(Message):

    def __repr__(self) -> str:
        return json.dumps(self.to_packet())

    @classmethod
    def from_packet(cls, packet: Packet) -> Self:
        """
        Try to decode the given packet as an UpdateBalanceMessage.

        :raises PacketDecodeError: If the given packet can not be decoded as such a message.
        """

        # TODO Throw error on fail
        return cls(
            uuid=UUID(packet["uuid"]),
            by_amount_cents=packet["action"]["by_amount_cents"],
            person_full_name=packet["person"]["full_name"],
        )