michaelhly / solana-py

Solana Python SDK
https://michaelhly.github.io/solana-py
MIT License
979 stars 252 forks source link

How to get token decimals from a given Public key #422

Closed Aviksaikat closed 4 months ago

Aviksaikat commented 4 months ago

I'm the js SDK for SPL token there is a method called getMint which retrieves information about a mint. Which can be further used to extract decimals, supply, metadata etc. How can do this using sonala-py?

Edit: I hacked a way to do this but it's quite complicated

from spl.token._layouts import MINT_LAYOUT
from solana.rpc.api import Client, Pubkey

http_client = Client("https://api.mainnet-beta.solana.com")
addr = Pubkey.from_string("EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v")
info = http_client.get_account_info(addr)
decimals = MINT_LAYOUT.parse(info.value.data).decimals

print(decimals)
michaelhly commented 4 months ago

https://michaelhly.com/solana-py/spl/token/client/#spl.token.client.Token.get_mint_info