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

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 will walk you through the process of creating an SPL token using Solana Playground Anchor. By the end of this guide, you will have a fully functional SPL token with a 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 installed the Solana SDK for Anchor (available at [
  • You have a basic understanding of the Solana blockchain and token management concepts

Step 1: Create a new token

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

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

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

Step 2: Configure SPL token functions

Next, we need to configure the SPL token functions:

  • Go 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 the Solana node (e.g. SolanaAdapters).
  • Save and rebuild the token contract using sol build.

Step 3: Configure Automatic Burning

To enable automatic burning, we need to create a custom SPL function that will stop 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:

pragma solidity ^0.8.0;

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

struct THEALTCOIN {

address public tokenAddress;

uint256 public totalSupply;

}

contract SPL {

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;

}

}

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

Step 4: Deploy the token

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

  • Launch Anchor CLI by running anchor launch in the terminal.
  • Select the “Token” option from the drop-down menu.
  • Select the token name THEALTCOIN and select the contract type SPL.
  • Upload the Solana node configuration file (e.g. config.json) to the token config directory.

Step 5: Verify the token

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

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

2.

Related Posts