|
1 | 1 | from hiero_sdk_python.account.account_create_transaction import AccountCreateTransaction |
2 | 2 | from hiero_sdk_python.account.account_id import AccountId |
| 3 | +from hiero_sdk_python.account.account_info import AccountInfo |
3 | 4 | from hiero_sdk_python.query.account_info_query import AccountInfoQuery |
4 | 5 | from hiero_sdk_python.hbar import Hbar |
5 | 6 | from hiero_sdk_python.response_code import ResponseCode |
6 | 7 | from hiero_sdk_python.transaction.transaction_receipt import TransactionReceipt |
| 8 | +from tck.errors import JsonRpcError |
7 | 9 | from tck.handlers.registry import rpc_method |
8 | 10 | from tck.param.account import CreateAccountParams, GetAccountInfoParams |
9 | 11 | from tck.response.account import ( |
@@ -72,64 +74,119 @@ def create_account(params: CreateAccountParams) -> CreateAccountResponse: |
72 | 74 | return CreateAccountResponse(account_id, ResponseCode(receipt.status).name) |
73 | 75 |
|
74 | 76 |
|
75 | | -def _build_account_info_response(info) -> GetAccountInfoResponse: |
76 | | - staking_info_response = None |
77 | | - if info.staking_info: |
78 | | - staking_info_response = StakingInfoResponse( |
79 | | - declineStakingReward=info.staking_info.decline_staking_reward, |
80 | | - stakePeriodStart=str(info.staking_info.stake_period_start) |
81 | | - if info.staking_info.stake_period_start |
82 | | - else None, |
83 | | - pendingReward=str(info.staking_info.pending_reward.to_tinybars()) |
84 | | - if info.staking_info.pending_reward |
85 | | - else None, |
86 | | - stakedToMe=str(info.staking_info.staked_to_me.to_tinybars()) if info.staking_info.staked_to_me else None, |
87 | | - stakedAccountId=str(info.staking_info.staked_account_id) if info.staking_info.staked_account_id else None, |
88 | | - stakedNodeId=str(info.staking_info.staked_node_id) if info.staking_info.staked_node_id else None, |
| 77 | +def _enum_name(value) -> str | None: |
| 78 | + if value is None: |
| 79 | + return None |
| 80 | + return getattr(value, "name", str(value)) |
| 81 | + |
| 82 | + |
| 83 | +def _serialize_key(key) -> str | None: |
| 84 | + if key is None: |
| 85 | + return None |
| 86 | + |
| 87 | + to_string_der = getattr(key, "to_string_der", None) |
| 88 | + if callable(to_string_der): |
| 89 | + return to_string_der() |
| 90 | + |
| 91 | + return key.to_bytes().hex() |
| 92 | + |
| 93 | + |
| 94 | +def _to_staking_info_response(info: AccountInfo) -> StakingInfoResponse: |
| 95 | + if info.staking_info is None: |
| 96 | + return StakingInfoResponse( |
| 97 | + declineStakingReward=False, |
| 98 | + pendingReward="0", |
| 99 | + stakedToMe="0", |
| 100 | + ) |
| 101 | + |
| 102 | + staking_info = info.staking_info |
| 103 | + return StakingInfoResponse( |
| 104 | + declineStakingReward=staking_info.decline_reward, |
| 105 | + stakePeriodStart=str(staking_info.stake_period_start) |
| 106 | + if staking_info.stake_period_start is not None |
| 107 | + else None, |
| 108 | + pendingReward=str(staking_info.pending_reward.to_tinybars()) |
| 109 | + if staking_info.pending_reward is not None |
| 110 | + else "0", |
| 111 | + stakedToMe=str(staking_info.staked_to_me.to_tinybars()) |
| 112 | + if staking_info.staked_to_me is not None |
| 113 | + else "0", |
| 114 | + stakedAccountId=str(staking_info.staked_account_id) |
| 115 | + if staking_info.staked_account_id is not None |
| 116 | + else None, |
| 117 | + stakedNodeId=str(staking_info.staked_node_id) |
| 118 | + if staking_info.staked_node_id is not None |
| 119 | + else None, |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +def _to_token_relationships_response(info: AccountInfo) -> dict[str, TokenRelationshipResponse]: |
| 124 | + token_relationships_response: dict[str, TokenRelationshipResponse] = {} |
| 125 | + |
| 126 | + for relationship in info.token_relationships: |
| 127 | + token_id = str(relationship.token_id) if relationship.token_id is not None else None |
| 128 | + if token_id is None: |
| 129 | + continue |
| 130 | + |
| 131 | + token_relationships_response[token_id] = TokenRelationshipResponse( |
| 132 | + tokenId=token_id, |
| 133 | + symbol=relationship.symbol, |
| 134 | + balance=str(relationship.balance) if relationship.balance is not None else "0", |
| 135 | + kycStatus=_enum_name(relationship.kyc_status), |
| 136 | + freezeStatus=_enum_name(relationship.freeze_status), |
| 137 | + decimals=str(relationship.decimals) if relationship.decimals is not None else None, |
| 138 | + automaticAssociation=relationship.automatic_association, |
89 | 139 | ) |
90 | 140 |
|
91 | | - token_relationships_response = [] |
92 | | - if info.token_relationships: |
93 | | - for rel in info.token_relationships: |
94 | | - token_relationships_response.append( |
95 | | - TokenRelationshipResponse( |
96 | | - tokenId=str(rel.token_id) if rel.token_id else None, |
97 | | - symbol=rel.symbol, |
98 | | - balance=str(rel.balance) if rel.balance is not None else None, |
99 | | - kycStatus=str(rel.kyc_status) if rel.kyc_status is not None else None, |
100 | | - freezeStatus=str(rel.freeze_status) if rel.freeze_status is not None else None, |
101 | | - decimals=str(rel.decimals) if rel.decimals is not None else None, |
102 | | - automaticAssociation=rel.automatic_association, |
103 | | - ) |
104 | | - ) |
| 141 | + return token_relationships_response |
| 142 | + |
105 | 143 |
|
| 144 | +def _build_account_info_response(info: AccountInfo) -> GetAccountInfoResponse: |
106 | 145 | return GetAccountInfoResponse( |
107 | | - accountId=str(info.account_id) if info.account_id else None, |
108 | | - contractAccountId=info.contract_account_id, |
109 | | - isDeleted=info.is_deleted, |
110 | | - proxyReceived=str(info.proxy_received.to_tinybars()) if info.proxy_received else None, |
111 | | - key=info.key.to_bytes().hex() if info.key else None, |
112 | | - balance=str(info.balance.to_tinybars()) if info.balance else None, |
113 | | - isReceiverSignatureRequired=info.receiver_signature_required, |
114 | | - expirationTime=str(info.expiration_time) if info.expiration_time else None, |
115 | | - autoRenewPeriod=str(info.auto_renew_period.seconds) if info.auto_renew_period else None, |
116 | | - tokenRelationships=token_relationships_response if token_relationships_response else None, |
117 | | - accountMemo=info.account_memo, |
118 | | - ownedNfts=str(info.owned_nfts) if info.owned_nfts is not None else None, |
| 146 | + accountId=str(info.account_id) if info.account_id is not None else None, |
| 147 | + contractAccountId=info.contract_account_id or "", |
| 148 | + isDeleted=bool(info.is_deleted), |
| 149 | + proxyAccountId="", |
| 150 | + proxyReceived=str(info.proxy_received.to_tinybars()) |
| 151 | + if info.proxy_received is not None |
| 152 | + else "0", |
| 153 | + key=_serialize_key(info.key), |
| 154 | + balance=str(info.balance.to_tinybars()) if info.balance is not None else "0", |
| 155 | + sendRecordThreshold="0", |
| 156 | + receiveRecordThreshold="0", |
| 157 | + isReceiverSignatureRequired=bool(info.receiver_signature_required), |
| 158 | + expirationTime=str(info.expiration_time) if info.expiration_time is not None else None, |
| 159 | + autoRenewPeriod=str(info.auto_renew_period.seconds) |
| 160 | + if info.auto_renew_period is not None |
| 161 | + else "0", |
| 162 | + tokenRelationships=_to_token_relationships_response(info), |
| 163 | + accountMemo=info.account_memo or "", |
| 164 | + ownedNfts=str(info.owned_nfts) if info.owned_nfts is not None else "0", |
119 | 165 | maxAutomaticTokenAssociations=str(info.max_automatic_token_associations) |
120 | 166 | if info.max_automatic_token_associations is not None |
121 | | - else None, |
122 | | - stakingInfo=staking_info_response, |
| 167 | + else "0", |
| 168 | + aliasKey="", |
| 169 | + ledgerId="", |
| 170 | + ethereumNonce="0", |
| 171 | + stakingInfo=_to_staking_info_response(info), |
123 | 172 | ) |
124 | 173 |
|
125 | 174 |
|
126 | 175 | @rpc_method("getAccountInfo") |
127 | 176 | def get_account_info(params: GetAccountInfoParams) -> GetAccountInfoResponse: |
128 | 177 | client = get_client(params.sessionId) |
129 | 178 |
|
130 | | - query = AccountInfoQuery() |
131 | | - if params.accountId: |
132 | | - query.set_account_id(AccountId.from_string(params.accountId)) |
| 179 | + if not params.accountId: |
| 180 | + raise JsonRpcError.hiero_error({"status": ResponseCode.INVALID_ACCOUNT_ID.name}) |
| 181 | + |
| 182 | + try: |
| 183 | + account_id = AccountId.from_string(params.accountId) |
| 184 | + except (TypeError, ValueError) as error: |
| 185 | + raise JsonRpcError.hiero_error( |
| 186 | + {"status": ResponseCode.INVALID_ACCOUNT_ID.name} |
| 187 | + ) from error |
| 188 | + |
| 189 | + query = AccountInfoQuery().set_account_id(account_id) |
133 | 190 |
|
134 | 191 | info = query.execute(client) |
135 | 192 | return _build_account_info_response(info) |
0 commit comments