Associated Token Accounts (ATAs)
Suggest an editTo send someone tokens you need the address of their token account, and their wallet address does not tell you what it is. The Associated Token Account is one canonical address per wallet per mint, and anyone can compute it from the wallet and the mint.
A sender knows the wallet, not the token account
If you want to send Bob 50 USDC, you need to know the address of Bob's USDC token account. There's nothing in Bob's wallet pubkey that tells you. You could ask Bob, but that requires out-of-band coordination and does not scale to a public, permissionless system. You could let Bob create a token account at any address he likes, but then every sender would need a directory mapping Bob to his token account, and every token Bob holds would need its own entry.
The standard answer everyone uses is to make the token account's address a deterministic function of the wallet and the mint. Given Bob's wallet pubkey and the USDC mint pubkey, anyone can compute one address where Bob's USDC token account lives, if he has one. That address is the Associated Token Account.
The address is a PDA of the wallet, the Token Program ID, and the mint
The ATA address is a Program-Derived Address computed under the Associated Token Program. The seeds are the wallet pubkey, the Token Program's ID, and the mint pubkey, in that order. The derivation runs find_program_address as you'd compute any other PDA, so the address is computable offline, before any account at that address even exists.
The Associated Token Program creates the account at that address
The Associated Token Program is deployed at ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL, and the one instruction you need from it is Create. Create takes a payer, a wallet, and a mint. It verifies that the account you're asking to create lives at the derived address, has the System Program allocate it with rent from the payer, and has the Token Program initialize it as a TokenAccount with the right owner and mint. The payer does not have to be the wallet, so a sender can create the recipient's ATA.
init_if_needed is safe on an ATA, and the payer covers the rent
Anchor provides a constraint called init_if_needed that creates an account when it doesn't exist and uses the existing account otherwise. For ATAs this looks ideal: a handler that needs the user to have an ATA can guarantee that condition without forcing the client to make a separate Create call beforehand.
The constraint is disabled by default in Anchor, behind a feature flag, because on your own program's accounts it can lead to bugs. With it the handler runs in two situations, on a fresh account and on an existing account whose fields hold whatever earlier calls wrote, and a handler written only for the first is wrong in the second. The classic exploits lesson shows such a bug.
An ATA does not have this problem, because your handler never initializes its fields. The only way to create an account at that address is through the Associated Token Program's Create instruction, which always initializes the account as a proper TokenAccount with the correct mint and owner. An ATA that already exists can hold tokens, so the handler still must not assume a zero balance.
Using init_if_needed means the payer of your transaction pays for the ATA creation when the account doesn't already exist, and the payer may not have expected to pay. The handler also behaves differently depending on whether the account already exists, which makes its semantics implicit. Production protocols often prefer to require the client to create the ATA in a separate instruction before calling the handler, so the create-or-use distinction is plainly visible in the transaction's instruction list.
For your own program's PDAs, use plain init and require the client to create the account once. If the account already exists when init runs, the constraint fails, which is the safe behavior.
In Anchor, two constraints check that the account is the ATA
The Anchor account type for an ATA is Account<'info, TokenAccount>, the same as any other token account, with extra constraints to express "this must be the canonical ATA for this wallet and this mint."
use anchor_spl::associated_token::AssociatedToken;
use anchor_spl::token::{Mint, Token, TokenAccount};
#[derive(Accounts)]
pub struct Deposit<'info> {
#[account(mut)]
pub user: Signer<'info>,
pub mint: Account<'info, Mint>,
#[account(
mut,
associated_token::mint = mint,
associated_token::authority = user,
)]
pub user_ata: Account<'info, TokenAccount>,
pub token_program: Program<'info, Token>,
pub associated_token_program: Program<'info, AssociatedToken>,
pub system_program: Program<'info, System>,
}The associated_token::mint = mint and associated_token::authority = user constraints tell Anchor to verify that the account at user_ata is in fact the canonical ATA derived from user and mint. If the constraint fails, the instruction is rejected before your handler runs. The client supplies the address, Anchor verifies it's the right one, and your handler uses it without further checks.
When you want to create the ATA only if it doesn't exist, you'd add init_if_needed to the constraint block, along with payer = user to fund the rent if creation is needed. You'd also need to enable the init-if-needed feature on the anchor-lang dependency in your Cargo.toml.
Someone pays for the recipient's account, and the ATA is not the only token account
Computing the address is free, but the account still has to be created, and whoever creates it pays the rent deposit. If Bob has no USDC account yet, either he creates it first or the sender pays for it. The ATA is also a convention and not a rule of the Token Program, so a wallet can own token accounts at other addresses.
For every token account your instruction takes, decide whether you would accept one that is not the ATA, and what breaks in each case.