This section focuses on creating a minting program using Solidity. You'll learn how to set up and run tests for your project, create and mint SPL tokens using Solidity, and write test cases for your Solidity code.
illustration
b
Building Your Own Token Minting Program

In this lesson, you will:

  • Write Solidity code for your token minting project.
  • Learn about the different elements of a Solana program.

In this lesson, we'll be coding our token minter. You'll learn how to add token metadata and create functions for minting tokens.

First, clear out any standard code, as we're building a custom SPL token minter program from scratch.

Understanding SPL Token Minting on Solana

Minting SPL Tokens is straightforward but requires understanding Solana's system for tracking SPL Token balances. Unlike direct balance tracking like with SOL, Solana uses Associated Token Accounts for each SPL Token.

Consider a new token, "JOE." To manage a user's JOE balance, you need to:

  1. Mint JOE tokens.
  2. Create an Associated Token Account for the user's wallet, dedicated to tracking their JOE balance.
  3. Mint or transfer JOE tokens to this Associated Token Account.

Associated Token Accounts act as specialized counters linked to a Mint and a Wallet, indicating the balance of a specific Mint for a specific Wallet.

We're creating a Solana program for minting fungible SPL tokens. We'll use specific Solidity libraries for creating tokens, setting up a token mint, and sending new tokens to a user's wallet.

  1. In your project folder, create a libraries directory. We'll import and use methods from here to create and mint SPL tokens.

  2. Add these Solidity library files:

  3. Now, head over to your spl-token-minter.sol file and start by importing these libraries:

import "../libraries/spl_token.sol";
import "../libraries/mpl_metadata.sol";

These import statements allow your contract to access functions and data from the SPL token and Metaplex libraries, enhancing your contract's capabilities for token creation and minting.

  1. Define your program with the @program_id annotation, which specifies the on-chain address of your Solana program:
@program_id("4Huxs8ujxKqT76qqfmfJXMThmo4WMsvmdiCkqzNKF5kq")
  1. Write your program's contract structure:
contract spl_token_minter {
 @payer(payer)
 constructor() {}
}

In the constructor, the @payer annotation specifies the Solana account that will fund the initialization of the contract's data account.

In Solana, storing data on the blockchain requires allocating SOL for storage costs. The @payer annotation identifies who will cover these costs for creating the data storage account.

After your constructor, add the createTokenMint function:

function createTokenMint(
 address payer, // payer account
 address mint, // mint account to be created
 address mintAuthority, // mint authority for the mint account
 address freezeAuthority, // freeze authority for the mint account
 address metadata, // metadata account to be created
 uint8 decimals, // decimals for the mint account
 string name, // name for the metadata account
 string symbol, // symbol for the metadata account
 string uri // URI for the metadata account
 ) public {
 // Code for creating mint and metadata accounts
 }

Difference from Ethereum :- Decimals for the mint account (unlike Ethereum, which does not have a decimal concept for creating and minting tokens. Ethereum has different types of interfaces, such as ERC-20 for fungible tokens and ERC-721 for NFTs. In contrast, Solana employs a single SPL token program responsible for minting both types of tokens, using the decimal concept.)

This function handles creating a new token mint and its metadata. It involves interacting with Solana's System Program and Token Program to establish the Mint account and set various authorities and properties for it. Then, it uses the Metadata Program to create an account for storing metadata related to the minted tokens.

Next, include the create_mint function:


 // Mint tokens to the token account
 SplToken.create_mint(
 payer,            // payer account
 mint,            // mint account
 mintAuthority,   // mint authority
 freezeAuthority, // freeze authority
 decimals         // decimals
 );

This function mints tokens directly to a user's associated token account. It's a crucial part of how Solana manages and tracks the distribution of SPL tokens.

Finally, add the create_metadata_account function:


 MplMetadata.create_metadata_account(
 metadata, // The new metadata account's address
 mint,  // The mint account associated with the new token
 mintAuthority, // The authority that can mint new tokens
 payer, // The account that pays for the transaction and storage
 payer, // The authority that can update the metadata account
 name, // The name of the new token
 symbol, // The symbol for the new token
 uri // The URI where the off-chain metadata JSON is stored
 );

To provide a practical example:

  • Let's say you create a new token, "JOE."
  • To track a user's JOE balance, you first mint JOE tokens.
  • You create an Associated Token Account specifically for that user's wallet, designed to hold their JOE tokens.
  • Next, you create a metadata account for storing metadata related to the JOE token.
  • Finally, you mint or transfer JOE tokens into this account.

With these functions in your Solidity program, you have the capability to create SPL tokens, along with their metadata, and mint them to specific accounts on the Solana blockchain.

Now that your Solidity program is ready, the next steps involve building, deploying, and testing it on the Solana blockchain, using the Devnet cluster. The Anchor framework simplifies these processes, allowing you to efficiently run and test your program.

In the upcoming lessons, we'll dive into setting up your development environment for running these operations and conducting tests to ensure your program functions as intended. Stay tuned for a hands-on experience in blockchain development with Solana!