I'm looking to query token metadata by looking up the token address(pubkey or mint). Specifically looking for data around the name
, symbol
, and decimals
. Is there an RPC method for this?
How does SolScan do this?
I'm looking to query token metadata by looking up the token address(pubkey or mint). Specifically looking for data around the name
, symbol
, and decimals
. Is there an RPC method for this?
How does SolScan do this?
Updated
SPL-Token's name, symbol, logo, and more can be retrieved today the following way:
@solana/spl-token-registry
This means that first priority should be Metaplex Metadata, then @solana/spl-token-registry
Solflare's Unified Token List does this automatically if you want to use their source code.
from How do I get token name, symbol, and logo?
JSON RPC API
use getAccountInfo
and make sure to add jsonParsed
curl http://localhost:8899 -X POST -H "Content-Type: application/json" -d '
{
"jsonrpc": "2.0",
"id": 1,
"method": "getAccountInfo",
"params": [
"4fYNw3dojWmQ4dXtSGE9epjRGy9pFSx62YypT7avPYvA",
{
"encoding": "jsonParsed"
}
]
}
'
@solana/web3.js
use connection.getParsedAccountInfo
let mint = await connection.getParsedAccountInfo(
new PublicKey('3MoHgE6bJ2Ak1tEvTt5SVgSN2oXiwt6Gk5s6wbBxdmmN')
)
// all the token data is here
console.log(mint.value.data.parsed)
In Python you can do the following
from spl.token._layouts import MINT_LAYOUT
from solana.rpc.api import Client, Pubkey
http_client = Client("https://api.mainnet-beta.solana.com")
# WIF token address
addr = Pubkey.from_string("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v")
info = http_client.get_account_info(addr)
decimals = MINT_LAYOUT.parse(info.value.data).decimals
# All token data
token_data = MINT_LAYOUT.parse(info.value.data)
print(decimals)
# Output: 6
In this answer I discuss some important details for your question: