Extensions
Learn how to enable token extensions to add optional features to token mints and accounts using the Token Extensions Program (Token 2022) in an Anchor program.
What are Token Extensions?
The Token Extensions Program (Token 2022) provides additional features through additional instructions referred to as extensions. Extensions are optional functionality that can be added to a token mint or token account.You can find the implementation of these extension instructions in the Token Extensions Program source code.
Each extension adds specific state that must be initialized during mint or token account creation. When initializing either type of account, you can enable multiple extensions simultaneously to add different functionality. However, extensions cannot be added after an account is created - you must include all desired extensions during the initial account creation. This is an important consideration when designing your token, as you'll need to plan ahead for which features you want your token to support.
Some extensions are incompatible with each other and cannot be enabled simultaneously on the same token mint or token account. For example, you cannot combine the NonTransferable extension with the TransferFeeConfig extension, since they have conflicting behaviors.
The Token Extensions Program defines an
ExtensionType
enum that specifies all available extensions that can be added to a token mint
or token account. Each variant represents a different extension with unique
functionality.
The ExtensionType
enum is defined as follows:
Each extension adds specialized functionality by including additional state that
must be initialized when creating a mint or token account. All extension
specific state is stored in the in the
tlv_data
field, which follows the base account data type. The tlv_data
(containing
extension state) must be further deserialized according to the specific
extension types enabled for that account.
Examples
The anchor-spl
crate provides a
token_2022_extensions
module that contains helper functions and types for working with token extension
instructions.
You can find examples for how to work with Token Extensions in an Anchor program in this program examples repository.
Note that while the anchor-spl crate provides helper functions for working with Token Extensions, not all extension instructions have been fully implemented yet. You may need to manually implement CPI calls for some extension instructions.