As part of our blockchain development class, our tutor gave us an assignment through a GitHub Gist. The assignment was to study, understand, and explain a Solidity code.
This is my personal explanation and breakdown of the code, written based on how I understood each part.
// SPDX-License-Identifier: MIT
This tells us the license type. It simply means anyone can use or modify this code as long as they keep the license note. The compiler also requires it to avoid warnings.
pragma solidity ^0.8.0;
This sets the Solidity version the code should work with. It means the code will work with version 0.8.0. It helps to prevent errors from version differences.
contract UserRegistry {
Here we are declaring a new contract called UserRegistry. A contract in Solidity is like a class that contains data and functions that will run on the blockchain.
struct User {
A struct is like a container that groups related information. Here, we are defining a User structure.
uint256 id;
This holds the user’s ID number. uint256 means an unsigned integer (a number that cannot be negative).
string name;
This stores the name of the user as text.
uint256 age;
This keeps the user’s age as a number.
bool isActive;
This shows whether the user is active or not. True means active, and false means deleted or inactive.
mapping(uint256 => User) private _users;
A mapping works like a dictionary. It connects a user’s ID to their details (the User struct). The keyword private means only this contract can access it directly.
uint256 private _nextId = 1;
This is a counter that starts from 1. Each time a new user is created, this number goes up by one. That’s how new users get unique IDs.
mapping(address => uint256) private _addressToUserId;
This links a wallet address (the person creating a user) to their user ID. It helps to know who owns which user.
event UserCreated(uint256 id, string name, uint256 age, address indexed owner);
event UserUpdated(uint256 id, string name, uint256 age);
event UserDeleted(uint256 id);
Events are used to log actions on the blockchain. They make it easy for apps to track what happens.
modifier onlyUserOwner(uint256 userId) { ... }
A modifier is used to control who can use a function. This one checks if the person calling the function is the owner of that user.
Inside it, there’s a require statement that checks two things: if the user exists and if the caller is the supposed owner. If not, the function stops with the error “Not the owner.”
function createUser(string memory name, uint256 age) external { ... }
This function allows a user to create a new account. It takes in the user’s name and age.
Inside it:
~ It assigns the next available ID to the user.
~ It stores the user’s information in the _users mapping.
~ It links the creator’s address to their user ID.
~ It emits the UserCreated event to announce a new user was created.
function getUser(uint256 userId) external view returns (User memory) { ... }
This is a read-only function. It doesn’t change anything. It just returns the user’s information for a given ID. The keyword view means it only reads from the blockchain.
function updateUser(uint256 userId, string memory newName, uint256 newAge) external onlyUserOwner(userId) { ... }
This function allows a user to update their own name and age. The onlyUserOwner modifier ensures only the owner of that user profile can make changes. After updating, it emits the UserUpdated event.
function deleteUser(uint256 userId) external onlyUserOwner(userId) { ... }
This is used to deactivate a user instead of deleting them completely. It sets isActive to false and emits the UserDeleted event.
function getMyUserId() external view returns (uint256) { ... }
This is a helper function that returns the user ID linked to the caller’s wallet address. If the address has not created a user, it returns 0.
My Overall Understanding
This code allows people to create, view, update, and deactivate their user profiles on the blockchain. It uses mappings to store and connect data, events to log actions, and modifiers to control access.
Top comments (0)