SolDevHub

Create Token

This guide explains how a Solana SPL token is created programmatically, including mint creation, metadata, and optional authority revocation.

Overview

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.

Code

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,
    ),
  );
}

1. Create Mint Account

A new account is created on-chain to represent the token mint. This defines storage and assigns ownership to the token program.

2. Initialize Mint

The mint is initialized with decimals and authority settings. The wallet becomes both mint and freeze authority initially.

3. Create Associated Token Account (ATA)

An ATA is created to hold the tokens for the wallet. Every wallet-token pair has a unique ATA.

4. Mint Initial Supply

Tokens are minted into the ATA. The amount is adjusted using decimals to ensure correct precision.

5. Add Metadata

Metadata is stored using Metaplex. This includes name, symbol, and image URI for wallets and explorers.

6. Revoke Authorities (Optional)

Mint and freeze authority can be removed to make the token immutable. This is commonly done for trust and security.

Once authorities are revoked, you cannot mint more tokens or freeze accounts. This action is permanent.