Education only. Not financial advice.
Education only. Not financial advice.
Watch the video
Show transcript
Welcome to Lesson C2. Before we start, this lesson is education only. It is not financial advice. Today you'll write your first smart contract, have Grok explain it, and run it on a test chain inside your browser. Nothing to install, and only play money.
So what's a smart contract? Ethereum dot org says, quote, "A 'smart contract' is simply a program that runs on the Ethereum blockchain." End quote. One language for writing them is called Solidity. Its docs say, quote, "Solidity is an object-oriented, high-level language for implementing smart contracts." End quote.
Let's open Remix in the browser. In the File Explorer, make a new file called Counter dot sol, and paste in the counter contract from the lesson page. It stores one number. One button adds one. Another subtracts one.
Now let's have Grok explain it. Paste the prompt from the lesson page, then the contract. We ask for a line by line explanation in plain words, what uint256 means, what require does, and what happens if we subtract at zero. Look for this: count stores a number, increment adds one, and decrement refuses to go below zero.
Back in Remix. Click the Solidity compiler icon, and press the Compile button. A green check means it compiled.
Now click the Deploy & Run icon. Under Environment, pick Remix VM. The Remix docs say, quote, "The Remix VM is a sandbox blockchain in the browser. Transactions do not require an approval to run." End quote. Press Deploy.
Under Deployed Contracts, open Counter. Press increment two times, then count. Two. Press decrement once. Count shows one. Now press decrement two more times. The second one fails, with the message Count is already zero. That's the require line doing its job.
One more thing. Don't write security code from scratch. OpenZeppelin calls its library, quote, "A library for secure smart contract development." End quote. It says, quote, "Build on a solid foundation of community-vetted code." End quote. Our tip: when you're ready for real projects, start from well-known building blocks like these.
Safety check. Ethereum dot org describes smart contracts as, quote, "running immutable logic based on code deployed on the blockchain." End quote. Immutable means it can't be changed. So a bug is hard to fix after launch. Our tip: keep your contracts on test networks or in the Remix VM while you learn.
Quick check. What's a smart contract? A program that runs on a blockchain. Was the ETH in the Remix VM real? No, it's play money. Why did the last decrement fail? The require line stops it below zero. Education only. Not financial advice. Next, in Lesson C3, you'll hunt for a bug with Grok.
What you'll learn
- What a smart contract and Solidity are
- How to have Grok explain a contract line by line
- How to deploy and use a contract on Remix's in-browser test chain
Step 1: What is a smart contract?
Ethereum.org says: "A 'smart contract' is simply a program that runs on the Ethereum blockchain." (Ethereum.org smart contracts)
One language for writing them is Solidity. Its docs say: "Solidity is an object-oriented, high-level language for implementing smart contracts." (Solidity docs)
Step 2: Paste the counter contract into Remix
- Open Remix in your browser.
- In the File Explorer, create a new file named
Counter.sol. - Paste this in:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
// A tiny practice contract. Test networks or the Remix VM only.
contract Counter {
uint256 public count; // the number we store
function increment() public { // add 1
count += 1;
}
function decrement() public { // subtract 1
require(count > 0, "Count is already zero");
count -= 1;
}
}
Step 3: Ask Grok to explain it
Paste this into Grok, followed by the contract:
I'm a beginner. Explain this Solidity contract line by line in plain words.
What does "uint256" mean? What does "require" do? What happens if I call
decrement when count is 0? Keep it short. Here is the contract:
Look for: count stores a number, increment adds 1, and decrement refuses to go below zero with the message "Count is already zero".
Grok's real answer (our test run)
We ran this exact prompt through Grok (model grok-4.7) on Oct 30, 2026. Grok's answers change from run to run, so yours will differ. Quoted as Grok returned it, except where we mark [trimmed] and where we swapped dashes for commas to match our style.
This is a tiny counter. Anyone can add 1 or subtract 1, but it will not go below 0.
- The license line is only a comment. It does not change how the code runs.
pragma solidity ^0.8.20means “compile this with Solidity 0.8.20 or a newer 0.8 version.”contract Counter { ... }is the contract: the program stored on the chain.uint256 public countis the stored number.uint256means a whole number from 0 up to a very large limit (no negatives).publicmakes a free reader so anyone can checkcount. It starts at 0.incrementadds 1 tocount.publicmeans anyone can call it.decrementsubtracts 1, but only after the check.require(count > 0, "Count is already zero")means “continue only ifcountis greater than 0.” If not, the call fails and that message is returned.If you call
decrementwhencountis 0, therequirefails. The transaction reverts: nothing is saved,countstays 0, and you see"Count is already zero". In 0.8, subtracting from 0 would revert anyway; this check makes the reason clear.
Education only. Not financial advice.
Step 4: Compile and deploy on the test chain
- Click the Solidity compiler icon and press the Compile button.
- Click the Deploy & Run icon. Under Environment, pick Remix VM.
- Press Deploy.
The Remix docs say: "The Remix VM is a sandbox blockchain in the browser. Transactions do not require an approval to run." (Remix docs: Deploy & Run) Everything here is play money.
Step 5: Click the buttons
Under Deployed Contracts, open Counter. 1. Press increment two times, then press count. You should see 2. 2. Press decrement one time, then count. You should see 1. 3. Press decrement two more times. The second one fails with "Count is already zero".
We tested this exact contract on a local test chain on our own computer: it compiled, the count read 1 after two increments and one decrement, and calling decrement at zero was refused.
Step 6: Don't write security code from scratch
OpenZeppelin Contracts calls itself "A library for secure smart contract development." and says: "Build on a solid foundation of community-vetted code." (OpenZeppelin Contracts docs) Our tip: when you're ready for real projects, start from well-known building blocks like these instead of inventing your own.
Want more practice? CryptoZombies teaches Solidity with a game.
Check yourself
- What is a smart contract? (Answer: a program that runs on a blockchain)
- Was the ETH in the Remix VM real? (Answer: No. It's play money in your browser.)
- Why did the last decrement fail? (Answer: the
requireline stops count from going below zero)
Caution box Ethereum.org describes smart contracts as "running immutable logic based on code deployed on the blockchain." (Ethereum.org smart contract security) Immutable means it can't be changed, so a bug is hard to fix after launch. Our tip: keep your contracts on test networks or in the Remix VM while you learn. Education only. Not financial advice.
Builds on
- Lesson A3: Build with Grok as your coding partner
- Lesson C1: Blockchain basics: wallets and seed phrases
Sources
- Ethereum.org smart contracts: https://ethereum.org/en/developers/docs/smart-contracts/
- Ethereum.org smart contract security: https://ethereum.org/en/developers/docs/smart-contracts/security/
- Solidity docs: https://docs.soliditylang.org/
- Remix docs, Deploy & Run: https://remix-ide.readthedocs.io/en/latest/run.html
- OpenZeppelin Contracts docs: https://docs.openzeppelin.com/contracts/5.x/
- CryptoZombies: https://cryptozombies.io/
Creator credits
- xAI (Grok): X https://x.com/grok , https://x.com/spacexsi
- Ethereum.org: X https://x.com/ethdotorg
- Solidity: X https://x.com/solidity_lang
- Remix: X https://x.com/EthereumRemix , YouTube https://www.youtube.com/@EthereumRemix
- OpenZeppelin: X https://x.com/OpenZeppelin
- CryptoZombies: X https://x.com/CryptoZombiesHQ
Education only. Not financial advice.