From 697576f99ace50879901a86156d380478eaa3f16 Mon Sep 17 00:00:00 2001 From: Geoff Lee Date: Wed, 20 Apr 2022 20:03:07 +0900 Subject: [PATCH 1/5] fix datetime parsing error --- terra_sdk/core/authz/data.py | 2 +- terra_sdk/core/authz/msgs.py | 5 +---- terra_sdk/core/gov/data.py | 8 ++++---- terra_sdk/core/staking/data/delegation.py | 23 ++++++++++++++++++++--- terra_sdk/core/staking/data/validator.py | 2 +- terra_sdk/util/converter.py | 2 +- terra_sdk/util/json.py | 4 ++++ 7 files changed, 32 insertions(+), 14 deletions(-) diff --git a/terra_sdk/core/authz/data.py b/terra_sdk/core/authz/data.py index b0cd55d8..d8ad2d84 100644 --- a/terra_sdk/core/authz/data.py +++ b/terra_sdk/core/authz/data.py @@ -201,7 +201,7 @@ def from_amino(cls, amino: dict) -> AuthorizationGrant: value = amino["value"] return cls( authorization=Authorization.from_amino(value["authorization"]), - expiration=value["expiration"] + expiration=parser.parse(value["expiration"]) ) diff --git a/terra_sdk/core/authz/msgs.py b/terra_sdk/core/authz/msgs.py index b2249cb6..f5ae8166 100644 --- a/terra_sdk/core/authz/msgs.py +++ b/terra_sdk/core/authz/msgs.py @@ -126,10 +126,7 @@ def from_data(cls, data: dict) -> MsgGrantAuthorization: return cls( granter=data["granter"], grantee=data["grantee"], - grant=AuthorizationGrant( - authorization=Authorization.from_data(data["grant"]["authorization"]), - expiration=str(data["grant"]["expiration"]), - ), + grant=AuthorizationGrant.from_data(data["grant"]) ) def to_proto(self) -> MsgGrant_pb: diff --git a/terra_sdk/core/gov/data.py b/terra_sdk/core/gov/data.py index ae08eaae..26d220e2 100644 --- a/terra_sdk/core/gov/data.py +++ b/terra_sdk/core/gov/data.py @@ -112,11 +112,11 @@ def from_data(cls, data: dict) -> Proposal: content=parse_content(data["content"]), status=data["status"], final_tally_result=data["final_tally_result"], - submit_time=data["submit_time"], - deposit_end_time=data["deposit_end_time"], + submit_time=parser.parse(data["submit_time"]), + deposit_end_time=parser.parse(data["deposit_end_time"]), total_deposit=Coins.from_data(data["total_deposit"]), - voting_start_time=data["voting_start_time"], - voting_end_time=data["voting_end_time"], + voting_start_time=parser.parse(data["voting_start_time"]), + voting_end_time=parser.parse(data["voting_end_time"]), ) def to_proto(self) -> Proposal_pb: diff --git a/terra_sdk/core/staking/data/delegation.py b/terra_sdk/core/staking/data/delegation.py index 34838ef2..bc27ce82 100644 --- a/terra_sdk/core/staking/data/delegation.py +++ b/terra_sdk/core/staking/data/delegation.py @@ -115,7 +115,7 @@ def from_data(cls, data: dict) -> UnbondingDelegationEntry: initial_balance=data["initial_balance"], balance=data["balance"], creation_height=data["creation_height"], - completion_time=data["completion_time"], + completion_time=parser.parse(data["completion_time"]), ) def to_proto(self) -> UnbondingDelegationEntry_pb: @@ -141,12 +141,12 @@ class UnbondingDelegation(JSONSerializable): def to_amino(self) -> dict: return { "delegator_address": self.delegator_address, - "validator_addresS": self.validator_address, + "validator_address": self.validator_address, "entries": [entry.to_amino() for entry in self.entries], } @classmethod - def from_data(cls, data) -> UnbondingDelegation: + def from_data(cls, data: dict) -> UnbondingDelegation: entries = [ UnbondingDelegationEntry.from_data(entry) for entry in data["entries"] ] @@ -179,6 +179,23 @@ def to_amino(self) -> dict: return { "initial_balance": str(self.initial_balance), "shares_dst": str(self.shares_dst), + "creation_height": str(self.creation_height), + "completion_time": to_isoformat(self.completion_time), + } + + @classmethod + def from_data(cls, data: dict) -> RedelegationEntryInfo: + return cls( + initial_balance=data["initial_balance"], + shares_dst=Dec.from_data(data("shares_dst")), + creation_height=data["creation_height"], + completion_time=parser.parse(data["completion_time"]) + ) + + def to_data(self) -> dict: + return { + "initial_balance": self.initial_balance, + "shares_dst": self.shares_dst.to_data(), "creation_height": self.creation_height, "completion_time": to_isoformat(self.completion_time), } diff --git a/terra_sdk/core/staking/data/validator.py b/terra_sdk/core/staking/data/validator.py index f4afa7cb..57b1bfb4 100644 --- a/terra_sdk/core/staking/data/validator.py +++ b/terra_sdk/core/staking/data/validator.py @@ -95,7 +95,7 @@ def to_amino(self) -> dict: def from_data(cls, data: dict) -> Commission: return cls( commission_rates=CommissionRates.from_data(data["commission_rates"]), - update_time=data["update_time"], + update_time=parser.parse(data["update_time"]), ) def to_proto(self) -> Commission_pb: diff --git a/terra_sdk/util/converter.py b/terra_sdk/util/converter.py index 9dab6b76..938acb06 100644 --- a/terra_sdk/util/converter.py +++ b/terra_sdk/util/converter.py @@ -5,5 +5,5 @@ def to_isoformat(dt: datetime) -> str: return ( dt.isoformat(timespec="milliseconds") .replace("+00:00", "Z") - .replace("000Z", "Z") + .replace(".000Z", "Z") ) diff --git a/terra_sdk/util/json.py b/terra_sdk/util/json.py index 6bd0918f..119583db 100644 --- a/terra_sdk/util/json.py +++ b/terra_sdk/util/json.py @@ -12,6 +12,8 @@ def to_data(x: Any) -> Any: return x.to_data() if isinstance(x, int): return str(x) + if isinstance(x, datetime): + return to_isoformat(x) if isinstance(x, list): return [to_data(g) for g in x] if isinstance(x, dict): @@ -24,6 +26,8 @@ def to_amino(x: Any) -> Any: return x.to_amino() if isinstance(x, list): return [to_data(g) for g in x] + if isinstance(x, datetime): + return to_isoformat(x) if isinstance(x, dict): return dict_to_amino(x) if isinstance(x, int): From ee441d1cbea7c3185a6daa63941e173d3518c9ac Mon Sep 17 00:00:00 2001 From: Geoff Lee Date: Wed, 20 Apr 2022 20:42:02 +0900 Subject: [PATCH 2/5] add to_data for CompactBitArray --- terra_sdk/core/compact_bit_array.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/terra_sdk/core/compact_bit_array.py b/terra_sdk/core/compact_bit_array.py index 99479ced..b4590a26 100644 --- a/terra_sdk/core/compact_bit_array.py +++ b/terra_sdk/core/compact_bit_array.py @@ -1,6 +1,7 @@ """CompactBitArray types related to multisig.""" from __future__ import annotations +import base64 import math import attr @@ -20,7 +21,13 @@ class CompactBitArray(JSONSerializable): @classmethod def from_data(cls, data: dict) -> CompactBitArray: - return cls(data["extra_bits_stored"], bytearray(data["elems"])) + return cls(data["extra_bits_stored"], bytearray(base64.b64decode(data["elems"]))) + + def to_data(self) -> dict: + return { + "extra_bits_stored": self.extra_bits_stored, + "elems": base64.b64encode(self.elems) + } @classmethod def from_proto(cls, proto: CompactBitArray_pb) -> CompactBitArray: From 7121347a7d6fc8152194b1ddebbf7d8caeee1fb3 Mon Sep 17 00:00:00 2001 From: DusanFedorcak Date: Wed, 20 Apr 2022 10:46:40 +0200 Subject: [PATCH 3/5] fix datetime json serialization --- terra_sdk/util/json.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/terra_sdk/util/json.py b/terra_sdk/util/json.py index 119583db..94d5ebe0 100644 --- a/terra_sdk/util/json.py +++ b/terra_sdk/util/json.py @@ -18,6 +18,8 @@ def to_data(x: Any) -> Any: return [to_data(g) for g in x] if isinstance(x, dict): return dict_to_data(x) + if isinstance(x, datetime): + return to_isoformat(x) return x From 37df2b6094113cfc72fea844152aa47910feb175 Mon Sep 17 00:00:00 2001 From: DusanFedorcak Date: Wed, 20 Apr 2022 11:20:09 +0200 Subject: [PATCH 4/5] fix deserialization: add base64 decode --- terra_sdk/core/compact_bit_array.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/terra_sdk/core/compact_bit_array.py b/terra_sdk/core/compact_bit_array.py index b4590a26..fa0d0f24 100644 --- a/terra_sdk/core/compact_bit_array.py +++ b/terra_sdk/core/compact_bit_array.py @@ -1,7 +1,7 @@ """CompactBitArray types related to multisig.""" from __future__ import annotations - import base64 + import math import attr From 8a951917a248be20ce6268fbdd9d4dab1b0310a7 Mon Sep 17 00:00:00 2001 From: Geoff Lee Date: Wed, 20 Apr 2022 20:50:11 +0900 Subject: [PATCH 5/5] add to_data for CompactBitArray --- terra_sdk/core/compact_bit_array.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/terra_sdk/core/compact_bit_array.py b/terra_sdk/core/compact_bit_array.py index fa0d0f24..49286b8c 100644 --- a/terra_sdk/core/compact_bit_array.py +++ b/terra_sdk/core/compact_bit_array.py @@ -29,6 +29,12 @@ def to_data(self) -> dict: "elems": base64.b64encode(self.elems) } + def to_data(self) -> dict: + return { + "extra_bits_stored": self.extra_bits_stored, + "elems": base64.b64encode(self.elems).decode() + } + @classmethod def from_proto(cls, proto: CompactBitArray_pb) -> CompactBitArray: return cls(proto["extra_bits_stored"], bytearray(proto.elems))