-
Notifications
You must be signed in to change notification settings - Fork 169
feat: add contract_id support for CryptoGetAccountBalanceQuery #1389
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
exploreriii
merged 19 commits into
hiero-ledger:main
from
AntonioCeppellini:1293-add-contract_id-support-for-CryptoGetAccountBalanceQuery
Jan 11, 2026
Merged
Changes from 12 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
780941e
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini 1b25945
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini 0080627
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini b34cbb4
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini df9ac72
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini 3a7b1a4
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini 341585b
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini 522c8d9
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini cd2a455
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini 78afcc5
Merge branch 'main' into 1293-add-contract_id-support-for-CryptoGetAc…
AntonioCeppellini b776d26
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini 238d85f
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini d70c6ef
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini 1d0a3df
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini 7e62179
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini fa540c7
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini ae391d0
Merge branch 'main' into 1293-add-contract_id-support-for-CryptoGetAc…
AntonioCeppellini 33f2bd0
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini 1130e58
feat: add contract_id support for CryptoGetAccountBalanceQuery
AntonioCeppellini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| """ | ||
| Contract Balance Query Example | ||
|
|
||
| This script demonstrates how to: | ||
| 1. Set up a client connection to the Hedera network | ||
| 2. Create a file containing contract bytecode | ||
| 3. Create a contract | ||
| 4. Query the contract balance using CryptoGetAccountBalanceQuery.set_contract_id() | ||
|
|
||
| Run with: | ||
| uv run -m examples.contract.contract_balance_query | ||
| python -m examples.contract.contract_balance_query | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
| from dotenv import load_dotenv | ||
|
|
||
| from hiero_sdk_python import ( | ||
| Client, | ||
| ContractCreateTransaction, | ||
| CryptoGetAccountBalanceQuery, | ||
| Hbar, | ||
| ) | ||
|
|
||
| from hiero_sdk_python.contract.contract_id import ContractId | ||
|
|
||
| from .contracts import SIMPLE_CONTRACT_BYTECODE | ||
| from hiero_sdk_python.response_code import ResponseCode | ||
|
|
||
| load_dotenv() | ||
|
|
||
|
|
||
| def setup_client() -> Client: | ||
| print("Initializing client from environment variables...") | ||
| try: | ||
| client = Client.from_env() | ||
| print(f"✅ Success! Connected as operator: {client.operator_account_id}") | ||
| except Exception as e: | ||
| print(f"❌ Failed: {e}") | ||
| sys.exit(1) | ||
|
|
||
| return client | ||
|
|
||
|
|
||
| def create_contract(client: Client, initial_balance_tinybars: int) -> ContractId: | ||
| """Create a contract using the bytecode file and return its ContractId.""" | ||
| bytecode = bytes.fromhex(SIMPLE_CONTRACT_BYTECODE) | ||
|
|
||
| receipt = ( | ||
| ContractCreateTransaction() | ||
| .set_bytecode(bytecode) | ||
| .set_gas(2_000_000) | ||
| .set_initial_balance(initial_balance_tinybars) | ||
| .set_contract_memo("Contract for balance query example") | ||
| .execute(client) | ||
| ) | ||
|
|
||
| status_code = ResponseCode(receipt.status) | ||
| status_name = status_code.name | ||
|
|
||
| if status_name == ResponseCode.SUCCESS.name: | ||
| print("✅ Transaction succeeded!") | ||
| elif status_code.is_unknown: | ||
| print(f"❓ Unknown transaction status: {status_name}") | ||
| sys.exit(1) | ||
| else: | ||
| print("❌ Transaction failed!") | ||
| sys.exit(1) | ||
|
|
||
| return receipt.contract_id | ||
|
|
||
|
|
||
| def get_contract_balance(client: Client, contract_id: ContractId): | ||
| """Query contract balance using CryptoGetAccountBalanceQuery.set_contract_id().""" | ||
| print(f"Querying balance for contract {contract_id} ...") | ||
| balance = CryptoGetAccountBalanceQuery().set_contract_id(contract_id).execute(client) | ||
|
|
||
| print("✅ Balance retrieved successfully!") | ||
| print(f" Contract: {contract_id}") | ||
| print(f" Hbars: {balance.hbars}") | ||
| return balance | ||
|
|
||
|
|
||
| def main(): | ||
| try: | ||
| client = setup_client() | ||
|
|
||
| initial_balance_tinybars = Hbar(1) | ||
| contract_id = create_contract(client, initial_balance_tinybars.to_tinybars()) | ||
|
|
||
| print(f"✅ Contract created with ID: {contract_id}") | ||
| get_contract_balance(client, contract_id) | ||
|
|
||
| except Exception as e: | ||
| print(f"❌Error: {e}", file=sys.stderr) | ||
| sys.exit(1) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,100 @@ | ||
| import pytest | ||
|
|
||
| from hiero_sdk_python.account.account_id import AccountId | ||
| from hiero_sdk_python.contract.contract_id import ContractId | ||
| from hiero_sdk_python.query.account_balance_query import CryptoGetAccountBalanceQuery | ||
| from hiero_sdk_python.contract.contract_create_transaction import ContractCreateTransaction | ||
| from hiero_sdk_python.response_code import ResponseCode | ||
| from tests.integration.utils import IntegrationTestEnv | ||
exploreriii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| from examples.contract.contracts import SIMPLE_CONTRACT_BYTECODE | ||
|
|
||
|
|
||
| pytestmark = pytest.mark.integration | ||
|
|
||
|
|
||
| def _create_test_contract(env: IntegrationTestEnv): | ||
| bytecode = bytes.fromhex(SIMPLE_CONTRACT_BYTECODE) | ||
|
|
||
| receipt = ( | ||
| ContractCreateTransaction() | ||
| .set_bytecode(bytecode) | ||
| .set_gas(2_000_000) | ||
| .set_contract_memo("integration test: contract balance query") | ||
| .execute(env.client) | ||
| ) | ||
|
|
||
| if ResponseCode(receipt.status) != ResponseCode.SUCCESS: | ||
| raise RuntimeError( | ||
| f"ContractCreateTransaction failed with status: {ResponseCode(receipt.status).name}" | ||
| ) | ||
|
|
||
| if receipt.contract_id is None: | ||
| raise RuntimeError("ContractCreateTransaction succeeded but receipt.contract_id is None") | ||
|
|
||
| return receipt.contract_id | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| @pytest.mark.integration | ||
| def test_integration_account_balance_query_can_execute(): | ||
| env = IntegrationTestEnv() | ||
|
|
||
| try: | ||
| CryptoGetAccountBalanceQuery(account_id=env.operator_id).execute(env.client) | ||
| balance = CryptoGetAccountBalanceQuery(account_id=env.operator_id).execute(env.client) | ||
| assert balance is not None | ||
| assert hasattr(balance, "hbars") | ||
| finally: | ||
| env.close() | ||
AntonioCeppellini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
AntonioCeppellini marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def test_integration_contract_balance_query_can_execute(): | ||
| env = IntegrationTestEnv() | ||
| try: | ||
| contract_id = _create_test_contract(env) | ||
|
|
||
| balance = CryptoGetAccountBalanceQuery().set_contract_id(contract_id).execute(env.client) | ||
|
|
||
| assert balance is not None | ||
| assert hasattr(balance, "hbars") | ||
| assert balance.hbars.to_tinybars() >= 0 | ||
| finally: | ||
| env.close() | ||
exploreriii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def test_integration_balance_query_raises_when_neither_source_set(): | ||
| env = IntegrationTestEnv() | ||
| try: | ||
| with pytest.raises(ValueError, match=r"Either Account ID or Contract ID must be set before making the request\."): | ||
| CryptoGetAccountBalanceQuery().execute(env.client) | ||
| finally: | ||
| env.close() | ||
|
|
||
|
|
||
| def test_integration_balance_query_raises_when_both_sources_set(): | ||
| env = IntegrationTestEnv() | ||
| try: | ||
| query = CryptoGetAccountBalanceQuery( | ||
| account_id=env.operator_id, | ||
| contract_id=ContractId(0, 0, 1234), | ||
| ) | ||
|
|
||
| with pytest.raises(ValueError, match=r"Specify either account_id or contract_id, not both\."): | ||
| query.execute(env.client) | ||
| finally: | ||
| env.close() | ||
AntonioCeppellini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_integration_balance_query_with_invalid_account_id_raises(): | ||
| env = IntegrationTestEnv() | ||
| try: | ||
| with pytest.raises(ValueError, match=r"account_id must be an AccountId\."): | ||
AntonioCeppellini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CryptoGetAccountBalanceQuery().set_account_id("0.0.12345").execute(env.client) | ||
| finally: | ||
| env.close() | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
|
|
||
| def test_integration_balance_query_with_invalid_contract_id_raises(): | ||
| env = IntegrationTestEnv() | ||
| try: | ||
| with pytest.raises(ValueError, match=r"contract_id must be a ContractId\."): | ||
AntonioCeppellini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| CryptoGetAccountBalanceQuery().set_contract_id("0.0.12345").execute(env.client) | ||
| finally: | ||
| env.close() | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.