- The problem
This is the code that I am working with. It's supposed to track my wallet balances on Solana, SOL and Memecoins/SPL tokens included. The code works only partially, it writes out my SOL balance, but then prints some error messages and stops working, I haven't been able to find a solution even with ChatGPT input.
The error message is the following: c:\Users\vhrom\Desktop\Coding vol.2# List of numbers to be sorted.py:35: DeprecationWarning: There is no current event loop loop = asyncio.get_event_loop() Error: expected a sequence of length 32 (got 43)
import asyncio
from solana.rpc.api import Client
from solders.pubkey import Pubkey
from solana.rpc.async_api import AsyncClient
from solana.rpc.types import TokenAccountOpts
from solana.rpc.commitment import Confirmed
# Define the wallet address directly in the code
wallet_address = "BdL79v8z3mTnNhVfQBmtzRvL1gyw7U1vPTuJ4LKN3hpT"
# Initialize the Solana client
solana_client = Client("https://api.mainnet-beta.solana.com")
async def get_token_accounts(wallet_pubkey):
async with AsyncClient("https://api.mainnet-beta.solana.com") as client:
resp = await client.get_token_accounts_by_owner_json_parsed(
wallet_pubkey,
TokenAccountOpts(program_id=Pubkey("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")),
commitment=Confirmed
)
return resp['result']['value']
def get_wallet_balance():
try:
# Convert the wallet address to a Pubkey object
wallet_pubkey = Pubkey.from_string(wallet_address)
# Get the balance of the wallet in SOL
balance_info = solana_client.get_balance(wallet_pubkey)
balance_lamports = balance_info.value # Directly access 'value' attribute
balance_sol = balance_lamports / 1e9 # Convert lamports to SOL
print(f"The balance of wallet {wallet_address} is {balance_sol} SOL.")
# Get token accounts
loop = asyncio.get_event_loop()
token_accounts = loop.run_until_complete(get_token_accounts(wallet_pubkey))
for account in token_accounts:
token_balance = account['account']['data']['parsed']['info']['tokenAmount']['uiAmount']
token_mint = account['account']['data']['parsed']['info']['mint']
print(f"Token: {token_mint}, Balance: {token_balance}")
except Exception as e:
print(f"Error: {e}")
def main():
print("Welcome to the Solana Wallet Tracker!")
# Get and print the balance of the specified wallet
get_wallet_balance()
print()
if __name__ == "__main__":
main()
Thank you for any help I can get.