Skip to content

Commit 2d5dcd2

Browse files
chore: again
Signed-off-by: mukundkumarjha <mukundiiitg@gmail.com>
1 parent 0d0cc36 commit 2d5dcd2

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ This changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.
167167
- Added missing type hints to sign method in Transaction class (#1630)
168168
- Refactored `examples/consensus/topic_create_transaction.py` to use `Client.from_env()` (#1611)
169169
- Updated GitHub Actions setup-node action to v6.2.0.
170+
- Refactored AccountInfo class to use the staking_info(#1366)
170171
- chore: format tests/unit/mock_server.py with black (#1542)
171172
- Refactored AccountInfo class to use the staking_info
172173
- Updated actions/checkout to v6.0.1 and actions/github-script v8.0.0 in bot-next-issue-recommendation workflow (#1586)

src/hiero_sdk_python/account/account_info.py

Lines changed: 12 additions & 16 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
@@ -99,13 +99,14 @@ def _from_proto(cls, proto: CryptoGetInfoResponse.AccountInfo) -> "AccountInfo":
9999
account_memo=proto.memo,
100100
owned_nfts=proto.ownedNfts,
101101
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+
)
102107
)
103108

104-
staking_info=(
105-
StakingInfo.from_proto(proto.staking_info)
106-
if proto.HasField("staking_info")
107-
else None
108-
)
109+
109110

110111
return account_info
111112

@@ -140,9 +141,9 @@ def _to_proto(self) -> CryptoGetInfoResponse.AccountInfo:
140141
ownedNfts=self.owned_nfts,
141142
max_automatic_token_associations=self.max_automatic_token_associations,
142143
staking_info=(
143-
self.staking_info.to_proto()
144-
if self.staking_info is not None
145-
else None
144+
self.staking_info._to_proto()
145+
if self.staking_info is not None
146+
else None
146147
),
147148
)
148149

@@ -158,8 +159,7 @@ def __str__(self) -> str:
158159
(self.account_memo, "Memo"),
159160
(self.owned_nfts, "Owned NFTs"),
160161
(self.max_automatic_token_associations, "Max Automatic Token Associations"),
161-
(self.staking_info, "Staked Account ID"),
162-
(self.staking_info, "Staked Node ID"),
162+
(self.staking_info, "Staked Info"),
163163
(self.proxy_received, "Proxy Received"),
164164
(self.expiration_time, "Expiration Time"),
165165
(self.auto_renew_period, "Auto Renew Period"),
@@ -174,9 +174,6 @@ def __str__(self) -> str:
174174

175175
if self.receiver_signature_required is not None:
176176
lines.append(f"Receiver Signature Required: {self.receiver_signature_required}")
177-
178-
if self.staking_info is not None:
179-
lines.append(f"Decline Staking Reward: {self.staking_info}")
180177

181178
if self.token_relationships:
182179
lines.append(f"Token Relationships: {len(self.token_relationships)}")
@@ -194,7 +191,6 @@ def __repr__(self) -> str:
194191
f"receiver_signature_required={self.receiver_signature_required!r}, "
195192
f"owned_nfts={self.owned_nfts!r}, "
196193
f"account_memo={self.account_memo!r}, "
197-
f"staked_node_id={self.staking_info!r}, "
198-
f"staked_account_id={self.staking_info!r}"
194+
f"staked_info={self.staking_info!r}, "
199195
f")"
200196
)

tests/unit/account_info_test.py

Lines changed: 55 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,7 +70,31 @@ 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
6875

76+
def test_proto_conversion_with_staking_info():
77+
"""Test converting AccountInfo with StakingInfo to proto and back preserves data"""
78+
public_key = PrivateKey.generate_ed25519().public_key()
79+
80+
staking_info = StakingInfo(
81+
decline_reward=True,
82+
staked_node_id=3,
83+
)
84+
85+
account_info = AccountInfo(
86+
account_id=AccountId(0, 0, 100),
87+
key=public_key,
88+
staking_info=staking_info,
89+
)
90+
91+
proto = account_info._to_proto()
92+
converted = AccountInfo._from_proto(proto)
93+
94+
# Now these assertions should work
95+
assert converted.staking_info is not None
96+
assert converted.staking_info.decline_reward is True
97+
assert converted.staking_info.staked_node_id == 3
6998

7099
def test_account_info_default_initialization():
71100
"""Test the default initialization of the AccountInfo class"""
@@ -82,7 +111,25 @@ def test_account_info_default_initialization():
82111
assert account_info.token_relationships == []
83112
assert account_info.account_memo is None
84113
assert account_info.owned_nfts is None
114+
assert account_info.max_automatic_token_associations is None
115+
assert account_info.staking_info is None
85116

117+
def test_staking_info_persistence(account_info):
118+
"""Ensure staking info is preserved through proto conversion"""
119+
120+
account_info.staking_info = StakingInfo(
121+
decline_reward=True,
122+
staked_node_id=5,
123+
staked_account_id=None
124+
)
125+
126+
proto = account_info._to_proto()
127+
converted_info = AccountInfo._from_proto(proto)
128+
129+
assert converted_info.staking_info is not None
130+
assert converted_info.staking_info.decline_reward is True
131+
assert converted_info.staking_info.staked_node_id == 5
132+
assert converted_info.staking_info.staked_account_id is None
86133

87134
def test_from_proto(proto_account_info):
88135
"""Test the from_proto method of the AccountInfo class"""
@@ -100,6 +147,8 @@ def test_from_proto(proto_account_info):
100147
assert account_info.token_relationships == []
101148
assert account_info.account_memo == "Test account memo"
102149
assert account_info.owned_nfts == 5
150+
assert account_info.max_automatic_token_associations == 10
151+
assert account_info.staking_info == None
103152

104153

105154
def test_from_proto_with_token_relationships():
@@ -141,6 +190,11 @@ def test_to_proto(account_info):
141190
assert proto.tokenRelationships == []
142191
assert proto.memo == "Test account memo"
143192
assert proto.ownedNfts == 5
193+
assert proto.max_automatic_token_associations == 10
194+
assert not proto.HasField("staking_info")
195+
196+
197+
144198

145199

146200
def test_to_proto_with_none_values():
@@ -192,6 +246,7 @@ def test_proto_conversion(account_info):
192246
)
193247
assert converted_account_info.account_memo == account_info.account_memo
194248
assert converted_account_info.owned_nfts == account_info.owned_nfts
249+
assert converted_account_info.staking_info == account_info.staking_info
195250

196251

197252
def test_str_and_repr(account_info):

0 commit comments

Comments
 (0)