Crypto basics

Smart contract

Also called: contract · on-chain contract

Definition

A smart contract is a program stored on a blockchain that runs automatically when its conditions are met, letting people trade, lend or issue tokens without a middleman.

How smart contract works

A smart contract holds code and often holds funds. When you interact with it, for example by swapping tokens on a decentralized exchange, every node runs the same code and records the result. Nobody can quietly change the outcome, and the contract keeps working as long as the blockchain runs.

Ethereum made smart contracts popular, and today most major chains support them. Stablecoins, ERC20 tokens, lending protocols, NFT collections and liquid staking all run on smart contracts. Each interaction costs a gas fee paid to the network.

"Code is law" cuts both ways. If a contract has a bug, attackers can exploit it and funds may be lost with no bank to reverse it. Some contracts are upgradeable, which means an admin key can change the code. Audits reduce risk but do not remove it. Before you approve a contract to spend your tokens, make sure it is the official address.

Example

Suppose a lending contract lets you deposit 1 ETH as collateral and borrow up to 70 percent of its value in USDC. With ETH at 2,000 USDC, you can borrow up to 1,400 USDC. You borrow 1,000 USDC. If ETH falls to 1,500 USDC, your collateral is worth 1,500 and your loan is 1,000, about 67 percent of the collateral. If the protocol's liquidation threshold is 75 percent and ETH drops further to about 1,333 USDC, the contract can automatically sell part of your ETH to repay the loan, with no human approval needed.

Common mistakes

  • Approving unlimited token spending for a contract you found through a random link or airdrop.
  • Assuming an audited contract cannot be hacked.
  • Interacting with a copycat contract that has the same token name but a different address.

Learn and practise

FAQ

Are smart contracts legally binding?

Not by themselves. They are code that executes automatically. Whether they create legal obligations depends on the situation and the country.

Can a smart contract be changed?

Some are immutable once deployed. Others use an upgradeable design that lets an admin or governance vote replace the code, which you can usually check in the project documentation or block explorer.

Which blockchains support smart contracts?

Ethereum and its layer 2 networks, BNB Chain, Solana, Avalanche, Tron and many others support smart contracts. Bitcoin's scripting is far more limited.