18 min read
Overview
Have you ever wanted to reward your users with a token for completing a task? Smart contracts allow you to create rules that govern how your token is distributed and used. This guide will teach you how to create and mint fungible SPL tokens on Solana using Anchor. We will cover the necessary code for both the program and tests to ensure a seamless transfer of tokens between accounts.
What You Will Do
- Create a Solana program using Anchor and Solana Playground
- Create a program instruction to create a new SPL token with metadata
- Create a program instruction to mint tokens to a specified account
- Write tests to verify and execute the instructions
What You Will Need
- Basic experience with building in Anchor (Guide: Getting Started with Anchor)
- Experience with Solana Fungible tokens
- Experience with Rust programming language
- Basic knowledge of the JavaScript/TypeScript
- A modern web browser (e.g., Google Chrome)
- About 8 SOL on Solana Devnet to deploy your program (check our Complete Guide to Airdropping Test SOL on Solana)
Dependencies Used in this Guide
Dependency | Version |
---|---|
anchor-lang | 0.29.0 |
anchor-spl | 0.29.0 |
solana-program | 1.16.24 |
spl-token | 4.0.0 |
Initiate Your Project
Create a new project in Solana Playground by going to https://beta.solpg.io/. Solana Playground is a browser-based Solana code editor that will allow us to get up and running with this project quickly. You're welcome to follow along in your own code editor, but this guide will be tailored to Solana Playground's required steps. First, click "Create a new project":
Enter a project name, "token-minter," and select "Anchor (Rust)":
Set up Your Workspace
Connect to a Solana Cluster with Your QuickNode Endpoint
To build on Solana, you'll need an API endpoint to connect with the network. You're welcome to use public nodes or deploy and manage your own infrastructure; however, if you'd like 8x faster response times, you can leave the heavy lifting to us.
See why over 50% of projects on Solana choose QuickNode and sign up for a free account here. We're going to use a Solana Devnet endpoint.
Copy the HTTP Provider link:
Now that you have an endpoint, head back to Solana Playground and click the Settings Gear (⚙️) button in the bottom left corner of the browser window. You will see a dropdown for "Endpoint". Open the dropdown and select "Custom":
Paste your QuickNode endpoint into the text box and click "Add":
Create and Connect a Wallet
Since this project is just for demonstration purposes, we can use a "throw-away" wallet. Solana Playground makes it easy to create one. You should see a red dot "Not connected" in the bottom left corner of the browser window. Click it:
Solana Playground will generate a wallet for you (or you can import your own). Feel free to save it for later use, and click continue when you're ready. A new wallet will be initiated and connected to Solana devnet. Solana Playground airdrops some SOL to your new wallet automatically, but we will request a little extra to ensure we have enough for deploying our program. In the browser terminal, you can use Solana CLI commands. Enter solana airdrop 1
to drop 1 SOL into your wallet. Note: Due to limitations in SOL airdrops, you may have to run this multiple times over different periods. You may also claim additional SOL from the QuickNode Faucet.
Your wallet should now be connected to devnet with a balance of about 8 SOL (you may need to send yourself some additional devnet SOL from another address to have sufficient SOl to deploy to devnet.):
You are ready to go! Let's build!