2727 uv run examples/account/account_allowance_approve_transaction_nft.py
2828"""
2929
30-
3130import os
3231import sys
3332from dotenv import load_dotenv
4645 NftId ,
4746 TokenAssociateTransaction ,
4847 AccountAllowanceApproveTransaction ,
49- TransferTransaction
48+ TransferTransaction ,
5049)
5150from hiero_sdk_python .account .account_create_transaction import AccountCreateTransaction
5251
5352load_dotenv ()
5453
55- network_name = os .getenv ('NETWORK' , 'testnet' ).lower ()
54+ network_name = os .getenv ("NETWORK" , "testnet" ).lower ()
55+
5656
5757def setup_client ():
5858 """Initialize and set up the client with operator account"""
@@ -70,32 +70,34 @@ def setup_client():
7070
7171 operator_id = AccountId .from_string (operator_id_str )
7272 operator_key = PrivateKey .from_string (operator_key_str )
73-
73+
7474 client .set_operator (operator_id , operator_key )
7575 print (f"Client setup for NFT Owner (Operator): { client .operator_account_id } " )
7676 return client , operator_id , operator_key
7777
78+
7879def create_account (client , memo = "Test Account" ):
7980 """Create a new Hedera account with an initial balance."""
8081 private_key = PrivateKey .generate_ed25519 ()
8182 public_key = private_key .public_key ()
82-
83+
8384 tx = (
8485 AccountCreateTransaction ()
8586 .set_key (public_key )
8687 .set_initial_balance (Hbar (10 ))
8788 .set_account_memo (memo )
8889 .execute (client )
8990 )
90-
91+
9192 if tx .status != ResponseCode .SUCCESS :
9293 print (f"Account creation failed: { ResponseCode (tx .status ).name } " )
9394 sys .exit (1 )
94-
95+
9596 account_id = tx .account_id
9697 print (f"Created new account ({ memo } ): { account_id } " )
9798 return account_id , private_key
9899
100+
99101def create_nft_token (client , owner_id , owner_key ):
100102 """Create a new non-fungible token (NFT) with the owner as treasury."""
101103
@@ -113,16 +115,17 @@ def create_nft_token(client, owner_id, owner_key):
113115 .freeze_with (client )
114116 .sign (owner_key )
115117 )
116-
118+
117119 receipt = tx .execute (client )
118-
120+
119121 if receipt .status != ResponseCode .SUCCESS :
120122 print (f"Token creation failed: { ResponseCode (receipt .status ).name } " )
121123 sys .exit (1 )
122-
124+
123125 print (f"NFT Owner ({ owner_id } ) created NFT Token: { receipt .token_id } " )
124126 return receipt .token_id
125127
128+
126129def mint_nft (client , token_id , metadata_list ):
127130 """Mint NFT(s) with metadata."""
128131 tx = (
@@ -131,15 +134,18 @@ def mint_nft(client, token_id, metadata_list):
131134 .set_metadata (metadata_list )
132135 .execute (client )
133136 )
134-
137+
135138 if tx .status != ResponseCode .SUCCESS :
136139 print (f"Mint failed: { ResponseCode (tx .status ).name } " )
137140 sys .exit (1 )
138-
141+
139142 serials = tx .serial_numbers
140- print (f"NFT Owner ({ client .operator_account_id } ) minted { len (serials )} NFT(s) for Token { token_id } : { serials } " )
143+ print (
144+ f"NFT Owner ({ client .operator_account_id } ) minted { len (serials )} NFT(s) for Token { token_id } : { serials } "
145+ )
141146 return [NftId (token_id , s ) for s in serials ]
142147
148+
143149def associate_token_with_account (client , account_id , private_key , token_id ):
144150 """Associate a token with an account."""
145151 tx = (
@@ -150,46 +156,53 @@ def associate_token_with_account(client, account_id, private_key, token_id):
150156 .sign (private_key )
151157 .execute (client )
152158 )
153-
159+
154160 if tx .status != ResponseCode .SUCCESS :
155161 print (f"Association failed: { ResponseCode (tx .status ).name } " )
156162 sys .exit (1 )
157-
163+
158164 print (f"Associated token { token_id } with Receiver account { account_id } " )
159165
166+
160167def approve_nft_allowance (client , nft_id , owner_id , spender_id , owner_key ):
161168 """Approve NFT allowance for a spender."""
162169 tx = (
163170 AccountAllowanceApproveTransaction ()
164- .approve_token_nft_allowance_all_serials (
165- nft_id .token_id , owner_id , spender_id
166- )
171+ .approve_token_nft_allowance_all_serials (nft_id .token_id , owner_id , spender_id )
167172 .freeze_with (client )
168173 .sign (owner_key )
169174 .execute (client )
170175 )
171-
176+
172177 if tx .status != ResponseCode .SUCCESS :
173178 print (f"Approval failed: { ResponseCode (tx .status ).name } " )
174179 sys .exit (1 )
175-
176- print (f"NFT Owner ({ owner_id } ) approved Spender ({ spender_id } ) for NFT { nft_id .token_id } (all serials)" )
180+
181+ print (
182+ f"NFT Owner ({ owner_id } ) approved Spender ({ spender_id } ) for NFT { nft_id .token_id } (all serials)"
183+ )
184+
177185
178186def transfer_nft_using_allowance (spender_client , nft_id , owner_id , receiver_id ):
179187 """Transfer an NFT using approved allowance via the spender client."""
180- print (f"Spender ({ spender_client .operator_account_id } ) transferring NFT { nft_id } from Owner ({ owner_id } )..." )
181-
188+ print (
189+ f"Spender ({ spender_client .operator_account_id } ) transferring NFT { nft_id } from Owner ({ owner_id } )..."
190+ )
191+
182192 tx = (
183193 TransferTransaction ()
184194 .add_approved_nft_transfer (nft_id , owner_id , receiver_id )
185195 .execute (spender_client )
186196 )
187-
197+
188198 if tx .status != ResponseCode .SUCCESS :
189199 print (f"Transfer failed: { ResponseCode (tx .status ).name } " )
190200 sys .exit (1 )
191-
192- print (f"SUCCESS: Spender ({ spender_client .operator_account_id } ) transferred NFT { nft_id } to Receiver ({ receiver_id } )" )
201+
202+ print (
203+ f"SUCCESS: Spender ({ spender_client .operator_account_id } ) transferred NFT { nft_id } to Receiver ({ receiver_id } )"
204+ )
205+
193206
194207def main ():
195208 """End-to-end demonstration."""
@@ -216,16 +229,18 @@ def main():
216229 spender_client = Client (owner_client .network )
217230 spender_client .set_operator (spender_id , spender_key )
218231 print (f"Client setup for Spender: { spender_id } " )
219-
232+
220233 # Transfer NFT using the allowance
221234 transfer_nft_using_allowance (spender_client , nft_id , owner_id , receiver_id )
222235
223236 except Exception as e :
224237 print (f"Error: { e } " )
225238 import traceback
239+
226240 traceback .print_exc ()
227241 finally :
228242 owner_client .close ()
229243
244+
230245if __name__ == "__main__" :
231246 main ()
0 commit comments