-
Notifications
You must be signed in to change notification settings - Fork 174
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 1 commit
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,95 @@ | ||
| """ | ||
| Contract Balance Query Example | ||
|
|
||
| This script demonstrates how to query the balance of a *contract* using: | ||
| - CryptoGetAccountBalanceQuery().set_contract_id(...) | ||
|
|
||
| Run with: | ||
| uv run examples/query/contract_balance_query.py | ||
| python examples/query/contract_balance_query.py | ||
|
|
||
| Environment variables required: | ||
| - OPERATOR_ID | ||
| - OPERATOR_KEY | ||
| Optional: | ||
| - NETWORK (default: testnet) | ||
| - CONTRACT_ID (e.g. "0.0.1234") | ||
| """ | ||
|
|
||
| import os | ||
| import sys | ||
| from dotenv import load_dotenv | ||
|
|
||
| from hiero_sdk_python import Network, Client, AccountId, PrivateKey | ||
| from hiero_sdk_python.query.account_balance_query import CryptoGetAccountBalanceQuery | ||
| from hiero_sdk_python.contract.contract_id import ContractId | ||
|
|
||
| load_dotenv() | ||
| network_name = os.getenv("NETWORK", "testnet").lower() | ||
|
|
||
|
|
||
| def setup_client(): | ||
| """ | ||
| Initialize and configure the Hiero SDK client with operator credentials. | ||
| """ | ||
| network = Network(network_name) | ||
| print(f"Connecting to Hedera {network_name} network!") | ||
| client = Client(network) | ||
|
|
||
| operator_id_str = os.getenv("OPERATOR_ID") | ||
| operator_key_str = os.getenv("OPERATOR_KEY") | ||
|
|
||
| if not operator_id_str or not operator_key_str: | ||
| raise ValueError("OPERATOR_ID and OPERATOR_KEY environment variables must be set") | ||
|
|
||
| operator_id = AccountId.from_string(operator_id_str) | ||
| operator_key = PrivateKey.from_string(operator_key_str) | ||
| client.set_operator(operator_id, operator_key) | ||
|
|
||
| print(f"Client set up with operator id {client.operator_account_id}") | ||
| return client | ||
|
|
||
|
|
||
| def get_contract_balance(client: Client, contract_id: ContractId): | ||
| """ | ||
| Query and retrieve the HBAR balance of a contract. | ||
|
|
||
| Use account_id when you want an *account* balance. | ||
| Use contract_id when you want a *contract* balance (smart contract entity). | ||
| """ | ||
| print(f"Querying balance for contract {contract_id} ...") | ||
|
|
||
| balance = CryptoGetAccountBalanceQuery().set_contract_id(contract_id).execute(client) | ||
|
|
||
| # AccountBalance object: print a friendly summary | ||
| print("✓ Balance retrieved successfully!") | ||
| print(f" Contract: {contract_id}") | ||
| print(f" Hbars: {balance.hbars}") | ||
| if getattr(balance, "token_balances", None): | ||
| print(f" Token balances entries: {len(balance.token_balances)}") | ||
| return balance | ||
|
|
||
|
|
||
| def main(): | ||
| try: | ||
| client = setup_client() | ||
|
|
||
| contract_id_str = os.getenv("CONTRACT_ID") | ||
| if not contract_id_str: | ||
| raise ValueError("CONTRACT_ID environment variable must be set (e.g. '0.0.1234')") | ||
|
|
||
| contract_id = ContractId.from_string(contract_id_str) | ||
|
|
||
| # Uncommenting the following would raise due to oneof constraints: | ||
| # query = CryptoGetAccountBalanceQuery(account_id=client.operator_account_id, contract_id=contract_id) | ||
| # query.execute(client) | ||
|
|
||
| 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,29 @@ | ||
| import pytest | ||
|
|
||
| from hiero_sdk_python.query.account_balance_query import CryptoGetAccountBalanceQuery | ||
| from hiero_sdk_python.contract.contract_id import ContractId | ||
| from tests.integration.utils import IntegrationTestEnv | ||
exploreriii marked this conversation as resolved.
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) | ||
| finally: | ||
| env.close() | ||
| env.close() | ||
|
|
||
| @pytest.mark.integration | ||
| def test_integration_contract_balance_query_can_execute(): | ||
| env = IntegrationTestEnv() | ||
|
|
||
| contract_id_str = os.getenv("HEDERA_TEST_CONTRACT_ID") | ||
AntonioCeppellini marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if not contract_id_str: | ||
| pytest.skip("Set HEDERA_TEST_CONTRACT_ID (e.g. '0.0.1234') to run this test.") | ||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| contract_id = ContractId.from_string(contract_id_str) | ||
|
|
||
| try: | ||
| CryptoGetAccountBalanceQuery().set_contract_id(contract_id).execute(env.client) | ||
| finally: | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| env.close() | ||
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.