Solidity Fundamentals

Lesson 7 of 7

Final Project: Build a Simple Token Contract

Combine everything from this course into a simple, ERC-20-style fungible token contract, the same basic pattern underlying most tokens on Ethereum.

Requirements

  1. Declare state variables for name, symbol, totalSupply, and a mapping(address => uint256) for balances.
  2. In the constructor, mint the entire totalSupply to msg.sender (the deployer).
  3. Implement function transfer(address to, uint256 amount) public returns (bool), using require to check the sender has enough balance, updating both balances, and emitting a Transfer event.
  4. Implement function balanceOf(address account) public view returns (uint256).
  5. Add an onlyOwner-style modifier and a mint(address to, uint256 amount) function restricted to the contract's owner, that increases totalSupply and the recipient's balance.
  6. Use a custom error (not a plain string) for the insufficient-balance case in transfer.

Stretch goals

  • Add approve/transferFrom to support spending on someone else's behalf (the rest of the real ERC-20 standard).
  • Add a burn(uint256 amount) function that destroys tokens from the caller's own balance.
  • Write a few test cases (using Hardhat or Foundry) proving transfer fails correctly when the sender has insufficient balance.

Submit a link to your finished contract below, an instructor will review it before you can mark this lesson complete. Good luck!

This lesson ends in a project. Build it on your own machine, there's nowhere to submit it here, but the brief above is everything you need.