
SolDevHub
SolDevHubThis guide explains how a Solana SPL token is created programmatically, including mint creation, metadata, and optional authority revocation.
Creating a token on Solana involves multiple steps: creating the mint account, initializing it, creating an associated token account (ATA), minting supply, attaching metadata, and optionally revoking authorities.
const tokenProgram = TOKEN_PROGRAM_ID;
const ixMintAcct = SystemProgram.createAccount({
fromPubkey: publicKey,
newAccountPubkey: mint.publicKey,
space: MINT_SIZE,
lamports,
programId: tokenProgram,
});
const ixInitMint = createInitializeMintInstruction(
mint.publicKey,
decimalsNum,
publicKey,
publicKey,
tokenProgram,
);
const ata = await getAssociatedTokenAddress(
mint.publicKey,
publicKey,
false,
tokenProgram,
);
const ixATA = createAssociatedTokenAccountInstruction(
publicKey,
ata,
publicKey,
mint.publicKey,
tokenProgram,
);
const amount = BigInt(Number(supply) * 10 ** decimalsNum);
const ixMintTo = createMintToInstruction(
mint.publicKey,
ata,
publicKey,
amount,
[],
tokenProgram,
);
/* ---- Metadata ---- */
const [metadataPda] = PublicKey.findProgramAddressSync(
[
Buffer.from("metadata"),
TOKEN_METADATA_PROGRAM_ID.toBuffer(),
mint.publicKey.toBuffer(),
],
TOKEN_METADATA_PROGRAM_ID,
);
const ixMetadata = createCreateMetadataAccountV3Instruction(
{
metadata: metadataPda,
mint: mint.publicKey,
mintAuthority: publicKey,
payer: publicKey,
updateAuthority: publicKey,
},
{
createMetadataAccountArgsV3: {
data: {
name,
symbol,
uri: metadataUrl,
sellerFeeBasisPoints: 0,
creators: null,
uses: null,
collection: null,
},
isMutable: false,
collectionDetails: null,
},
},
);
/* ---- Optional: Revoke Authorities ---- */
const ixList: any[] = [ixMintAcct, ixInitMint, ixATA, ixMintTo, ixMetadata];
if (revokeMint) {
ixList.push(
createSetAuthorityInstruction(
mint.publicKey,
publicKey,
AuthorityType.MintTokens,
null,
[],
tokenProgram,
),
);
}
if (revokeFreeze) {
ixList.push(
createSetAuthorityInstruction(
mint.publicKey,
publicKey,
AuthorityType.FreezeAccount,
null,
[],
tokenProgram,
),
);
}A new account is created on-chain to represent the token mint. This defines storage and assigns ownership to the token program.
The mint is initialized with decimals and authority settings. The wallet becomes both mint and freeze authority initially.
An ATA is created to hold the tokens for the wallet. Every wallet-token pair has a unique ATA.
Tokens are minted into the ATA. The amount is adjusted using decimals to ensure correct precision.
Metadata is stored using Metaplex. This includes name, symbol, and image URI for wallets and explorers.
Mint and freeze authority can be removed to make the token immutable. This is commonly done for trust and security.