Smart Contracts
What smart contracts are, how they work, writing in Solidity, the importance of audits, and how self-executing code powers DeFi, NFTs, and DAOs.
The basics
What Is a Smart Contract?
A smart contract is a program stored on a blockchain that automatically executes when specific conditions are met. Think of it like a vending machine: you insert money (input), the machine verifies the amount (condition), and dispenses your item (execution) — all without a human operator. Smart contracts work the same way but for financial agreements, token swaps, NFT mints, voting systems, and much more.
Once deployed to a blockchain, a smart contract's code is public and immutable — anyone can read it, and nobody (not even the creator) can change it. This transparency and predictability are what make smart contracts trustworthy. Instead of trusting a company to honor an agreement, you trust code that executes exactly as written.
What Can Smart Contracts Do?
- Automatically swap one token for another on a DEX when you click "Swap"
- Issue a loan when collateral is deposited, and liquidate it if the collateral value drops too low
- Mint a new NFT and record its ownership on-chain
- Distribute governance votes based on token holdings
- Release payment from escrow when both parties confirm delivery
Where Do Smart Contracts Run?
Smart contracts run on blockchains that support them — most importantly Ethereum, but also Solana, Avalanche, Polygon, Arbitrum, Base, and many others. Bitcoin's scripting language is intentionally limited, which is why most smart contract development happens on Ethereum and its ecosystem.
Key Takeaways
- Smart contracts are self-executing programs stored on a blockchain
- They run automatically when conditions are met — no human intervention needed
- Their code is public and immutable, providing transparency and trust
- Smart contracts power DeFi, NFTs, DAOs, and almost everything in modern crypto
Going deeper
Solidity and Smart Contract Development
Solidity is the most widely used programming language for writing smart contracts on Ethereum and all EVM-compatible chains (Polygon, Arbitrum, Base, BSC, Avalanche C-Chain). Its syntax is similar to JavaScript, making it accessible to web developers. Other smart contract languages include Rust (used by Solana and NEAR), Move (used by Aptos and Sui), and Vyper (an alternative to Solidity designed for simplicity and auditability).
A typical smart contract development workflow involves writing the contract in Solidity, compiling it to EVM bytecode, testing it against a local blockchain fork (using tools like Hardhat or Foundry), deploying it to a testnet for further validation, and finally deploying to mainnet. Popular development frameworks include Hardhat (JavaScript-based, with Solidity tests in Hardhat 3), Foundry (Rust-based, faster testing), and Remix (browser-based IDE for beginners).
How Smart Contracts Execute
- A user submits a transaction that calls a specific function on a smart contract
- The transaction is included in a block by a validator
- Every node on the network executes the contract code in the EVM (Ethereum Virtual Machine)
- The state changes (balances, storage variables) are recorded on-chain
- The user pays gas fees proportional to the computational complexity of the execution
The Importance of Audits
Because smart contracts are immutable, bugs cannot be patched after deployment. A single vulnerability can lead to millions in losses. Professional security audits by firms like Trail of Bits, OpenZeppelin, Cantina (where Spearbit now lives), and Cyfrin are essential before any mainnet deployment. Even audited contracts are not guaranteed to be bug-free, which is why bug bounties and formal verification are additional layers of protection.
Common Smart Contract Standards
- ERC-20 — the standard for fungible tokens (USDC, UNI, LINK)
- ERC-721 — the standard for non-fungible tokens (NFTs)
- ERC-1155 — a multi-token standard supporting both fungible and non-fungible tokens
- ERC-4337 — account abstraction, enabling smart contract wallets with custom logic
- ERC-4626 — a standard for tokenized yield-bearing vaults
Key Takeaways
- Solidity is the dominant language for EVM smart contracts; Rust is used on Solana
- Development tools like Hardhat and Foundry enable local testing before deployment
- Security audits are critical because smart contracts cannot be changed after deployment
- Token standards (ERC-20, ERC-721, etc.) ensure interoperability across the ecosystem
Under the hood
Upgradeability Patterns and Proxy Contracts
While smart contracts are immutable by default, the proxy pattern enables upgradeability. A proxy contract delegates all calls to an implementation contract via delegatecall, which executes the implementation's logic in the proxy's storage context. Upgrading means deploying a new implementation contract and updating the proxy's pointer. OpenZeppelin's TransparentProxy and UUPS (Universal Upgradeable Proxy Standard) are the most common patterns.
Upgradeability is a double-edged sword: it allows bug fixes and feature additions, but it also means users must trust the upgrade authority (typically a multi-sig or timelock) not to introduce malicious changes. The Diamond pattern (EIP-2535) takes this further by allowing multiple implementation contracts (facets) to be composed behind a single proxy, enabling modular contract architecture. For maximum trustlessness, some protocols deploy immutable contracts and use governance to migrate liquidity to new versions.
Formal Verification and Advanced Security
Formal verification uses mathematical proofs to verify that a smart contract's behavior matches its specification for all possible inputs — not just the test cases a developer thought of. Tools like Certora Prover, Halmos, and KEVM allow developers to write invariants (properties that must always hold) and prove them against the bytecode. While traditional testing finds specific bugs, formal verification can prove the absence of entire categories of bugs.
- Static analysis tools (Slither, Mythril) — automated scanners that detect common vulnerability patterns
- Fuzz testing (Foundry, Echidna) — generates random inputs to find edge cases that break invariants
- Formal verification (Certora, Halmos) — mathematical proof that code satisfies specified properties
- Bug bounties (Immunefi) — offer financial rewards to white-hat hackers who find and responsibly disclose vulnerabilities
The Rise of ZK Smart Contracts
Zero-knowledge proof systems are enabling a new class of smart contracts that can verify computations performed off-chain without revealing the underlying data. zkEVMs (like Linea and Scroll) run standard Solidity contracts but use ZK proofs for Layer 2 scaling. Fully programmable ZK applications on frameworks like Noir and Circom are enabling privacy-preserving DeFi, identity verification, and voting.
Advanced Proxy Patterns and the Diamond Standard (EIP-2535)
The UUPS (Universal Upgradeable Proxy Standard, EIP-1822) pattern places the upgrade logic in the implementation contract rather than the proxy, reducing gas costs for users and simplifying the proxy contract. Unlike the Transparent Proxy pattern (where the proxy admin is handled by a separate ProxyAdmin contract), UUPS implementations must include an upgrade function — if the team deploys an implementation without it, the contract becomes permanently non-upgradeable. The Diamond Standard (EIP-2535) takes modularity further: a single proxy contract delegates to multiple implementation contracts called 'facets,' each handling different functionality. A Diamond can add, replace, or remove facets independently, enabling protocols to upgrade individual features without redeploying everything. This is especially powerful for protocols exceeding Ethereum's 24KB contract size limit. Projects like Aavegotchi use Diamonds for modular smart contract architecture, and tools such as Louper (the Ethereum Diamond Inspector) exist for inspecting them.
- Transparent Proxy — proxy delegates all calls to implementation; admin interactions handled by separate ProxyAdmin to prevent selector clashing; higher per-call gas overhead
- UUPS Proxy (EIP-1822) — upgrade logic lives in the implementation contract; smaller proxy, lower gas; but forgetting the upgrade function in a new implementation bricks the contract
- Diamond Standard (EIP-2535) — single proxy delegates to multiple facets via a mapping of function selectors to implementation addresses; enables modular, unlimited-size contract systems
- Beacon Proxy — multiple proxies share a single implementation address stored in a Beacon contract; upgrading the Beacon upgrades all proxies simultaneously
- ERC-1967 stores the implementation address at a standardised storage slot so that it does not conflict with the storage layout of the implementation, avoiding one of the most dangerous proxy bugs: storage collision between proxy and implementation slots
Key Takeaways
- Proxy patterns (Transparent, UUPS, Diamond) enable smart contract upgradeability with trust trade-offs
- Formal verification mathematically proves contract correctness for all possible inputs
- A defense-in-depth security approach combines audits, fuzzing, formal verification, and bug bounties
- ZK smart contracts enable off-chain computation verification and privacy-preserving applications
- The Diamond Standard (EIP-2535) enables modular multi-facet contracts that bypass the 24KB limit
- OpenZeppelin's docs now lean towards UUPS for upgradeable proxies, but it requires careful implementation to avoid bricking
Related Content
Related Coins
More Topics
Browse all 189 topicsConsensus Mechanisms
Learn how blockchain networks agree on a single source of truth — from Proof of Work mining to Proof of Stake validation, Delegated PoS, and Proof of Authority.
Crypto Wallets
Everything about storing cryptocurrency safely — hot vs. cold wallets, custodial vs. non-custodial, seed phrases, hardware wallets, and best practices for protecting your assets.
DeFi Basics
Explore decentralized finance — how DEXs, lending protocols, yield farming, and liquidity pools work, and what TVL really means.
Mining & Staking
How mining works in Proof of Work, staking mechanics in Proof of Stake, validator requirements, rewards, and the economics behind securing blockchain networks.
Trading Basics
Learn the fundamentals of crypto trading — exchanges, trading pairs, order types, market and limit orders, fees, and how to avoid common beginner mistakes.
Security 101
Protect your crypto — learn about 2FA, hardware wallets, seed phrase storage, common scams, and operational security practices that keep your assets safe.
Regulation & Taxes
Navigate the complex and evolving landscape of crypto regulation — KYC/AML requirements, SEC enforcement, MiCA in Europe, tax treatment of crypto transactions, and DeFi-specific tax challenges.
DAOs & Governance
How decentralized autonomous organizations work — governance tokens, voting mechanisms, Snapshot, treasury management, delegation, and the risks of governance attacks.
References & further reading
- primarySolidity documentation
The official language docs for the most widely used contract language.
- primaryethereum.org — Smart contracts
Official conceptual introduction with runnable examples.
archived copy ↗ - primaryEthereum Yellow Paper (Wood)
The original formal EVM specification. Its README says it is out of date (it reflects Ethereum only up to the Shanghai upgrade of April 2023); the Python execution-specs are the actively maintained specification.
archived copy ↗ - primaryOpenZeppelin Contracts - Proxy API (Transparent, UUPS, Beacon)
Official docs for the proxy patterns described in the advanced section, including the shift towards UUPS.
- primaryEIP-2535 - Diamonds, Multi-Facet Proxy
The Diamond standard described in the advanced section.