With any form of transaction, there's often a desire to collect or apply a fee. Similar to a small service charge every time you transfer money at a bank or the way royalties or taxes are collected for particular transfers.
The TransferFee
extension allows you to configure a transfer fee directly on
the Mint Account, enabling fees to be collected at a protocol level. Every time
tokens are transferred, the fee is set aside in the recipient's Token Account.
This fee is untouchable by the recipient and can only be accessed by the
Withdraw Authority.
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.
In this guide, we'll walk through an example of creating a mint with the
TransferFee
extension enabled using Solana Playground. Here is the
final script.
The Transfer Fee extension can ONLY take a fee from its same Token Mint. (e.g.
if you created TokenA
, all transfer fees via the Transfer Fee extension will
be in TokenA
). If you wish to achieve a similar transfer fee in a token other
that itself, use the Transfer Hook extension.
Getting Started
Start by opening this Solana Playground link with the following starter code.
If it is your first time using Solana Playground, you'll first need to create a Playground Wallet and fund the wallet with devnet SOL.
If you do not have a Playground wallet, you may see a type error within the
editor on all declarations of pg.wallet.publicKey
. This type error will clear
after you create a Playground wallet.
To get devnet SOL, run the solana airdrop
command in the Playground's
terminal, or visit this devnet faucet.
Once you've created and funded the Playground wallet, click the "Run" button to run the starter code.
Add Dependencies
Let's start by setting up our script. We'll be using the @solana/web3.js
and
@solana/spl-token
libraries.
Replace the starter code with the following:
Mint Setup
First, let's define the properties of the Mint Account we'll be creating in the following step.
Next, let's determine the size of the new Mint Account and calculate the minimum lamports needed for rent exemption.
With Token Extensions, the size of the Mint Account will vary based on the extensions enabled.
Build Instructions
Next, let's build the set of instructions to:
- Create a new account
- Initialize the
TransferFee
extension - Initialize the remaining Mint Account data
First, build the instruction to invoke the System Program to create an account and assign ownership to the Token Extensions Program.
Next, build the instruction to initialize the TransferFee
extension for the
Mint Account.
Lastly, build the instruction to initialize the rest of the Mint Account data. This is the same as with the original Token Program.
Send Transaction
Finally, we add the instructions to a new transaction and send it to the
network. This will create a mint account with the TransferFee
extension.
Run the script by clicking the Run
button. You can then inspect the
transactions on the SolanaFM.
Create Token Accounts
Next, let's set up two Token Accounts to demonstrate the functionality of the
TransferFee
extension.
First, create a sourceTokenAccount
owned by the Playground wallet.
Next, generate a random keypair and use it as the owner of a
destinationTokenAccount
.
Lastly, mint 2000 tokens to the sourceTokenAccount
to fund it.
Transfer Tokens
Next, let's try to transfer tokens from the sourceTokenAccount
to the
destinationTokenAccount
. The transfer fee will automatically be deducted from
the transfer amount and remain in the destinationTokenAccount
account.
To transfer tokens, we have to use the either the transferChecked
or
transferCheckedWithFee
instructions.
In this example, we'll use transferCheckedWithFee
. The transfer only succeeds
if the correct transfer fee amount is passed into the instruction.
Withdraw Fee from Token Accounts
When tokens are transferred, transfer fees automatically accumulate in the recipient Token Accounts. The Withdraw Authority can freely withdraw these withheld tokens from each Token Account of the Mint.
To find the Token Accounts that have accumulated fees, we need to fetch all Token Accounts for the mint and then filter for ones which have withheld tokens.
First, we fetch all Token Accounts for the Mint Account.
Next, we filter for Token Accounts that hold transfer fees.
Finally, we use the withdrawWithheldAuthority
instruction to withdraw the fees
from the Token Accounts to a specified destination Token Account.
Run the script by clicking the Run
button. You can then inspect the
transaction on the SolanaFM.
Harvest Fee to Mint Account
Token Accounts holding any tokens, including withheld ones, cannot be closed. However, a user may want to close a Token Account with withheld transfer fees.
Users can permissionlessly clear out Token Accounts of withheld tokens using the
harvestWithheldTokensToMint
instruction. This transfers the fees accumulated
on the Token Account directly to the Mint Account.
Let's first send another transfer so the destinationTokenAccount
has withheld
transfer fees.
Next, we'll "harvest" the fees from the destinationTokenAccount
. Note that
this can be done by anyone and not just the owner of the Token Account.
Withdraw Fee from Mint Account
Tokens "harvested" to the Mint Account can then be withdrawn at any time by the Withdraw Authority to a specified Token Account.
Run the script by clicking the Run
button. You can then inspect the
transaction on the SolanaFM.
Conclusion
The TransferFee
extension enables token creators to enforce fees on each
transfer without requiring extra instructions or specialized programs. This
approach ensures that fees are collected in the same currency as the transferred
tokens, simplifying the transaction process.