How tokens work on Solana
ERC20 is an interface. Each token deploys a contract that implements it and stores every holder's balance in that contract. Solana takes the opposite approach: the Token Program is deployed once, and it stores each token and each balance in separate accounts.
transfer, approve, transferFromThree account types replace the contract:
- A mint account represents one token. Its address, the mint address, identifies the token the way a contract address does on Ethereum. It stores the supply, the decimals, and the mint and freeze authorities.
- A token account holds one owner's balance of one mint.
- An associated token account (ATA) is a token account at an address derived from the owner, the mint, and the mint's token program, so anyone can compute where a wallet holds a given token.
Solana has two token programs. The original Token Program covers minting, transfers, and delegates. The Token Extensions Program, also called Token-2022, includes the same instructions and adds optional features per mint.
Create a token
You create a token by sending transactions to the Token Program. You write no contract and deploy nothing. With the spl-token CLI, three commands create a mint, create your token account, and mint 100 tokens:
spl-token create-token
spl-token create-account <MINT_ADDRESS>
spl-token mint <MINT_ADDRESS> 100| Step | Ethereum (ERC20) | Solana (Token Program) |
|---|---|---|
| Write the token | Write or import a Solidity contract, often from OpenZeppelin. | Nothing to write. The Token Program is already deployed. |
| Deploy | Compile and deploy the contract. The contract address identifies the token. | Create a mint account. The mint address identifies the token. |
| Mint supply | Call the constructor or a mint function. | The mint authority sends a mint instruction to a token account. |
| Receive | Any address can hold a balance in the contract's storage. | The recipient needs a token account for the mint. The sender can create it in the same transaction. |
| Change behavior | Upgrade through a proxy or deploy a new contract. | Enable extensions when you create the mint, or write a separate program. |
| Audit | Each token contract needs its own review. | Every token runs one of two audited token programs. A transfer hook program is custom code and needs its own review. |
ERC20 functions on Solana
Each ERC20 function maps to a field you read from an account or an instruction you send to the Token Program.
name(), symbol()decimals()decimals from the mint account.totalSupply()supply from the mint account.balanceOf()getTokenAccountsByOwner.transfer()TransferCheckedapprove()ApproveCheckedtransferFrom()TransferChecked signed by the delegateallowance()delegate and delegated_amount from the token account.Read token data
ERC20 view functions become account reads. Fetch the mint account for the decimals and supply, and fetch the owner's associated token account for the balance. This example uses @solana/kit and @solana-program/token with a mint under the original Token Program. For a Token Extensions mint, pass that program's address as tokenProgram. The mint account's owner tells you which program it uses.
import { address, createSolanaRpc } from "@solana/kit";
import {
fetchMint,
findAssociatedTokenPda,
TOKEN_PROGRAM_ADDRESS,
} from "@solana-program/token";
const rpc = createSolanaRpc("https://api.devnet.solana.com");
const mint = address("<MINT_ADDRESS>");
const owner = address("<WALLET_ADDRESS>");
// decimals() and totalSupply()
const { data } = await fetchMint(rpc, mint);
console.log(data.decimals, data.supply);
// balanceOf(owner)
const [tokenAccount] = await findAssociatedTokenPda({
owner,
mint,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
});
const { value } = await rpc.getTokenAccountBalance(tokenAccount).send();
console.log(value.amount);The name and symbol are not fields of the mint. A token stores them in one of two places: a Metaplex Token Metadata account linked to the mint, or the metadata extension on a Token Extensions mint. For a mint that uses Metaplex metadata:
import { fetchMetadataFromSeeds } from "@metaplex-foundation/mpl-token-metadata-kit";
// name() and symbol()
const metadata = await fetchMetadataFromSeeds(rpc, { mint });
console.log(metadata.data.name, metadata.data.symbol);Transfer tokens
A transfer moves tokens from the sender's token account to the recipient's token account. Both accounts must exist, so a transfer to a new holder usually carries two instructions in one transaction.
TransferChecked, which verifies the mint and decimals before it moves tokens.const [source] = await findAssociatedTokenPda({
owner: sender.address,
mint,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
});
const [destination] = await findAssociatedTokenPda({
owner: recipient,
mint,
tokenProgram: TOKEN_PROGRAM_ADDRESS,
});
const instructions = [
// Creates the recipient's token account only if it does not exist yet.
getCreateAssociatedTokenIdempotentInstruction({
payer: sender,
ata: destination,
owner: recipient,
mint,
}),
getTransferCheckedInstruction({
source,
mint,
destination,
authority: sender,
amount: 1_000_000n, // 1 token with 6 decimals
decimals: 6,
}),
];1_000_000n. See Transfer tokens for the code that signs and sends the transaction.Approvals and delegates
Solana has an approval model, and you need it less often. An ERC20 flow such as a swap takes an approve transaction followed by a call that runs transferFrom. On Solana, the user signs the transaction that calls the program, and that signature lets the program transfer from the user's token account in the same transaction, so most flows need no standing allowance.
When another address must spend later, for example a subscription or an escrow, approve it as a delegate:
const approve = getApproveCheckedInstruction({
source, // the sender's token account
mint,
delegate, // address allowed to spend
owner: sender, // the token account owner signs the approval
amount: 1_000_000n,
decimals: 6,
});The owner, or the delegate itself, removes a delegate with the Revoke instruction. If a program needs custody across several users, hold the tokens in a token account owned by a program-derived address (PDA), an address that only the program can sign for, instead of collecting delegations.
Beyond ERC20: Token Extensions
ERC20 projects add features by editing the contract. On Solana, the Token Extensions Program ships common features as options you enable on a mint: