By the end you will have read a complete, real smart contract, every line, pragma to closing brace. Not a toy written for a lesson: WETH9, live on Ethereum, touched by nearly every protocol you have heard of.
Here is a real contract. Not an example written for a lesson: this is WETH9, live on Ethereum, and nearly every DeFi protocol you have heard of touches it. Sixty two lines, complete, on the board right now. By the end of this lesson you will have read all of them.
Its job fits in one sentence. ETH is not a token, and most of DeFi only speaks token. This contract takes your ETH, holds it, and gives you a token that stands for it. Put ETH in, take ETH out. That is the whole idea.
Line 1 is a note to the compiler. Solidity has versions, and this line names which ones the text was written for: at least 0.4.22, below 0.6. Today's Solidity is 0.8. You are reading an older dialect, and that is not a mistake. It is most of what is deployed.
Something else about this file is worth knowing. Above this line sit fifteen lines of licence comment, and after the closing brace another six hundred and seventy nine. The contract is seven percent of the file it lives in. Real code arrives wrapped in paperwork.
Line 3 opens the box. Function first: a program that lives at its own address and follows its own text, nothing more and nothing less. The name Solidity gives that is a contract. Everything down to the closing brace on line 62 is inside it.
Three values follow and they are the contract's memory: variables written into the chain that survive between calls. The word public means anyone may read them. Eighteen decimals is a convention rather than a rule: it is where a wallet puts the point when it shows you a balance.
Four announcements. Function first: a record written into the transaction's log, kept by the chain, which the contract itself can never read back. The name: an event. Approval, Transfer, Deposit, Withdrawal.
The word indexed marks a field the logs can be searched by. Before reading on, settle one thing. If the contract cannot read its own events back, who are they for?