Consica Labs

Consica Labs
Chapter 9

Smart Contracts

Self-executing agreements on the blockchain

Definition

A Smart Contract is a self-executing computer program that runs on a blockchain and automatically enforces the terms of an agreement when predefined conditions are met. Unlike traditional contracts that require human enforcement (lawyers, courts), Smart Contract execute automatically and deterministically — the same inputs always produce the same outputs on every node in the network. Key concepts include Deploy.

Smart Contract extend blockchain beyond simple value transfer. They are written in programming languages like Solidity (for Ethereum), compiled to bytecode, and deployed to the blockchain via a special transaction. Once deployed, they have their own blockchain address and balance. Users interact with Smart Contract by sending transactions that call specific functions. The contract code executes on the Ethereum Virtual Machine (EVM), and execution costs Gas — fees paid in ETH that compensate validators and prevent abuse.

Real-Life Example

Think of a Smart Contract like a vending machine. A vending machine has hard-coded rules: insert $2, press A1, get a soda. No human needs to verify the transaction — the machine enforces the rules automatically. A Smart Contract is the same but far more flexible. It can hold funds, release them when conditions are met, interact with other contracts, and maintain permanent records on the blockchain. And unlike a vending machine that one company owns and maintains, Smart Contract run on thousands of computers worldwide.

A real-world example: In 2021, the city of Berkeley, California, issued a blockchain-based micro-bond using Smart Contract. Residents could buy municipal bonds for as little as $25 using cryptocurrency. The Smart Contract automatically tracked ownership, paid interest on schedule, and allowed bonds to be traded on secondary markets. This made municipal bonds accessible to ordinary citizens for the first time, bypassing the traditional financial system that typically requires minimum investments of $5,000 or more.

Interactive Diagram

Launch the interactive diagram to see this in action.

Open Interactive Diagram

The interactive diagram for this chapter demonstrates Distributed Ledgers. It shows a network of computers each holding a complete copy of the blockchain ledger.

What to explore:

  • add a transaction; watch it propagate to all nodes; see each node update its ledger; take one node offline and see the network continues
  • in a distributed ledger, every participant has a copy of the entire blockchain — there is no single point of failure

Introduction

A vending machine is a simple kind of Smart Contract. You put in money, select a product, and the machine automatically dispenses it. No human needs to verify the transaction — the machine enforces the rules. Smart Contract are computer programs that run on a blockchain and automatically execute when predefined conditions are met. They are like vending machines for digital agreements.

Smart Contract extend blockchain beyond simple currency transactions. With Smart Contract, you can create programmable money, decentralized applications, automated marketplaces, and trustless agreements. Ethereum, launched in 2015, was the first blockchain designed specifically for Smart Contract. Today, thousands of decentralized applications run on Smart Contract platforms.

In this chapter, you will learn what Smart Contract are, how they work on platforms like Ethereum, and what they can do. You will understand the concept of 'code is law' and the power of automated, trustless agreements. By the end, you will see how Smart Contract unlock blockchain's full potential beyond cryptocurrency.

How It Works

A Smart Contract is code stored on the blockchain. It has an address (like a cryptocurrency wallet address) and a balance of cryptocurrency. The code defines rules and functions. Users interact with the Smart Contract by sending transactions to its address, which triggers its functions. The code executes automatically on every node in the network — meaning it runs exactly as written, with no possibility of censorship, downtime, or interference.

On Ethereum, Smart Contract are written in a language called Solidity (similar to JavaScript). The code is compiled into bytecode and deployed to the blockchain via a special transaction. When a function is called, every Ethereum node executes it using the Ethereum Virtual Machine (EVM). The result is computed, and any changes to the contract's state are recorded on the blockchain. Execution costs 'Gas' — a fee paid in ETH — which prevents infinite loops and compensates validators for computation.

Household Object Analogy

Think of a Smart Contract like a programmable vending machine. A regular vending machine has fixed rules: insert $2, press A1, get a soda. A programmable vending machine could have complex rules: if it is after 5 PM and you insert $2, get a soda and automatically notify your friend that you bought one. The machine executes these rules automatically without needing a store clerk. Smart Contract are the same — they execute rules automatically without needing lawyers or banks.

Deeper Dive

Smart Contract are deterministic — given the same inputs and the same state, they always produce the same output. This is essential for blockchain consensus because all nodes must agree on the result. If a Smart Contract could behave differently on different nodes (non-deterministic behavior), consensus would be impossible. Solidity is designed to eliminate non-determinism, though developers must still avoid pitfalls like using block timestamps for randomness (miners can manipulate timestamps slightly).

