1

I'm working on a Python script to pull successful transfers from the mint address. When I call the script below this is the error:

Traceback (most recent call last): File "/home/user/Documents/test12.py", line 13, in response = client.get_token_accounts_by_owner(mint_pubkey, opts=opts) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.local/lib/python3.11/site-packages/solana/rpc/api.py", line 860, in get_token_accounts_by_owner body = self._get_token_accounts_by_owner_body(owner, opts, commitment) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.local/lib/python3.11/site-packages/solana/rpc/core.py", line 422, in get_token_accounts_by_owner_body pubkey, filter, config = self._get_token_accounts_convert(owner, opts, commitment) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/user/.local/lib/python3.11/site-packages/solana/rpc/core.py", line 384, in _get_token_accounts_convert encoding_to_use = _ACCOUNT_ENCODING_TO_SOLDERS[opts.encoding] ^^^^^^^^^^^^^ AttributeError: 'dict' object has no attribute 'encoding'

from solana.rpc.api import Client
from solanasdk.publickey import PublicKey

# Initialize the Solana RPC client
client = Client("https://api.mainnet-beta.solana.com")

# Token Mint Address as a PublicKey
token_mint_address = "TOKENADDRESSHERE"
mint_pubkey = PublicKey(token_mint_address)

# Fetching token accounts by mint address
opts = {"mint": mint_pubkey}
response = client.get_token_accounts_by_owner(mint_pubkey, opts=opts)

I thought I would be getting a string, not a dict back. I saw a reference somewhere that said if you are not passing a PublicKey for the mint address (passing its string instead) then this would happen. I am passing the mint address as a PublicKey.

1 Answer 1

1

You either need to manually add encoding value in your opts dictionary or simply use TokenAccountOpts and pass it in your options.

Specifying "encoding" for get_token_accounts_by_owner request is mandatory in the Solana.py library, which you are not doing right now. TokenAccountOpts already has the default encoding filled as base64. You can choose to enter base58 or jsonParsed encoding as well for human readable response.

Solana.py reference: get_token_accounts_by_owner

JSON RPC reference: https://solana.com/docs/rpc/http/gettokenaccountsbyowner

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.