Let's mint tokens directly to the Minter's Wallet. Here's how you can do it:
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.