Solana: How to create SPL token using anchor/solana playground?

February 3, 2025 yanz@123457 No comments exist

Creating an SPL Token on Anchor/Solana Playground: A Step-by-Step Guide

Are you ready to mint your own Solana SPL token? In this article, we’ll walk you through the process of creating an SPL token using Anchor’s Solana Playground. At the end of this guide, you will have a fully functional SPL token with 2% burn on all transactions and an automatic burn stop mechanism when 65% of the total supply is reached.

Prerequisites

  • You have a Solana node set up and running on Anchor
  • You have the Solana SDK for Anchor installed (available at [
  • You have a basic understanding of Solana’s blockchain and token management concepts

Step 1: Create a new token

To create a new token on Anchor, follow these steps:

  • Launch the Anchor CLI by running “anchor launch” in your terminal.
  • Select the “Token” option from the dropdown menu.
  • Select “Create new token” and choose the SPL (Solidity-based) token type.
  • Enter a token name: THEALTCOIN
  • Set and symbol: TALTC
  • Specify a total supply: 99,999,999,999,999

Step 2: Configure the SPL token functions

Next, we need to configure the SPL token functions:

  • Navigate to the Functions tab.
  • Add the following Solidity code to the THEALTCOIN.sol file:

pragma solidity ^0.8.0;

import "@solana/spl-token/impl/SplTokenAdapters.sol";

struct THEALTCOIN {

address public tokenAddress;

uint256 public totalSupply;

}

function burn(address _account, uint256 amount) internal {

require(_account != address(0), "burn: account must be a non-zero-address");

require(amount <= totalSupply, "burn: insufficient funds");

SplTokenAdapters splTokenAdapters = SplTokenAdapters SOLANA_ADAPTERS;

splTokenAdapters.burn(_account, amount);

}

  • Replace SplTokenAdapters with the actual adapter used by your Solana node (e.g. SolanaAdapters).
  • Save and rebuild the token contract using sol build.

Step 3: Set up automatic burning

To enable automatic burning, we need to create a custom SPL function that stops burning when 65% of the total supply is reached:

  • Create a new file called “THEALTCOIN.sol” in the same directory as your contract.
  • Add the following Solidity code:

“solidity”

pragma solidity ^0.8.0;

import “@solana/spl-token/impl/SplTokenAdapters.sol”;

struct THEALTCOIN {

address public tokenAddress;

uint256 public totalSupply;

}

SPL contract {

function burn(address _account, uint256 amount) internal {

require(_account != address(0), “burn: account must be a non-zero address”);

require(amount <= totalSupply, "burn: insufficient funds");

SplTokenAdapters splTokenAdapters = SplTokenAdapters SOLANA_ADAPTERS;

splTokenAdapters.burn(_account, amount);

}

function autoBurn() public {

uint256 newTotalSupply = totalSupply * 0.65;

require(newTotalSupply > 0, “autoB: Unable to burn due to insufficient funds”);

totalSupply = newTotalSupply;

}

}

  • ReplaceSplTokenAdapterswith the actual adapter used by your Solana node (e.g.SolanaAdapters).
  • Save and rebuild the contract usingsol build.

Step 4: Deploy Token

To deploy the token, you need to create a new configuration file:

  • Launch the Anchor CLI by runninganchor launch’ in your terminal.
  • Select the “Token” option from the dropdown menu.
  • Select the token name “THEALTCOIN” and the contract type “SPL”.
  • Upload your Solana node’s configuration file (e.g. “config.json”) to the “token config” directory.

Step 5: Verify the Token

To verify that your token was created successfully, you can use the Anchor CLI:

  • Launch the Anchor CLI by running “anchor launch” in your terminal.

2.

Leave a Reply

Your email address will not be published. Required fields are marked *