Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dferrandizmont/ded2a351e224001313fde52b5cc5e0ed to your computer and use it in GitHub Desktop.
Save dferrandizmont/ded2a351e224001313fde52b5cc5e0ed to your computer and use it in GitHub Desktop.
Fetching basic token (mint) info (through getAccountInfo), e.g.: total supply, mintAuthority, freezeAuthority...
import * as web3 from "@solana/web3.js";
import { TOKEN_PROGRAM_ID, unpackMint, Mint } from "@solana/spl-token";
//////////////////////////////////////////////////
async function getTokenInfo(conn: web3.Connection, tokenMint: web3.PublicKey): Promise<Mint | null> {
const info = await conn.getAccountInfo(tokenMint);
if (!info) return null;
try {
let mint: Mint | null = unpackMint(tokenMint, info, TOKEN_PROGRAM_ID);
return typeof mint === "object" ? mint : null;
} catch (e) {}
return null;
}
//////////////////////////////////////////////////
(async (rpcUrl: string) => {
const conn = new web3.Connection(rpcUrl, "confirmed");
const tokenMint = new web3.PublicKey("EKpQGSJtjMFqKZ9KQanSqYXRcF8fBopzLHYxdM65zcjm");
const tokenInfo = await getTokenInfo(conn, tokenMint);
if (!tokenInfo) return;
//////////////////////////////////////////////////
console.log(`Token info (${tokenMint.toBase58()}):`);
console.dir(tokenInfo);
//
// --- OUTPUT ---
//
// Token info (EKpQGSJtjMFqKZ9KQanSqYXRcF8fBopzLHYxdM65zcjm):
// {
// address: PublicKey [PublicKey(EKpQGSJtjMFqKZ9KQanSqYXRcF8fBopzLHYxdM65zcjm)] {
// _bn: BN { negative: 0, words: [Array], length: 10, red: null }
// },
// mintAuthority: null,
// supply: 998905893700562n,
// decimals: 6,
// isInitialized: true,
// freezeAuthority: null,
// tlvData: Buffer(0) [Uint8Array] []
// }
//
// --------------
//
})(process.env.SOL_RPC_URL || "https://mainnet.helius-rpc.com/?api-key=00000000-0000-0000-0000-000000000000");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment