SuperteamDAO / solathon

☄️ High performance, easy to use and feature-rich Solana SDK for Python.
https://solathon.vercel.app
MIT License
87 stars 32 forks source link

how to get number of SPL tokens in a account #59

Closed Mrfranken closed 6 months ago

Mrfranken commented 8 months ago

hi, I own USDC and other tokens in my account, I want to get how many USDC in my account, what should I do? I've tried method "get_token_accounts_by_owner", just like

from solathon import Client, PublicKey

client = Client("https://api.mainnet-beta.solana.com")
public_key = PublicKey("xxxxxxxxxxxxxxxxx")

tokens = client.get_token_accounts_by_owner(public_key, program_id="TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA")
print(tokens)

but it only shows the account string, seems solana is so different from evm chain, could u help to give a example? many thx

GitBolt commented 8 months ago

Hi! Seems like the RPC method to fetch balance was not implemented. I just added it. You can download the package again. In order to get balances of tokens, you have to first fetch all token accounts, which you are doing by the get_token_accounts_by_owner method above, and then run a loop with each token account public key being passed to get_token_account_balance method, which will return all information regarding the balance of tokens held in each token account :)

Check out example code in the docs here, just added: https://solathon.vercel.app/clients/client#get_token_account_balance

GitBolt commented 6 months ago

Closing due to inactivity, feel free to re-open for further queries