forked from hiero-ledger/hiero-sdk-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschedule_delete.py
More file actions
143 lines (111 loc) · 4.39 KB
/
schedule_delete.py
File metadata and controls
143 lines (111 loc) · 4.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
Example demonstrating schedule deletion on the network.
"""
import datetime
import os
import sys
from dotenv import load_dotenv
from hiero_sdk_python.account.account_create_transaction import AccountCreateTransaction
from hiero_sdk_python.account.account_id import AccountId
from hiero_sdk_python.client.client import Client
from hiero_sdk_python.client.network import Network
from hiero_sdk_python.crypto.private_key import PrivateKey
from hiero_sdk_python.hbar import Hbar
from hiero_sdk_python.response_code import ResponseCode
from hiero_sdk_python.schedule.schedule_delete_transaction import (
ScheduleDeleteTransaction,
)
from hiero_sdk_python.schedule.schedule_info_query import ScheduleInfoQuery
from hiero_sdk_python.timestamp import Timestamp
from hiero_sdk_python.transaction.transfer_transaction import TransferTransaction
load_dotenv()
def setup_client():
"""Initialize and set up the client with operator account"""
network = Network(os.getenv('NETWORK'))
client = Client(network)
operator_id = AccountId.from_string(os.getenv("OPERATOR_ID"))
operator_key = PrivateKey.from_string(os.getenv("OPERATOR_KEY"))
client.set_operator(operator_id, operator_key)
return client
def create_account(client):
"""Create a test account"""
account_private_key = PrivateKey.generate_ed25519()
account_public_key = account_private_key.public_key()
receipt = (
AccountCreateTransaction()
.set_key(account_public_key)
.set_initial_balance(Hbar(2))
.set_account_memo("Test account for schedule")
.freeze_with(client)
.sign(account_private_key)
.execute(client)
)
if receipt.status != ResponseCode.SUCCESS:
print(
f"Account creation failed with status: {ResponseCode(receipt.status).name}"
)
sys.exit(1)
account_id = receipt.account_id
print(f"\nAccount created with ID: {account_id}")
return account_id, account_private_key
def create_schedule(client, account_id, account_private_key):
"""Create a scheduled transaction"""
# Amount to transfer in tinybars
amount = Hbar(1).to_tinybars()
# Create a transfer transaction
transfer_tx = (
TransferTransaction()
.add_hbar_transfer(account_id, -amount)
.add_hbar_transfer(client.operator_account_id, amount)
)
# Convert the transfer transaction into a scheduled transaction
schedule_tx = transfer_tx.schedule()
# Set expiration time for the scheduled transaction (90 seconds from now)
expiration_time = datetime.datetime.now() + datetime.timedelta(seconds=90)
receipt = (
schedule_tx.set_payer_account_id(
client.operator_account_id
) # payer of the transaction fee
.set_admin_key(
client.operator_private_key.public_key()
) # delete/modify the transaction
.set_expiration_time(Timestamp.from_date(expiration_time))
.set_wait_for_expiry(True) # wait to expire to execute
.freeze_with(client)
.sign(
account_private_key
) # sign with the account private key as it transfers money
.execute(client)
)
if receipt.status != ResponseCode.SUCCESS:
print(
f"Schedule creation failed with status: {ResponseCode(receipt.status).name}"
)
sys.exit(1)
print(f"Schedule created with ID: {receipt.schedule_id}")
return receipt.schedule_id
def schedule_delete():
"""
Demonstrates schedule deletion functionality by:
1. Setting up client with operator account
2. Creating a test account
3. Creating a scheduled transaction
4. Deleting the scheduled transaction
"""
client = setup_client()
# Create an account first
account_id, account_private_key = create_account(client)
# Create a schedule
schedule_id = create_schedule(client, account_id, account_private_key)
# Delete the schedule
print("\nDeleting schedule...")
receipt = ScheduleDeleteTransaction().set_schedule_id(schedule_id).execute(client)
if receipt.status != ResponseCode.SUCCESS:
print(
f"Schedule deletion failed with status: {ResponseCode(receipt.status).name}"
)
sys.exit(1)
info = ScheduleInfoQuery().set_schedule_id(schedule_id).execute(client)
print(f"Schedule {info.schedule_id} deleted at {info.deleted_at}")
if __name__ == "__main__":
schedule_delete()