- A Beginner’s Guide to Solidity Smart Contracts: A Babies’ Guide
- Solidity Basics Part-1-: Understanding Solidity Data Types and Variables
Okay, let’s explain this Solidity Smart Contracts stuff like a baby who loves toys and candy!
We’re going to talk about two super cool examples: a toy box that holds a number and a candy coin system.
I’ll use simple stories, like playing with a piggy bank or running a candy store, to make it easy to understand, even if you’ve never heard of coding or blockchains before. Ready? Let’s dive in!
What’s a Smart Contract?

Imagine a smart contract as a magic toy box that follows your rules perfectly. It lives on the Ethereum blockchain, which is like a big, public notebook that everyone can read but nobody can erase or cheat. You write the rules in a language called Solidity, and the toy box does exactly what you say, like keeping track of toys or sharing candy coins!
Story 1: The Magic Toy Box (Simple Storage Contract)
A smart contract is like a toy box that can hold one number, like the number of teddy bears you have. Anyone can check the number or change it, and the blockchain notebook keeps it safe forever. Let’s look at the code and explain it like a story.

The Code
solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}
The Story, Step by Step
- The License Sticker (SPDX-License-Identifier): The first line, `
// SPDX-License-Identifier: GPL-3.0
, is like putting a sticker on your toy box that says, “You can share my toy box design, but follow these rules!” It tells everyone the code is free to use under a special license called GPL 3.0. It’s like telling your friends they can borrow your toy box idea if they’re nice about it. - The Tool Rule (pragma solidity): The second line,
pragma solidity >=0.4.16 <0.9.0
, is like saying, “This toy box only works with certain tools, like a specific screwdriver.” It tells the computer to use a version of Solidity (the coding language) between 0.4.16 and just before 0.9.0. This makes sure the toy box works the same way every time, even if someone tries a new, fancy tool. - The Toy Box Name (contract SimpleStorage): The contract
SimpleStorage
is the name of your toy box. It lives on the Ethereum blockchain, which is like a special address in the big notebook. The toy box has two parts: some code (instructions, like buttons to press) and some data (the number it holds). Think of it as a box with a piece of paper inside that says how many teddy bears you have. - The Number Slot (
uint storedData
): The line `uint storedData
; creates a spot or place in the toy box to hold one number, like “5 teddy bears.” The uint means it’s a big number (up to 256 bits, which is super huge!) and it can’t be negative. It’s like a little slot in a database, but it’s stored on the blockchain, so it’s safe and public. - Changing the Number (
function set
): Theset function
is like a button on the toy box. If you press it and say set(10), it writes “10 teddy bears” on the paper, replacing whatever was there before. The public part means anyone can press this button—your friends, neighbors, anyone! They just say, “Hey, toy box, store this number!” - Checking the Number (function get): The
get function
is like another button that lets you peek at the paper without changing it. If you press get(), the box says, “I have 10 teddy bears!” The view part means it’s just looking, not touching, and returns (uint) means it gives you back a number. Anyone can press this button, too. - No “this.” Needed: In some other coding languages, you’d say
this.storedData
to talk about the number in the box. In Solidity, you just say storedData. It’s like calling your teddy bear “Teddy” instead of “My Teddy.” This is important because usingthis.
does something totally different, but we’ll talk about that later. - Why It’s Cool: This toy box is awesome because anyone in the world can see or change the number, and it’s stored on the blockchain, so it’s super safe. If your friend changes “10 teddy bears” to “7 teddy bears,” the blockchain notebook remembers both numbers in its history, like a diary. But right now, anyone can change the number, which isn’t always what you want. Later, we can add a lock so only you can change it!
- Watch Out for Funny Letters (Unicode Warning): The warning about Unicode text is like saying, “Be careful when writing on the paper! Some letters that look the same might confuse the toy box.” For example, some weird symbols might look like an “A” but act different in the computer. Stick to simple letters and numbers to keep things clear.
- Only Simple Names (Note on Identifiers): The note about identifiers means names like
SimpleStorage or storedData
can only use regular letters (A-Z, a-z, 0-9). It’s like naming your toy box with plain stickers, not fancy emojis. But you can store fancier text (like emojis) in special variables called strings if you need to.
Real-Life Example
Imagine you have a toy box at the park where you write how many cookies you brought to share. Anyone can check the number by pressing get (“Hey, there’s 10 cookies!”) or change it by pressing set (“Now it’s 15 cookies!”). The blockchain notebook makes sure everyone sees the same number, and it never gets lost. It’s simple but powerful!
Story 2: The Candy Coin Store (Coin Solidity Smart Contract)
Now, let’s pretend you’re running a candy store where you make your own Candy Coins. Only you can make new coins, but anyone with coins can give them to friends. The blockchain notebook keeps track of who has how many coins, like a super fair scorekeeper.
The Code
solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.26;
contract Coin {
address public minter;
mapping(address => uint) public balances;
event Sent(address from, address to, uint amount);
constructor() {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
require(msg.sender == minter);
balances[receiver] += amount;
}
error InsufficientBalance(uint requested, uint available);
function send(address receiver, uint amount) public {
require(amount <= balances[msg.sender], InsufficientBalance(amount, balances[msg.sender]));
balances[msg.sender] -= amount;
balances[receiver] += amount;
emit Sent(msg.sender, receiver, amount);
}
}
The Story, Step by Step
- The License and Version: Just like the toy box, the first line (
SPDX-License-Identifier
) is a sticker saying, “Share my candy store rules nicely!” The second line (pragma solidity ^0.8.26
) says, “Use this exact tool (Solidity version 0.8.26
) to build my candy store.” This keeps everything working the same way. - The Candy Store (
contract Coin
): Thecontract Coin
is your candy store. It lives on the blockchain at a special address, like a shop in the big notebook. It has rules (code) and information (data), like who has how many Candy Coins. - The Boss (
address public minter
): Theminter
is you, the candy store owner! The address type is like yourID card
on the blockchain—a 160-bit code that identifies you or another contract. The public part means anyone can check who the boss is. The compiler (the computer that builds the contract) makes a special button (a function) that says, “Who’s the minter?” and answers with your ID. It’s like a sign saying, “This store belongs to you!” - The Candy Notebook (
mapping(address => uint) public balances
): The balances is like a notebook where you write down how many Candy Coins everyone has. It’s a mapping, which is like a magic list where every person’s blockchain ID (their address) is paired with a number (their coins). For example, “Sally’s ID: 10 coins, Bob’s ID: 5 coins.” Since it’s public, the compiler makes a button that lets anyone check someone’s balance, like saying, “How many coins does Sally have?” and getting “10” back. - The Setup (
constructor
): Theconstructor
is like opening the candy store for the first time. It runs once when you create the contract and saves your blockchain ID as the minter. Themsg.sender
is a special code that says, “Whoever’s setting up this store is the boss!” After that, nobody else can be the boss. - Making New Coins (
function mint
): Themint function
is like a magic candy machine only you can use. You say, mint(Sally, 10), and it gives Sally 10 new Candy Coins. Therequire(msg.sender == minter)
is like a guard checking your ID to make sure you’re the boss. If someone else tries, the guard says, “Nope, only the boss can make coins!” The linebalances[receiver] +=
amount adds the coins to Sally’s notebook entry, like writing “Sally: 10 coins.” - Sharing Coins (
function send
): Thesend function
lets anyone with coins give them to someone else. If Sally has 10 coins and says, send(Bob, 3), the contract checks if she has enough coins withrequire(amount <= balances[msg.sender])
. If she tries to send 15 coins but only has 10, it throws an error calledInsufficientBalance
, like saying, “Sally, you don’t have enough!” If it’s okay, it takes 3 coins from Sally(balances[msg.sender] -= amount)
and gives 3 to Bob(balances[receiver] += amount)
. - Shouting About It (
event Sent
): When coins are sent, the contract shouts, “Sally gave Bob 3 coins!” This is called an event. It’s like writing a note in the blockchain notebook that everyone can read. Apps can listen for this shout to keep track of coin transfers, like a scoreboard showing who gave what to whom. - Safety Check (
Overflow
): The contract is careful not to make mistakes with big numbers. If you try to give someone too many coins (more than a huge number, like 2^256 – 1), the contract says, “Whoa, that’s too much!” and stops. This is called “checked arithmetic” and keeps the candy store safe. - Errors for Clarity (
error InsufficientBalance
): The error line creates a message that explains why something went wrong, like “You asked for 15 coins, but you only have 10!” This helps people understand mistakes, like a friendly note from the store. - Blockchain Explorer Note: When you send Candy Coins, they don’t show up in someone’s regular blockchain wallet, like normal money. They’re stored in the candy store’s notebook (the contract’s data). To see who has coins, you check the store’s address, not Bob or Sally’s personal addresses. The Sent event helps apps build a special scoreboard to track everything.
- Listening to Shouts (
Event Example
): The JavaScript code shows how an app can listen for the Sent shout. It’s like a friend watching the store and saying, “Ooh, Sally sent Bob 3 coins! Now Sally has 7, and Bob has 3!” This helps build a cool app to show all the coin trades.
Real-Life Example
Imagine you run a candy store and make Candy Coins. Only you can make new coins, like giving Sally 10 coins for helping out. Sally can then give 3 coins to Bob for cleaning up. The blockchain notebook keeps track of everyone’s coins, and everyone can see it’s fair. If Sally tries to give away coins she doesn’t have, the store says, “No way!” It’s like a super honest candy game!
Why This Is Awesome
- The Toy Box (SimpleStorage): It’s like a magic box that holds one number, like how many toys you have. Anyone can check or change it, and the blockchain notebook keeps it safe forever. Later, we can add a lock so only you can change the number.
- The Candy Store (Coin): It’s like your own candy money system! You make the coins, and your friends can trade them. The blockchain makes sure nobody cheats, and everyone can see who has what.
Both live on the Ethereum blockchain, so they’re super safe and public. It’s like having a toy box or candy store that nobody can break or steal from!