What Is a Smart Contract? How They Work
A smart contract is a program stored on a blockchain that runs exactly as written. How deployment, gas and execution work, with a Solidity example and limits.
Reviewed by Maurice Sobiera
A smart contract is a program stored on a blockchain that runs exactly as written when someone sends it a transaction. It holds code and data at a fixed address, and every node executes it the same way. On Ethereum, a contract cannot be deleted by default, and interactions with it are irreversible.
What is a smart contract?
A smart contract is a collection of code (its functions) and data (its state) that lives at a specific address on a blockchain. The Ethereum documentation describes it as a type of account: it has a balance and can be the target of transactions, but no person controls it. Users interact with it by sending a transaction that calls one of its functions.
The term is older than blockchains. Nick Szabo described it in 1994 as a computerized transaction protocol that executes the terms of a contract. He used a vending machine as the example: the right input guarantees a certain output, and no clerk is needed. Blockchains made it possible to run such programs without a trusted operator.
Despite the name, a smart contract is not a legal contract. It is software, and whether it has legal force depends on the jurisdiction and on any separate agreement between the parties.
How does a smart contract work?
A transaction calls a function, every node executes the code, and the resulting state change is written to the chain. The Solidity documentation defines a transaction as a message from one account to another that can carry data and Ether. If the target account contains code, that code runs with the data as input. If the transaction has no recipient, it creates a new contract.
Here is the smallest useful example, taken from the Solidity documentation. It stores one number that anyone can change and read.
pragma solidity >=0.4.16 <0.9.0;
contract SimpleStorage { uint storedData;
function set(uint x) public { storedData = x; }
function get() public view returns (uint) { return storedData; }}Calling set(42) is a transaction: it costs gas, and it changes the stored value after a block includes it. Calling get() only reads state, so it needs no transaction and costs nothing. A token such as USDT works the same way. Its contract keeps a table of balances, and transfer changes two entries in that table.
How do you deploy a smart contract?
You compile the source code to bytecode and send it in a transaction without a recipient. Developers write Ethereum contracts mostly in Solidity or Vyper. The network address of the new contract is derived from the deployer’s address and nonce. Deployment is a transaction, so it costs gas, and the Ethereum documentation notes that deployment costs are far higher than those of a simple ETH transfer.
After deployment the code is public and cannot be edited. To change behaviour, teams either deploy a new contract and move users over, or use a proxy pattern decided in advance. Any bug that ships stays in the code. This is why the Solidity security considerations and independent audits matter before launch.
What can smart contracts do?
They can hold and move funds, keep records and call other contracts. Typical uses are:
- Fungible tokens such as ERC20 and TRC20, which track balances per address
- Non-fungible tokens such as ERC721, which track ownership of unique IDs
- Escrow, where funds are released when a condition in the contract is met
- Decentralised exchanges that swap one token for another at a price calculated in code
- Multi-signature wallets that require several approvals for a payment
Because contracts are public and composable, one contract can call another. A decentralised exchange can use any token contract that follows the standard.
What are the limits of smart contracts?
Contracts have limited capacity, charge per operation and are isolated from the outside world. Each step costs gas, and block space is limited, so heavy computation and large data do not belong on-chain. A contract also cannot fetch a web page or an exchange rate by itself. The Ethereum documentation explains that this takes an oracle: an off-chain component that writes the data into the chain for contracts to read. Finally, a contract is only as correct as its code. It enforces what was written, not what the parties intended.
Older descriptions of smart contracts claim that only the involved parties can see the results. That is wrong for public chains such as Ethereum or Tron: code, state and transactions are readable by anyone, although addresses are not tied to names.
How does this relate to Chaingateway?
Chaingateway does not deploy contracts for you. It lets your application interact with the ones that exist. You can send ERC20, TRC20, BEP20 and NFT transfers through REST endpoints and register a webhook that reports when a token contract moves funds to or from an address you watch. Every transfer is still a normal transaction that calls the token contract’s transfer function. The ERC20 article shows the interface and a request example.
Ready to build this yourself? Get your API key — 7-day trial, no card required — or see the Blockchain API for the full endpoint reference.