|
| 1 | +""" |
| 2 | +Example demonstrating contract info query on the network. |
| 3 | +
|
| 4 | +This module shows how to query a contract info on the network by: |
| 5 | +1. Setting up a client with operator credentials |
| 6 | +2. Creating a file containing contract bytecode |
| 7 | +3. Creating a contract using the file |
| 8 | +4. Querying the contract info |
| 9 | +
|
| 10 | +Usage: |
| 11 | + # Due to the way the script is structured, it must be run as a module |
| 12 | + # from the project root directory |
| 13 | +
|
| 14 | + # Run from the project root directory |
| 15 | + python -m examples.query_contract_info |
| 16 | +
|
| 17 | +""" |
| 18 | + |
| 19 | +import os |
| 20 | +import sys |
| 21 | + |
| 22 | +from dotenv import load_dotenv |
| 23 | + |
| 24 | +from hiero_sdk_python import AccountId, Client, Duration, Network, PrivateKey |
| 25 | +from hiero_sdk_python.contract.contract_create_transaction import ( |
| 26 | + ContractCreateTransaction, |
| 27 | +) |
| 28 | +from hiero_sdk_python.contract.contract_info_query import ContractInfoQuery |
| 29 | +from hiero_sdk_python.file.file_create_transaction import FileCreateTransaction |
| 30 | +from hiero_sdk_python.response_code import ResponseCode |
| 31 | + |
| 32 | +# Import the bytecode for a simple smart contract (SimpleContract.sol) that can be deployed |
| 33 | +# The contract bytecode is pre-compiled from Solidity source code |
| 34 | +from .contracts import SIMPLE_CONTRACT_BYTECODE |
| 35 | + |
| 36 | +load_dotenv() |
| 37 | + |
| 38 | + |
| 39 | +def setup_client(): |
| 40 | + """Initialize and set up the client with operator account""" |
| 41 | + network = Network(network="testnet") |
| 42 | + client = Client(network) |
| 43 | + |
| 44 | + operator_id = AccountId.from_string(os.getenv("OPERATOR_ID")) |
| 45 | + operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY")) |
| 46 | + client.set_operator(operator_id, operator_key) |
| 47 | + |
| 48 | + return client |
| 49 | + |
| 50 | + |
| 51 | +def create_contract_file(client): |
| 52 | + """Create a file containing the simple contract bytecode""" |
| 53 | + file_receipt = ( |
| 54 | + FileCreateTransaction() |
| 55 | + .set_keys(client.operator_private_key.public_key()) |
| 56 | + .set_contents(SIMPLE_CONTRACT_BYTECODE) |
| 57 | + .set_file_memo("Simple contract bytecode file") |
| 58 | + .execute(client) |
| 59 | + ) |
| 60 | + |
| 61 | + # Check if file creation was successful |
| 62 | + if file_receipt.status != ResponseCode.SUCCESS: |
| 63 | + print( |
| 64 | + f"File creation failed with status: {ResponseCode(file_receipt.status).name}" |
| 65 | + ) |
| 66 | + sys.exit(1) |
| 67 | + |
| 68 | + return file_receipt.file_id |
| 69 | + |
| 70 | + |
| 71 | +def create_contract(client, file_id): |
| 72 | + """Create a contract using the file""" |
| 73 | + receipt = ( |
| 74 | + ContractCreateTransaction() |
| 75 | + .set_admin_key(client.operator_private_key.public_key()) |
| 76 | + .set_initial_balance(1000) # 1000 tinybars |
| 77 | + .set_max_automatic_token_associations(10) |
| 78 | + .set_auto_renew_account_id(client.operator_account_id) |
| 79 | + .set_auto_renew_period(Duration(seconds=5184000)) # 60 days in seconds |
| 80 | + .set_gas(1000000) # 1M gas |
| 81 | + .set_bytecode_file_id(file_id) |
| 82 | + .set_contract_memo("Simple smart contract") |
| 83 | + .execute(client) |
| 84 | + ) |
| 85 | + |
| 86 | + # Check if contract creation was successful |
| 87 | + if receipt.status != ResponseCode.SUCCESS: |
| 88 | + print( |
| 89 | + f"Contract creation failed with status: {ResponseCode(receipt.status).name}" |
| 90 | + ) |
| 91 | + sys.exit(1) |
| 92 | + |
| 93 | + print(f"Contract created with ID: {receipt.contract_id}") |
| 94 | + |
| 95 | + return receipt.contract_id |
| 96 | + |
| 97 | + |
| 98 | +def query_contract_info(): |
| 99 | + """ |
| 100 | + Demonstrates querying a contract info by: |
| 101 | + 1. Setting up client with operator account |
| 102 | + 2. Creating a file containing contract bytecode |
| 103 | + 3. Creating a contract using the file |
| 104 | + 4. Querying the contract info |
| 105 | + """ |
| 106 | + client = setup_client() |
| 107 | + |
| 108 | + file_id = create_contract_file(client) |
| 109 | + |
| 110 | + contract_id = create_contract(client, file_id) |
| 111 | + |
| 112 | + # Query the contract info |
| 113 | + info = ContractInfoQuery().set_contract_id(contract_id).execute(client) |
| 114 | + |
| 115 | + print("\nContract Info:") |
| 116 | + print(f"Contract ID: {info.contract_id}") |
| 117 | + print(f"Account ID: {info.account_id}") |
| 118 | + print(f"Contract Account ID: {info.contract_account_id}") |
| 119 | + print(f"Admin Key: {info.admin_key}") |
| 120 | + print(f"Auto Renew Account ID: {info.auto_renew_account_id}") |
| 121 | + print(f"Contract Memo: {info.contract_memo}") |
| 122 | + print(f"Balance: {info.balance}") |
| 123 | + print(f"Expiration Time: {info.expiration_time}") |
| 124 | + print(f"Is Deleted: {info.is_deleted}") |
| 125 | + print(f"Ledger ID: {info.ledger_id}") |
| 126 | + print(f"Max Automatic Token Associations: {info.max_automatic_token_associations}") |
| 127 | + print(f"Token Relationships: {info.token_relationships}") |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + query_contract_info() |
0 commit comments