No history yet

Smart Contract Architecture

The World Computer

Blockchains like Ethereum do more than just record who owns what. They are programmable, meaning they can run code. This execution doesn't happen on a single server, but across thousands of computers simultaneously. Together, these computers form a global, decentralized processor called the Ethereum Virtual Machine, or EVM.

Think of the EVM as a giant that is constantly being updated by transactions. A state machine is just a system that can be in one of many possible states at any given time. A transaction provides an input that causes the machine to transition to a new state. In Ethereum's case, the "state" is a snapshot of all account balances, smart contract data, and contract code at a specific moment. Every new block of transactions updates this global state, and every node in the network must agree on the result.

Lesson image

Code as Law

The programs that run on the EVM are called smart contracts. They are the logic layer of the decentralized web, defining the rules for everything from digital collectibles to lending protocols.

Smart contracts are self-executing programs stored on a blockchain that automatically enforce the terms of an agreement when predefined conditions are met.

A smart contract goes through a distinct lifecycle. First, a developer writes the code in a high-level programming language. This code is then compiled into EVM-readable instructions called bytecode and deployed to the blockchain via a transaction. This deployment gives the contract a permanent address. From that point on, anyone can interact with it by sending transactions to its address, triggering its functions and causing state changes.

The Price of Computation

Executing code on thousands of computers is a resource-intensive task. To prevent spam and manage network load, every operation on the EVM costs a small fee. This fee, paid in the network's native currency (like ETH), is called "gas."

Each computational step, from simple addition (a+ba + b) to storing data, has a fixed gas cost. A simple transaction might cost 21,000 gas, while a complex operation involving multiple storage writes could cost millions. The final transaction fee is the total gas used multiplied by the gas price, which fluctuates based on network demand.

Transaction Fee=Total Gas Used×Gas Price\text{Transaction Fee} = \text{Total Gas Used} \times \text{Gas Price}

This mechanism creates a market for block space. When the network is busy, users bid higher gas prices to get their transactions included faster. It also incentivizes developers to write efficient, optimized code. Wasteful code is expensive code, so developers carefully manage how their contracts use storage and computation to minimize costs for users.

Languages and Trade-offs

A crucial feature of the EVM is that it is Turing complete , meaning it can, in theory, solve any computational problem that a regular computer can, given enough time and memory. This flexibility is what allows for such a diverse range of applications. However, this power comes with complexity and a large attack surface.

The vast majority of EVM smart contracts are written in Solidity , a high-level language with a syntax similar to JavaScript. It was designed specifically for the EVM and has a massive ecosystem of tools and developer support. Its close relationship with the EVM makes it a natural starting point.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

contract SimpleStorage {
    uint256 public favoriteNumber; // State variable stored on the blockchain

    function store(uint256 _favoriteNumber) public {
        favoriteNumber = _favoriteNumber;
    }

    function retrieve() public view returns (uint256) {
        return favoriteNumber;
    }
}

However, other ecosystems are exploring different models. Blockchains like Solana and Polkadot use Rust, a general-purpose language known for performance and memory safety. Instead of a virtual machine, Rust-based smart contracts are often compiled directly to a more efficient binary format like WebAssembly (Wasm).

This approach can offer significant speed advantages but introduces a different set of development challenges. The choice between Solidity's EVM model and Rust's Wasm model represents a fundamental trade-off between network effects and raw performance. The EVM is battle-tested and has a huge developer base, while newer Wasm-based chains are betting on superior speed and efficiency to attract the next wave of applications.

Quiz Questions 1/6

What is the primary role of the Ethereum Virtual Machine (EVM)?

Quiz Questions 2/6

What is the term for the EVM-readable instructions that a smart contract is converted into before being deployed on the blockchain?

Ultimately, smart contract architecture is about replacing trusted human intermediaries with transparent, autonomous code. Understanding how this code is executed, managed, and paid for is the key to building the next generation of decentralized applications.