Smart Contract Fundamentals

Loading concept...

Smart Contract Fundamentals: The Magic Vending Machine of the Blockchain

Imagine a magic vending machine that sits in the middle of a town square. Anyone can walk up to it, put in coins, and get exactly what they asked forβ€”no shopkeeper needed. This machine never lies, never cheats, and always follows its rules. That’s what a smart contract is!

Let’s explore this magical world together.


πŸ–₯️ EVM Overview: The Brain of Ethereum

What is the EVM?

Think of Ethereum like a giant computer shared by everyone in the world. The EVM (Ethereum Virtual Machine) is the brain of this computer.

Simple Example:

  • When you play a video game, your computer runs the game
  • The EVM is like a game console that runs smart contracts
  • Every computer on Ethereum runs the same β€œgame” and gets the same result!
graph TD A[Your Code] --> B[EVM Brain] B --> C[Same Result Everywhere] C --> D[Computer 1] C --> E[Computer 2] C --> F[Computer 3]

Why is the EVM Special?

The EVM makes sure:

  • βœ… Every computer gets the same answer
  • βœ… Nobody can cheat
  • βœ… Code runs exactly as written

Real Life Analogy: If 1000 kids do the same math problem using the same calculator, they all get the same answer. The EVM is that calculator for smart contracts!


βš™οΈ EVM Internals: Inside the Brain

How Does the EVM Think?

The EVM brain works like a simple calculator that only knows basic operations. It has:

1. Stack - A pile of numbers (like stacking blocks) 2. Memory - A temporary notepad (erased after use) 3. Storage - A permanent diary (saved forever)

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚      STACK          β”‚  ← Quick math (256 slots)
β”‚  [5] [3] [2] ...    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚      MEMORY         β”‚  ← Scratch paper
β”‚  (temporary notes)  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚      STORAGE        β”‚  ← Permanent diary
β”‚  (saved forever)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Opcodes: The EVM’s Alphabet

The EVM reads instructions called opcodes. Think of them like simple commands:

Opcode What It Does Like…
ADD Add two numbers 2 + 3 = 5
SUB Subtract 5 - 2 = 3
STORE Save to diary Write in notebook
LOAD Read from diary Read notebook

Example:

PUSH 5    ← Put 5 on the stack
PUSH 3    ← Put 3 on the stack
ADD       ← Add them: 5 + 3 = 8!

Gas: The Fuel

Every operation costs gas (like coins in an arcade). More complex = more gas!

  • Simple math (ADD): 3 gas
  • Saving data (SSTORE): 20,000 gas

Why gas? It stops people from running infinite loops and crashing the network!


πŸ“œ Smart Contracts Definition

What Exactly IS a Smart Contract?

A smart contract is code that lives on the blockchain and runs automatically when conditions are met.

The Vending Machine Analogy:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    🏭 VENDING MACHINE           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  Rules:                         β”‚
β”‚  β€’ Insert $1 β†’ Get candy        β”‚
β”‚  β€’ Insert $2 β†’ Get chips        β”‚
β”‚  β€’ Insert $5 β†’ Get toy          β”‚
β”‚                                 β”‚
β”‚  ✨ No human operator needed!   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

In Code:

contract VendingMachine {
    function buyCandy() public {
        // If you pay $1...
        // You get candy!
    }
}

Key Properties

Property Meaning Vending Machine Example
Immutable Can’t be changed Machine rules are permanent
Transparent Everyone can see Rules posted on glass
Trustless No trust needed Machine always delivers
Automatic Runs by itself No cashier required

πŸ’Ύ Contract State and Storage

State: What the Contract Remembers

Just like you remember your name and age, a contract remembers its state.

Example Contract Memory:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Piggy Bank Contract State     β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚  owner: "Alice"                 β”‚
β”‚  balance: 50 coins              β”‚
β”‚  lastDeposit: "2024-01-15"      β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Storage: The Permanent Diary

Storage is organized in slots (like numbered boxes):

Slot 0: [owner address         ]
Slot 1: [balance = 50          ]
Slot 2: [lastDeposit timestamp ]
...
Slot N: [more data...          ]

In Solidity:

contract PiggyBank {
    address owner;    // Slot 0
    uint balance;     // Slot 1
    uint lastDeposit; // Slot 2
}

Storage is EXPENSIVE!

Writing to storage costs ~20,000 gas. That’s like paying $5 to write one sentence in your diary!

Pro Tip: Use storage wiselyβ€”only save what you truly need forever.


πŸš€ Smart Contract Deployment

What is Deployment?

Deployment is like building your vending machine and placing it in the town square for everyone to use.

graph TD A[Write Code] --> B[Compile to Bytecode] B --> C[Send Transaction] C --> D[Contract Gets Address] D --> E[πŸŽ‰ Live on Ethereum!]

