BLOCKCHAIN

Smart Contracts: Code as Law

Code stored on a blockchain that executes automatically when its conditions are met — no intermediary required, no possibility of censorship.

1What is a Smart Contract?

A smart contract is a program deployed to a blockchain. Once deployed, its code is immutable and self-executing: anyone can call its functions, and the outcome is determined entirely by the code — not by a company, court, or intermediary. Nick Szabo coined the term in 1994, but it took until Ethereum (2015) to make general-purpose smart contracts practical.

Think of a vending machine: you insert coins, select an item, and it dispenses automatically — no cashier needed. A smart contract is a programmable vending machine that can hold funds, check conditions, and release assets to any party on Earth, 24/7, without requiring trust in any operator.

Trustless
Neither party needs to trust the other — they trust the code, which is publicly readable and immutably deployed.
Permissionless
Anyone with an Ethereum address can deploy or call a contract — no registration, no approval, no KYC.
Composable
Contracts can call other contracts. DeFi is built from hundreds of composable primitives stacked together.

2The EVM and Gas

Ethereum's Ethereum Virtual Machine (EVM) is a sandboxed execution environment. Every Ethereum node runs the EVM identically, so a contract always produces the same output from the same input — which is how thousands of nodes can verify execution without trusting each other. Computation is metered with gas: each opcode has a gas cost, and callers pre-pay, preventing infinite loops from halting the network.

Gas serves two purposes: (1) it compensates validators for computation, and (2) it prevents denial-of-service by making infinite or excessively expensive operations financially costly to the caller. If a transaction runs out of gas mid-execution, all state changes are reverted but the gas is consumed — punishment for submitting a poorly written transaction.

21,000 gas
Base cost of an ETH transfer. A complex DeFi operation might use 150,000–500,000 gas.
Solidity
The dominant smart contract language. Compiled to EVM bytecode. Statically typed, supports inheritance and interfaces.
ABI
Application Binary Interface — the specification of how to encode function calls into bytes the EVM understands. Required by every caller.

3Lifecycle: Deploy, Call, Selfdestruct

A smart contract goes through three phases. Deployment: compiled bytecode is sent as a transaction with no recipient — the EVM assigns it an address and stores it on-chain. Execution: external accounts or other contracts call its functions; state changes are committed atomically. Selfdestruct: (optional) the contract can mark itself for deletion, returning any remaining ETH to a specified address.

✓ Smart contract use cases

  • ✓ Escrow: hold funds until buyer confirms delivery
  • ✓ Token issuance: ERC-20 contracts define fungible tokens (USDC, DAI, UNI)
  • ✓ Automated market makers: Uniswap price discovery without an order book
  • ✓ DAOs: on-chain governance where token holders vote on proposals

✗ Key risks

  • ✗ Immutability: bugs cannot be patched post-deployment (proxy patterns mitigate this)
  • ✗ Re-entrancy: The DAO hack (2016, $60M) exploited a recursive call before state update
  • ✗ Oracle problem: contracts cannot read real-world data natively — requires trusted Chainlink feeds
  • ✗ Gas limits: an operation too expensive to execute on-chain is simply impossible

4Security Patterns

Smart contract security is a discipline of its own — billions of dollars have been lost to contract vulnerabilities. The most important patterns to know: checks-effects-interactions (update state before calling external contracts to prevent re-entrancy), pull over push (let users withdraw funds rather than pushing to them), and access control (restrict sensitive functions to authorised callers using require(msg.sender == owner)).

Re-entrancy Guard
A locked boolean that prevents a function from being called recursively. OpenZeppelin's ReentrancyGuard is the standard implementation.
Proxy Pattern
Deploy logic separately from storage. The proxy delegates calls to the logic contract, which can be swapped — enabling upgrades while preserving state and address.
Audit
Independent security review before deployment. Top firms: Trail of Bits, OpenZeppelin, Certik. Major protocols still get hacked post-audit — test-in-production risk is real.
Escrow smart contract — Python simulation

Recall check

0 of …

Say the answer out loud before revealing — recalling is what makes it stick.

  1. What is a smart contract?

  2. What is gas in Ethereum?

  3. What is a re-entrancy attack?

  4. Why can't smart contracts read real-world data directly?