Understanding the Solana Account Model: The Associated Token Account
Written on
Chapter 1: Introduction to Associated Token Accounts
In our previous discussion, we explored the creation of SPL tokens within Solana’s distinctive account framework. This article delves deeper into how Solana tracks the balances of SPL tokens held by each user. Let’s dive in!
Associated Token Account
As previously mentioned, the balances of SPL token holders are not found within the token's storage account. Instead, each user establishes a sub-account with their own keypair to manage their balance information. This sub-account is known as the Associated Token Account. Each Associated Token Account is specifically tied to one SPL token and a corresponding keypair. The structure of these accounts includes several key fields:
- SPL Token Address
- SPL Token Name
- Balance
- Owner
The SPL Token Address and SPL Token Name are self-explanatory, indicating the address and name related to the SPL token. The Balance field reflects the quantity of the SPL token that the account holds. Importantly, the Owner does not refer to the OwnerProgram (which has the authority to modify account data). Instead, it indicates the public key of the account to which this sub-account belongs, signifying who has the authority to authorize transactions involving funds within this account.
Management of these accounts falls under the purview of the Associated Token Account Program (ATAP). Thus, the program owner of each account is designated as ATAP. When a holder wishes to transfer their SPL token, they must sign an instruction and send it to ATAP. ATAP will verify the signer’s authority before executing the transaction.
If this seems a bit complex, don’t worry! Let’s put this into practice.
Creating an Associated Token Account
To create an associated token account for the SPL token we generated in the prior article, you would use the command:
$ spl-token create-account 3YDvXtpspistZfpJmcLBJMV8uomPRtqDb2zB2rxF3kLr
You will receive a response indicating the creation of the account, including a signature:
Creating account Bjjxh6B4r1WQHzRNubutJGgv8wirnQBXRAaxgFzWu8t5
Signature: 3gRsWGRhjjYZSmGoZKQVdxzG5ESLWYEAS8LrKWgsupURpuPru4ki8swhV8uvM7kTZdvmNTttEG66yyuXDRgtk8yJ
To observe the transaction, you can visit Solscan and enter the signature.
This transaction comprises four instructions. Initially, it directs the System Program to transfer some SOL to cover the rent for the associated token account. Next, it instructs the System Program to allocate a specific size for the associated token account. Following this, it sets the Token Program as the owner and finally interacts with the Token Program to initialize the account, specifying the token address, associated token address, and the account owner.
Let’s proceed to mint some tokens and observe the outcome:
$ spl-token mint 3YDvXtpspistZfpJmcLBJMV8uomPRtqDb2zB2rxF3kLr 100 Bjjxh6B4r1WQHzRNubutJGgv8wirnQBXRAaxgFzWu8t5
If you attempt to mint tokens from another wallet without mint authority, it will result in an error. However, if you possess the mint authority, the process should succeed. You can then check the instructions in Solscan.
You will see an instruction sent to the Token Program to mint 100 tokens and transfer them to the Associated Token Address.
Transferring SPL Tokens
Next, let’s initiate a transfer of this SPL token. We will create a new account with the following details:
pubkey: DKqUB1N1MCtdHewE5adNqBHCogr322vMygwmnkoLeNGi
To perform the transaction, enter:
$ spl-token transfer 3YDvXtpspistZfpJmcLBJMV8uomPRtqDb2zB2rxF3kLr 50 DKqUB1N1MCtdHewE5adNqBHCogr322vMygwmnkoLeNGi --allow-unfunded-recipient --fund-recipient
This will transfer 50 tokens with the following details:
- Sender: Bjjxh6B4r1WQHzRNubutJGgv8wirnQBXRAaxgFzWu8t5
- Recipient: DKqUB1N1MCtdHewE5adNqBHCogr322vMygwmnkoLeNGi
- Recipient associated token account: 3nsoNqXeYEHLZbMhqjgu9yfDS1i3VQmHUGDywqD3EoNt
- Funding recipient: 3nsoNqXeYEHLZbMhqjgu9yfDS1i3VQmHUGDywqD3EoNt (0.00203928 SOL)
- Signature: 2kRgvjNjEz8mGMC9GK5TsEc1KdkTPM49RskfY85X7cWn8DNd619d1W473m5AmVHrxKgpxfmhQmU9JrCVBLqTSLAF
Check Solscan for the transaction details:
With the insights gained from the above, these instructions should be straightforward. You should now have a clearer understanding of the underlying processes. However, if you have any questions, feel free to reach out or leave a comment below for further discussion.
Viewing the Token Account
Let’s take a look at the Token Account:
The Token Account clearly indicates that the Owner Program is the Token Program. Currently, it shows a total supply of 100 tokens, distributed among two holders. If you scroll down to the Holders section, you will find the Associated Token Addresses listed in the Address column, along with the owners of these addresses in the Owner column, displaying the quantity held by each owner.
Conclusion
By now, you should have a solid understanding of the Associated Token Account mechanism. In the next article, we will further explore how NFTs function within Solana and their associated metadata. Stay tuned for more insights!
This video titled "Who's really the owner of your account? [Solana Tutorial on Terminology]" explains the fundamental concepts surrounding account ownership within the Solana ecosystem.
In this updated video, "How to get the associated token account for a public key and mint address on Solana," you will learn the steps to obtain associated token accounts effectively.