The Gas system on Ethereum prevents abuse. Every operation in the EVM has a Gas cost: addition costs 3 Gas, storage operations cost 20,000 Gas, etc. When you call a Smart Contract, you specify a Gas limit (how much Gas you are willing to use). If the contract uses more Gas than the limit, execution stops and changes are reverted (but you still pay for the Gas used). This prevents attackers from creating infinite loops or excessively complex computations that would slow the network.

Smart Contract can hold and manage cryptocurrency. A crowdfunding contract can collect ETH from many contributors, and when a target is reached, automatically send the funds to the project creator — or automatically refund everyone if the target is not met. This is called a trustless escrow. No third party needs to hold the funds or decide whether the target was met. The Smart Contract enforces the rules transparently and automatically.

Key Insight

The term 'Smart Contract' was coined by computer scientist Nick Szabo in 1994, long before blockchain existed. Szabo envisioned automatic contract execution using digital protocols. He even proposed 'Bit Gold', a precursor to Bitcoin. But without a decentralized platform like blockchain to enforce execution, early Smart Contract concepts could not be practically implemented.

Advanced

The ERC-20 token standard is one of the most important Smart Contract standards. It defines a common interface for fungible tokens (tokens where each unit is identical, like currency). Any ERC-20 token can be used with any ERC-20-compatible wallet, exchange, or DApp. This interoperability sparked the explosion of cryptocurrencies and tokens on Ethereum. Thousands of tokens (like USDC, UNI, SHIB) follow the ERC-20 standard.

ERC-721 is the standard for non-fungible tokens (NFTs), where each token is unique. ERC-721 contracts track ownership of individual digital items — artwork, collectibles, in-game assets. ERC-1155 is a multi-token standard that can represent both fungible and non-fungible tokens in a single contract, reducing Gas costs for applications that need both types (like games with both currency and unique items).

The DAO (Decentralized Autonomous Organization) is an organization governed entirely by Smart Contract. Members hold tokens that represent voting power. Proposals are submitted, voted on, and automatically executed if approved. DAOs have no central management — rules are encoded in Smart Contract. The first major DAO was 'The DAO' in 2016, which raised $150 million but was hacked due to a code vulnerability, leading to Ethereum's controversial hard fork.

Vocabulary Table

TermDefinition
Smart ContractA self-executing program on the blockchain that enforces an agreement automatically.
SolidityThe primary programming language for writing Ethereum smart contracts.
EVMEthereum Virtual Machine — the decentralized computer that runs smart contracts.
GasA unit measuring computational effort required to execute a smart contract.
ERC-20The standard for fungible tokens on Ethereum.
ERC-721The standard for non-fungible tokens (NFTs) on Ethereum.
DeployTo publish a smart contract to the blockchain, making it permanently accessible.
DAppA decentralized application powered by smart contracts on the blockchain.

Fun Facts

The first Smart Contract platform, Ethereum, was proposed by Vitalik Buterin in late 2013 when he was just 19 years old. It launched in July 2015.

There are over 3 million Smart Contract deployed on Ethereum as of 2025, though many are inactive or abandoned.

The most expensive Smart Contract exploit in history was the 2016 DAO hack, where an attacker drained $60 million worth of ETH due to a reentrancy vulnerability in the contract code.

Smart Contract cannot be modified after deployment. This immutability is a feature (users can trust the code will not change) but also a risk (bugs cannot be patched). Some blockchains support upgradeable contracts through proxy patterns.

The Ethereum Virtual Machine runs on thousands of computers worldwide. Every node executes every Smart Contract transaction, which is why Gas fees exist — nodes must be compensated for their computation.

Common Misconceptions

Misconception: Smart Contract are legal contracts.

Truth: Smart Contract are code, not legal documents. They execute automatically based on code logic. While they can represent agreements, their legal status varies by jurisdiction and they do not replace traditional legal contracts.

Misconception: Smart Contract are always secure.

Truth: Smart Contract are only as secure as their code. Bugs, vulnerabilities (like reentrancy attacks), and logic errors can lead to loss of funds. Professional audits are essential for high-value contracts.

Misconception: A Smart Contract can do anything a regular program can.

Truth: Smart Contract have limitations: they cannot access external data directly (they use oracles for that), they cannot make HTTP requests, and they are limited by Gas costs. They are designed for deterministic, on-chain computation.

Misconception: Smart Contract remove all need for trust.

Truth: Smart Contract reduce trust in humans but replace it with trust in code. You must trust that the contract was coded correctly, that the deployment was honest, and that the underlying blockchain is secure. 'Trustless' means 'don't trust, verify' — anyone can verify the code.

Knowledge Check

1. What is a smart contract?

Answer: A self-executing program on the blockchain that automates agreements

2. What is gas in the context of smart contracts?

Answer: The fee paid for computational work on the EVM

3. What makes smart contracts different from traditional contracts?

Answer: They execute automatically when conditions are met, without human intervention