Mint Account
- create mint account
- manually do it
- use helper to do it
How to Create a Mint Account
https://beta.solpg.io/660ce32ecffcf4b13384d00f
Starter Code
- open solana playground
- starter code
client.ts
import {
Connection,
Keypair,
SystemProgram,
Transaction,
clusterApiUrl,
sendAndConfirmTransaction,
} from "@solana/web3.js";
import {
MINT_SIZE,
TOKEN_2022_PROGRAM_ID,
createInitializeMint2Instruction,
getMinimumBalanceForRentExemptMint,
} from "@solana/spl-token";
const wallet = pg.wallet;
const connection = new Connection(clusterApiUrl("devnet"), "confirmed");
Generate Keypair
client.ts
// Generate keypair to use as address of mint account
const mint = new Keypair();
// Calculate minimum lamports for space required by mint account
const rentLamports = await getMinimumBalanceForRentExemptMint(connection);
Build Create Account Instruction
client.ts
// Instruction to create new account with space for new mint account
const createAccountInstruction = SystemProgram.createAccount({
fromPubkey: wallet.publicKey,
newAccountPubkey: mint.publicKey,
space: MINT_SIZE,
lamports: rentLamports,
programId: TOKEN_2022_PROGRAM_ID,
});
Build Initialize Mint Instruction
client.ts
// Instruction to initialize mint account
const initializeMintInstruction = createInitializeMint2Instruction(
mint.publicKey,
2, // decimals
wallet.publicKey, // mint authority
wallet.publicKey, // freeze authority
TOKEN_2022_PROGRAM_ID,
);
Add Instructions to Transaction
client.ts
// Build transaction with instructions to create new account and initialize mint account
const transaction = new Transaction().add(
createAccountInstruction,
initializeMintInstruction,
);
Send Transaction
client.ts
const transactionSignature = await sendAndConfirmTransaction(
connection,
transaction,
[
wallet.keypair, // payer
mint, // mint address keypair
],
);
client.ts
console.log(
"\nTransaction Signature:",
`https://explorer.solana.com/tx/${transactionSignature}?cluster=devnet`,
);
console.log(
"\nMint Account:",
`https://explorer.solana.com/address/${mint.publicKey}?cluster=devnet`,
);
Run script
Terminal
run
Output
Running client...
client.ts:
Transaction Signature: https://explorer.solana.com/tx/2mmqzdqQtnSbJEBH4VEcTiMU36RFF13qWBXcXCm2no5ekGdhNDRV9erD1mdunBpLBEDcJMftaMtxVuFDQzy6rZyV?cluster=devnet
Mint Account: https://explorer.solana.com/address/9t2AZ968hs9u24kVUtaS72X58hbNHRxk3kKbwpChU2BB?cluster=devnet