Blockchain technology · · 6 min read
What is a smart contract and how does it work?
A smart contract is a program stored on a blockchain that runs automatically according to pre-written rules whenever a transaction calls it. Despite the name, it is not a legal document: the rules are defined in code, every computer on the network executes that code in the same way, and no single person or company can change the result.
The idea was proposed by computer scientist Nick Szabo in the 1990s. His often-quoted analogy is the vending machine: insert the right amount of money, press the button and the product drops out. No shopkeeper is involved, because the rule is built into the machine. Putting the idea into practice at scale became possible with Ethereum, which went live in 2015 and was designed to run general-purpose programs.
How does a smart contract work?
The life cycle of a smart contract can be broken into four steps:
- Writing: A developer writes the rules in a programming language. On Ethereum, the most common one is Solidity. The code is then compiled into bytecode that the network understands.
- Deploying: The code is sent to the blockchain in a transaction. From that moment the contract has its own address and, just like a wallet, it can hold a balance.
- Calling: Users send transactions to the contract's address to run its functions, such as "deposit", "swap" or "withdraw".
- Verification and recording: Nodes on the network run the same code with the same inputs and reach the same result. That result is written into a block, and the contract's state (for example, who holds what balance) is updated.
This design has a few important consequences. A contract never acts on its own; every step is triggered by a transaction. Each computational step costs a fee, which on Ethereum is called gas, and this also stops infinite loops from clogging the network. A transaction either goes through completely or is treated as if it never happened: if a condition is not met, every change is rolled back, and only the fee that was spent is not returned.
A hypothetical example: crowdfunding
Imagine a community that wants to raise 100 ETH for a project within 30 days. The rules are written into a contract:
- Anyone can send ETH to the contract, and it records who sent how much.
- When the period ends, if the total is 100 ETH or more, only the project owner's address can withdraw the funds.
- If the total is below 100 ETH, every participant can withdraw their own contribution.
Suppose 82 ETH has been raised after 30 days. If the project owner tries to withdraw, the contract rejects the transaction. When a participant who sent 2 ETH asks for a refund, the contract checks its records and returns the 2 ETH. Nobody has to trust an intermediary, wait for approval or file a complaint; the outcome depends on rules everyone could see from the start.
How it differs from a traditional contract
| Feature | Traditional contract | Smart contract |
|---|---|---|
| Enforced by | The parties, intermediaries, a court if needed | The blockchain network |
| Language of the rules | Legal language, open to interpretation | Precise code that can still contain bugs |
| Changes | By agreement between the parties | Code is fixed, or depends on whoever holds upgrade rights |
| When something goes wrong | Disputes and interpretation are possible | Whatever the code says is what happens |
| Visibility | Usually private | Code and transactions are public |
| Access | Limited by legal jurisdiction | Anyone with internet access |
The most important row is "when something goes wrong". In a traditional contract, a clearly unfair outcome can be corrected by a judge. With a smart contract, if the code contains a flaw, a transaction that exploits it still counts as "following the rules". The DAO incident in 2016 is one of the best-known examples of this in crypto history.
Where are smart contracts used?
Smart contracts are the foundation of many familiar applications in crypto:
- Tokens: A token is often just a contract that keeps track of who holds what balance. Ethereum's ERC-20 standard is a shared set of rules for interchangeable (fungible) tokens.
- Decentralized exchanges: A contract, rather than a company, matches buyers and sellers.
- Lending protocols: Depositing collateral, borrowing and liquidating when collateral falls short are all handled by code.
- NFTs: Ownership of unique digital items is recorded in a contract.
- DAOs: A community's voting and treasury rules are tied to contracts.
Because contracts can call one another, one application can be built on top of another. This property is called composability. It speeds up innovation, but it also means that a problem in one piece can spread to others.
Limits and things to watch
Smart contracts do not remove the need for trust; they move it. Instead of trusting an intermediary, you trust that the code is correct and that the people who control it act in good faith.
- Bugs are permanent. A flaw in a deployed contract can be exploited by anyone until it is fixed.
- It cannot see the outside world. A contract cannot know a match score or an asset's price on an exchange by itself; data providers called "oracles" bring that information in. If the data is wrong, the result is wrong.
- There may be admin keys. Some contracts are designed to be "upgradeable". In that case a key or a group has the power to change the code, and users are effectively trusting them too.
- Transactions are irreversible. There is no customer service for funds sent to the wrong address or a function called by mistake.
- Everything is public. Balances and transactions are open to anyone, which provides transparency but not privacy.
Before using an application, a few questions help: Has the contract's source code been published and verified? Who can change the code, and under what conditions? Has it been independently audited? How long has it been running, and how much value does it hold? None of these answers guarantees safety on its own, but together they show you where the risk sits.
Key takeaways
- A smart contract is a program on a blockchain that automatically applies pre-written rules when a transaction calls it.
- Because every node runs the code the same way, no single party controls the outcome, and a transaction either completes fully or not at all.
- Tokens, decentralized exchanges, lending protocols, NFTs and DAOs are all built on smart contracts.
- Code bugs, reliance on outside data and admin keys are the main risks: smart contracts do not eliminate trust, they shift it to the code and to those who control it.