Skip to content
Tutorials Center
My learning

Topics

Lesson 44 of 47 ยท Projects

Project: On-Chain To-Do List, Employee Registry and Wallet

Build three classic contracts: a to-do list with create, read, update and delete, an employee registry with access control, and a personal wallet, then connect them to a front end.

Many first "real" Solidity applications are small record-keeping contracts: a to-do list, a staff directory, a personal wallet. They are perfect practice because they combine everything from the earlier lessons: structs, arrays, mappings, events, modifiers and payable functions. They also show the four basic data operations, create, read, update and delete (CRUD), and how a web page talks to a contract.

You will design the three contracts, see the Solidity as read-only code, and run JavaScript stand-ins for the logic so that you can test behavior in your browser. Review Strings, Bytes, Arrays and Structs, Mappings and Modifiers and Access Control if needed.

Project 1: a to-do list

Design

A task has some text and a done flag. Each person has their own list, so the data is a mapping from an address to an array of tasks.

SOLIDITY
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract TodoList {
    struct Task {
        string text;
        bool done;
        bool deleted;
    }

    mapping(address => Task[]) private _tasks;

    event TaskAdded(address indexed user, uint256 indexed id, string text);
    event TaskUpdated(address indexed user, uint256 indexed id, bool done);
    event TaskDeleted(address indexed user, uint256 indexed id);

    error NoSuchTask(uint256 id);

    function addTask(string calldata text) external returns (uint256 id) {
        id = _tasks[msg.sender].length;
        _tasks[msg.sender].push(Task(text, false, false));
        emit TaskAdded(msg.sender, id, text);
    }

    function getTask(uint256 id) external view returns (string memory text, bool done) {
        Task storage t = _live(id);
        return (t.text, t.done);
    }

    function setDone(uint256 id, bool done) external {
        _live(id).done = done;
        emit TaskUpdated(msg.sender, id, done);
    }

    function deleteTask(uint256 id) external {
        _live(id).deleted = true;      // mark instead of shifting the array
        emit TaskDeleted(msg.sender, id);
    }

    function taskCount() external view returns (uint256) {
        return _tasks[msg.sender].length;   // includes deleted slots
    }

    function _live(uint256 id) private view returns (Task storage t) {
        if (id >= _tasks[msg.sender].length) revert NoSuchTask(id);
        t = _tasks[msg.sender][id];
        if (t.deleted) revert NoSuchTask(id);
    }
}

The four operations map as follows:

Operation Function Notes
Create addTask Pushes a struct and returns its index (the id)
Read getTask, taskCount view functions cost no gas when called from outside
Update setDone Changes a field of a storage reference
Delete deleteTask Marks the task as deleted

Two design choices deserve explanation. First, msg.sender selects the list, so each user sees and changes only their own tasks without any require. Second, deleteTask sets a flag instead of removing the element. Removing from the middle of an array means shifting every later element (cost grows with the list, and ids change) or swapping the last element into the hole (cheap, but changes an id). A flag keeps ids stable and the operation cheap. A front end simply skips deleted tasks, and the Task struct's old data stays in the chain's history anyway, which is why you should never put secrets in a to-do item.

Each state change emits an event, which is how a web page learns about changes without polling every task (see Events and Logs).

Project 2: an employee registry

Design

A company keeps a record per employee, but only the HR account may add or change them. A mapping from an employee's address to a struct stores the data, plus an array of addresses so that the list can be enumerated.

SOLIDITY
contract EmployeeRegistry {
    struct Employee {
        string name;
        string role;
        uint256 salary;       // in wei per month, for this example
        bool active;
    }

    address public immutable hr;
    mapping(address => Employee) private _employees;
    address[] private _ids;

    event EmployeeAdded(address indexed account, string name);
    event EmployeeUpdated(address indexed account);
    event EmployeeRemoved(address indexed account);

    error NotHR();
    error AlreadyExists(address account);
    error UnknownEmployee(address account);

    modifier onlyHR() {
        if (msg.sender != hr) revert NotHR();
        _;
    }

    constructor() {
        hr = msg.sender;
    }

    function addEmployee(address account, string calldata name, string calldata role, uint256 salary)
        external
        onlyHR
    {
        if (_employees[account].active) revert AlreadyExists(account);
        _employees[account] = Employee(name, role, salary, true);
        _ids.push(account);
        emit EmployeeAdded(account, name);
    }

    function getEmployee(address account) external view returns (Employee memory) {
        if (!_employees[account].active) revert UnknownEmployee(account);
        return _employees[account];
    }

    function updateRole(address account, string calldata role, uint256 salary) external onlyHR {
        if (!_employees[account].active) revert UnknownEmployee(account);
        _employees[account].role = role;
        _employees[account].salary = salary;
        emit EmployeeUpdated(account);
    }

    function removeEmployee(address account) external onlyHR {
        if (!_employees[account].active) revert UnknownEmployee(account);
        _employees[account].active = false;
        emit EmployeeRemoved(account);
    }

    function employeeCount() external view returns (uint256) {
        return _ids.length;
    }
}

