11"""
22Hedera Token Airdrop Example Script
33
4- This script demonstrates and end-to-end example for an account to automatically (no user action required) claim a set of airdrops.
4+ This script demonstrates an end-to-end example for an account to
5+ automatically (no user action required) claim a set of airdrops.
56
67Unique configurations of this account:
78- 10 auto-association slots.
1920uv run examples/tokens/token_airdrop_claim_auto.py
2021python examples/tokens/token_airdrop_claim_auto.py
2122"""
23+ # pylint: disable=import-error,
24+ # pylint: disable=too-many-arguments,
25+ # pylint: disable=too-many-positional-arguments,
26+ # pylint: disable=protected-access,
27+ # pylint: disable=broad-exception-caught
28+
2229import os
2330import sys
2431from typing import Iterable
2532from dotenv import load_dotenv
26- from hiero_sdk_python import (
27- Client ,
33+ from hiero_sdk_python import (Client ,
2834 Network ,
2935 AccountId ,
3036 PrivateKey ,
4551load_dotenv ()
4652
4753def setup_client ():
54+ """Set up and return a Hedera client using environment configuration."""
4855 network_name = os .getenv ("NETWORK" , "testnet" )
4956
5057 # Validate environment variables
@@ -63,7 +70,9 @@ def setup_client():
6370 print (f"Client set up with operator id { client .operator_account_id } " )
6471
6572 except Exception as e :
66- raise ConnectionError (f"Error initializing client: { e } " )
73+ raise ConnectionError (
74+ f"Error initializing client: { e } "
75+ ) from e
6776
6877 print (f"✅ Connected to Hedera { network_name } network as operator: { operator_id } " )
6978 return client , operator_id , operator_key
@@ -73,6 +82,8 @@ def create_receiver(
7382 signature_required : bool = False ,
7483 max_auto_assoc : int = 10
7584 ):
85+
86+ """Create and return a configured Hedera client."""
7687 receiver_key = PrivateKey .generate ()
7788 receiver_public_key = receiver_key .public_key ()
7889
@@ -110,8 +121,9 @@ def create_fungible_token(
110121 initial_supply : int = 50 ,
111122 max_supply : int = 1000 ,
112123 ):
124+ """Create and return a fungible token on the Hedera network."""
113125 try :
114- receipt = (
126+ receipt = (
115127 TokenCreateTransaction ()
116128 .set_token_name (name )
117129 .set_token_symbol (symbol )
@@ -143,8 +155,9 @@ def create_nft_token(
143155 symbol : str = "MNT" ,
144156 max_supply : int = 100
145157 ):
158+ """Create and return a non-fungible (NFT) token on the Hedera network."""
146159 try :
147- receipt = (
160+ receipt = (
148161 TokenCreateTransaction ()
149162 .set_token_name (name )
150163 .set_token_symbol (symbol )
@@ -174,8 +187,9 @@ def mint_nft_token(
174187 operator_key : PrivateKey ,
175188 nft_token_id : TokenId ,
176189 ):
190+ """Mint a new NFT for the given NFT token and return its serial number."""
177191 try :
178- receipt = (
192+ receipt = (
179193 TokenMintTransaction ()
180194 .set_token_id (nft_token_id )
181195 .set_metadata ([b"NFT Metadata Example" ])
@@ -189,8 +203,8 @@ def mint_nft_token(
189203 if receipt .status != ResponseCode .SUCCESS :
190204 status_message = ResponseCode (receipt .status ).name
191205 raise RuntimeError (f"❌ NFT token mint failed: { status_message } " )
192-
193- print (f"✅ NFT { nft_token_id } serial { serial } minted with NFT id of { nft_id } . Total NFT supply is { total_supply } " )
206+ print ( f"✅ NFT { nft_token_id } serial { serial } minted with NFT id of { nft_id } ." )
207+ print (f"Total NFT supply is { total_supply } " )
194208 return nft_id
195209 except Exception as e :
196210 raise RuntimeError (f"❌ Error minting NFT token: { e } " ) from e
@@ -202,16 +216,25 @@ def log_balances(
202216 nft_ids : Iterable [NftId ],
203217 prefix : str = ""
204218):
219+ """Fetch and log token balances for operator and receiver accounts."""
205220 print (f"\n ===== { prefix } Balances =====" )
206221
207222 try :
208- operator_balance = CryptoGetAccountBalanceQuery ().set_account_id (operator_id ).execute (client )
209- receiver_balance = CryptoGetAccountBalanceQuery ().set_account_id (receiver_id ).execute (client )
210- except Exception as e :
223+ operator_balance = (
224+ CryptoGetAccountBalanceQuery ()
225+ .set_account_id (operator_id )
226+ .execute (client )
227+ )
228+ receiver_balance = (
229+ CryptoGetAccountBalanceQuery ()
230+ .set_account_id (receiver_id )
231+ .execute (client )
232+ )
233+ except Exception as e : # pylint: disable=broad-exception-caught
211234 print (f"❌ Failed to fetch balances: { e } " )
212235 return
213236
214- def log_fungible (account_id : AccountId , balances : dict , token_ids : Iterable [TokenId ]):
237+ def log_fungible (_account_id : AccountId , balances : dict , token_ids : Iterable [TokenId ]):
215238 print (" Fungible tokens:" )
216239 for token_id in token_ids :
217240 print (f" { token_id } : { balances .get (token_id , 0 )} " )
@@ -249,16 +272,23 @@ def perform_airdrop(
249272 receiver_id : AccountId ,
250273 fungible_ids : Iterable [TokenId ],
251274 nft_ids : Iterable [NftId ],
252- ft_amount : int = 100
275+ ft_amount : int = 100 ,
253276 ):
254-
277+ """Perform a token airdrop from operator to receiver."""
255278 try :
256279 tx = TokenAirdropTransaction ()
257280
258281 for fungible_id in fungible_ids :
259282 tx .add_token_transfer (fungible_id , operator_id , - ft_amount )
260283 tx .add_token_transfer (fungible_id , receiver_id , ft_amount )
261- print (f"📤 Transferring { ft_amount } of fungible token { fungible_id } from { operator_id } → { receiver_id } " )
284+
285+ message = (
286+ f"📤 Transferring { ft_amount } of fungible token { fungible_id } "
287+ f"from { operator_id } → { receiver_id } "
288+ )
289+ print (message )
290+
291+
262292
263293 for nft_id in nft_ids :
264294 tx .add_nft_transfer (nft_id , operator_id , receiver_id )
@@ -278,15 +308,24 @@ def perform_airdrop(
278308 raise RuntimeError ("Airdrop execution failed" ) from e
279309
280310def main ():
311+ """Run the token airdrop auto-claim example workflow."""
281312 # Set up client and return client, operator_id, operator_key
282313 client , operator_id , operator_key = setup_client ()
283314
284- # Create and return a fungible token to airdrop
315+ # Create and return a fungible token to airdrop
285316 print ("Create 50 fungible tokens and 1 NFT to airdrop" )
286- fungible_id = create_fungible_token (client , operator_id , operator_key , name = "My Fungible Token" , symbol = "123" , initial_supply = 50 , max_supply = 2000 )
287-
288- # Create and return an nft token to airdrop
289- nft_token_id = create_nft_token (client , operator_id , operator_key , name = "My NFT Token" , symbol = "MNFT" , max_supply = 1000 )
317+ fungible_id = create_fungible_token (client , operator_id ,
318+ operator_key ,
319+ name = "My Fungible Token" ,
320+ symbol = "123" , initial_supply = 50 ,
321+ max_supply = 2000
322+ )
323+
324+ # Create and return an nft token to airdrop
325+ nft_token_id = create_nft_token (client , operator_id ,
326+ operator_key , name = "My NFT Token" ,
327+ symbol = "MNFT" , max_supply = 1000
328+ )
290329
291330 # Mint and return an nft to airdrop
292331 nft_serial = mint_nft_token (client , operator_key , nft_token_id )
@@ -300,16 +339,38 @@ def main():
300339
301340 # Check pre-airdrop balances
302341 print ("\n 🔍 Verifying sender has tokens to airdrop and receiver neither:" )
303- log_balances (client , operator_id , receiver_id , [fungible_id ], [nft_serial ], prefix = "Before airdrop" )
342+ log_balances (client , operator_id , receiver_id , [fungible_id ], [nft_serial ],
343+ prefix = "Before airdrop" )
304344
305345 # Initiate airdrop of 20 fungible tokens and 1 nft token id
306- perform_airdrop (client , operator_id , operator_key , receiver_id , [fungible_id ], [nft_serial ], 20 )
307-
308- print ("\n 🔍 Verifying receiver has received airdrop contents automatically and sender has sent:" )
309- log_balances (client , operator_id , receiver_id , [fungible_id ], [nft_serial ], prefix = "After airdrop" )
310-
311- print ("✅ Auto-association successful: Receiver accepted airdropped tokens without pre-association." )
312- print ("✅ Airdrop successful: Receiver accepted new fungible tokens without pre-association." )
346+ perform_airdrop (client ,
347+ operator_id ,
348+ operator_key ,
349+ receiver_id ,
350+ [fungible_id ],
351+ [nft_serial ],
352+ 20
353+ )
354+
355+ print ("\n 🔍 Verifying receiver has received airdrop contents automatically"
356+ "and sender has sent:"
357+ )
358+ log_balances (
359+ client ,
360+ operator_id ,
361+ receiver_id ,
362+ [fungible_id ],
363+ [nft_serial ],
364+ prefix = "After airdrop" ,
365+ )
366+
367+
368+ print ("✅ Auto-association successful: Receiver accepted airdropped tokens "
369+ "without pre-association."
370+ )
371+ print ("✅ Airdrop successful: Receiver accepted new fungible tokens "
372+ "without pre-association."
373+ )
313374
314375if __name__ == "__main__" :
315376 main ()
0 commit comments