I have successfully tracked my wallet balances, and subscribed to the transaction tracking.
I keep getting the following error message when I make a transaction on the wallet.
Subscribed to wallet AcsUpwP4Dxt8PHqMuunw7FgQDYTPmtRh4U1BdUZ2kksf with subscription ID: {"jsonrpc":"2.0","result":9190858,"id":1}Error parsing message: 'signature'Message: {'jsonrpc': '2.0', 'method': 'accountNotification', 'params': {'result': {'context': {'slot': 298236797}, 'value': {'lamports': 979076666, 'data': ['', 'base64'], 'owner': '11111111111111111111111111111111', 'executable': False, 'rentEpoch': 18446744073709551615, 'space': 0}}, 'subscription': 9190858}}
This is my code,
import asyncioimport jsonimport websocketsfrom solana.rpc.api import Clientfrom solders.pubkey import Pubkeyfrom solana.rpc.async_api import AsyncClientfrom solana.rpc.types import TokenAccountOptsfrom solana.rpc.commitment import Confirmedimport base58# Define the wallet address directly in the codewallet_address = "AcsUpwP4Dxt8PHqMuunw7FgQDYTPmtRh4U1BdUZ2kksf"# Initialize the Solana clientsolana_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(base58.b58decode('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA'))), commitment=Confirmed ) resp = resp.to_json() resp_json = json.loads(resp) return resp_json['result']['value']async def get_transaction_details(signature): async with AsyncClient("https://api.mainnet-beta.solana.com") as client: resp = await client.get_transaction(signature) return resp.valueasync def track_transactions(wallet_pubkey): uri = "wss://api.mainnet-beta.solana.com" async with websockets.connect(uri) as websocket: subscription_message = {"jsonrpc": "2.0","id": 1,"method": "accountSubscribe","params": [ str(wallet_pubkey), {"encoding": "jsonParsed","commitment": "confirmed" } ] } await websocket.send(json.dumps(subscription_message)) subscription_id = await websocket.recv() print(f"Subscribed to wallet {wallet_address} with subscription ID: {subscription_id}") while True: message = await websocket.recv() message_json = json.loads(message) if 'params' in message_json and 'result' in message_json['params']: try: tx_signature = message_json['params']['result']['value']['signature'] tx_details = await get_transaction_details(tx_signature) print(f"New Transaction Signature: {tx_signature}") print(f"Transaction Details: {json.dumps(tx_details, indent=4)}") except KeyError as e: print(f"Error parsing message: {e}") print(f"Message: {message_json}")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 token_accounts = asyncio.run(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}") # Start tracking transactions asyncio.run(track_transactions(wallet_pubkey)) 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()
Any help would be awesome.