In under five minutes, you’ll learn how to check for freeze authority, mintThe process of creating new tokens on the Solana blockchain;… More authority, and update authority on a token mint using a reliable RPC endpointA specific URL or IP address (often including a designated p… More from gsnode.io. This tutorial is ideal for beginners and leverages TypeScript for robust, type-safe code.
For the complete source code and further updates, please visit our GitHub repository: https://github.com/gsnode/fetching-solana-authorities-typescript/
Before starting, ensure you have installed the Metaplex SDK with one of the following commands:
npm install @metaplex-foundation/js
or:
yarn add @metaplex-foundation/js
First, we need to establish a connection to the Solana network. In our example, we use the free RPC endpoint provided by gsnode.io to ensure our calls are fast and reliable. This connection will be used for all subsequent authority queries.
Below is a snippet of code that shows how to set up the connection using the @solana/web3.js
library and import the required modules:
import { Connection, PublicKey } from "@solana/web3.js";
import { Metaplex } from "@metaplex-foundation/js";
// Initialize connection to the Solana network using gsnode.io's free RPC endpoint
console.log("Initializing connection to the Solana network using the gsnode.io RPC endpoint...");
const connection = new Connection("https://rpc.free.gsnode.io");
// Verify the connection by fetching the network version
connection.getVersion()
.then((versionInfo) => {
console.log("Successfully connected to Solana network. Network version:", versionInfo);
})
.catch((err) => {
console.error("Failed to connect to Solana network:", err);
});
You should see something like this:
Every token mint on Solana has a freeze authority field, which indicates whether the token can be frozen. In this section, we retrieve the parsed account info for a token mint and inspect the freezeAuthority
field. If it is not null or undefined, then the token has a freeze authority.
Here’s a simplified snippet of the function used to check the freeze authority:
async function checkFreezeAuthority(mint: PublicKey): Promise {
console.log(`Checking freeze authority for mint: ${mint.toBase58()}`);
try {
const data = await connection.getParsedAccountInfo(mint, "processed");
const parsedMintInfo = (data?.value?.data as any)?.parsed?.info;
if (!parsedMintInfo) {
console.warn(`Mint information for ${mint.toBase58()} not found or invalid.`);
return false;
}
const hasFreezeAuthority = parsedMintInfo.freezeAuthority !== null && parsedMintInfo.freezeAuthority !== undefined;
console.log(`Freeze authority for ${mint.toBase58()}: ${hasFreezeAuthority}`);
return hasFreezeAuthority;
} catch (error) {
console.error(`Error validating freeze authority for ${mint.toBase58()}:`, error);
return false;
}
}
*Screenshot Suggestion:* Show the code snippet in your code editor.
async function checkMintAuthority(mint: PublicKey): Promise {
console.log(`Checking mint authority for mint: ${mint.toBase58()}`);
try {
const data = await connection.getParsedAccountInfo(mint, "processed");
const parsedMintInfo = (data?.value?.data as any)?.parsed?.info;
if (!parsedMintInfo) {
console.warn(`Mint information for ${mint.toBase58()} not found or invalid.`);
return false;
}
const hasMintAuthority = parsedMintInfo.mintAuthority !== null && parsedMintInfo.mintAuthority !== undefined;
console.log(`Mint authority for ${mint.toBase58()}: ${hasMintAuthority}`);
return hasMintAuthority;
} catch (error) {
console.error(`Error validating mint authority for ${mint.toBase58()}:`, error);
return false;
}
}
*Screenshot Suggestion:* Capture the mint authority check code in your IDE.
The update authority controls who can modify a token’s metadata. Here, we leverage the Metaplex SDK to fetch asset data using the mint address and check if the update authority is set (particularly useful for NFTs and SFTs).
async function checkUpdateAuthority(mint: PublicKey): Promise {
console.log(`Checking update authority for mint: ${mint.toBase58()}`);
try {
const metaplex = new Metaplex(connection);
const asset = await metaplex.nfts().findByMint({ mintAddress: mint });
if (asset.model === "nft" || asset.model === "sft") {
const hasUpdateAuthority = asset.updateAuthorityAddress !== null;
console.log(`Update authority for ${mint.toBase58()}: ${hasUpdateAuthority}`);
return hasUpdateAuthority;
}
console.log(`Asset model for ${mint.toBase58()} is neither NFT nor SFT. Skipping update authority check.`);
return false;
} catch (error) {
console.error(`Error during update authority validation for ${mint.toBase58()}:`, error);
return false;
}
}
*Screenshot Suggestion:* Include an image of the update authority function in your code editor.
And that’s it! With these functions, you can quickly check the freeze, mint, and update authorities for any token on Solana. This information is crucial whether you’re building a decentralized application or managing tokens.
This tutorial walked you through setting up a connection to Solana using gsnode.io’s RPC endpoint and creating simple functions to check token authorities using TypeScript. By following this guide, you’ll have a solid foundation for building more complex Solana applications.
Happy coding and happy trading!
Engineer. CEO of GS Node. Marketing Manager at Smithii.