What is a smart contract? How they work, with examples
A smart contract is a program stored on a blockchain that runs automatically when set conditions are met. Once it is live, nobody can stop it, change its rules or block a transaction it is meant to carry out, which is why smart contracts can move money without a bank or broker in between.
Smart contracts are the engine behind decentralized finance (DeFi), decentralized exchanges, stablecoins and NFTs. If you have ever swapped a token in a wallet app, you have used one.
What is a smart contract, in plain words?
A smart contract is a set of “if this, then that” rules written as code and stored on a blockchain network. When the predefined conditions are met, the contract executes its instructions on its own, and the result is recorded on the blockchain for anyone to check.
The classic comparison is a vending machine. You put in the right amount, choose a snack, and the machine hands it over. Nobody needs to check your money or approve the sale: the rules are built into the machine. Smart contracts work the same way for digital assets. The computer scientist Nick Szabo described the idea in the 1990s, but it only became practical when Ethereum launched in 2015 with a blockchain built to run these computer programs.
Despite the name, a smart contract is not a legal agreement. It is software. It can carry out the terms two parties agree on, but it doesn’t replace legal contracts or dispute resolution in court.
How smart contracts work
Smart contracts work in four steps: they are written, deployed, triggered and executed.
- Written. A developer writes the smart contract code in a programming language, most often Solidity on Ethereum.
- Deployed. The code is stored on a blockchain and gets its own contract address, just like a wallet has an address. From then on it is stored on a blockchain where every node holds a copy.
- Triggered. Someone sends a transaction to the contract address, for example to swap tokens or deposit funds.
- Executed. Every node in the network runs the code in the same way, on Ethereum inside the Ethereum Virtual Machine, and agrees on the result. If the conditions are met, the contract moves the funds. If not, the transaction fails.
Each step costs computing power, so you pay a gas fee to the network for every transaction. During network congestion those fees rise, and a simple swap can cost more than the trade is worth.
Where smart contracts get outside data
A blockchain can’t see the real world by itself. A contract that pays out when the euro reaches a certain asset price needs external data. That data comes from “oracles”, services that feed real world data such as prices or weather into the blockchain. They are also a weak point: if an oracle reports a wrong price, the contract executes on wrong data.
A smart contract example
Say you want to swap €500 worth of ether for a euro stablecoin on a decentralized exchange. You connect your own wallet and confirm the swap. One smart contract then checks your balance, calculates the price from the tokens in its liquidity pool, takes your ether, and sends back the stablecoin, all in one transaction. No company holds your money at any point, and if anything fails, the whole transaction is reversed and only the gas fee is spent.
Other smart contract examples you will find in crypto:
- Tokens. Every ERC-20 token, including most stablecoins, is a smart contract that keeps track of who owns how much.
- Lending. DeFi lending apps hold deposits and release loans automatically when enough collateral is locked.
- Non fungible tokens. NFT contracts record ownership rights to a specific digital item.
- Governance. DAOs use smart contracts to count votes and carry out decisions.
Outside crypto, companies have tested smart contracts for supply chain management, insurance payouts, real estate transactions and global trade documents. These ideas are promising, but most real-world use is still small compared with use in DeFi.
The potential benefits of smart contracts
Smart contracts offer several advantages over traditional contracts when the rules are simple and the assets are digital:
- Automatic. They automatically execute as soon as conditions are met, without paperwork or waiting for a bank.
- Transparent. The code and all blockchain transactions are public, so anyone can see what a contract does.
- Fewer middlemen. Cutting out intermediaries can lower administrative costs and remove single points of failure.
- Hard to tamper with. The immutable nature of the blockchain records means nobody can quietly change a past transaction.
Smart contract development
Smart contract development is a specialised kind of programming, because mistakes are public and usually permanent. On Ethereum and compatible networks, developers mostly write in Solidity; other blockchains use other languages, such as Rust on Solana. Before launch, a careful team tests the code, publishes it so anyone can read it, and pays outside auditors to look for flaws. Many projects also run bug bounties that pay hackers to report problems instead of exploiting them.
For you as a user, that process is something to check rather than something to do. A project with public, audited code, a long track record and many users still carries risk, but it is a much smaller bet than a contract launched last week by an anonymous team.
Risks and smart contract vulnerabilities
The biggest risk of smart contracts is that code does exactly what it says, including its mistakes. Faulty code can’t be fixed by calling customer service, and a stolen balance usually can’t be reversed.
The best-known case is The DAO in 2016, an early Ethereum investment fund run by a smart contract. A bug let an attacker drain a large part of its ether, and the Ethereum community split the network with a hard fork to undo it. Since then, smart contract vulnerabilities have remained one of the main ways DeFi users lose money.
Watch for these risks:
- Bugs. Even audited contracts have been hacked. An audit lowers risk, it doesn’t remove it.
- Admin keys. Many contracts can be upgraded or paused by the team that built them. That helps fix bugs but means you trust that team.
- Unlimited approvals. When you use a DeFi app, your wallet often asks you to approve the contract to spend your tokens. An unlimited approval to a malicious or hacked contract lets it empty that token from your wallet. Approve only the amount you need and revoke old approvals.
- Fake contracts. Scammers copy popular tokens. Check the contract address on the project’s official site before you buy.
Smart contracts and EU rules
In the EU, MiCA regulates companies that provide crypto services, not code. Services provided in a fully decentralized way, without any intermediary, fall outside MiCA, so most DeFi apps have no licensed firm behind them and no regulator to complain to.
That is the practical difference for you. Buying crypto on a MiCA-licensed exchange gives you a supervised company with a complaints process. Using a smart contract from your own wallet gives you more control but no safety net, which is part of the trade-off of self-custody. If you are new to this, start with small amounts and read our page on DeFi first.
The bottom line
A smart contract is code on a blockchain that runs by itself when conditions are met. It makes tokens, DEX swaps and DeFi lending possible without a middleman, but bugs, bad data and sloppy approvals can cost you money that no one can return. Use well-known contracts, approve only what you need, and keep larger amounts away from experimental apps.
Questions people ask
What is a smart contract in simple words?
It is a small program on a blockchain that does exactly what its code says when certain conditions are met, such as sending tokens once a payment arrives. Nobody can stop or change it halfway.
Is Bitcoin a smart contract?
No. Bitcoin has a simple scripting system that supports basic conditions, such as payments that need several signatures, but it was not designed for complex programs. Ethereum and similar networks were built for smart contracts.
Can smart contracts be trusted?
You can trust that a smart contract runs as written, but not that it was written well. Bugs, admin keys and bad price data have caused large losses, so a contract is only as trustworthy as its code and the team behind it.
Are smart contracts legally binding?
Not automatically. A smart contract is code, not a legal contract, although it can be used to carry out an agreement that is legally binding. Whether a court treats it as a contract depends on national law.
How do you write a smart contract?
Developers write smart contracts in a programming language such as Solidity, test them, and then deploy them to a blockchain by paying a gas fee. Large projects usually pay for an independent security audit before launch.