1

I want to reference this post: https://solana.stackexchange.com/a/518/7486.

I am trying to achieve the same as OP of that post. I want to fetch the price of a token. One response (the one I referenced) says that I can use getTokenAccountBalance RPC method however that doesn't work for me.

This is the token
name: Silly Dragon
mint: 7EYnhQoR9YM3N7UoaKRoA44Uy8JeaZV3qyouov87awMs
LP: DsD69qYsFvMX4cBvHbssGneB2aYwECkL3ehYjQ6NH6aq

When I execute

{
    "jsonrpc": "2.0",
    "id": 1,
    "method": "getTokenAccountBalance",
    "params": [
        "DsD69qYsFvMX4cBvHbssGneB2aYwECkL3ehYjQ6NH6aq"
    ]
}

I receive this error.

{
    "jsonrpc": "2.0",
    "error": {
        "code": -32602,
        "message": "Invalid param: not a Token account"
    },
    "id": 1
}

(I also tried with the mint address, that did give me back the same error)

Now I am wondering if that RPC method is actually working or if I am doing something wrong.

2 Answers 2

3

The getTokenAccountBalance method returns the amount of tokens you hold in your token account. Every wallet has a respective token account to hold the specific tokens. In this case, there are two things wrong:

  1. You want to fetch token price, but this method returns token amount
  2. This method accepts token account, and not the mint of the token itself

The getTokenAccountBalance is a Solana JSON RPC method. If you want to learn more, here is the documentation to it: https://docs.solana.com/api/http#gettokenaccountbalance

Your solution would be to use Birdeye's API to fetch prices of any tokens, as you cannot do so using JSON RPC. But let's say you want to check your wallet value, then you can first fetch price using Birdeye's API and then use the getTokenAccountBalance method to fetch the amount of tokens you hold and multiply them together to see the final value of your holdings.

2
  • +1; Is there a way to find out the price without having to rely on external API's. Isn't the actual price on the blockchain. By checking the last swap and determining the price? I just checked Raydium's HTTP requests, they fetch the price but I cannot make those RPC requests to the mainnet solana RPC endpoint Commented Dec 30, 2023 at 12:54
  • The best option is Raydium's API if you don't want third party APIs. Raydium is the AMM that determines price of any newly launched tokens on Solana so it won't be considered third party, similar to how prices on exchanges are based on their orderbook data and hence you'd use their APIs to fetch prices. No two exchanges would be considered third party as both of their prices are valid, that's the same here. Solana's JSON RPC is not directly responsible for managing liquidity, so it doesn't have any method to retrieve price. The price itself doesn't live on the blockchain as an integer value. Commented Dec 30, 2023 at 13:46
2

You have to run getTokenAccountBalance on the liquidity pair for that trading market. Once you get the tokenA tokenB token amounts you can calculate the spot price.

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.