Tokens on Solana

Tokens are digital assets that represent ownership over diverse categories of assets. Tokenization enables the digitalization of property rights. Tokens on Solana are referred to as SPL (Solana Program Library) Tokens.

This section covers the basics concepts of how tokens are represented on Solana. Refer to the SPL Token Basics section for code examples.

Key Points

  • Token Programs contain all instruction logic for interacting with tokens on the network (both fungible and non-fungible).

  • A Mint Account represents a specific token and stores global metadata about the token such as the total supply and mint authority (address authorized to create new units of a token).

  • A Token Account tracks individual ownership of tokens for a specific mint account for a specific owner.

  • An Associated Token Account is a Token Account created with an address derived from the owner and mint account addresses.

Token Programs

The Solana ecosystem has two main Token Programs. Source code for both programs below.

Token Programs contains all instruction logic for interacting with tokens on the network (both fungible and non-fungible). All tokens on Solana are effectively data accounts owned by a Token Program.

Token ProgramToken Program

Mint Account

Tokens on Solana are uniquely identified by the address of a Mint Account owned by the Token Program. This account acts as a global counter for a specific token and stores data such as:

  • Supply: Total supply of the token
  • Decimals: Decimal precision of the token
  • Mint authority: The account authorized to create new units of the token, increasing the supply
  • Freeze authority: The account authorized to freeze tokens in a Token Account, preventing them from being transferred or burned

Mint AccountMint Account

The full details stored on each Mint Account include the following:

Mint Account State
pub struct Mint {
/// Optional authority used to mint new tokens. The mint authority may only
/// be provided during mint creation. If no mint authority is present
/// then the mint has a fixed supply and no further tokens may be
/// minted.
pub mint_authority: COption<Pubkey>,
/// Total supply of tokens.
pub supply: u64,
/// Number of base 10 digits to the right of the decimal place.
pub decimals: u8,
/// Is `true` if this structure has been initialized
pub is_initialized: bool,
/// Optional authority to freeze token accounts.
pub freeze_authority: COption<Pubkey>,
}

For reference, here is a Solana Explorer link to the USDC Mint Account.

Token Account

The Token Program creates Token Accounts to track individual ownership of each token unit. A Token Account stores data such as:

  • Mint: The token the Token Account holds units of
  • Owner: The account authorized to transfer tokens from the Token Account
  • Amount: Number of the tokens the Token Account currently holds

Token AccountToken Account

The full details stored on each Token Account include the following:

Token Account State
pub struct Account {
/// The mint associated with this account
pub mint: Pubkey,
/// The owner of this account.
pub owner: Pubkey,
/// The amount of tokens this account holds.
pub amount: u64,
/// If `delegate` is `Some` then `delegated_amount` represents
/// the amount authorized by the delegate
pub delegate: COption<Pubkey>,
/// The account's state
pub state: AccountState,
/// If is_native.is_some, this is a native token, and the value logs the
/// rent-exempt reserve. An Account is required to be rent-exempt, so
/// the value is used by the Processor to ensure that wrapped SOL
/// accounts do not drop below this threshold.
pub is_native: COption<u64>,
/// The amount delegated
pub delegated_amount: u64,
/// Optional authority to close the account.
pub close_authority: COption<Pubkey>,
}

A wallet needs a token account for each token (mint) it wants to hold, with the wallet address set as the token account owner. Each wallet can own multiple token accounts for the same token (mint), but a token account can only have one owner and hold units of one token (mint).

Account RelationshipAccount Relationship

Note that each Token Account's data includes an owner field identifying who has authority over the Token Account. This differs from the program owner specified in the base Account type, which is the Token Program for all Token Accounts.

Associated Token Account

Associated Token Accounts simplify the process of finding a token account's address for a specific mint and owner. Think of the Associated Token Account as the "default" token account for a specific mint and owner.

An Associated Token Account is created with an address derived from the owner's address and the mint account's address. It's important to understand that an Associated Token Account is just a token account with a specific address.

This introduces a key concept in Solana development: Program Derived Address (PDA). A PDA derives an address deterministically using predefined inputs, making it easy to find the address of an account.

Associated Token AccountAssociated Token Account

Note that each wallet needs its own token account to hold tokens from the same mint.

Accounts Relationship ExpandedAccounts Relationship Expanded

Token CLI Examples

The spl-token CLI helps you experiment with SPL tokens. The examples below use the Solana Playground terminal to run CLI commands directly in the browser without local installation.

