|
| 1 | +""" |
| 2 | +Example demonstrating schedule info query on the network. |
| 3 | +""" |
| 4 | + |
| 5 | +import datetime |
| 6 | +import os |
| 7 | +import sys |
| 8 | + |
| 9 | +from dotenv import load_dotenv |
| 10 | + |
| 11 | +from hiero_sdk_python import AccountId, Client, Hbar, Network, PrivateKey |
| 12 | +from hiero_sdk_python.account.account_create_transaction import AccountCreateTransaction |
| 13 | +from hiero_sdk_python.response_code import ResponseCode |
| 14 | +from hiero_sdk_python.schedule.schedule_info_query import ScheduleInfoQuery |
| 15 | +from hiero_sdk_python.timestamp import Timestamp |
| 16 | +from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction |
| 17 | + |
| 18 | +load_dotenv() |
| 19 | + |
| 20 | + |
| 21 | +def setup_client(): |
| 22 | + """Initialize and set up the client with operator account""" |
| 23 | + network = Network(network="testnet") |
| 24 | + client = Client(network) |
| 25 | + |
| 26 | + operator_id = AccountId.from_string(os.getenv("OPERATOR_ID")) |
| 27 | + operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY")) |
| 28 | + client.set_operator(operator_id, operator_key) |
| 29 | + |
| 30 | + return client |
| 31 | + |
| 32 | + |
| 33 | +def create_account(client): |
| 34 | + """Create a test account""" |
| 35 | + account_private_key = PrivateKey.generate_ed25519() |
| 36 | + account_public_key = account_private_key.public_key() |
| 37 | + |
| 38 | + receipt = ( |
| 39 | + AccountCreateTransaction() |
| 40 | + .set_key(account_public_key) |
| 41 | + .set_initial_balance(Hbar(2)) |
| 42 | + .set_account_memo("Test account for schedule") |
| 43 | + .freeze_with(client) |
| 44 | + .sign(account_private_key) |
| 45 | + .execute(client) |
| 46 | + ) |
| 47 | + |
| 48 | + if receipt.status != ResponseCode.SUCCESS: |
| 49 | + print(f"Account creation failed with status: {ResponseCode(receipt.status).name}") |
| 50 | + sys.exit(1) |
| 51 | + |
| 52 | + account_id = receipt.account_id |
| 53 | + print(f"\nAccount created with ID: {account_id}") |
| 54 | + |
| 55 | + return account_id, account_private_key |
| 56 | + |
| 57 | + |
| 58 | +def create_schedule(client, account_id, account_private_key): |
| 59 | + """Create a scheduled transaction""" |
| 60 | + # Amount to transfer in tinybars |
| 61 | + amount = Hbar(1).to_tinybars() |
| 62 | + |
| 63 | + # Create a transfer transaction |
| 64 | + transfer_tx = ( |
| 65 | + TransferTransaction() |
| 66 | + .add_hbar_transfer(account_id, -amount) |
| 67 | + .add_hbar_transfer(client.operator_account_id, amount) |
| 68 | + ) |
| 69 | + |
| 70 | + # Convert the transfer transaction into a scheduled transaction |
| 71 | + schedule_tx = transfer_tx.schedule() |
| 72 | + |
| 73 | + # Set expiration time for the scheduled transaction (90 seconds from now) |
| 74 | + expiration_time = datetime.datetime.now() + datetime.timedelta(seconds=90) |
| 75 | + |
| 76 | + receipt = ( |
| 77 | + schedule_tx.set_payer_account_id( |
| 78 | + client.operator_account_id |
| 79 | + ) # payer of the transaction fee |
| 80 | + .set_admin_key( |
| 81 | + client.operator_private_key.public_key() |
| 82 | + ) # delete/modify the transaction |
| 83 | + .set_expiration_time(Timestamp.from_date(expiration_time)) |
| 84 | + .freeze_with(client) |
| 85 | + .sign( |
| 86 | + account_private_key |
| 87 | + ) # sign with the account private key as it transfers money |
| 88 | + .execute(client) |
| 89 | + ) |
| 90 | + |
| 91 | + if receipt.status != ResponseCode.SUCCESS: |
| 92 | + print( |
| 93 | + f"Schedule creation failed with status: {ResponseCode(receipt.status).name}" |
| 94 | + ) |
| 95 | + sys.exit(1) |
| 96 | + |
| 97 | + print(f"Schedule created with ID: {receipt.schedule_id}") |
| 98 | + |
| 99 | + return receipt.schedule_id |
| 100 | + |
| 101 | + |
| 102 | +def query_schedule_info(): |
| 103 | + """ |
| 104 | + Demonstrates querying a schedule info by: |
| 105 | + 1. Setting up client with operator account |
| 106 | + 2. Creating a test account |
| 107 | + 3. Creating a scheduled transaction |
| 108 | + 4. Querying the schedule info |
| 109 | + """ |
| 110 | + client = setup_client() |
| 111 | + |
| 112 | + # Create an account first |
| 113 | + account_id, account_private_key = create_account(client) |
| 114 | + |
| 115 | + # Create a schedule |
| 116 | + schedule_id = create_schedule(client, account_id, account_private_key) |
| 117 | + |
| 118 | + # Query the schedule info |
| 119 | + info = ScheduleInfoQuery().set_schedule_id(schedule_id).execute(client) |
| 120 | + |
| 121 | + print("\nSchedule Info:") |
| 122 | + print(f"Schedule ID: {info.schedule_id}") |
| 123 | + print(f"Creator Account ID: {info.creator_account_id}") |
| 124 | + print(f"Payer Account ID: {info.payer_account_id}") |
| 125 | + print(f"Deleted At: {info.deleted_at}") |
| 126 | + print(f"Executed At: {info.executed_at}") |
| 127 | + print(f"Expiration Time: {info.expiration_time}") |
| 128 | + print(f"Scheduled Transaction ID: {info.scheduled_transaction_id}") |
| 129 | + print(f"Schedule Memo: {info.schedule_memo}") |
| 130 | + print(f"Admin Key: {info.admin_key}") |
| 131 | + print(f"Signers: {len(info.signers)} signer(s)") |
| 132 | + for i, signer in enumerate(info.signers): |
| 133 | + print(f" Signer {i+1}: {signer}") |
| 134 | + print(f"Ledger ID: {info.ledger_id}") |
| 135 | + print(f"Wait For Expiry: {info.wait_for_expiry}") |
| 136 | + |
| 137 | + # Alternative print |
| 138 | + # print("\nSchedule Info:") |
| 139 | + # print(info) |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == "__main__": |
| 143 | + query_schedule_info() |
0 commit comments