Your First 90 DaysAcademy
First Contact · What a Contract Even Is · Week 1 · Checkpoint 1

Your First Contract

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.

13 steps~28 min3 nodes for your map
01 · The whole machine

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.

02 · The paperwork and the dialect

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.

03 · The box and what it calls itself

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.

04 · The receipts

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?

A contract cannot read its own logs. So who are these four events written for?
your balance2,400
BANK_DBowner: the bank
you2,400
what the app is actually showing you
BANK_DBowner: the bank ✍
you2,400their pen
you hold a claim. they hold the pen.
your digital life
BANK · you2,400the bank ✍
INSTAGRAM · you2.1M followersMeta ✍
STEAM · you134 gamesValve ✍
AIRLINE · you58,200 milesthe airline ✍
four tables. zero pens that are yours.
BANK_DBowner: the bank ✍
you2,400
DENIED ✗
try both pens
PLATFORM_DBowner: the platform ✍
her · 8 years2,000,000 followers
one automated decision away
BANK_DB · you · 2,400intentcompetencecontinuity
your row stands on all three
FTX_DBowner: FTX ✍
you5 BTC
the backing vault●●●●●
the row stayed. the backing did not.
CARD_DBowner: your bank ✍
TV you never bought−1,100
fraud reversal+1,100 ✓
someone holds the pen, so someone can fix it
?_DBowner: nobody
youstill yours?
?
can a table exist that nobody owns?
?
?_DBowner: ̶n̶o̶b̶o̶d̶y̶
you100
no owner, no pen, no trust?
keeper 1
you100
keeper 2
you100
keeper 3
you100
keeper 4
you100
keeper 5
you100
no THE copy, only copies.
keeper 2
you100
keeper 3
you100
keeper 4
you100
keeper 5
you100
your copy
you100
five copies. one of them is yours.
one attacker
one attacker, ten thousand faces.
real machinesburned wattsnext page, sealed
writing costs watts. faking voters buys nothing.
cost paid OUTSIDE: hardware and power
proof of work, burn energy to vote.
page 1you · 100page 2you · 100page 3you · 100page 4you · 100
rewrite one line, break every lock after it.
office lunchtrusted keeperconsensusfive keepers, real cost
the price buys trustlessness. the office already has trust.
?
ownerless ledger
you?
a key, not a login?
nobody owns the table. so who owns your row?
contractstatefunctions

Three new nodes on your map

contract · state · functions · +10 Lynx