Skip to main content

Extension Guide

The Token-2022 program provides additional functionality on mints and token accounts through an extension model.

This guide explains all of the available extensions, along with some examples of how to use them.

Please see the Token-2022 Introduction more general information about Token-2022 and the concept of extensions.

Setupโ€‹

See the Token Setup Guide to install the client utilities. Token-2022 shares the same CLI and NPM packages for maximal compatibility.

All JS examples are adapted from the tests, and available in full at the Token JS examples.

Extensionsโ€‹

Mint Close Authorityโ€‹

The Token program allows owners to close token accounts, but it is impossible to close mint accounts. In Token-2022, it is possible to close mints by initializing the MintCloseAuthority extension before initializing the mint.

Example: Initializing a mint with mint close authorityโ€‹

CLI support coming soon!

Example: Closing a mintโ€‹

With the MintCloseAuthority extension on the mint and a valid authority, it's possible to close the mint account and reclaim the lamports on the mint account. Note: The supply on the mint must be 0.

CLI support coming soon!

Transfer Feesโ€‹

In the Token program, it is impossible to assess a fee on every transfer. The existing systems typically involve freezing user accounts, and forcing them to go through a third party to unfreeze, transfer, and refreeze the accounts.

With Token-2022, it's possible to configure a transfer fee on a mint so that fees are assessed at the protocol level. On every transfer, some amount is withheld on the recipient account, untouchable by the recipient. These tokens can be withheld by a separate authority on the mint.

Important note: Transferring tokens with a transfer fee requires using transfer_checked or transfer_checked_with_fee instead of transfer. Otherwise, the transfer will fail.

Example: Creating a mint with a transfer feeโ€‹

Transfer fee configurations contain a few important fields:

  • Fee in basis points: fee assessed on every transfer, as basis points of the transfer amount. For example, with 50 basis points, a transfer of 1,000 tokens yields 5 tokens
  • Maximum fee: cap on transfer fees. With a maximum fee of 5,000 tokens, even a transfer of 10,000,000,000,000 tokens only yields 5,000 tokens
  • Transfer fee authority: entity that can modify the fees
  • Withdraw withheld authority: entity that can move tokens withheld on the mint or token accounts

Let's create a mint with 50 basis point transfer fee, and a maximum fee of 5,000 tokens.

CLI support coming soon!

Example: Transferring tokens with the fee checkedโ€‹

As part of the extension, there is a new transfer_checked_with_fee instruction, which accepts the expected fee. The transfer only succeeds if the fee is correctly calculated, in order to avoid any surprises during the transfer.

CLI support coming soon!

Example: Find accounts with withheld tokensโ€‹

As users transfer their tokens, transfer fees accumulate in the various recipient accounts. The withdraw withheld authority, configured at initialization, can move these tokens wherever they wish using withdraw_withheld_tokens_from_accounts or harvest_withheld_tokens_to_mint.

Before doing that, however, they must find which accounts have withheld tokens by iterating over all accounts for the mint.

CLI support coming soon!

Example: Withdraw withheld tokens from accountsโ€‹

With the accounts found, the withheld withdraw authority may move the withheld tokens.

CLI support coming soon!

Note: The design of pooling transfer fees at the recipient account is meant to maximize parallelization of transactions. Otherwise, one configured fee recipient account would be write-locked between parallel transfers, decreasing throughput of the protocol.

Example: Harvest withheld tokens to mintโ€‹

Users may want to close a token account with withheld transfer fees, but it is impossible to close an account that holds any tokens, including withheld ones.

To clear out their account of withheld tokens, they can use the permissionless harvest_withheld_tokens_to_mint instruction.

CLI support coming soon!

Example: Withdraw withheld tokens from mintโ€‹

As users move the withheld tokens to the mint, the withdraw authority may choose to move those tokens from the mint to any other account.

CLI support coming soon!

Default Account Stateโ€‹

