Skip to main content

Overview

The SimpleStorage contract is a foundational smart contract that demonstrates core Solidity concepts. It manages a favorite number and maintains a list of people with their associated favorite numbers. Key Features:
  • Store and retrieve a favorite number
  • Manage a dynamic list of people with their favorite numbers
  • Quick name-to-number lookup using mappings
  • Demonstrates structs, arrays, and mappings in action

Contract Structure

State Variables

uint256 myFavoriteNumber = 3;
Person[] public listOfPeople;
mapping(string => uint256) public nameToNumber;
  • myFavoriteNumber - Stores a single unsigned integer (initialized to 3)
  • listOfPeople - Dynamic array of Person structs
  • nameToNumber - Maps person names to their favorite numbers for efficient lookup

Data Types

Person Struct

struct Person {
    uint256 favoriteNumber;
    string name;
}
The Person struct defines a custom data type that bundles together a person’s name and their favorite number.

Core Functions

store()

function store(uint256 _favoriteNumber) public {
    myFavoriteNumber = _favoriteNumber;
}
Stores a new favorite number, overwriting the previous value. Parameters:
  • _favoriteNumber - The new favorite number to store
Visibility: public - Can be called by anyone
This is a state-changing function that costs gas to execute since it modifies blockchain storage.

retrieve()

function retrieve() public view returns (uint256) {
    return myFavoriteNumber;
}
Retrieves the currently stored favorite number. Returns: The current value of myFavoriteNumber Visibility: public view - Read-only function that doesn’t cost gas when called externally

addPerson()

function addPerson(string memory _name, uint256 _favoriteNumber) public {
    listOfPeople.push(Person(_favoriteNumber, _name));
    nameToNumber[_name] = _favoriteNumber;
}
Adds a new person to the list and creates a mapping entry for quick lookup. Parameters:
  • _name - The person’s name (stored in memory during function execution)
  • _favoriteNumber - The person’s favorite number
What it does:
  1. Creates a new Person struct
  2. Adds it to the listOfPeople array
  3. Creates a mapping entry for name-based lookup
The memory keyword indicates that _name is temporarily stored in memory during function execution, not permanently in contract storage.

Full Contract Code

// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

contract SimpleStorage {
    uint256 myFavoriteNumber = 3;

    struct Person {
        uint256 favoriteNumber;
        string name;
    }

    Person[] public listOfPeople;
    mapping(string => uint256) public nameToNumber;

    function store(uint256 _favoriteNumber) public {
        myFavoriteNumber = _favoriteNumber;
    }

    function retrieve() public view returns (uint256) {
        return myFavoriteNumber;
    }

    function addPerson(string memory _name, uint256 _favoriteNumber) public {
        listOfPeople.push(Person(_favoriteNumber, _name));
        nameToNumber[_name] = _favoriteNumber;
    }
}

Usage Examples

// Store the number 42
simpleStorage.store(42);

Concepts Demonstrated

Data Types

  • uint256 for numbers
  • string for text
  • Custom struct types

Storage Structures

  • Dynamic arrays (Person[])
  • Mappings for key-value pairs
  • State variables

Functions

  • State-changing functions
  • view functions for reading
  • Public visibility

Memory Keywords

  • memory for temporary data
  • Understanding storage locations

Key Takeaways

SimpleStorage teaches fundamental Solidity concepts:
  • How to declare and use state variables
  • Working with structs, arrays, and mappings
  • The difference between view and state-changing functions
  • Memory vs storage data locations
  • Public visibility and automatic getters
In production contracts, consider adding access controls to prevent unauthorized modifications to stored data.

Build docs developers (and LLMs) love