I'm looking to get the token balance for an SPL token in solana based on the:
- Wallet address of the token holder
- The token address
How can I do this?
I thought it would be something like:
import requests
import os
url = os.getenv("SOLANA_RPC_URL")
MY_WALLET_ADDRESS = "XXXXX"
MY_TOKEN_ADDRESS = "XXX"
MINTER = "XXXX"
TOKEN_PROGRAM_ID = "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"
payload = {
"id": 1,
"jsonrpc": "2.0",
"method": "getTokenAccountsByOwner",
"params": [
MY_WALLET,
{"programId": TOKEN_PROGRAM_ID},
{"encoding": "jsonParsed"},
],
}
headers = {"accept": "application/json", "content-type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)
But I keep getting a blank response for what seems to be valid addresses:
{"jsonrpc":"2.0","result":{"context":{"apiVersion":"1.13.5","slot":176104484},"value":[]},"id":1}
I'm using alchemy as my node for Solana.
base-58 encoded string
. if not try to usewallet-address.toBase58()
toBase58
it will return a BN object. your response points that you are reaching the server but getting an empty result which is a kinda 404 response.