The NonTransferable
extension makes it possible to create tokens that cannot
be transferred. This enables the creation of "soul-bound" tokens, where digital
assets are intrinsically linked to an individual. While these tokens cannot be
transferred, the owner can still burn tokens and close the Token Account. This
prevents users from being "stuck" with an unwanted asset.
In this guide, we will walk through an example of creating "soul-bound" tokens
with the NonTransferable
extension using Solana Playground. Here is the
final script.
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
NonTransferable
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 NonTransferable
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
Next, let's add the instructions to a new transaction and send it to the
network. This will create a Mint Account with the NonTransferable
extension
enabled.
Run the script by clicking the Run
button. You can then inspect the
transaction on the SolanaFM.
Create Token Accounts
Next, let's set up two Token Accounts to demonstrate the functionality of the
NonTransferable
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 1 token to the sourceTokenAccount
to test the non-transferrable
enforcement.
Attempt Token Transfer
Next, let's try to transfer tokens from the sourceTokenAccount
to the
destinationTokenAccount
. We expect this transaction to fail due to the
NonTransferable
extension.
Run the script by clicking the Run
button. You can then inspect the error in
the Playground terminal. You should see a message similar to the following:
Burn Tokens and Close Token Account
While tokens can't be transferred, they can still be burned.
The Token Account can then be closed to recover the SOL that was allocated to the account. Note that the token balance must be 0.
Run the script by clicking the Run
button. You can then inspect the
transaction on the SolanaFM.
Conclusion
The NonTransferable
mint extension enables the creation of "soul-bound"
tokens, ensuring that digital assets are bound to an individual account. This
feature enables a unique mechanism for digital ownership such as for personal
achievements, identity, or credentials that are inherently non-transferable.