Understanding Smart Contracts
Lesson by Uvin Vindula
What is a Smart Contract?
A smart contract is a computer program stored on a blockchain that automatically executes when predetermined conditions are met. Think of it as a digital vending machine: you put in the right input (money + selection), and you automatically get the right output (snack). No human intermediary needed.
The term was coined by computer scientist Nick Szabo in 1994, but smart contracts became practical with the launch of Ethereum in 2015. Ethereum was specifically designed to support smart contracts, which is why it's the foundation of most DeFi applications.
How Smart Contracts Work
A smart contract follows a simple logic: "If this, then that."
Here's a real-world analogy. Imagine you and a friend bet on a cricket match — Sri Lanka vs. India. Normally, you'd need to trust each other or have a third party hold the money. With a smart contract:
- Both you and your friend send the bet amount to the smart contract
- The smart contract locks the funds and waits for the match result
- An oracle (a trusted data feed) provides the match result to the contract
- The smart contract automatically sends all funds to the winner
- Neither party can cheat, and no third party is needed
Smart Contracts Power DeFi
Every DeFi application you've learned about runs on smart contracts:
- DEXes (Uniswap): Smart contracts manage liquidity pools and execute token swaps using the AMM formula
- Lending (Aave): Smart contracts accept deposits, issue loans, calculate interest, and trigger liquidations automatically
- Stablecoins (DAI): Smart contracts lock collateral and mint/burn stablecoins based on collateral ratios
- Yield vaults (Beefy): Smart contracts auto-compound your farming rewards at optimal intervals
Key Properties of Smart Contracts
| Property | Description |
|---|---|
| Immutable | Once deployed, the code cannot be changed (unless specifically designed with upgrade mechanisms) |
| Deterministic | Given the same inputs, the contract always produces the same outputs |
| Trustless | You don't need to trust anyone — the code is the law |
| Transparent | Anyone can read the contract's code on the blockchain |
| Irreversible | Once a transaction is executed, it cannot be undone |
The Risk of Smart Contract Bugs
Here's the critical caveat: "Code is law" means that if the code has a bug, the bug is also the law. Smart contract vulnerabilities have led to some of the biggest losses in crypto history:
- The DAO Hack (2016): A bug in a smart contract on Ethereum allowed a hacker to drain $60 million. This was so severe that Ethereum hard-forked (split) to reverse the hack, creating Ethereum (ETH) and Ethereum Classic (ETC).
- Wormhole Bridge Hack (2022): A vulnerability allowed hackers to steal $320 million from the Wormhole cross-chain bridge.
- Ronin Bridge Hack (2022): $625 million was stolen from the bridge connecting Axie Infinity to Ethereum.
How to Evaluate Smart Contract Safety
Before interacting with any DeFi protocol, check:
- Has the contract been audited? Look for audits from reputable firms like CertiK, Trail of Bits, or OpenZeppelin. Note: an audit does not guarantee safety — it only reduces risk.
- Is the code open source? Can you view the code on a block explorer like Etherscan?
- Is there a bug bounty program? Legitimate protocols reward white-hat hackers for finding vulnerabilities.
- How long has the contract been live? Contracts that have held billions for years are generally safer than brand-new ones.
- Are there admin keys? Can the team upgrade or change the contract? Upgradeable contracts introduce additional trust requirements.
Sri Lankan Context
Understanding smart contracts is essential because every DeFi interaction involves approving a smart contract to access your tokens. When you click "Approve" in MetaMask, you are giving a smart contract permission to move your tokens. Many Sri Lankan users have lost funds by approving malicious contracts from phishing links shared in Telegram and WhatsApp groups. Always verify the contract address and the protocol's official website before approving anything.
⚠️ Disclaimer: Smart contracts carry inherent risks including code vulnerabilities, hacks, and exploits. Even audited contracts can be compromised. Never approve unlimited token spending, and always use official protocol websites. IAMUVIN and uvin.lk do not endorse any specific smart contract or DeFi protocol.
Key Takeaways
- •Smart contracts are self-executing programs on the blockchain that automatically enforce agreements when conditions are met
- •They power all of DeFi — from DEXes and lending protocols to stablecoins and yield vaults
- •Smart contracts are immutable, transparent, and trustless, but bugs in code can lead to catastrophic losses
- •Always check for security audits, open-source code, bug bounties, and contract age before interacting with a DeFi protocol
- •When approving a smart contract in your wallet, you are granting it permission to move your tokens — only approve contracts from verified sources
Quick Quiz
Question 1 of 3
0 correct so far
What is a smart contract?