Learn / Beginner · DeFi and Apps
What is a smart contract?
Beginner · DeFi and Apps · about 5 min read
A smart contract is a program stored on a blockchain that runs by itself when its conditions are met.
How it works
- Think of a vending machine. You choose an item, put in the right payment, and the machine hands it over. If you pay too little or choose nothing, you get nothing. A smart contract follows fixed rules in the same way, except the rules are written in code.
- Because the program sits on a public blockchain, anyone can read its rules and see what it has done.
- The idea is older than Ethereum. Computer scientist Nick Szabo coined the term in the 1990s, and Ethereum, launched in 2015, made smart contracts something anyone could use.
- Smart contracts power many things you meet in crypto, including stablecoins, NFTs, and apps that swap one token for another.
Keep in mind
- Smart contracts are immutable by design: once created, the code cannot be changed, so a mistake can be permanent. Some are built so their owners can swap in new code, which means you must trust whoever holds that control.
- Code can have bugs. In 2016 a flaw in a contract called The DAO let an attacker drain millions of dollars’ worth of ether.
- When you approve a transaction, the details can be hard to read, so you may agree to something you do not understand. Only connect your wallet to apps you trust.
- General information, not financial advice.
An example you can picture
Ethereum's guide gives the example of a contract that holds money for a child and releases it after a set date. No bank manager needs to approve it, and no one can stop it because they changed their mind. Another simple case is a sale: the contract receives the buyer's payment, checks that the seller delivered a digital item, and releases the money. If the conditions are not met, nothing moves.
What makes them useful
- Automatic. Once the conditions are met, the contract runs with no middleman.
- Predictable. The same conditions give the same result every time.
- Public. Anyone can see what a contract holds and what it has done.
- Visible rules. Anyone with the skill can read the code before using it.
What they cannot do
A contract follows its code, whether or not the code does what its author meant. It does not judge whether a deal is fair, and it does not fix a mistake. If you send tokens to the wrong place under its rules, they stay there. Because most contracts cannot be changed after they are created, a bug can stay in place for good. This is why reading and auditing code matters, and why an audit is a help and not a guarantee.
Reading what you sign
Ethereum's guide warns that raw transaction data is hard for humans to read, which causes "blind signing", where people approve something they do not understand. Two kinds of request look similar in a wallet but are very different. A transfer sends tokens once. An approval gives a contract permission to spend a token on your behalf, sometimes with no limit, and that permission stays until you cancel it. Before you sign, ask what is being approved, for which token and how much, and to whom.
Common mistakes
- Assuming a contract is safe because it is public. Public does not mean checked.
- Approving unlimited spending when a fixed amount would do.
- Treating "audited" as "safe".
- Signing something you cannot read, because everyone else did.
- Sending tokens to a contract address that was never meant to receive them.
Try it yourself
- Write the rules for a lemonade stand as "if this, then that" lines: if the buyer pays the price, then the cup is handed over. Then add one rule for what happens when the buyer pays too little. This is how a contract is built.
- Look up a well-known stablecoin's contract on a block explorer. Note when it was created and how many transactions it has handled. You do not need a wallet.
- Write down what you would want to check before approving a contract to spend your tokens.
Key terms
Smart contract, code, on-chain, deploy, wallet approval.
Sources and credits
What smart contracts are, their properties and limits: ethereum.org, Intro to smart contracts. Security and approvals: ethereum.org, Stay safe. General information, not financial advice.
Module quiz
3 questions. Tap an answer to see if you are right and why. Get 2 right to complete the module.
1. What is a smart contract?
It is code that carries out its rules automatically.
2. What does the vending machine comparison show?
A smart contract does what its code says, no more and no less.
3. Why should you be careful with smart contracts?
A flaw in the code can stay in place, as The DAO showed in 2016.