Transfer Fees
How to implement the TransferFeeExtension
The
TransferFeeExtension
enables automatic fee collection on every token transfer. Fees accumulate in the
recipient token accounts and can be withdrawn by the withdraw authority set on
the mint account.
Typescript
import { getCreateAccountInstruction } from "@solana-program/system";import {extension,getInitializeAccountInstruction,getInitializeMintInstruction,getInitializeTransferFeeConfigInstruction,getMintSize,getTokenSize,TOKEN_2022_PROGRAM_ADDRESS} from "@solana-program/token-2022";import {airdropFactory,appendTransactionMessageInstructions,createSolanaRpc,createSolanaRpcSubscriptions,createTransactionMessage,generateKeyPairSigner,getSignatureFromTransaction,lamports,pipe,sendAndConfirmTransactionFactory,setTransactionMessageFeePayerSigner,setTransactionMessageLifetimeUsingBlockhash,signTransactionMessageWithSigners} from "@solana/kit";// Create Connection, local validator in this exampleconst rpc = createSolanaRpc("http://localhost:8899");const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");// Generate the authority for the mint (also acts as fee payer)const authority = await generateKeyPairSigner();// Fund authority/fee payerawait airdropFactory({ rpc, rpcSubscriptions })({recipientAddress: authority.address,lamports: lamports(5_000_000_000n), // 5 SOLcommitment: "confirmed"});// Generate keypair to use as address of mintconst mint = await generateKeyPairSigner();// And a transfer fee config extension.const transferFees = {epoch: 0n,maximumFee: 1_000_000n,transferFeeBasisPoints: 150 // 1.5%};const transferFeeConfigExtension = extension("TransferFeeConfig", {transferFeeConfigAuthority: authority.address,withdrawWithheldAuthority: authority.address,withheldAmount: 0n,newerTransferFee: transferFees,// Used for transitioning configs. Starts by being the same as newerTransferFee.olderTransferFee: transferFees});// Get mint account size with transfer fee extensionconst space = BigInt(getMintSize([transferFeeConfigExtension]));// Get minimum balance for rent exemptionconst rent = await rpc.getMinimumBalanceForRentExemption(space).send();// Get latest blockhash to include in transactionconst { value: latestBlockhash } = await rpc.getLatestBlockhash().send();// Instruction to create new account for mint (token program)// Invokes the system programconst createMintAccountInstruction = getCreateAccountInstruction({payer: authority,newAccount: mint,lamports: rent,space,programAddress: TOKEN_2022_PROGRAM_ADDRESS});// Instruction to initialize transfer fee config extensionconst initializeTransferFeeConfigInstruction =getInitializeTransferFeeConfigInstruction({mint: mint.address,transferFeeConfigAuthority: authority.address,withdrawWithheldAuthority: authority.address,transferFeeBasisPoints: 100, // 1% feemaximumFee: 1_000_000n // Maximum fee of 1 token});// Instruction to initialize mint account data// Invokes the token22 programconst initializeMintInstruction = getInitializeMintInstruction({mint: mint.address,decimals: 6,mintAuthority: authority.address,freezeAuthority: authority.address});// Generate keypair to use as address of token accountconst tokenAccount = await generateKeyPairSigner();// get token account sizeconst tokenAccountLen = BigInt(getTokenSize([transferFeeConfigExtension]));// Get minimum balance for rent exemptionconst tokenAccountRent = await rpc.getMinimumBalanceForRentExemption(tokenAccountLen).send();// Instruction to create new account for the token account// Invokes the system programconst createTokenAccountInstruction = getCreateAccountInstruction({payer: authority,newAccount: tokenAccount,lamports: tokenAccountRent,space: tokenAccountLen,programAddress: TOKEN_2022_PROGRAM_ADDRESS});// Instruction to initialize the created token accountconst initializeTokenAccountInstruction = getInitializeAccountInstruction({account: tokenAccount.address,mint: mint.address,owner: authority.address});console.log("token account", tokenAccount.address); // ! debugconst instructions = [createMintAccountInstruction,initializeTransferFeeConfigInstruction,initializeMintInstruction,createTokenAccountInstruction,initializeTokenAccountInstruction];// Create transaction messageconst transactionMessage = pipe(createTransactionMessage({ version: 0 }),(tx) => setTransactionMessageFeePayerSigner(authority, tx),(tx) => setTransactionMessageLifetimeUsingBlockhash(latestBlockhash, tx),(tx) => appendTransactionMessageInstructions(instructions, tx));// Sign transaction message with all required signersconst signedTransaction =await signTransactionMessageWithSigners(transactionMessage);// Send and confirm transactionawait sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(signedTransaction,{ commitment: "confirmed", skipPreflight: true });// Get transaction signatureconst transactionSignature = getSignatureFromTransaction(signedTransaction);console.log("Mint Address with Transfer Fees:", mint.address.toString());console.log("Token Account:", tokenAccount.address.toString());console.log("Transfer Fee: 1.5% (150 basis points)");console.log("Maximum Fee: 1 token");console.log("Withdraw Authority:", authority.address.toString());console.log("Transaction Signature:", transactionSignature);
Console
Click to execute the code.
Rust
use anyhow::Result;use solana_client::nonblocking::rpc_client::RpcClient;use solana_sdk::{commitment_config::CommitmentConfig,signature::{Keypair, Signer},system_instruction::create_account,transaction::Transaction,};use spl_token_2022::{ID as TOKEN_2022_PROGRAM_ID,extension::{ExtensionType, transfer_fee::instruction::initialize_transfer_fee_config},instruction::{initialize_account, initialize_mint},state::{Account, Mint},};#[tokio::main]async fn main() -> Result<()> {// Create connection to local validatorlet client = RpcClient::new_with_commitment(String::from("http://localhost:8899"),CommitmentConfig::confirmed(),);let latest_blockhash = client.get_latest_blockhash().await?;// Generate a new keypair for the fee payerlet fee_payer = Keypair::new();// Airdrop 5 SOL to fee payerlet airdrop_signature = client.request_airdrop(&fee_payer.pubkey(), 5_000_000_000).await?;client.confirm_transaction(&airdrop_signature).await?;loop {let confirmed = client.confirm_transaction(&airdrop_signature).await?;if confirmed {break;}}// Generate keypair to use as address of mintlet mint = Keypair::new();// Get mint account size with transfer fee extension enabledlet mint_space =ExtensionType::try_calculate_account_len::<Mint>(&[ExtensionType::TransferFeeConfig])?;let mint_rent = client.get_minimum_balance_for_rent_exemption(mint_space).await?;// Instruction to create new account for mint (token22)let create_mint_account_instruction = create_account(&fee_payer.pubkey(), // payer&mint.pubkey(), // new account (mint)mint_rent, // lamportsmint_space as u64, // space&TOKEN_2022_PROGRAM_ID, // program id);// Instruction to initialize transfer fee config extensionlet initialize_transfer_fee_config_instruction = initialize_transfer_fee_config(&TOKEN_2022_PROGRAM_ID, // program_id&mint.pubkey(), // mintSome(&fee_payer.pubkey()), // transfer_fee_config_authoritySome(&fee_payer.pubkey()), // withdraw_withheld_authority100, // transfer_fee_basis_points (1% = 100 basis points)1_000_000, // maximum_fee (1 token with 6 decimals))?;// Instruction to initialize mint account datalet initialize_mint_instruction = initialize_mint(&TOKEN_2022_PROGRAM_ID, // program id&mint.pubkey(), // mint&fee_payer.pubkey(), // mint authoritySome(&fee_payer.pubkey()), // freeze authority6, // decimals)?;// Generate keypair to use as address of token accountlet token_account = Keypair::new();// Get default token account size (in bytes),let token_account_space =ExtensionType::try_calculate_account_len::<Account>(&[ExtensionType::TransferFeeConfig])?;let token_account_rent = client.get_minimum_balance_for_rent_exemption(token_account_space).await?;// Instruction to create new account for token account (token22)let create_token_account_instruction = create_account(&fee_payer.pubkey(), // payer&token_account.pubkey(), // new account (token account)token_account_rent, // renttoken_account_space as u64, // space&TOKEN_2022_PROGRAM_ID, // program id);// initialize token accountlet initialize_token_account = initialize_account(&TOKEN_2022_PROGRAM_ID, // program_id&token_account.pubkey(), // token account&mint.pubkey(), // mint&fee_payer.pubkey(), // authority)?;// Construct transaction with previous instructionslet transaction = Transaction::new_signed_with_payer(&[create_mint_account_instruction,initialize_transfer_fee_config_instruction,initialize_mint_instruction,create_token_account_instruction,initialize_token_account,],Some(&fee_payer.pubkey()),&[&fee_payer, &mint, &token_account],latest_blockhash,);// Send and confirm transactionlet transaction_signature = client.send_and_confirm_transaction(&transaction).await?;println!("Mint Address with Transfer Fees: {}", mint.pubkey());println!("Token Account Address: {}", token_account.pubkey());println!("Transfer Fee: 1% (100 basis points)");println!("Maximum Fee: 1 token");println!("Withdraw Authority: {}", fee_payer.pubkey());println!("\nSuccessfully enabled transfer fees on mint");println!("Transaction Signature: {}", transaction_signature);Ok(())}
Console
Click to execute the code.
Is this page helpful?