Skip to content

Commit 9832ebd

Browse files
committed
Merge branch 'accountinfo' into test-PR-coderabbit-proto
2 parents ed21012 + 52a4ac1 commit 9832ebd

4 files changed

Lines changed: 77 additions & 30 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
153153
- Replaced the docstring in `entity_id_helper.py` with one that is correct. (#1623)
154154

155155
### Changed
156+
- Refactored AccountInfo class to use the staking_info(#1366)
156157
- Refactored `setup_client()` in all `examples/query/` files to use `Client.from_env()` for simplified client initialization (#1449)
157158
- Updated return of to_bytes function in `src/hiero_sdk_python/transaction/transaction.py`. (#1631)
158159
- Added missing return type `src/hiero_sdk_python/utils/entity_id_helper.py`. (#1622)

src/hiero_sdk_python/account/account_info.py

Lines changed: 16 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from hiero_sdk_python.account.account_id import AccountId
1010
from hiero_sdk_python.crypto.public_key import PublicKey
1111
from hiero_sdk_python.Duration import Duration
12-
from hiero_sdk_python.hapi.services.basic_types_pb2 import StakingInfo
12+
from hiero_sdk_python.staking_info import StakingInfo
1313
from hiero_sdk_python.hapi.services.crypto_get_info_pb2 import CryptoGetInfoResponse
1414
from hiero_sdk_python.hbar import Hbar
1515
from hiero_sdk_python.timestamp import Timestamp
@@ -56,9 +56,8 @@ class AccountInfo:
5656
account_memo: Optional[str] = None
5757
owned_nfts: Optional[int] = None
5858
max_automatic_token_associations: Optional[int] = None
59-
staked_account_id: Optional[AccountId] = None
60-
staked_node_id: Optional[int] = None
61-
decline_staking_reward: Optional[bool] = None
59+
staking_info: Optional[StakingInfo] = None
60+
6261

6362
@classmethod
6463
def _from_proto(cls, proto: CryptoGetInfoResponse.AccountInfo) -> "AccountInfo":
@@ -100,20 +99,14 @@ def _from_proto(cls, proto: CryptoGetInfoResponse.AccountInfo) -> "AccountInfo":
10099
account_memo=proto.memo,
101100
owned_nfts=proto.ownedNfts,
102101
max_automatic_token_associations=proto.max_automatic_token_associations,
102+
staking_info=(
103+
StakingInfo._from_proto(proto.staking_info)
104+
if proto.HasField("staking_info")
105+
else None
106+
)
103107
)
104108

105-
staking_info = proto.staking_info if proto.HasField('staking_info') else None
106-
107-
if staking_info:
108-
account_info.staked_account_id = (
109-
AccountId._from_proto(staking_info.staked_account_id)
110-
if staking_info.HasField('staked_account_id') else None
111-
)
112-
account_info.staked_node_id = (
113-
staking_info.staked_node_id
114-
if staking_info.HasField('staked_node_id') else None
115-
)
116-
account_info.decline_staking_reward = staking_info.decline_reward
109+
117110

118111
return account_info
119112

@@ -147,11 +140,11 @@ def _to_proto(self) -> CryptoGetInfoResponse.AccountInfo:
147140
memo=self.account_memo,
148141
ownedNfts=self.owned_nfts,
149142
max_automatic_token_associations=self.max_automatic_token_associations,
150-
staking_info=StakingInfo(
151-
staked_account_id=self.staked_account_id._to_proto() if self.staked_account_id else None,
152-
staked_node_id=self.staked_node_id if self.staked_node_id else None,
153-
decline_reward=self.decline_staking_reward
154-
),
143+
staking_info=(
144+
self.staking_info._to_proto()
145+
if self.staking_info is not None
146+
else None
147+
),
155148
)
156149

157150
def __str__(self) -> str:
@@ -166,8 +159,7 @@ def __str__(self) -> str:
166159
(self.account_memo, "Memo"),
167160
(self.owned_nfts, "Owned NFTs"),
168161
(self.max_automatic_token_associations, "Max Automatic Token Associations"),
169-
(self.staked_account_id, "Staked Account ID"),
170-
(self.staked_node_id, "Staked Node ID"),
162+
(self.staking_info, "Staked Info"),
171163
(self.proxy_received, "Proxy Received"),
172164
(self.expiration_time, "Expiration Time"),
173165
(self.auto_renew_period, "Auto Renew Period"),
@@ -182,9 +174,6 @@ def __str__(self) -> str:
182174

183175
if self.receiver_signature_required is not None:
184176
lines.append(f"Receiver Signature Required: {self.receiver_signature_required}")
185-
186-
if self.decline_staking_reward is not None:
187-
lines.append(f"Decline Staking Reward: {self.decline_staking_reward}")
188177

189178
if self.token_relationships:
190179
lines.append(f"Token Relationships: {len(self.token_relationships)}")
@@ -202,7 +191,6 @@ def __repr__(self) -> str:
202191
f"receiver_signature_required={self.receiver_signature_required!r}, "
203192
f"owned_nfts={self.owned_nfts!r}, "
204193
f"account_memo={self.account_memo!r}, "
205-
f"staked_node_id={self.staked_node_id!r}, "
206-
f"staked_account_id={self.staked_account_id!r}"
194+
f"staked_info={self.staking_info!r}, "
207195
f")"
208196
)

tests/unit/account_info_test.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from hiero_sdk_python.tokens.token_relationship import TokenRelationship
1010
from hiero_sdk_python.tokens.token_id import TokenId
1111
from hiero_sdk_python.hapi.services.crypto_get_info_pb2 import CryptoGetInfoResponse
12+
from hiero_sdk_python.staking_info import StakingInfo
1213

1314
pytestmark = pytest.mark.unit
1415

@@ -28,6 +29,8 @@ def account_info():
2829
token_relationships=[],
2930
account_memo="Test account memo",
3031
owned_nfts=5,
32+
max_automatic_token_associations=10,
33+
staking_info=None
3134
)
3235

3336

@@ -47,6 +50,8 @@ def proto_account_info():
4750
tokenRelationships=[],
4851
memo="Test account memo",
4952
ownedNfts=5,
53+
max_automatic_token_associations=10,
54+
staking_info=None
5055
)
5156
return proto
5257

