Mint To Instruction
Creates new tokens and deposits them into a specified account. Only the mint authority can perform this operation.
Before we can mint any token, we'll need to already have:
- Initialized a
Mint
account which we hold themintAuthority
- Initialized a
Token
account orAssociated Token
account where we're going to mint tokens to
The amount of tokens we mint are "normalized" for decimals. This means that if we want to mint 1 token that has 6 decimals, we'll need to actually put
1e6
as amount
Raw Instruction
By using just "raw" instruction without any abstraction, this is how minting a token would look like:
const destination = Keypair.generate();
const tokenAccount = await getAssociatedTokenAddress(
mint.publicKey,
destination.publicKey,
);
// Create ATA creation instruction
const createAtaInstruction = createAssociatedTokenAccountIdempotentInstruction(
keypair.publicKey, // payer
tokenAccount, // associated token account address
destination.publicKey, // owner
mint.publicKey, // mint
);
// Mint tokens to ATA
const mintToInstruction = createMintToInstruction(
mint.publicKey, // mint
tokenAccount, // destination
keypair.publicKey, // mint authority
1_000e6, // amount of tokens
);
const transaction = new Transaction().add(
createAtaInstruction,
mintToInstruction,
);
const signature = await sendAndConfirmTransaction(connection, transaction, [keypair]);
console.log(`Token accounts created and tokens minted! Check out your TX here: https://explorer.solana.com/tx/${signature}?cluster=devnet`);
Abstracted Instruction
This is how the same instructions would look like abstracted away with the mintTo()
instruction:
const destination = Keypair.generate();
const ata = await getOrCreateAssociatedTokenAccount(
connection,
keypair,
mint,
destination.publicKey
);
console.log(`This is your ATA: ${ata.address}!`)
let tx = await mintTo(
connection,
keypair,
mint,
ata.address,
keypair.publicKey,
1e6,
);
console.log(`Succesfully Minted!. Transaction Here: https://explorer.solana.com/tx/${tx}?cluster=devnet`)