Skip to main content

This site is for educational purposes only. Nothing here constitutes financial advice.

Topic 6 of 189

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.

Beginner
8 min readUpdated October 2026Block Clarity Hub Editorial Team

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

  1. A user submits a transaction that calls a specific function on a smart contract
  2. The transaction is included in a block by a validator
  3. Every node on the network executes the contract code in the EVM (Ethereum Virtual Machine)
  4. The state changes (balances, storage variables) are recorded on-chain
  5. 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

References & further reading