Search code examples
pythonattributeerrorsolana

Why can’t I find a token count of a specific Solana address?


I've been trying to work on a Solana program in Python using the solana.py framework. However, I've run into a bit of a problem while trying to find the amount of USDC tokens designated to a specific wallet. I've been brewing in the Solana Cookbook for hours but still haven't found anything. My code can be found below.

# This is the public key of the Solana USDC token
usdc_key = PublicKey('4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU')

# This function is supposed to mimick the one presented in TS by the Solana Cookbook
def findUSDCBalance():
    usdc_balance = client.get_token_accounts_by_owner_json_parsed(sol_address, usdc_key)
    return(usdc_balance)

I also receive this error message, which I can't figure out due to trouble understanding the documentation (or lack thereof).

AttributeError: 'PublicKey' object has no attribute 'mint'

This question is not about parsing the response, but just trying to receive a proper JSON response. Is their any specific way I can do this using this framework or any existing solution?


Solution

  • the fcn youre calling expects TokenAccountOpts to define the mint of the token to get from, so your code should be

    from solana.rpc.types import TokenAccountOpts
    usdc_balance = client.get_token_accounts_by_owner_json_parsed(sol_address, TokenAccountOpts(mint=usdc_key))