In this part, you will learn how to edit and create components for minting tokens directly to the minter's wallet on the frontend side of your DApp.
illustration
b
Creating a Mint-To Transaction for Token Minting

Let's mint tokens directly to the Minter's Wallet. Here's how you can do it:

  1. Go to the "MintTo" component in your project.
  2. In this component, you'll see familiar steps from earlier, like importing necessary elements and setting up the IDL for creating the Program API. This setup is used to make RPC calls for minting tokens.

When you use the "mintTo" function, it starts by getting a mint key from the user through a form. This mint key comes from the token the user minted.

Additionally, you take the token amount the user enters and adjust it for the smallest fractions of the token (called lamports). For example:

let amount = event.target.amount.value;
amount = amount * 10 ** 9; // 10^9 lamports (fractions) of that token

Next, we create an Associated Token Account (ATA) for the minter using their wallet's public key.

const associatedToken = await getAssociatedTokenAddress(
 mintPubKey,
 publicKey,
    false,
 TOKEN_PROGRAM_ID,
 ASSOCIATED_TOKEN_PROGRAM_ID
);

Now, we set up the RPC call to mint tokens into the user’s ATA.

const createMint = await program.methods
    .mintTo(amount)
    .accounts({
 mint: mintPubKey,
 tokenAccount: associatedToken,
 mintAuthority: publicKey,
    })
    .rpc({ skipPreflight: true });

In this phase, we use the contract's "mintTo" method. These methods, written in Solidity, are specifically designed for minting tokens.

It's equivalent to this Solidity function:

@mutableAccount(mint)
@mutableAccount(tokenAccount)
@mutableSigner(mintAuthority)
function mintTo(uint64 amount) external {
 // Mint tokens to the token account
 SplToken.mint_to(
 tx.accounts.mint.key, // mint account
 tx.accounts.tokenAccount.key, // token account
 tx.accounts.mintAuthority.key, // mint authority
 amount // amount
 );
}

With this, the user will receive their minted tokens in their wallet.