SuperteamDAO / solathon

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

How to get token name and symbol #87

Open chigozie-coder opened 5 hours ago

chigozie-coder commented 5 hours ago

I'm trying to get the name and symbol of an spl token. I've tried the normal method; get token accounts by owner,but it doesn't include the name and symbol. How can I do this please? @mircial @GitBolt

GitBolt commented 5 hours ago

The token name and symbol are not stored in the token account itself, so they are not included in the data returned by JSON RPC. The best way to get the name and symbol is to use a DAS API, such as Helius'. Go to helius.com and get an API key and try the following:

import requests
import json

url = 'https://mainnet.helius-rpc.com/?api-key=<API Key>'
headers = {
    "Content-Type": "application/json"
}
payload = {
    "jsonrpc": "2.0",
    "id": "text",
    "method": "getAsset",
    "params": {"id": 'token_mint'}
}

response = requests.post(url, headers=headers, json=payload)
data = response.json()

name = data["result"]["content"]["metadata"]["name"]
symbol = data["result"]["content"]["metadata"]["symbol"]

print(f'Name: {name}')
print(f'Symbol: {symbol}')

Be sure to enter token's mint in the params, not the token account address. When you use get_token_accounts_by_owner, you get mint returned in the response object, use that.

chigozie-coder commented 3 hours ago

It works thanks so much But I still want to ask further of there's a way to do this directly from Solana RPC? If not, then thanks for your answer(i will close the issue if not)

GitBolt commented 3 hours ago

Yeah no way to do it via Solana RPC alone