Skill 2.2.4
Implement ERC-20 and ERC-721 tokens
Key Concepts
ERC-20 defines fungible tokens, where every unit is identical. Its functions are totalSupply, balanceOf, transfer, approve, allowance and transferFrom, with the Transfer and Approval events; optional name, symbol and decimals (usually 18) describe it. A contract that needs to move a user's tokens, such as an exchange, is first approved by the user and then calls transferFrom.
ERC-721 defines non-fungible tokens (NFTs), where each token ID is unique and has one owner: ownerOf, balanceOf, transferFrom, safeTransferFrom, approve and setApprovalForAll, with metadata through tokenURI. safeTransferFrom calls onERC721Received on a receiving contract so tokens are not locked in contracts that cannot handle them. ERC-1155 combines fungible and non-fungible tokens in one contract.
In practice, tokens inherit from audited OpenZeppelin implementations, for example contract MyToken is ERC20 { constructor() ERC20("My Token", "MTK") { _mint(msg.sender, 1_000_000 * 10 ** decimals()); } }.