Hello, future blockchain developer! In Module Two, we learned about Ethereum and smart contracts. We learned how Ethereum works and why smart contracts are powerful. Now it is time to learn how to write smart contracts using Solidity.
Solidity is the programming language used to write smart contracts on Ethereum. It is designed to be easy to learn and powerful enough to build complex applications. In this module, we will learn the basics of Solidity — data types, variables, functions, and more. By the end of this module, you will be able to write your first smart contract.
Chidi is a young developer in Lagos. He wanted to write his first smart contract. He opened Remix, an online tool for writing Solidity. He wrote a simple contract that stored a message. He deployed it to a test network. He was amazed when it worked.
His contract was simple: it stored a string and allowed anyone to read it. It was like a digital notice board. Chidi learned that Solidity is not as hard as he thought. He continued to learn and eventually built more complex contracts.
Definition: Solidity is a programming language used to write smart contracts for Ethereum and other blockchain platforms.
Why important: Solidity is the most popular language for smart contract development.
Simple explanation: Solidity is the language you use to tell the blockchain what your smart contract should do.
Real-life example: A developer writes a smart contract for a token.
School example: A student writes a contract for a class project.
Home example: A hobbyist writes a contract for a game.
Nigerian example: A Nigerian developer writes a contract for a DeFi app.
Illustration:
+----------------------+
| Solidity = |
| Language for |
| smart contracts |
| → Ethereum |
| → dApps |
+----------------------+
Mini summary: Solidity is the language for writing smart contracts.
Definition: A Solidity contract is a collection of code (functions and data) that lives on the blockchain.
Why important: Every smart contract starts with a structure.
Simple explanation: Like a blueprint for a house.
Real-life example: A contract for a token.
School example: A contract for a class record.
Home example: A contract for a family agreement.
Nigerian example: A contract for a land registry.
Illustration:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyContract {
// Code goes here
}
Mini summary: A contract is the basic building block of Solidity.
Definition: Pragma is a statement that tells the compiler which version of Solidity to use.
Why important: It ensures your contract works with the right version.
Simple explanation: Like saying "Use this version of the language."
Real-life example: pragma solidity ^0.8.0;
School example: A student specifies the version for their project.
Home example: A family uses a specific recipe version.
Nigerian example: A developer specifies the version for their contract.
Illustration:
pragma solidity ^0.8.0;
Mini summary: Pragma specifies the Solidity version.
Definition: Data types define the kind of data you can store in a variable. Common types are uint, string, bool, and address.
Why important: You need to know what type of data you are working with.
Simple explanation: Like different containers for different things.
Real-life example: uint for numbers, string for text.
School example: A student uses uint for grades.
Home example: A parent uses string for names.
Nigerian example: A developer uses address for wallet addresses.
Illustration:
uint: 100
string: "Hello"
bool: true
address: 0x123...
Mini summary: Data types define the kind of data you store.
Definition: Variables are containers that store data. In Solidity, you can have state variables (stored on the blockchain) and local variables (temporary).
Why important: Variables store the data your contract needs.
Simple explanation: Like boxes that hold information.
Real-life example: uint public count = 0;
School example: A student stores their name.
Home example: A family stores a message.
Nigerian example: A developer stores a balance.
Illustration:
uint public count = 0;
string public message = "Hello";
address public owner;
Mini summary: Variables store data in your contract.
Definition: Functions are blocks of code that perform specific tasks. They can be called by users or other contracts.
Why important: Functions are how you interact with a smart contract.
Simple explanation: Like buttons that do something when pressed.
Real-life example: function setMessage(string memory _message) public { message = _message; }
School example: A function to update a grade.
Home example: A function to add a chore.
Nigerian example: A function to transfer tokens.
Illustration:
function setMessage(string memory _message) public {
message = _message;
}
Mini summary: Functions perform actions in your contract.
Definition: Visibility determines who can call a function or access a variable. The main types are public, private, and internal.
Why important: It controls access to your contract's data and functions.
Simple explanation: Like who is allowed to enter a room.
Real-life example: public means anyone can call it.
School example: A public function can be used by anyone.
Home example: A private variable is only for the contract.
Nigerian example: A public function for users.
Illustration:
public: Anyone can call
private: Only this contract can call
internal: Only this contract and derived contracts
Mini summary: Visibility controls access to functions and variables.
Definition: The Hello World contract is the simplest smart contract. It stores and returns a message.
Why important: It is the first step in learning Solidity.
Simple explanation: Like writing your first program.
Real-life example: A contract that says "Hello, World!"
School example: A student writes their first contract.
Home example: A family stores a greeting.
Nigerian example: A developer writes a test contract.
Illustration:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
string public message = "Hello, World!";
}
Mini summary: Hello World is the simplest smart contract.
Definition: You have learned the basics of Solidity — data types, variables, functions, and visibility.
Why important: You are now ready to write your own smart contracts.
Simple explanation: You know how to write a simple contract.
Real-life example: You can write a Hello World contract.
School example: You can build a simple project.
Home example: You can store a message on the blockchain.
Nigerian example: You can start building dApps.
Illustration:
+----------------------+
| You can write |
| Solidity! 🎉 |
| → Data types |
| → Variables |
| → Functions |
| → Visibility |
+----------------------+
Mini summary: You are now ready to write Solidity!
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract HelloWorld {
string public message = "Hello, World!";
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract MyContract {
// State variables
// Functions
}
| Type | Description |
|---|---|
| uint | Non-negative integer |
| string | Text |
| bool | True/False |
| address | Ethereum address |
public: Anyone can call
private: Only this contract
internal: This contract and derived
In this module, we learned the basics of Solidity. We discovered that Solidity is the language used to write smart contracts on Ethereum. We learned about the structure of a contract, data types, variables, functions, and visibility. We also wrote a simple "Hello World" contract. With this knowledge, you can now start writing your own smart contracts. Great job!
Match the word with its meaning:
| Word | Meaning |
|---|---|
| 1. Solidity | A) A non-negative integer |
| 2. uint | B) A block of code |
| 3. Function | C) The language for smart contracts |
| 4. Visibility | D) Controls access |
| 5. Pragma | E) Specifies the version |
(Answers: 1-C, 2-A, 3-B, 4-D, 5-E)
Scenario 1: You want to store a message on the blockchain. What contract would you write?
Scenario 2: You want to create a counter that increments. What functions would you write?
In groups of 3, write a Hello World contract in Remix. Deploy it and test it. Share your experience.
Write a simple contract that stores and retrieves a message. Deploy it on a test network.
Write a contract that stores and updates a message. Add a function to change the message. Deploy it and test it.
Write a Hello World contract in Remix. Take a screenshot of the deployed contract and share it.
Write a contract that stores a number and allows users to increment it. Add a function to reset the number.
Fill-in-the-blank: 1-smart contracts, 2-contract, 3-Pragma, 4-uint, 5-Function
True/False: 1-T, 2-T, 3-F, 4-T, 5-F
Matching: 1-C, 2-A, 3-B, 4-D, 5-E
In Module Four, we will learn advanced Solidity concepts like mappings, events, and inheritance. We will build more complex contracts. Before the next session, think about what you would like your contract to do.
See you in Module Four! 🎉
End of Module Three · Solidity Basics