Creating tokens and accounts requires SOL for account rent deposits and transaction fees. For first-time Solana Playground users, create a Playground wallet and run the solana airdrop command in the Playground terminal. You can also get devnet SOL from the public web faucet.

Terminal
$
solana airdrop 2

Run spl-token --help to see all available commands.

Terminal
$
spl-token --help

To install the spl-token CLI locally, run:

Terminal
$
cargo install spl-token-cli

Find more examples in the spl-token documentation.

The account addresses shown in your terminal output differ from the examples below. Use the addresses from your Playground terminal when following along. For example, the create-token command outputs a mint account address with your Playground wallet as the mint authority.

Create a New Token

To create a new token (mint account), run:

Terminal
$
spl-token create-token

The output looks like:

Terminal Output
Creating token 99zqUzQGohamfYxyo8ykTEbi91iom3CLmwCA75FK5zTg
Address: 99zqUzQGohamfYxyo8ykTEbi91iom3CLmwCA75FK5zTg
Decimals: 9
Signature: 44fvKfT1ezBUwdzrCys3fvCdFxbLMnNvBstds76QZyE6cXag5NupBprSXwxPTzzjrC3cA6nvUZaLFTvmcKyzxrm1

A new mint account starts with zero supply. Check the current supply with:

Terminal
$
spl-token supply <TOKEN_ADDRESS>

The supply command for a new token returns 0:

Example
spl-token supply 99zqUzQGohamfYxyo8ykTEbi91iom3CLmwCA75FK5zTg

Creating a new Mint Account requires a transaction with two instructions. Here's a Javascript example on Solana Playground.

  1. The System Program creates a new account with space for the Mint Account data and transfers ownership to the Token Program.

  2. The Token Program initializes the data of the new account as a Mint Account

Create Token Account

To hold tokens of a specific mint, create a token account:

Terminal
$
spl-token create-account <TOKEN_ADDRESS>

Example command:

Example
spl-token create-account 99zqUzQGohamfYxyo8ykTEbi91iom3CLmwCA75FK5zTg

Output:

Terminal Output
Creating account AfB7uwBEsGtrrBqPTVqEgzWed5XdYfM1psPNLmf7EeX9
Signature: 2BtrynuCLX9CNofFiaw6Yzbx6hit66pup9Sk7aFjwU2NEbFz7NCHD9w9sWhrCfEd73XveAGK1DxFpJoQZPXU9tS1

The create-account command creates an associated token account with your wallet address as the owner.

To create a token account with a different owner:

Terminal
$
spl-token create-account --owner <OWNER_ADDRESS> <TOKEN_ADDRESS>

Note: <TOKEN_ADDRESS> specifies the mint account for the token account.

Example command:

Example
spl-token create-account --owner 2i3KvjDCZWxBsqcxBHpdEaZYQwQSYE6LXUMx5VjY5XrR 99zqUzQGohamfYxyo8ykTEbi91iom3CLmwCA75FK5zTg

Output:

Terminal Output
Creating account Hmyk3FSw4cfsuAes7sanp2oxSkE9ivaH6pMzDzbacqmt
Signature: 44vqKdfzspT592REDPY4goaRJH3uJ3Ce13G4BCuUHg35dVUbHuGTHvqn4ZjYF9BGe9QrjMfe9GmuLkQhSZCBQuEt

Creating an Associated Token Account requires one instruction that invokes the Associated Token Program. Here's a Javascript example on Solana Playground.

The Associated Token Program uses Cross Program Invocations to:

To create a new Token Account with a new keypair instead of an Associated Token Account address, send a transaction with two instructions. Here's a Javascript example on Solana Playground.

  1. The System Program creates a new account with space for the Token Account data and transfers ownership to the Token Program.

  2. The Token Program initializes the data as a Token Account

Mint Tokens

To create new units of a token, mint tokens to a Token Account:

Terminal
$
spl-token mint [OPTIONS] <TOKEN_ADDRESS> <TOKEN_AMOUNT> [--] [RECIPIENT_TOKEN_ACCOUNT_ADDRESS]

Example command:

Example
spl-token mint 99zqUzQGohamfYxyo8ykTEbi91iom3CLmwCA75FK5zTg 100

Output:

Terminal Output
Minting 100 tokens
Token: 99zqUzQGohamfYxyo8ykTEbi91iom3CLmwCA75FK5zTg
Recipient: AfB7uwBEsGtrrBqPTVqEgzWed5XdYfM1psPNLmf7EeX9
Signature: 2NJ1m7qCraPSBAVxbr2ssmWZmBU9Jc8pDtJAnyZsZJRcaYCYMqq1oRY1gqA4ddQno3g3xcnny5fzr1dvsnFKMEqG

