What Is a Smart Contract and How Does It Work?
- Primary Signal: What Is a Smart Contract and How Does It Work?
- Overview: A smart contract is code on a blockchain that runs itself when its conditions are met. Escrow and DEX swap examples, gas cost math, and why bugs can't be patched....
- Verification: Analyzed and compiled by 1UpTrade Quant editorial monitoring desk.
A smart contract is a self-executing computer program stored on a blockchain. It automatically carries out the terms of an agreement when preset conditions are met, with no bank, broker, or lawyer in the middle. The terms are written directly into code, so anyone can read them before agreeing.
Smart contracts run on if-then logic. If the condition is met, the contract performs the programmed action. If it is not met, the contract does nothing or reverses the transaction. The idea came from computer scientist Nick Szabo in 1994. Ethereum, launched in 2015, made smart contracts practical at scale with the Ethereum Virtual Machine.
How a smart contract works, in four stages. First, agreement: the parties define the terms, for example, Alice receives 100 USDC from Bob if the price of ETH passes $5,000. Second, deployment: the code is compiled and sent to the blockchain as a transaction, and the contract gets its own address. Third, trigger: a user sends a transaction, a time condition passes, or an oracle feeds in outside data. Fourth, execution: the network runs the code, and the result is recorded permanently on the blockchain.
Worked example: an escrow deal with no middleman. A buyer deposits 1 ETH into a smart contract — at an example price of $3,000, that is $3,000 held by code, not by a person. The contract's rule is simple: if the delivery oracle confirms the goods arrived, release the 1 ETH to the seller. If no delivery is confirmed within 7 days, refund the 1 ETH to the buyer. Neither side can change the rule after deployment, and neither side has to trust the other — both trust the code they read before depositing.
Worked example: a token swap on a decentralized exchange, which is itself a smart contract. You swap 1 ETH for USDC at an example price of $3,000. The pool charges a 0.3 percent fee: $3,000 x 0.3 percent = $9.00. The network charges gas for running the contract, say $2.50. You receive $3,000 - $9.00 - $2.50 = $2,988.50 in USDC. The contract checks the price, takes its fee, and settles the swap in one transaction. See What Is a Decentralized Exchange (DEX) and How Does It Work? for the full DEX picture.
Gas is the cost of running a contract. Every interaction costs gas, paid in the network's native token. A simple interaction using 100,000 gas at 20 gwei costs 100,000 x 20 / 1,000,000,000 = 0.002 ETH, which is $6.00 at an example price of $3,000 per ETH. Deploying a contract is heavier: 1,500,000 gas at 20 gwei is 0.03 ETH, or $90.00 at the same example price.
The catch is immutability. Once deployed, a smart contract's code generally cannot be changed. A bug in the code is a bug forever, unless the contract was built with an upgradeable proxy. That is why audits matter, and why you should never send money to a contract whose code you have not checked. Smart contracts power decentralized exchanges, lending protocols, NFT marketplaces, and DAOs — but the code is the contract, and the code is final.
Q: What is a smart contract in simple terms? A: A program on a blockchain that automatically does what it was coded to do when its conditions are met, without any middleman.
Q: What is an example of a smart contract? A: An escrow contract that holds a buyer's payment and releases it to the seller only when delivery is confirmed, or a DEX contract that swaps one token for another at the pool price.
Q: Do smart contracts cost money to use? A: Yes. Every interaction costs gas, paid in the network's native token. A simple interaction can cost a few dollars, and deploying a contract can cost much more.
Q: Can a smart contract be changed after deployment? A: Generally no. Deployed code is immutable unless it was built with an upgradeable proxy, so bugs cannot be patched like normal software.