Skip to content

LeaderMalang/anchor-token-2022

Repository files navigation

Overview

In Dec 2024, Anchor released version 0.30.1, which includes several significant updates and improvements for developers. One major addition is the support for token account constraints for Token Extensions (Token 2022). This guide will walk you through these updates and demonstrate how to create and validate tokens using Anchor's sample program with Token Extensions.

What You Will Do

  • Understand the new token constraints in Anchor.
  • Review and recreate a sample program that creates and validates tokens with Token Extensions.

Prerequisites

  • Basic experience with building in Anchor (Guide: Getting Started with Anchor)
  • Familiarity with Solana Token Extensions
  • Experience with Rust programming language
  • Basic knowledge of JavaScript/TypeScript
  • Anchor v0.30.1 or later installed

Dependencies Used in this Guide

Dependency Version
solana-cli 1.18.26
anchor-cli 0.30.1
anchor-lang 0.30.1
anchor-spl 0.30.1
spl-tlv-account-resolution 0.6.3
spl-transfer-hook-interface 0.6.3
spl-type-length-value 0.4.3
spl-pod 0.2.2

Token Extensions Recap

Token Extensions, also known as Token-2022, extend the capabilities of the original SPL Token Program. They offer enhanced flexibility and additional functionalities such as metadata, transfer fees, and hooks, without compromising existing token safety.

Token Extensions in Anchor

With Anchor v0.30.1, developers can create and validate tokens with Token Extensions using Anchor constraints. Example:

defined::group_pointer::authority = YOUR_AUTH.key()

These constraints can be applied when creating a new token or validating an existing one.

Creating a Token Extension Program with Anchor

Step 1: Initiate a New Anchor Project

Ensure Anchor v0.30.1 is installed by running:

anchor --version

Initiate a new Anchor project:

anchor init token-extensions
cd token-extensions

Step 2: Update Cargo.toml

Add the following dependencies in programs/token-extensions/Cargo.toml:

[dependencies]
anchor-lang = { version = "0.30.1", features = ["init-if-needed"] }
anchor-spl = "0.30.1"
spl-tlv-account-resolution = "0.6.3"
spl-transfer-hook-interface = "0.6.3"
spl-type-length-value = "0.4.3"
spl-pod = "0.2.2"

Add the idl-build feature:

[features]
idl-build = ["anchor-lang/idl-build", "anchor-spl/idl-build"]

Step 3: Add Utility Functions

Create utils.rs inside programs/token-extensions/src/ and add:

use anchor_lang::prelude::*;
use anchor_spl::token_interface::spl_token_2022::*;

pub fn update_account_lamports_to_minimum_balance<'info>(
    account: AccountInfo<'info>,
    payer: AccountInfo<'info>,
    system_program: AccountInfo<'info>,
) -> Result<()> {
    let extra_lamports = Rent::get()?.minimum_balance(account.data_len()) - account.get_lamports();
    invoke(
        &transfer(payer.key, account.key, extra_lamports),
        &[payer, account, system_program],
    )?;
    Ok(())
}

Step 4: Update lib.rs

Replace contents of lib.rs with:

use anchor_lang::prelude::*;

pub mod instructions;
pub mod utils;

#[program]
pub mod token_extensions {
    use super::*;

    pub fn create_mint_account(
        ctx: Context<CreateMintAccount>,
        args: CreateMintAccountArgs,
    ) -> Result<()> {
        instructions::handler(ctx, args)
    }
}

Step 5: Define Instructions

Create instructions.rs and define:

#[derive(Accounts)]
pub struct CreateMintAccount<'info> {
    #[account(mut)]
    pub payer: Signer<'info>,
    #[account(init, mint::authority = payer)]
    pub mint: Box<InterfaceAccount<'info, Mint>>,
    pub system_program: Program<'info, System>,
}

pub fn handler(ctx: Context<CreateMintAccount>, args: CreateMintAccountArgs) -> Result<()> {
    Ok(())
}

Step 6: Test the Program

Replace tests/token-extensions.ts with:

import * as anchor from "@coral-xyz/anchor";
import { assert } from "chai";

it("Create mint account test passes", async () => {
    const tx = await program.methods.createMintAccount()
        .rpc();
    assert.ok(tx);
});

Run tests:

anchor test

Wrap Up

You have successfully created a new Anchor program that:

  • Creates mint accounts with Token Extensions
  • Validates mint extension constraints

Stay tuned for more updates and enhancements to Anchor's Token Extensions!

About

Token Extensions (also known as Token-2022) are an advanced token program on Solana that extends the capabilities of the original SPL Token Program. It is designed to offer developers enhanced flexibility and additional functionalities without compromising the safety of current tokens.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors