Пример #1
0
class Block(BlockHeader, JsonDeserializable):

    __schema__ = S.OBJECT(
        block_meta=BlockHeader.__schema__,
        block=S.OBJECT(
            data=S.OBJECT(txs=S.OPTIONAL(S.ARRAY(S.STRING))),
            evidence={},
            last_commit={},
        ),
    )

    evidence: JiguBox  # TODO: improve and document
    last_commit: JiguBox  # TODO: improve and document
    txs: List[
        Union[str, StdTx, TxInfo,
              TxInfosQuery]]  # ordered last for convenience of pretty printing

    @classmethod
    def from_data(cls, data: dict) -> Block:
        header = BlockHeader.from_data(data["block_meta"])
        txs = (data["block"]["data"]["txs"]
               or [])  # these will be Amino-encoded tx strings
        evidence = JiguBox(data["block"].get("evidence"))
        last_commit = JiguBox(data["block"].get("last_commit"))
        return cls(txs=txs,
                   evidence=evidence,
                   last_commit=last_commit,
                   **header.__dict__)
Пример #2
0
class MsgEditValidator(StdMsg):

    type = "staking/MsgEditValidator"
    action = "edit_validator"

    __schema__ = S.OBJECT(
        type=S.STRING_WITH_PATTERN(r"^staking/MsgEditValidator\Z"),
        value=S.OBJECT(
            Description=Description.__schema__,
            address=S.VAL_ADDRESS,
            commission_rate=S.OPTIONAL(Dec.__schema__),
            min_self_delegation=S.OPTIONAL(S.STRING_INTEGER),
        ),
    )

    Description: Description
    address: ValAddress
    commission_rate: Optional[Dec] = None
    min_self_delegation: Optional[int] = None

    def __post_init__(self):
        if self.commission_rate is not None:
            self.commission_rate = Dec(self.commission_rate)
        if self.min_self_delegation is not None:
            self.min_self_delegation = int(self.min_self_delegation)
        self.address = validate_val_address(self.address)

    def msg_value(self) -> dict:
        d = dict(self.__dict__)
        msd = self.min_self_delegation
        if msd is not None:
            msd = str(msd)
        d["min_self_delegation"] = msd
        return d

    @classmethod
    def from_data(cls, data: dict) -> MsgEditValidator:
        data = data["value"]
        msd = int(data["min_self_delegation"]) if data["min_self_delegation"] else None
        cr = Dec(data["commission_rate"]) if data["commission_rate"] else None
        return cls(
            Description=data["Description"],
            address=data["address"],
            commission_rate=cr,
            min_self_delegation=msd,
        )
Пример #3
0
class Account(JsonSerializable, JsonDeserializable):

    __schema__ = S.OBJECT(
        type=S.STRING_WITH_PATTERN(r"^core/Account\Z"),
        value=S.OBJECT(
            address=S.ACC_ADDRESS,
            coins=Coins.__schema__,
            public_key=S.OPTIONAL(PublicKey.__schema__),
            account_number=S.STRING_INTEGER,
            sequence=S.STRING_INTEGER,
        ),
    )

    address: str
    coins: Coins
    public_key: Optional[PublicKey]
    account_number: int
    sequence: int

    def __post_init__(self):
        if self.address:
            self.address = validate_acc_address(self.address)
        if self.coins:
            self.coins = Coins(self.coins)
        self.account_number = int(self.account_number)
        self.sequence = int(self.sequence)

    def to_data(self) -> dict:
        d = dict(self.__dict__)
        d["account_number"] = str(self.account_number)
        d["sequence"] = str(self.sequence)
        return {"type": "core/Account", "value": d}

    @classmethod
    def from_data(cls, data: dict) -> Account:
        if "type" in data:
            data = data["value"]
        pk = data["public_key"]
        if pk is not None:
            pk = PublicKey.deserialize(data["public_key"])
        return cls(
            address=data["address"],
            coins=Coins.from_data(data["coins"]),
            public_key=pk,
            account_number=data["account_number"],
            sequence=data["sequence"],
        )
Пример #4
0
class Proposal(JsonSerializable, JsonDeserializable):

    __schema__ = S.OBJECT(
        content=Content.__schema__,
        id=S.STRING_INTEGER,
        proposal_status=S.STRING,
        final_tally_result=S.OPTIONAL(
            S.OBJECT(  # this gets marshalled into Coin
                yes=S.STRING_INTEGER,
                abstain=S.STRING_INTEGER,
                no=S.STRING_INTEGER,
                no_with_veto=S.STRING_INTEGER,
            )
        ),
        submit_time=Timestamp.__schema__,
        deposit_end_time=Timestamp.__schema__,
        total_deposit=Coins.__schema__,
        voting_start_time=Timestamp.__schema__,
        voting_end_time=Timestamp.__schema__,
    )

    content: Type[Content]
    id: int
    proposal_status: str
    final_tally_result: JiguBox[str, Coin]
    submit_time: Timestamp
    deposit_end_time: Timestamp
    total_deposit: Coins
    voting_start_time: Timestamp
    voting_end_time: Timestamp

    def to_data(self) -> dict:
        d = JiguBox(self.__dict__)
        d.id = str(d.id)
        for x in d.final_tally_result:
            d.final_tally_result[x] = d.final_tally_result[x].amount
        return d

    @property
    def pretty_data(self):
        d = dict(self.__dict__)
        proposal_id = d.pop("id")
        content = d.pop("content")
        ix = [
            ("id", proposal_id),
            ("type", content.type),
            *list(content.pretty_data),
            *list(d.items()),
        ]
        return ix

    @classmethod
    def from_data(cls, data: dict) -> Proposal:
        final_tally_result = data["final_tally_result"]
        for key in final_tally_result:
            final_tally_result[key] = Coin(uLuna, int(final_tally_result[key]))
        p_type = PROPOSAL_TYPES[data["content"]["type"]]
        content = p_type.from_data(data["content"])
        return cls(
            content=content,
            id=int(data["id"]),
            proposal_status=ProposalStatus(data["proposal_status"]),
            final_tally_result=JiguBox(final_tally_result),
            submit_time=Timestamp.from_data(data["submit_time"]),
            deposit_end_time=Timestamp.from_data(data["deposit_end_time"]),
            total_deposit=Coins.from_data(data["total_deposit"]),
            voting_start_time=Timestamp.from_data(data["voting_start_time"]),
            voting_end_time=Timestamp.from_data(data["voting_end_time"]),
        )