1616from hiero_sdk_python .transaction .transfer_transaction import TransferTransaction
1717
1818load_dotenv ()
19- network_name = os .getenv (' NETWORK' , ' testnet' ).lower ()
19+ network_name = os .getenv (" NETWORK" , " testnet" ).lower ()
2020
21- def setup_client ():
22- """Initialize and set up the client with operator account"""
21+
22+ def setup_client () -> Client :
23+ """Initialize and set up the client with operator account using env vars."""
2324 network = Network (network_name )
2425 print (f"Connecting to Hedera { network_name } network!" )
2526 client = Client (network )
2627
27- operator_id = AccountId .from_string (os .getenv ("OPERATOR_ID" ,"" ))
28- operator_key = PrivateKey .from_string (os .getenv ("OPERATOR_KEY" ,"" ))
28+ operator_id = AccountId .from_string (os .getenv ("OPERATOR_ID" , "" ))
29+ operator_key = PrivateKey .from_string (os .getenv ("OPERATOR_KEY" , "" ))
2930 client .set_operator (operator_id , operator_key )
3031 print (f"Client set up with operator id { client .operator_account_id } " )
3132
3233 return client
3334
3435
35- def create_account (client ):
36- """Create an account"""
36+ def create_account (client : Client ):
37+ """Create a new Hedera account with initial balance. """
3738 account_private_key = PrivateKey .generate_ed25519 ()
3839 account_public_key = account_private_key .public_key ()
3940
@@ -46,77 +47,80 @@ def create_account(client):
4647 )
4748
4849 if account_receipt .status != ResponseCode .SUCCESS :
49- print (f"Account creation failed with status: { ResponseCode (account_receipt .status ).name } " )
50+ print (
51+ "Account creation failed with status: "
52+ f"{ ResponseCode (account_receipt .status ).name } "
53+ )
5054 sys .exit (1 )
5155
5256 account_account_id = account_receipt .account_id
5357
5458 return account_account_id , account_private_key
5559
5660
57- def approve_hbar_allowance (client , owner_account_id , spender_account_id , amount ):
58- """Approve Hbar allowance for spender"""
61+ def approve_hbar_allowance (
62+ client : Client ,
63+ owner_account_id : AccountId ,
64+ spender_account_id : AccountId ,
65+ amount : Hbar ,
66+ ):
67+ """Approve Hbar allowance for spender."""
5968 receipt = (
6069 AccountAllowanceApproveTransaction ()
6170 .approve_hbar_allowance (owner_account_id , spender_account_id , amount )
6271 .execute (client )
6372 )
6473
6574 if receipt .status != ResponseCode .SUCCESS :
66- print (f"Hbar allowance approval failed with status: { ResponseCode (receipt .status ).name } " )
75+ print (
76+ "Hbar allowance approval failed with status: "
77+ f"{ ResponseCode (receipt .status ).name } "
78+ )
6779 sys .exit (1 )
6880
6981 print (f"Hbar allowance of { amount } approved for spender { spender_account_id } " )
7082 return receipt
7183
7284
73- def delete_hbar_allowance (client , owner_account_id , spender_account_id ):
74- """Delete hbar allowance by setting amount to 0"""
85+ def transfer_hbar_with_allowance (
86+ client : Client ,
87+ owner_account_id : AccountId ,
88+ spender_account_id : AccountId ,
89+ spender_private_key : PrivateKey ,
90+ receiver_account_id : AccountId ,
91+ amount : Hbar ,
92+ ):
93+ """Transfer hbars using a previously approved allowance."""
7594 receipt = (
76- AccountAllowanceApproveTransaction ()
77- .approve_hbar_allowance (owner_account_id , spender_account_id , Hbar (0 ))
95+ TransferTransaction ()
96+ # Transaction is paid for / initiated by spender
97+ .set_transaction_id (TransactionId .generate (spender_account_id ))
98+ .add_approved_hbar_transfer (owner_account_id , - amount .to_tinybars ())
99+ .add_approved_hbar_transfer (receiver_account_id , amount .to_tinybars ())
100+ .freeze_with (client )
101+ .sign (spender_private_key )
78102 .execute (client )
79103 )
80104
81105 if receipt .status != ResponseCode .SUCCESS :
82- print (f"Hbar allowance deletion failed with status: { ResponseCode (receipt .status ).name } " )
106+ print (f"Hbar transfer failed with status: { ResponseCode (receipt .status ).name } " )
83107 sys .exit (1 )
84108
85- print (f"Hbar allowance deleted for spender { spender_account_id } " )
86- return receipt
87-
88-
89- def transfer_hbar_without_allowance (client , spender_account_id , spender_private_key , amount ):
90- """Transfer hbars without allowance"""
91- print ("Trying to transfer hbars without allowance..." )
92- owner_account_id = client .operator_account_id
93- client .set_operator (spender_account_id , spender_private_key ) # Set operator to spender
94-
95- receipt = (
96- TransferTransaction ()
97- .add_approved_hbar_transfer (owner_account_id , - amount .to_tinybars ())
98- .add_approved_hbar_transfer (spender_account_id , amount .to_tinybars ())
99- .execute (client )
109+ print (
110+ f"Successfully transferred { amount } from { owner_account_id } "
111+ f"to { receiver_account_id } using allowance"
100112 )
101113
102- if receipt .status != ResponseCode .SPENDER_DOES_NOT_HAVE_ALLOWANCE :
103- print (
104- f"Hbar transfer should have failed with SPENDER_DOES_NOT_HAVE_ALLOWANCE "
105- f"status but got: { ResponseCode (receipt .status ).name } "
106- )
107-
108- print (f"Hbar transfer successfully failed with { ResponseCode (receipt .status ).name } status" )
114+ return receipt
109115
110116
111- def hbar_allowance ():
117+ def main ():
112118 """
113119 Demonstrates hbar allowance functionality by:
114120 1. Setting up client with operator account
115121 2. Creating spender and receiver accounts
116122 3. Approving hbar allowance for spender
117123 4. Transferring hbars using the allowance
118- 5. Deleting the allowance
119- 6. Attempting to transfer hbars without allowance (should fail)
120124 """
121125 client = setup_client ()
122126
@@ -129,33 +133,20 @@ def hbar_allowance():
129133
130134 # Approve hbar allowance for spender
131135 allowance_amount = Hbar (2 )
136+ owner_account_id = client .operator_account_id
132137
133- approve_hbar_allowance (client , client . operator_account_id , spender_id , allowance_amount )
138+ approve_hbar_allowance (client , owner_account_id , spender_id , allowance_amount )
134139
135140 # Transfer hbars using the allowance
136- receipt = (
137- TransferTransaction ()
138- .set_transaction_id (TransactionId .generate (spender_id ))
139- .add_approved_hbar_transfer (client .operator_account_id , - allowance_amount .to_tinybars ())
140- .add_approved_hbar_transfer (receiver_id , allowance_amount .to_tinybars ())
141- .freeze_with (client )
142- .sign (spender_private_key )
143- .execute (client )
141+ transfer_hbar_with_allowance (
142+ client = client ,
143+ owner_account_id = owner_account_id ,
144+ spender_account_id = spender_id ,
145+ spender_private_key = spender_private_key ,
146+ receiver_account_id = receiver_id ,
147+ amount = allowance_amount ,
144148 )
145149
146- if receipt .status != ResponseCode .SUCCESS :
147- print (f"Hbar transfer failed with status: { ResponseCode (receipt .status ).name } " )
148- sys .exit (1 )
149-
150- print (f"Successfully transferred { allowance_amount } from" , end = " " )
151- print (f"{ client .operator_account_id } to { receiver_id } using allowance" )
152-
153- # Delete allowance
154- delete_hbar_allowance (client , client .operator_account_id , spender_id )
155-
156- # Try to transfer hbars without allowance
157- transfer_hbar_without_allowance (client , spender_id , spender_private_key , allowance_amount )
158-
159150
160151if __name__ == "__main__" :
161- hbar_allowance ()
152+ main ()
0 commit comments