Overview
Curated: · Written: · Reviewed:
Token Standards (ERC-20, ERC-721, ERC-1155)
In one line
One wire format per asset shape: ERC-20 for interchangeable units, ERC-721 for unique items, ERC-1155 for many token types in one contract. Reach for it when one integration must work with any compliant token, and say first that conformance proves call signatures and events, not value, custody or safety.
Key definitions
- Base units: the integers stored on chain.
decimals()is display metadata and changes no arithmetic. - Allowance: a spender's remaining
transferFromauthority.approveoverwrites it. - Token identity: (chain, contract, token ID) for NFTs. A token ID alone identifies nothing.
- Operator approval:
setApprovalForAll, which grants one address every token one owner holds in that contract. - Receiver hook:
onERC721ReceivedoronERC1155Received. It must return its exact selector or the transfer reverts. - ERC-2612 permit: an EIP-712 signature that sets an allowance and consumes the owner's nonce.
Formulas and complexity
Balance-difference accounting costs two balanceOf calls per deposit.
How it works
transfermoves the caller's balance.transferFromchecksallowance[owner][spender]and decrements it.- A token may return nothing, return false instead of reverting, take a fee, or rebase. A caller that decodes a missing bool reverts even though the transfer succeeded.
- Safe NFT and ERC-1155 transfers update state, then call the receiver hook on contract recipients. A wrong return value reverts the whole transfer.
- Event logs alone rebuild ownership and balances, so indexers key rows by canonical identity and roll back on reorgs.
When to use and when not to
| Situation | Standard | Trade-off to state |
|---|---|---|
| Interchangeable units, full DeFi tooling | ERC-20 | Read decimals per token. Fee and rebase tokens need balance-difference accounting. |
| Unique items, per-token provenance | ERC-721 | Enumeration is optional. Index events off-chain at scale. |
| Many token types or batch moves in one contract | ERC-1155 | setApprovalForAll is all-token authority. There is no per-token approve. |
| Royalty display | ERC-2981 | A lookup. Payment is voluntary, and peer-to-peer transfers skip it. |
| Tokenizing an asset with no real obligations | None | Document issuer, upgrade, custody and exit first. |
Traps interviewers probe
- Trusting the bool. The token returned empty data on success, so the router's ABI decode reverted in its own frame. Fix: use SafeERC20
safeTransfer, which accepts empty return data and treats an explicit false as failure. Aneth_callthat returns0xpoints to this. - Hardcoding decimals. 1,000,000 base units is 1 USDC at 6 decimals and 0.000000000001 at 18. Fix: call
decimals()per token and convert at the boundary. - Crediting the requested amount. A 2% fee on a 1,000 token deposit delivers 980. Minting 1,000 shares leaves the vault 20 tokens short. Fix: credit the balance difference, and reject tokens you cannot account for.
- Racing an allowance overwrite. Lowering an allowance from 100 to 50 lets the spender take 100 with the old value, then 50 after the new one lands, 150 in total. Fix: set zero first, or use increase and decrease allowance.
- Keying NFTs by token ID alone. Collections and chains reuse IDs, so a burn or transfer in one overwrites another's row. Fix: key on (chain, contract, token ID). Worked example: mainnet IDs 1 to 3,000 and Base IDs 2,501 to 5,500 sharing one (contract, token_id) key, with chain_id overwritten by the later write, leave 5,500 rows, 2,500 of them carrying chain ID 1.
Interview tips
Open with: "This is an interface standard. Conformance guarantees the ABI, so I check behaviour separately." Then answer the question asked.
Follow-ups to expect:
- Fee-on-transfer token in a vault: balance-difference accounting, or reject the token at the allowlist.
- Permit replay: the EIP-712 domain binds chain and contract, and the nonce is consumed once. With the nonce at 5, a permit signed for nonce 6 reverts. Once the nonce-5 permit lands, the nonce is 6.
- Token that lies about interfaces: ERC-165 picks a code path, then behaviour is verified.
supportsInterface(0xffffffff)must return false. - Operator approval in an NFT marketplace:
setApprovalForAllcovers future mints and removes per-token control, so disclose the breadth. - Proxy upgrade: append new state variables only. Inserting one shifts later slots, so
balancesandtotalSupplyread the wrong data. - Indexer reorg: roll back by block and key rows by (chain, contract, block, transaction, log index).
30-second answer outline:
- Name the standard and its shape: fungible, unique, or multi-token.
- State what conformance guarantees and what it does not.
- Give the integration rule: read decimals per token, check return data, credit the amount received.
- Name the trap that matches the question, then the fix.
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.