By the end you will have read a complete, real smart contract, every line, pragma to closing brace, and understood exactly what each one does. Not a toy diagram of a contract. The contract.
You have heard that smart contracts move real money, and you have probably nodded along without ever seeing one. Here is one. Twenty four lines, complete, on the board right now. There is no hidden file and no part two. By the end of this lesson you will have read all of it.
Think of it as a piggy bank with rules instead of a lock. A glass pig, bolted to a public square, its rules engraved on the front. Anyone can read the engraving; the pig itself enforces it. Reading the engraving is this lesson.
Start at the very top. The first line is a note to humans: it names the license this text is published under. The machine ignores it entirely. It begins with two slashes, and everything after two slashes is a comment: words for readers, never instructions.
The second line is a note to the compiler. Solidity has dialects, and this line names which one the text is written in: version 0.8.20 or compatible. The keyword is pragma. Neither line does anything on chain. The paperwork is done.
Line 4 opens the box. Function first: a program that lives at its own address on the chain and follows its own text, nothing more and nothing less. The name Solidity gives that: a contract. Ours is called PiggyBank.
The opening brace starts a box with two kinds of contents: things the pig remembers, and things the pig can do. Everything until the closing brace on line 24 lives inside. The engraving on the glass starts here.
Two things the pig remembers. An address is the type for an account's identity: a wallet, or another contract. A uint256 is the type for a whole number. So the pig remembers a who and a how much, and names them owner and goal.
Variables that live in the contract's permanent memory have a name: state variables. And the word public means anyone may read them. That is all it means. It grants no one the right to change them. Glass, remember: everyone sees in, nobody reaches in.
This block is the birth certificate. Function first: code that runs exactly once, the moment the contract is deployed, and then never again. The name: the constructor. Ana deploys the pig and passes a goal along with it.
Line 9 stores a special value: always, in any function, whoever is calling right now. Its name is msg.sender. Line 10 stores the goal Ana passed in. Now, before the reveal: Ana deploys with a goal of 5 ETH. Who is owner?