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
Mintaccount which we hold themintAuthorityInitialized a
Tokenaccount orAssociated Tokenaccount where we're going to mint tokens to
Raw Instruction
By using just "raw" instruction without any abstraction, this is how minting a token would look like:
ts
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`,
);Expand
[12 more lines]
Abstracted Instruction
This is how the same instructions would look like abstracted away with the mintTo() instruction:
ts
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`,
);Expand
[1 more lines]