Points to notice: hr is immutable (set once in the constructor), the onlyHR modifier guards every write, custom errors keep reverts cheap and informative, and getEmployee returns the whole struct. Privacy deserves honesty: private only blocks other contracts; salary data is public to anyone reading the chain. A real HR system would store only a hash or keep personal data off-chain.

Project 3: a personal wallet

A wallet contract holds ETH for its owner and lets only the owner spend it. The classic version tracks the owner, accepts deposits and has a withdrawal function.

SOLIDITY
contract PersonalWallet {
    address public immutable owner;

    event Deposited(address indexed from, uint256 amount);
    event Withdrawn(address indexed to, uint256 amount);

    error NotOwner();
    error InsufficientBalance(uint256 requested, uint256 available);
    error TransferFailed();

    constructor() {
        owner = msg.sender;
    }

    receive() external payable {
        emit Deposited(msg.sender, msg.value);
    }

    function balance() external view returns (uint256) {
        return address(this).balance;
    }

    function withdraw(address payable to, uint256 amount) external {
        if (msg.sender != owner) revert NotOwner();
        if (amount > address(this).balance) revert InsufficientBalance(amount, address(this).balance);
        (bool ok, ) = to.call{value: amount}("");
        if (!ok) revert TransferFailed();
        emit Withdrawn(to, amount);
    }
}

The words you will see in explanations of this contract: uint is the unsigned number type, msg.sender is the address that called the function, and a modifier (here an inline check) enforces who may call. The wallet follows the safe ordering from Sending and Receiving Ether: check, then send. A contract that also kept a per-user balance would update storage before the call (see Reentrancy Step by Step).

Connecting a front end

A web page for the to-do contract needs the contract's ABI and address, a provider and a signer (a wallet such as MetaMask). The pieces:

  1. Connect to the wallet and get the signer.
  2. Create a contract object from the address, the ABI and the signer.
  3. For reads, call contract.getTask(id). For writes, call contract.addTask("buy milk"), wait for the transaction to be mined, then refresh the list.
  4. Listen to TaskAdded events to update the page live.

With a local chain (Hardhat, Anvil or the legacy Ganache from Truffle, Ganache and web3.js), you deploy first, copy the address, and use ethers in the page. Every component you would also use in a larger application is here: a UI library for forms, a contract object, a wallet and an RPC node.

Try it: the to-do list as a JavaScript model

This model follows the contract: tasks per user, soft delete, reverts for invalid ids, and an event log.

JS TodoList: create, read, update, delete

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Try it: the registry and the wallet

The second model combines the HR-only registry and the owner-only wallet, showing access control and error cases side by side.

JS EmployeeRegistry and PersonalWallet

Press Run (or Ctrl + Enter) to see the console output here.

No output.

What is real: the contracts are complete and compile with Solidity 0.8.24, and the data-modeling choices (soft delete, per-user mapping, onlyHR, immutable owner, custom errors, events) are standard practice. What is the stand-in: the models run on plain JavaScript maps with string addresses and have no gas, signatures or storage, so write real tests in Foundry or Hardhat before deploying.

Ideas to extend the projects

  • Add a completedCount function to the to-do list, and think about whether to keep a counter or compute it.
  • Let employees update their own name but not their salary.
  • Add a Withdrawal limit per day to the wallet using block.timestamp / 1 days.
  • Put the registry behind OpenZeppelin's Ownable or AccessControl (Using OpenZeppelin).

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why does the to-do list mark a task as deleted instead of removing it from the array?

    Shifting is expensive and changes ids. A boolean flag keeps the operation cheap and indexes stable, and the front end skips deleted items.
  2. 2. How does TodoList give each user a private list without any require statements?

    The mapping is keyed by msg.sender, so a caller can only reach the list stored under their own address.
  3. 3. Does marking the employee mapping private keep salaries secret?

    Visibility only limits which code can read the variable. Storage is public, so sensitive data should be kept off-chain.
  4. 4. Why is the hr address declared immutable?

    An immutable is assigned during construction and baked into the deployed code, which saves gas and guarantees it cannot change.
  5. 5. How does a web page learn that a new task was added without polling every task?

    Events are written to the transaction logs and can be subscribed to or queried efficiently by clients.

Summary

  • A to-do list, an employee registry and a wallet cover CRUD with structs, mappings, arrays, events, modifiers and payable functions.
  • Key mapping from msg.sender for per-user data; use soft deletes to keep ids stable; use immutable for an owner set at deployment.
  • Guard writes with modifiers or checks, use custom errors, and emit an event for every state change so front ends can follow along.
  • private limits code access, not visibility on the chain, so never store secrets on-chain.
  • A front end needs the ABI, the address, a provider and a signer; reads are free calls and writes are transactions.

Next: test your knowledge with Practice Exercises With Solutions.