🎓 A1Learner
🔍
0 of 50 lessons completed

Solidity Project: Simple Storage

The Simple Storage project is a complete contract that stores a favourite number, a name, and one number for each caller. It brings state variables, functions, strings, mappings, and msg.sender together in one small program.

Store and retrieve a favourite number

The store function saves a number in state. The retrieve function reads the saved number back.

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

contract NumberStore {
    uint256 public favouriteNumber;

    function store(uint256 newNumber) public {
        favouriteNumber = newNumber;
    }

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

A name stored as a state string

A string state variable stores a name in the contract. The setName function replaces the stored name with a new one.

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

contract NameStore {
    string public storedName = "Ada";

    function setName(string memory newName) public {
        storedName = newName;
    }
}

Each caller stores a number under their own address

A mapping keyed by address gives every caller a separate slot. The storeMine function writes the number under msg.sender, the caller.

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

contract PersonalStore {
    mapping(address => uint256) public numberByPerson;

    function storeMine(uint256 newNumber) public {
        numberByPerson[msg.sender] = newNumber;
    }
}

Example

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

contract SimpleStorage {
    uint256 public favouriteNumber;
    string public storedName = "Ada";
    mapping(address => uint256) public numberByPerson;

    function store(uint256 newNumber) public {
        favouriteNumber = newNumber;
    }

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

    function setName(string memory newName) public {
        storedName = newName;
    }

    function storeMine(uint256 newNumber) public {
        numberByPerson[msg.sender] = newNumber;
    }
}

Key points

  • State variable: a value stored in the contract that lasts between calls.
  • store and retrieve: one function writes the favourite number, and one function reads it back.
  • State string: a piece of text stored in the contract like any other value.
  • Mapping: stored pairs that find a value from a key, here an address.
  • msg.sender: the address of the account or contract that made the current call.
  • Per-caller storage: each caller stores a separate number under their own address.

In this example, the contract holds a favourite number, a stored name, and a mapping from addresses to numbers. The store function writes the favourite number, and retrieve reads it back. The setName function replaces the stored name. The storeMine function writes a number under msg.sender, so two different callers keep two different numbers in the same mapping.

Note

a mapping value that was never set reads as the default, which is 0 for a number.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Remix IDE

Exercise

In Simple Storage, how does storeMine keep one caller's number separate from another caller's number?