Solana CookbookTokens
How to Create a Token
Creating tokens is done by creating what is called a "mint account". This mint account is later used to mint tokens to a user's token account.
create-mint-account.ts
import {clusterApiUrl,Connection,Keypair,sendAndConfirmTransaction,SystemProgram,Transaction,} from "@solana/web3.js";import {createInitializeMintInstruction,TOKEN_PROGRAM_ID,MINT_SIZE,getMinimumBalanceForRentExemptMint,createMint,} from "@solana/spl-token";import bs58 from "bs58";(async () => {// connectionconst connection = new Connection(clusterApiUrl("devnet"), "confirmed");const recentBlockhash = await connection.getLatestBlockhash();// 5YNmS1R9nNSCDzb5a7mMJ1dwK9uHeAAF4CmPEwKgVWr8const feePayer = Keypair.fromSecretKey(bs58.decode("588FU4PktJWfGfxtzpAAXywSNt74AvtroVzGfKkVN1LwRuvHwKGr851uH8czM5qm4iqLbs1kKoMKtMJG4ATR7Ld2",),);// G2FAbFQPFa5qKXCetoFZQEvF9BVvCKbvUZvodpVidnoYconst alice = Keypair.fromSecretKey(bs58.decode("4NMwxzmYj2uvHuq8xoqhY8RXg63KSVJM1DXkpbmkUY7YQWuoyQgFnnzn6yo3CMnqZasnNPNuAT2TLwQsCaKkUddp",),);// 1) use build-in functionlet mintPubkey = await createMint(connection, // connectionfeePayer, // fee payeralice.publicKey, // mint authorityalice.publicKey, // freeze authority (you can use `null` to disable it. when you disable it, you can't turn it on again)8, // decimals);console.log(`mint: ${mintPubkey.toBase58()}`);// or// 2) compose by yourselfconst mint = Keypair.generate();console.log(`mint: ${mint.publicKey.toBase58()}`);const transaction = new Transaction().add(// create mint accountSystemProgram.createAccount({fromPubkey: feePayer.publicKey,newAccountPubkey: mint.publicKey,space: MINT_SIZE,lamports: await getMinimumBalanceForRentExemptMint(connection),programId: TOKEN_PROGRAM_ID,}),// init mint accountcreateInitializeMintInstruction(mint.publicKey, // mint pubkey8, // decimalsalice.publicKey, // mint authorityalice.publicKey, // freeze authority (you can use `null` to disable it. when you disable it, you can't turn it on again)),);// Send transactionconst transactionSignature = await sendAndConfirmTransaction(connection,transaction,[feePayer, mint], // Signers);console.log(`txhash: ${transactionSignature}`);})();