Skip to content

​What Are Transfer Fees?

The Token Extension Program’s TransferFeeConfig mint extension applies a fee to each transfer for that mint. At transfer time:
  • The transferred amount is reduced by the configured fee
  • The withheld fee is tracked on the destination token account
  • Withheld fees can be withdrawn directly from token accounts or harvested to the mint
  • The withdraw_withheld_authority can move harvested fees to a fee receiver token account
Token Account ExtensionsWhen a token account is initialized for a mint with TransferFeeConfig, the token account is initialized with TransferFeeAmount. When the token account is created through the Associated Token Program, the required account size is calculated and the token account is created with the required size and rent-exempt lamports.

​Before you start

The examples on this page run against a local validator at http://localhost:8899 (WebSocket ws://localhost:8900) and fund their accounts with airdrops from it. Start one in a separate terminal with Surfpool (surfpool start) or solana-test-validator. Each code section below lists the packages or crates it needs. The TypeScript examples use top-level await, so run them as ES modules, for example with npx tsx index.ts in a project whose package.json sets "type": "module".

​How to Create a Transfer Fee Mint

To create a transfer fee mint:
  1. Calculate the mint account size and rent needed for the mint and the TransferFeeConfig extension.
  2. Create the mint account with CreateAccount, initialize TransferFeeConfig, and initialize the mint with InitializeMint.
  3. Create token accounts for the mint. TransferFeeAmount is automatically enabled for token accounts.
  4. Use TransferCheckedWithFee to transfer tokens and verify the expected fee. TransferChecked also works and automatically withholds the configured fee on the destination token account.
  5. Use WithdrawWithheldTokensFromAccounts to move withheld fees directly from token accounts to a fee receiver token account, signed by the mint’s withdraw_withheld_authority.
  6. Use permissionless HarvestWithheldTokensToMint to move withheld fees from token accounts to the mint account, then use WithdrawWithheldTokensFromMint to withdraw the mint account’s withheld fees to a fee receiver token account, signed by the mint’s withdraw_withheld_authority.
  7. Use SetTransferFee to update the next transfer fee configuration, which takes effect starting two epochs later.
1

Calculate account size

Calculate the mint account size for the base mint plus the TransferFeeConfig extension. This is the size used in CreateAccount.
Example
2

Calculate rent

Calculate rent using the size needed for the mint plus the TransferFeeConfig extension.
Example
3

Create the mint account

Create the mint account with the calculated space and lamports.
Example
4

Initialize TransferFeeConfig

Initialize the TransferFeeConfig extension on the mint.
Example
5

Initialize the mint

Initialize the mint with InitializeMint in the same transaction.
Example
6

Create token accounts and mint tokens

Create token accounts for the source, recipients, and fee receiver, then mint tokens to the source token account.
Example
7

Transfer with TransferCheckedWithFee

Transfer tokens with TransferCheckedWithFee and verify the expected fee.
Example
8

Transfer with TransferChecked

Transfer tokens with TransferChecked. The configured fee is still withheld on the destination token account.
Example
9

Withdraw withheld fees from token accounts

Use WithdrawWithheldTokensFromAccounts to move withheld fees directly from token accounts to the fee receiver token account.
Example
10

Harvest withheld fees to the mint

Use HarvestWithheldTokensToMint to move withheld fees from token accounts to the mint account’s withheld accumulator.
Example
11

Withdraw withheld fees from the mint

Use WithdrawWithheldTokensFromMint to move harvested fees from the mint account to the fee receiver token account.
Example
12

Update the next transfer fee

Use SetTransferFee to update the next transfer fee configuration.
Example
Instruction OrderInitializeTransferFeeConfig must come before InitializeMint. CreateAccount, InitializeTransferFeeConfig, and InitializeMint must be included in the same transaction.

​Source Reference

ItemDescriptionSource
TransferFeeConfigMint extension that stores the fee authorities, accumulated withheld amount, and the older and newer transfer fee configurations.Source
TransferFeeFee configuration struct stored in TransferFeeConfig that defines the epoch when the fee takes effect, the maximum fee, and the fee in basis points.Source
TransferFeeAmountToken account extension that stores the amount withheld during transfers for a token account.Source
TransferFeeInstruction::InitializeTransferFeeConfigInstruction that initializes transfer-fee settings before InitializeMint.Source
TransferFeeInstruction::TransferCheckedWithFeeTransfer instruction that verifies the caller-supplied fee matches the mint’s current transfer-fee config.Source
TransferFeeInstruction::WithdrawWithheldTokensFromAccountsInstruction that moves withheld fees directly from token accounts to a fee receiver token account, signed by the mint’s withdraw_withheld_authority.Source
TransferFeeInstruction::HarvestWithheldTokensToMintPermissionless instruction that moves withheld fees from token accounts to the mint account’s withheld accumulator.Source
TransferFeeInstruction::WithdrawWithheldTokensFromMintInstruction that withdraws the mint account’s withheld fees to a fee receiver token account, signed by the mint’s withdraw_withheld_authority.Source
TransferFeeInstruction::SetTransferFeeInstruction that updates the newer transfer fee configuration, which takes effect starting two epochs later.Source
process_initialize_transfer_fee_configProcessor logic that initializes the TransferFeeConfig extension on an uninitialized mint and sets both the older and newer transfer fee configurations to the initial fee.Source
process_withdraw_withheld_tokens_from_accountsProcessor logic that validates withdraw_withheld_authority and moves withheld fees from token accounts to a destination token account.Source
process_harvest_withheld_tokens_to_mintProcessor logic that harvests withheld fees from token accounts to the mint account’s withheld accumulator.Source
process_withdraw_withheld_tokens_from_mintProcessor logic that validates withdraw_withheld_authority and moves the mint account’s withheld amount to a destination token account.Source
process_set_transfer_feeProcessor logic that validates the transfer-fee config authority and updates the newer transfer fee configuration to take effect starting two epochs later.Source
get_required_init_account_extensionsUsed to automatically add token account extensions when token accounts are initialized based on extensions enabled on the mint. For TransferFeeConfig mints, it adds TransferFeeAmount.Source

​Kit

These examples use explicit Token-2022 instructions.
Terminal
Instructions

​Web3.js v3

These examples use the class-based @solana/web3.js v3 API with @solana-program instruction builders.
Terminal
Instructions

​Rust

Add these dependencies to Cargo.toml (tested with Rust 1.89, 1.98, and 1.99):
Cargo.toml
Rust

​Web3.js (legacy)

These examples use legacy @solana/web3.js with @solana/spl-token.
@solana/web3.js 1.x is no longer actively maintainedIt receives critical fixes only. Use Kit for new code. To migrate existing code, follow the Kit upgrade guide, or the web3.js v3 migration guide to keep the class-based API.To run this code, install 1.x with npm install @solana/web3.js@1. A plain npm install @solana/web3.js installs v3, which this code doesn’t run on.
Terminal
Instructions