Step-by-Step Deployment

Step 1: Write your contract

contract HelloWorld {
    string message = "Hello!";
}

Step 2: Compile it (translate to EVM language)

608060405234801561001057...
(bytecode - computer language)

Step 3: Send deployment transaction

  • No β€œto” address (creating new!)
  • Include bytecode + constructor args
  • Pay gas fee

Step 4: Get your contract address!

Contract deployed at:
0x1234...abcd

Constructor: The Birth Function

When deployed, the constructor runs ONCE:

contract Greeting {
    string public message;

    constructor(string memory _msg) {
        message = _msg; // Set initial message
    }
}

🀝 Smart Contract Interaction

Talking to Contracts

Once deployed, anyone can interact with your contract!

Two Types of Interactions:

Type Costs Gas? Changes State? Example
Read (call) No ❌ No ❌ Check balance
Write (transaction) Yes βœ… Yes βœ… Send money

Reading Data (Free!)

// Anyone can read:
function getBalance() public view returns(uint) {
    return balance;
}

Like: Looking through the vending machine glass to see what’s inside.

Writing Data (Costs Gas!)

// Changes the blockchain:
function deposit() public payable {
    balance += msg.value;
}

Like: Actually putting coins in and getting candy.

ABI: The Instruction Manual

The ABI (Application Binary Interface) tells apps how to talk to your contract:

{
  "name": "deposit",
  "type": "function",
  "inputs": [],
  "outputs": []
}

Think of it as: The buttons and labels on the vending machine showing what each button does!


πŸ“ž CALL vs DELEGATECALL

The Phone Call Analogy

This is the trickiest partβ€”but let’s make it simple!

CALL: Regular Phone Call

When Contract A CALLS Contract B:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”         β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Alice   β”‚  CALL   β”‚  Bob    β”‚
β”‚ (A)     β”‚ ──────> β”‚  (B)    β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜         β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β€’ Bob runs the code
β€’ Bob uses his own memory
β€’ Bob's data changes
β€’ Alice's data stays same

Like: You call a pizza shop. The pizza shop makes the pizza at THEIR kitchen using THEIR ingredients.

Example:

// Contract A calls Contract B
contractB.call(
    abi.encodeWithSignature("doSomething()")
);
// B's storage changes, A's stays same

DELEGATECALL: β€œCome to MY Kitchen”

When Contract A DELEGATECALLS Contract B:

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚         Alice (A)           β”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”‚
β”‚  β”‚ Bob's recipe runs   β”‚   β”‚
β”‚  β”‚ in Alice's kitchen! β”‚   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

β€’ Bob's code runs
β€’ But uses Alice's storage!
β€’ Alice's data changes
β€’ Bob's data stays same

Like: You invite the pizza chef to YOUR kitchen. They use YOUR ingredients, YOUR ovenβ€”you just borrow their recipe!

Example:

// Contract A delegatecalls Contract B
contractB.delegatecall(
    abi.encodeWithSignature("doSomething()")
);
// A's storage changes using B's logic!

Side-by-Side Comparison

Feature CALL DELEGATECALL
Code from Target Target
Storage used Target’s Caller’s
msg.sender Caller Original sender
Use case Talk to other contracts Upgradeable contracts

Real-World Use: Proxy Patterns

DELEGATECALL enables upgradeable contracts:

graph LR U[User] --> P[Proxy Contract] P -->|delegatecall| L1[Logic v1] P -.->|upgrade| L2[Logic v2]

The Proxy holds the data. Logic can be swapped out!


🎯 Quick Recap

Concept One-Liner
EVM Ethereum’s shared computer brain
EVM Internals Stack + Memory + Storage
Smart Contract Self-running code on blockchain
State/Storage Contract’s permanent memory
Deployment Putting your code live
Interaction Reading (free) & writing (costs gas)
CALL Use other contract’s code AND storage
DELEGATECALL Use other’s code, YOUR storage

🌟 You Did It!

You now understand how the magic vending machines of Ethereum work! From the EVM brain, to storage, to the mysterious DELEGATECALLβ€”you’ve got the fundamentals down.

Remember: Smart contracts are just code that runs automatically, lives forever, and keeps everyone honest. The vending machine never sleeps, never lies, and always delivers! πŸŽ‰

Loading story...

No Story Available

This concept doesn't have a story yet.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

Interactive Preview

Interactive - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Interactive Content

This concept doesn't have interactive content yet.

Cheatsheet Preview

Cheatsheet - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Cheatsheet Available

This concept doesn't have a cheatsheet yet.

Quiz Preview

Quiz - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.

No Quiz Available

This concept doesn't have a quiz yet.