anchor-spl crate provides CPI clients and helper modules for interacting with Solana’s core SPL programs from your Anchor programs. This page covers the available modules and how to use them effectively.
Installation
Addanchor-spl to your program’s dependencies:
Cargo.toml to include the IDL build feature:
Cargo.toml
Available Modules
Theanchor-spl crate provides the following modules for common SPL program interactions:
Token Programs
| Module | Description |
|---|---|
token | Original Token Program instructions and account types |
token_2022 | Token Extensions Program base instructions |
token_2022_extensions | Token Extensions Program extension-specific instructions |
token_interface | Account types compatible with both Token Program and Token Extensions |
associated_token | Associated Token Account Program instructions |
Other SPL Programs
| Module | Description |
|---|---|
metadata | Metaplex Token Metadata Program integration |
memo | SPL Memo Program integration |
stake | Solana Stake Program integration |
governance | SPL Governance Program integration |
Token Program Integration
The most common use case is interacting with Solana’s Token Programs. Theanchor-spl crate provides both program-specific modules (token, token_2022) and a unified interface (token_interface).
Using Token Interface (Recommended)
Thetoken_interface module provides account types that work with both the original Token Program and Token Extensions Program. This is the recommended approach for maximum compatibility:
Program-Specific Modules
For scenarios requiring specific token program features:Associated Token Accounts
Theassociated_token module provides helper functions for working with Associated Token Accounts (ATAs):
Token Extensions
Token 2022 introduces extensions that add functionality to mints and token accounts. Use thetoken_2022_extensions module:
Metadata Integration
For NFTs and fungible tokens with metadata, use themetadata module:
The metadata module requires enabling the
metadata feature in your Cargo.toml:Memo Program
Add on-chain memos to your transactions:Feature Flags
Theanchor-spl crate uses feature flags to control which modules are included:
token, token_2022, token_2022_extensions, associated_token, and mint features are enabled.
Common Patterns
Creating and Minting Tokens
Transfer with Authority
Best Practices
Use InterfaceAccount for Token Compatibility
Use InterfaceAccount for Token Compatibility
Always use
InterfaceAccount<'info, T> and Interface<'info, TokenInterface> when you want your program to work with both Token Program and Token Extensions Program:Check Account Ownership
Check Account Ownership
Always validate that token accounts belong to the expected token program:
Validate Mint Relationships
Validate Mint Relationships
Ensure token accounts belong to the correct mint: