Skip to content

Commit fbdf88d

Browse files
feat: add detail to token_airdrop and token_airdrop_cancel
Signed-off-by: tech0priyanshu <[email protected]>
1 parent 68a6951 commit fbdf88d

File tree

2 files changed

+100
-24
lines changed

2 files changed

+100
-24
lines changed

examples/token_airdrop.py

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def create_account(client, operator_key):
5757

5858
def create_token(client, operator_id, operator_key):
5959
"""Create a fungible token"""
60-
print("\nCreating a token...")
60+
print("\nStep 1: Creating a fungible token (TKA)...")
6161
try:
6262
token_tx = (
6363
TokenCreateTransaction()
@@ -79,7 +79,7 @@ def create_token(client, operator_id, operator_key):
7979

8080
def create_nft(client, operator_key, operator_id):
8181
"""Create a NFT"""
82-
print("\nCreating a nft...")
82+
print("\nStep 2: Creating a non-fungible token (NFTA)...")
8383
try:
8484
nft_tx = (
8585
TokenCreateTransaction()
@@ -101,7 +101,7 @@ def create_nft(client, operator_key, operator_id):
101101

102102
def mint_nft(client, operator_key, nft_id):
103103
"""Mint the NFT with metadata"""
104-
print("\nMinting a nft...")
104+
print("\nStep 3: Minting an NFT for NFTA...")
105105
try:
106106
mint_tx = TokenMintTransaction(token_id=nft_id, metadata=[b"NFT data"])
107107
mint_tx.freeze_with(client)
@@ -117,7 +117,7 @@ def mint_nft(client, operator_key, nft_id):
117117

118118
def associate_tokens(client, recipient_id, recipient_key, tokens):
119119
"""Associate the token and nft with the recipient"""
120-
print("\nAssociating tokens to recipient...")
120+
print("\nStep 4: Associating tokens to recipient...")
121121
try:
122122
assocciate_tx = TokenAssociateTransaction(
123123
account_id=recipient_id,
@@ -132,10 +132,9 @@ def associate_tokens(client, recipient_id, recipient_key, tokens):
132132
.execute(client)
133133
.token_balances
134134
)
135-
print("Tokens associated with recipient:")
136-
print(f"{tokens[0]}: {balance_before.get(tokens[0])}")
137-
print(f"{tokens[1]}: {balance_before.get(tokens[1])}")
138-
135+
print("Tokens associated with recipient (should be 0 for both):")
136+
print(f" {tokens[0]}: {balance_before.get(tokens[0], 0)}")
137+
print(f" {tokens[1]}: {balance_before.get(tokens[1], 0)}")
139138
print("\n✅ Success! Token association complete.")
140139

141140
except Exception as e:
@@ -160,14 +159,25 @@ def token_airdrop():
160159
# Create a nft
161160
nft_id = create_nft(client, operator_key, operator_id)
162161

163-
#Mint nft
162+
# Mint nft
164163
serial_number = mint_nft(client, operator_key, nft_id)
165164

166165
# Associate tokens
167166
associate_tokens(client, recipient_id, recipient_key, [token_id, nft_id])
168167

169-
# Airdrop Tthe tokens
170-
print("\nAirdropping tokens...")
168+
# Log balances before airdrop
169+
print("\nStep 5: Checking balances before airdrop...")
170+
sender_balances_before = CryptoGetAccountBalanceQuery(account_id=operator_id).execute(client).token_balances
171+
recipient_balances_before = CryptoGetAccountBalanceQuery(account_id=recipient_id).execute(client).token_balances
172+
print(f"Sender ({operator_id}) balances before airdrop:")
173+
print(f" {token_id}: {sender_balances_before.get(token_id, 0)}")
174+
print(f" {nft_id}: {sender_balances_before.get(nft_id, 0)}")
175+
print(f"Recipient ({recipient_id}) balances before airdrop:")
176+
print(f" {token_id}: {recipient_balances_before.get(token_id, 0)}")
177+
print(f" {nft_id}: {recipient_balances_before.get(nft_id, 0)}")
178+
179+
# Airdrop the tokens
180+
print(f"\nStep 6: Airdropping fungible token TKA ({token_id}) and NFTA ({nft_id}, serial {serial_number}) to recipient {recipient_id}...")
171181
try:
172182
airdrop_receipt = (
173183
TokenAirdropTransaction()
@@ -183,24 +193,35 @@ def token_airdrop():
183193
)
184194

185195
if airdrop_receipt.status != ResponseCode.SUCCESS:
186-
print(f"Fail to cancel airdrop: Status: {airdrop_receipt.status}")
196+
print(f"Fail to airdrop: Status: {airdrop_receipt.status}")
187197
sys.exit(1)
188198

189199
print(f"Token airdrop ID: {airdrop_receipt.transaction_id}")
190200

191-
after_balance = (
192-
CryptoGetAccountBalanceQuery(account_id=recipient_id)
193-
.execute(client)
194-
.token_balances
195-
)
196-
print("Recipient balance after token airdrop:")
197-
print(f"{token_id}: {after_balance.get(token_id)}")
198-
print(f"{nft_id}: {after_balance.get(nft_id)}")
199-
201+
# Log balances after airdrop
202+
sender_balances_after = CryptoGetAccountBalanceQuery(account_id=operator_id).execute(client).token_balances
203+
recipient_balances_after = CryptoGetAccountBalanceQuery(account_id=recipient_id).execute(client).token_balances
204+
print("\nBalances after airdrop:")
205+
print(f"Sender ({operator_id}):")
206+
print(f" {str(token_id)}: {sender_balances_after.get(str(token_id), 0)}")
207+
print(f" {str(nft_id)}: {sender_balances_after.get(str(nft_id), 0)}")
208+
print(f"Recipient ({recipient_id}):")
209+
print(f" {str(token_id)}: {recipient_balances_after.get(str(token_id), 0)}")
210+
print(f" {str(nft_id)}: {recipient_balances_after.get(str(nft_id), 0)}")
211+
212+
# Summary table
213+
print("\nSummary Table:")
214+
print("+----------------+----------------------+----------------------+----------------------+----------------------+")
215+
print("| Token Type | Token ID | NFT Serial | Sender Balance | Recipient Balance |")
216+
print("+----------------+----------------------+----------------------+----------------------+----------------------+")
217+
print(f"| Fungible (TKA) | {str(token_id):20} | -{'':20} | {str(sender_balances_after.get(str(token_id), 0)):20} | {str(recipient_balances_after.get(str(token_id), 0)):20} |")
218+
print(f"| NFT (NFTA) | {str(nft_id):20} | {str(serial_number):20} | {str(sender_balances_after.get(str(nft_id), 0)):20} | {str(recipient_balances_after.get(str(nft_id), 0)):20} |")
219+
print("+----------------+----------------------+----------------------+----------------------+----------------------+")
200220
print("\n✅ Success! Token Airdrop transaction successful")
201221
except Exception as e:
202222
print(f"❌ Error airdropping tokens: {e}")
203223
sys.exit(1)
204224

205225
if __name__ == "__main__":
206226
token_airdrop()
227+

examples/token_cancel_airdrop.py

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def token_cancel_airdrop():
5959
sys.exit(1)
6060

6161
# Create two new tokens.
62-
print("\nCreating two new tokens...")
62+
print("\nStep 1: Creating two new fungible tokens...")
6363
try:
6464
tx1 = TokenCreateTransaction().set_token_name("First Token").set_token_symbol("TKA").set_initial_supply(1).set_treasury_account_id(operator_id)
6565
receipt1 = tx1.freeze_with(client).sign(operator_key).execute(client)
@@ -69,13 +69,31 @@ def token_cancel_airdrop():
6969
receipt2 = tx2.freeze_with(client).sign(operator_key).execute(client)
7070
token_id_2 = receipt2.token_id
7171

72-
print(f"Created tokens: {token_id_1} and {token_id_2}")
72+
print(f"Created tokens: {token_id_1} (TKA) and {token_id_2} (TKB)")
7373
except Exception as e:
7474
print(f"Error creating tokens: {e}")
7575
sys.exit(1)
7676

77+
# Log balances before airdrop
78+
print("\nStep 2: Checking balances before airdrop...")
79+
from hiero_sdk_python import CryptoGetAccountBalanceQuery
80+
sender_balances_before = CryptoGetAccountBalanceQuery(account_id=operator_id).execute(client).token_balances
81+
82+
recipient_balances_before = CryptoGetAccountBalanceQuery(account_id=recipient_id).execute(client).token_balances
83+
print(f"Sender ({operator_id}) balances before airdrop:")
84+
85+
print(f" {str(token_id_1)}: {sender_balances_before.get(str(token_id_1), 0)}")
86+
87+
print(f" {str(token_id_2)}: {sender_balances_before.get(str(token_id_2), 0)}")
88+
89+
print(f"Recipient ({recipient_id}) balances before airdrop:")
90+
91+
print(f" {str(token_id_1)}: {recipient_balances_before.get(str(token_id_1), 0)}")
92+
93+
print(f" {str(token_id_2)}: {recipient_balances_before.get(str(token_id_2), 0)}")
7794
# Airdrop the tokens.
78-
print("\nAirdrop tokens...")
95+
print(f"\nStep 3: Airdropping tokens TKA ({token_id_1}) and TKB ({token_id_2}) to recipient {recipient_id}...")
96+
7997
try:
8098
receipt = (
8199
TokenAirdropTransaction()
@@ -90,6 +108,43 @@ def token_cancel_airdrop():
90108
print(f"Token airdrop complete: (status: {receipt.status}, transaction_id: {receipt.transaction_id})")
91109
airdrop_record = TransactionRecordQuery(receipt.transaction_id).execute(client)
92110
pending_airdrops_record = airdrop_record.new_pending_airdrops
111+
# Log balances after airdrop
112+
sender_balances_after = CryptoGetAccountBalanceQuery(account_id=operator_id).execute(client).token_balances
113+
114+
recipient_balances_after = CryptoGetAccountBalanceQuery(account_id=recipient_id).execute(client).token_balances
115+
116+
print("\nBalances after airdrop:")
117+
118+
print(f"Sender ({operator_id}):")
119+
120+
print(f" {str(token_id_1)}: {sender_balances_after.get(str(token_id_1), 0)}")
121+
122+
print(f" {str(token_id_2)}: {sender_balances_after.get(str(token_id_2), 0)}")
123+
124+
print(f"Recipient ({recipient_id}):")
125+
126+
print(f" {str(token_id_1)}: {recipient_balances_after.get(str(token_id_1), 0)}")
127+
128+
print(f" {str(token_id_2)}: {recipient_balances_after.get(str(token_id_2), 0)}")
129+
130+
131+
132+
# Summary table
133+
134+
print("\nSummary Table:")
135+
136+
print("+----------------+----------------------+----------------------+----------------------+\n"
137+
138+
"| Token Symbol | Token ID | Sender Balance | Recipient Balance |\n"
139+
140+
"+----------------+----------------------+----------------------+----------------------+")
141+
142+
print(f"| TKA | {str(token_id_1):<20} | {str(sender_balances_after.get(str(token_id_1), 0)):<20} | {str(recipient_balances_after.get(str(token_id_1), 0)):<20} |")
143+
144+
print(f"| TKB | {str(token_id_2):<20} | {str(sender_balances_after.get(str(token_id_2), 0)):<20} | {str(recipient_balances_after.get(str(token_id_2), 0)):<20} |")
145+
146+
print("+----------------+----------------------+----------------------+----------------------+")
147+
93148
except Exception as e:
94149
print(f"Error airdroping tokens: {e}")
95150
sys.exit(1)

0 commit comments

Comments
 (0)