-
Notifications
You must be signed in to change notification settings - Fork 309
Description
After a successful buy transaction on pump.fun, attempting to sell immediately fails with:
solana.rpc.core.RPCException: InvalidParamsMessage { message: "Invalid param: could not find account" }
This happens when calling get_token_account_balance() even though the buy transaction confirmed successfully and created the Associated Token Account (ATA).
Root Cause:
The issue occurs due to RPC node synchronization delays. When you:
Send a buy transaction that creates an ATA and deposits tokens
Transaction confirms on one RPC node
Immediately query for the token balance
The query might hit a different RPC node that hasn't synced the new account yet
The Fix:
Before (fails when ATA isn't synced):
token_balance = await self.client.get_token_account_balance(user_token_account)
After (with fallback to raw account data):
try:
token_balance = await self.client.get_token_account_balance(user_token_account)
except Exception as e:
if "could not find account" in str(e).lower():
logger.warning(f"Token account not found via RPC, checking raw account data")
try:
Fallback: Get raw account data and parse token amount manually
from solana.rpc.api import Client
client = Client(self.client.rpc_endpoint)
account_info = client.get_account_info(user_token_account)
if account_info.value and account_info.value.data:
data = account_info.value.data
if len(data) >= 72:
import struct
SPL Token account layout: owner (32 bytes), mint (32 bytes), amount (8 bytes at offset 64)
token_balance = struct.unpack('<Q', data[64:72])[0]
logger.info(f"Got token balance from raw data: {token_balance}")
else:
token_balance = 0
else:
token_balance = 0
except Exception as e2:
logger.error(f"Failed to get raw account data: {e2}")
token_balance = 0
else:
raise # Re-raise if it's a different error
Why This Works:
First attempt: Try the standard get_token_account_balance() RPC method
If account "not found": Fall back to get_account_info() which often succeeds even when the specialized method fails
Parse manually: Extract the token amount from raw bytes (SPL Token accounts store the amount as a little-endian u64 at byte offset 64)
Additional Considerations:
This is a temporary workaround for RPC inconsistency
The issue is more common with fast-paced trading where you buy and sell within seconds
Some RPC providers (like Helius) have better consistency than public endpoints
Adding a small delay (1-3 seconds) after buy can help but impacts trading speed