To mint tokens to a different token account:

Example
spl-token mint 99zqUzQGohamfYxyo8ykTEbi91iom3CLmwCA75FK5zTg 100 -- Hmyk3FSw4cfsuAes7sanp2oxSkE9ivaH6pMzDzbacqmt

Output:

Terminal Output
Minting 100 tokens
Token: 99zqUzQGohamfYxyo8ykTEbi91iom3CLmwCA75FK5zTg
Recipient: Hmyk3FSw4cfsuAes7sanp2oxSkE9ivaH6pMzDzbacqmt
Signature: 3SQvNM3o9DsTiLwcEkSPT1Edr14RgE2wC54TEjonEP2swyVCp2jPWYWdD6RwXUGpvDNUkKWzVBZVFShn5yntxVd7

The MintTo instruction on the Token Program creates new tokens. The mint authority must sign the transaction. The instruction mints tokens to a Token Account and increases the total supply on the Mint Account. Here's a Javascript example on Solana Playground.

Transfer Tokens

To transfer tokens between token accounts:

Terminal
spl-token transfer [OPTIONS] <TOKEN_ADDRESS> <TOKEN_AMOUNT> <RECIPIENT_ADDRESS or RECIPIENT_TOKEN_ACCOUNT_ADDRESS>

Example command:

Example
spl-token transfer 99zqUzQGohamfYxyo8ykTEbi91iom3CLmwCA75FK5zTg 100 Hmyk3FSw4cfsuAes7sanp2oxSkE9ivaH6pMzDzbacqmt

Output:

Terminal Output
Transfer 100 tokens
Sender: AfB7uwBEsGtrrBqPTVqEgzWed5XdYfM1psPNLmf7EeX9
Recipient: Hmyk3FSw4cfsuAes7sanp2oxSkE9ivaH6pMzDzbacqmt
Signature: 5y6HVwV8V2hHGLTVmTmdySRiEUCZnWmkasAvJ7J6m7JR46obbGKCBqUFgLpZu5zQGwM4Xy6GZ4M5LKd1h6Padx3o

The Transfer instruction on the Token Program handles token transfers. The owner of the sender's Token Account must sign the transaction. The instruction moves tokens between Token Accounts. Here's a Javascript example on Solana Playground.

Both sender and recipient need token accounts for the specific token (mint). The sender can include instructions to create the recipient's token account in the same transaction.

Create Token Metadata

The Token Extensions Program lets you store metadata (name, symbol, image link) directly on the Mint Account.

To create a token with metadata extension:

Example
spl-token create-token --program-id TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb --enable-metadata

Output:

Terminal Output
Creating token BdhzpzhTD1MFqBiwNdrRy4jFo2FHFufw3n9e8sVjJczP under program TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb
To initialize metadata inside the mint, please run `spl-token initialize-metadata BdhzpzhTD1MFqBiwNdrRy4jFo2FHFufw3n9e8sVjJczP <YOUR_TOKEN_NAME> <YOUR_TOKEN_SYMBOL> <YOUR_TOKEN_URI>`, and sign with the mint authority.
Address: BdhzpzhTD1MFqBiwNdrRy4jFo2FHFufw3n9e8sVjJczP
Decimals: 9
Signature: 5iQofFeXdYhMi9uTzZghcq8stAaa6CY6saUwcdnELST13eNSifiuLbvR5DnRt311frkCTUh5oecj8YEvZSB3wfai

To initialize the metadata:

Terminal
spl-token initialize-metadata <TOKEN_MINT_ADDRESS> <YOUR_TOKEN_NAME> <YOUR_TOKEN_SYMBOL> <YOUR_TOKEN_URI>

The token URI links to off-chain metadata. See an example JSON format here.

Example command:

Example
spl-token initialize-metadata BdhzpzhTD1MFqBiwNdrRy4jFo2FHFufw3n9e8sVjJczP "TokenName" "TokenSymbol" "https://raw.githubusercontent.com/solana-developers/opos-asset/main/assets/DeveloperPortal/metadata.json"

View the metadata on Solana Explorer.

Learn more in the Metadata Extension Guide. For details about Token Extensions, see the Token Extensions Getting Started Guide and the SPL documentation.

Is this page helpful?

सामग्री तालिका

पृष्ठ संपादित करें