@@ -65,6 +70,32 @@ def test_account_info_initialization(account_info):
6570
assert account_info.token_relationships == []
6671
assert account_info.account_memo == "Test account memo"
6772
assert account_info.owned_nfts == 5
73+
assert account_info.max_automatic_token_associations == 10
74+
assert account_info.staking_info is None
75+
76+
def test_from_proto_with_staking_info():
77+
"""Test the from_proto method of the AccountInfo class with staking info"""
78+
public_key = PrivateKey.generate_ed25519().public_key()
79+
80+
staking_info={
81+
"decline_reward": True,
82+
"staked_node_id": 3,
83+
"staked_account_id": None
84+
}
85+
86+
proto = CryptoGetInfoResponse.AccountInfo(
87+
accountID=AccountId(0, 0, 100)._to_proto(),
88+
key=public_key._to_proto(),
89+
balance=5000000,
90+
91+
92+
)
93+
94+
account_info = AccountInfo._from_proto(proto)
95+
96+
assert account_info.staking_info is not None
97+
assert account_info.staking_info.decline_reward is True
98+
assert account_info.staking_info.staked_node_id == 3
6899

69100

70101
def test_account_info_default_initialization():
@@ -82,7 +113,25 @@ def test_account_info_default_initialization():
82113
assert account_info.token_relationships == []
83114
assert account_info.account_memo is None
84115
assert account_info.owned_nfts is None
116+
assert account_info.max_automatic_token_associations is None
117+
assert account_info.staking_info is None
118+
119+
def test_staking_info_persistence(account_info):
120+
"""Ensure staking info is preserved through proto conversion"""
121+
122+
account_info.staking_info = StakingInfo(
123+
decline_reward=True,
124+
staked_node_id=5,
125+
staked_account_id=None
126+
)
127+
128+
proto = account_info._to_proto()
129+
converted_info = AccountInfo._from_proto(proto)
85130

131+
assert converted_info.staking_info is not None
132+
assert converted_info.staking_info.decline_reward is True
133+
assert converted_info.staking_info.staked_node_id == 5
134+
assert converted_info.staking_info.staked_account_id is None
86135

87136
def test_from_proto(proto_account_info):
88137
"""Test the from_proto method of the AccountInfo class"""
@@ -100,6 +149,8 @@ def test_from_proto(proto_account_info):
100149
assert account_info.token_relationships == []
101150
assert account_info.account_memo == "Test account memo"
102151
assert account_info.owned_nfts == 5
152+
assert account_info.max_automatic_token_associations == 10
153+
assert account_info.staking_info == None
103154

104155

105156
def test_from_proto_with_token_relationships():
@@ -141,6 +192,11 @@ def test_to_proto(account_info):
141192
assert proto.tokenRelationships == []
142193
assert proto.memo == "Test account memo"
143194
assert proto.ownedNfts == 5
195+
assert proto.max_automatic_token_associations == 10
196+
assert not proto.HasField("staking_info")
197+
198+
199+
144200

145201

146202
def test_to_proto_with_none_values():
@@ -192,6 +248,7 @@ def test_proto_conversion(account_info):
192248
)
193249
assert converted_account_info.account_memo == account_info.account_memo
194250
assert converted_account_info.owned_nfts == account_info.owned_nfts
251+
assert converted_account_info.staking_info == account_info.staking_info
195252

196253

197254
def test_str_and_repr(account_info):

tests/unit/topic_info_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ def test_repr_and_str(topic_info):
253253
assert "memo='Test topic memo'" in repr_output
254254
assert "sequence_number=42" in repr_output
255255
assert "running_hash=0x0102030405060708" in repr_output
256-
assert "expiration_time=2021-07-01 00:00:00" in repr_output
256+
assert "expiration_time=2021-07-01" in str_output
257257
assert "auto_renew_period=7776000" in repr_output
258258

259259
def test_str_formatting(topic_info):
@@ -264,7 +264,8 @@ def test_str_formatting(topic_info):
264264
assert "memo='Test topic memo'" in str_output
265265
assert "running_hash=0x0102030405060708" in str_output
266266
assert "sequence_number=42" in str_output
267-
assert "expiration_time=2021-07-01 00:00:00" in str_output
267+
# Instead of the full date-time string
268+
assert "expiration_time=2021-07-01" in str_output
268269
assert "admin_key=ed25519(" in str_output
269270
assert "submit_key=ed25519(" in str_output
270271
assert "auto_renew_period=7776000" in str_output

0 commit comments

Comments
 (0)