A mint creator may want to restrict who can use their token. There are many heavy-handed approaches to this problem, most of which include going through a centralized service at the beginning. Even through a centralized service, however, it's possible for anyone to create a new token account and transfer the tokens around.

To simplify the restriction, a mint creator may use the DefaultAccountState extension, which can force all new token accounts to be frozen. This way, users must eventually interact with some service to unfreeze their account and use tokens.

Example: Creating a mint with default frozen accountsโ€‹

CLI support coming soon!

Example: Updating default stateโ€‹

Over time, if the mint creator decides to relax this restriction, the freeze authority may sign an update_default_account_state instruction to make all accounts unfrozen by default.

CLI support coming soon!

Immutable Ownerโ€‹

Token account owners may reassign ownership to any other address. This is useful in many situations, but it can also create security vulnerabilities.

For example, the addresses for Associated Token Accounts are derived based on the owner and the mint, making it easy to find the "right" token account for an owner. If the account owner has reassigned ownership of their associated token account, then applications may derive the address for that account and use it, not knowing that it does not belong to the owner anymore.

To avoid this issue, Token-2022 includes the ImmutableOwner extension, which makes it impossible to reassign ownership of an account. The Associated Token Account program always uses this extension when creating accounts.

Example: Explicitly creating an account with immutable ownershipโ€‹

CLI support coming soon!

Example: Creating an associated token account with immutable ownershipโ€‹

All associated token accounts have the immutable owner extension included, so it's extremely easy to use the extension.

CLI support coming soon!

Non-Transferable Tokensโ€‹

To accompany immutably owner token accounts, the NonTransferable mint extension allows for "soul-bound" tokens that cannot be moved to any other entity. For example, this extension is perfect for achievements that can only belong to one person or account.

This extension is very similar to issuing a token and then freezing the account, but allows the owner to burn and close the account if they want.

Example: Creating a non-transferable mintโ€‹

CLI support coming soon!

Required Memo on Transferโ€‹

Traditional banking systems typically require a memo to accompany all transfers. The Token-2022 program contains an extension to satisfy this requirement.

By enabling required memo transfers on your token account, the program enforces that all incoming transfers must have an accompanying memo instruction right before the transfer instruction.

Note: This also works in CPI contexts, as long as a CPI is performed to log the memo before invoking the transfer.

Example: Create account with required memo transfersโ€‹

CLI support coming soon!

Example: Enabling or disabling required memo transfersโ€‹

An account owner may always choose to flip required memo transfers on or off.

CLI support coming soon!

Reallocateโ€‹

In the previous example, astute readers of the JavaScript code may have noticed that the EnableRequiredMemoTransfers instruction came after InitializeAccount, which means that this extension can be enabled after the account is already created.

In order to actually add this extension after the account is created, however, you may need to reallocate more space in the account for the additional extension bytes.

The Reallocate instruction allows an owner to reallocate their token account to fit room for more extensions.

Example: Reallocating existing account to enable required memo transfersโ€‹

CLI support coming soon!

Interest-Bearing Tokensโ€‹

Tokens that constantly grow or decrease in value have many uses in the real world. The most well known example is a bond.

With Token, this has only been possible through proxy contracts that require regular rebase or update operations.

With the Token-2022 extension model, however, we have the possibility to change how the UI amount of tokens are represented. Using the InterestBearingMint extension and the amount_to_ui_amount instruction, you can set an interest rate on your token and fetch its amount with interest at any time.

Interest is continuously compounded based on the timestamp in the network. Due to drift that may occur in the network timestamp, the accumulated interest could be lower than the expected value. Thankfully, this is rare.

Note: No new tokens are ever created, the UI amount returns the amount of tokens plus all interest the tokens have accumulated. The feature is entirely cosmetic.

Example: Create an interest-bearing mintโ€‹

CLI support coming soon!

Example: Update the interest rateโ€‹

The rate authority may update the interest rate on the mint at any time.

CLI support coming soon!