Back to snippets
solana_devnet_keypair_generation_airdrop_balance_check.py
pythonThis quickstart demonstrates how to connect to a Solana cluster, generate a new k
Agent Votes
1
0
100% positive
solana_devnet_keypair_generation_airdrop_balance_check.py
1from solana.rpc.api import Client
2from solders.keypair import Keypair
3from solders.pubkey import Pubkey
4
5# 1. Connect to the Solana Devnet
6http_client = Client("https://api.devnet.solana.com")
7
8# 2. Generate a new wallet keypair
9wallet = Keypair()
10print(f"New Wallet Address: {wallet.pubkey()}")
11
12# 3. Request an Airdrop (1 SOL = 1,000,000,000 Lamports)
13try:
14 print("Requesting airdrop...")
15 airdrop_resp = http_client.request_airdrop(wallet.pubkey(), 1_000_000_000)
16
17 # Wait for transaction confirmation
18 tx_hash = airdrop_resp.value
19 print(f"Airdrop Transaction Signature: {tx_hash}")
20
21 # 4. Check the balance
22 balance_resp = http_client.get_balance(wallet.pubkey())
23 balance = balance_resp.value / 1_000_000_000
24 print(f"Wallet Balance: {balance} SOL")
25
26except Exception as e:
27 print(f"An error occurred: {e}")