[EVM: Zero to Hero] Examining Smart Contracts
Explaining what powers the worlds most valuable blockchains
Welcome to Article 4 of EVM: Zero to Hero. This series helps you learn about technical web3 concepts in a simple and fun way.
In case you missed it, here’s what we covered in the last article:
How smart contract wallets work
The pros and cons of smart contract wallets
A peek into the future of the EVM with Account Abstraction
Smart contract wallets are one specific application of smart contracts. Today, we’ll zoom out and analyze smart contracts more generally. As a reminder from the first article:
Programs called smart contracts run on blockchains. Smart contracts are important because they provide a layer of customizability to the blockchain.
We’ve previously described smart contracts as “programs,” but we can do better. Let’s talk about the properties that make smart contracts well-suited to power many aspects of tomorrow’s economy.
Key Properties
Smart contracts have two notable properties:
Transparent. The blockchain’s data is open to everyone to browse and process. All money movement in and out of smart contracts can be tracked.
Decentralized. By virtue of being on the blockchain, smart contracts are maintained by a decentralized network. No single entity can arbitrarily change a smart contract’s state.
These properties stand in contrast to many of the current products we use today. A common complaint with traditional financial institutions is that it’s impossible to see what’s going on under the hood.
A bank run starts because customers lose trust in their bank’s solvency. Bank runs can’t happen with cryptocurrencies because smart contract data is transparent: anyone can view the balance sheet of a particular protocol or product at any time.
Another common issue with many of the products we use today is that they are controlled by a single entity. Millions of people that use a product like Twitter are subject to the whims of a few decision makers – the Twitter files anyone?
On the other hand, once a smart contract is deployed, it’s (mostly) unchangeable. There are limited ways for creators of web3 products to retroactively make changes that negatively affect their users.
🔎 Closer look 🔎
Of course, this implies that it’s also hard to make changes that positively affect users. Most products settle on some middle ground, where certain aspects of a smart contract can be updated while other parts cannot.
The exciting thing about smart contracts is that users can easily tell which parts can be changed and which cannot. Users can now make more informed decisions about which products they choose to spend their time on, which is a huge win!
A simple mental model for smart contracts can be derived from Google’s now infamous motto “Don’t be evil.” Smart contracts, and more generally the blockchain, take this motto one step further: “Can’t be evil.”
The Anatomy
Now that we understand why smart contracts are useful, let’s get our hands dirty. A smart contract has four key components:
Address: the unique identifier where the smart contract can be found on the blockchain
Balance: how much currency the smart contract holds
Storage: the current state of the smart contract, such as a record of who won each round in a game
Code: the instructions that tell the smart contract how to handle incoming transactions
To make this more concrete, here is a condensed version of a smart contract we’ve been working on that plays TicTacToe. It’s written in Solidity, which is the most common programming language for writing smart contracts on EVM compatible blockchains.
contract TicTacToe {
// games and _nextGameId will be storage
mapping(uint256 => Game) public games;
uint256 internal _nextGameId;
function newGame(address playerX, address playerO) public {
games[_nextGameId].id = _nextGameId;
games[_nextGameId].playerX = playerX;
games[_nextGameId].playerO = playerO;
_nextGameId = _nextGameId + 1;
}
// …more functions
}
In this example, you can see that the games
and _nextGameId
variables are examples of what would be used as storage.
The code itself is the logic that plays the game, and is mainly composed of functions. Functions are highly customizable parts of a smart contract, and can really do anything you dream of in code. The function in the example, newGame
, creates a new TicTacToe game!
We’ve deployed this contract on the Goerli blockchain at address 0xa274b39B7c5c7d0f831AE3732F4b07AfE1E4b74C. Feel free to check it out! (and notice that the balance of the smart contract is zero 😢).
Using dApps
There are two steps to launching a web3 product:
Deploy a smart contract onto a blockchain
Create a Decentralized App (dApp), a website that talks directly to the smart contract
If you’re familiar with traditional web applications, you can think of a smart contract as the backend and a dApp as the frontend. In fact, dApps are created with the same tools (React, Javascript, HTML, etc.) as other frontends, they just happen to talk directly with a blockchain!
🚨 Warning 🚨
It’s important to only use smart contracts from reputable sources. One key indicator of a trustworthy smart contract is if it’s been audited by a neutral third party. dApps will often clearly display the fact that their smart contract has been audited (rightly so)!
For example, Aave is a popular dApp that helps you lend crypto to others. Search for the word “audit” on their landing page!
In short, dApps are the user-friendly gateway to the blockchain.
Recap
Enjoyed this article? The next in this series is article is out now!
In today’s article, we covered:
The key properties of a smart contract
The anatomy of smart contracts
How people access smart contracts through dApps
As always, feel free to reach out on Twitter or via email at shekar@ramaswamy.org with any questions or comments.
Next up, we’re covering the mechanism that enables you to use smart contracts: transactions. Stay tuned!
Further Reading
Investopedia’s article on smart contracts
The most popular smart contract development course (it’s free)
The tutorial we used to create a TicTacToe smart contract