Mint Tokens
Learn how to mint tokens in Solana programs using Anchor. Covers creating new tokens via cross program invocations (CPI) to the Token Program with code examples.
How to Mint Tokens
Minting tokens refers to the process of creating new units of a token by
invoking the
mint_to
instruction on a token program. Only the address specified as the mint authority
on the mint account can mint new tokens. The instruction also requires the
existence of a token account for the mint as the destination of the minted
tokens.
The Token Program and Token Extension Program share similar implementations to achieve the same functionality.
Examples
To mint tokens through an Anchor program, you need to make a cross program
invocation (CPI) to the mint_to
instruction on either the Token Program or
Token Extension Program.
This means you are invoking the mint_to
instruction on the Token Program or
Token Extension Program from an instruction in your program. Your program acts
as an intermediary, passing along the required accounts, instruction data, and
signatures to the token program.
Mint Tokens via CPI
Use the token_interface::mint_to
function make a CPI to either the Token
Program or Token Extension Program. This function requires:
-
The
MintTo
struct which specifies the required accounts:mint
- The mint account to create new units of tokens forto
- The destination token account to receive the minted tokensauthority
- The mint authority with permission to mint tokens
-
The
amount
of tokens to mint, in base units of the token adjusted by decimals. (e.g. if the mint has 2 decimals, amount of 100 = 1 token)
The mint authority passed to the mint_to
instruction must match the
mint_authority
stored on the mint account. Additionally, the mint authority
must be a signer on the transaction. For example:
At minimum, the following accounts are required:
Within the instruction logic, use the:
MintTo
struct to specify the required accountstoken_interface::mint_to
function to make the CPI
Mint Tokens with PDA mint authority via CPI
You can create a mint account with a Program Derived Address (PDA) as the mint authority. This allows your program to programmatically mint tokens by "signing" with the PDA's seeds in the Cross Program Invocation (CPI). This pattern is useful when you want the program itself, rather than an external wallet, to control token minting.
In this example, mint authority is set to a Program Derived Address (PDA). The
PDA is derived using the seed b"mint"
. This means the program itself controls
minting through this PDA.
To mint tokens, the program must "sign" with the PDA by including the seeds and
bump in the CPI context. This is done by passing the seeds and bump to the
with_signer
method when creating the CPI context.
Note in this example the same PDA is used as both the address of the mint account and the mint authority.