MIVORA Start learning free

Smart Contracts: Code That Runs Itself

A program living on the ledger that holds money and enforces its own rules automatically — doing exactly what its code says, bugs and all.

Crypto & Tokenization · Lesson 8 · 9 min read

You want to bet a friend $100 on a coin flip, but neither of you trusts the other to actually pay up after losing. Normally you’d need a referee to hold the money — a trusted middleman, with every problem from lesson 1. But what if a piece of code could hold both stakes and automatically pay the winner, where neither of you (nor anyone else) could cheat, stall, or change the rules once it’s set? That’s a smart contract. Hold the question: what exactly is it — and what’s the catch?

A smart contract is code that lives on the ledger and runs itself

A smart contract is a program deployed on the blockchain. When someone calls it, every node runs the same code on the same inputs and must reach the same result — that’s how they keep consensus on what happened. It can hold tokens and move them by its own logic, with no human approving each step: “if the flip is heads, send the pot to Alice” simply executes.

Two myths to drop: it isn’t “smart” like AI, and it isn’t a legal contract. It’s just self-executing rules — plain code that the whole network runs and enforces.

Automatic and unstoppable — the double edge

Once deployed, a contract typically can’t be altered or stopped (the immutability from lesson 4). That’s the feature: the rules are fixed and predictable, and no one can renege or move the goalposts.

But it’s also the danger. The contract does exactly what its code says — including its bugs. If there’s a flaw, an attacker can exploit it and the loss is irreversible; there’s no support line to undo a drained contract. “Code is law” cuts both ways: unstoppable when it’s right, unstoppable when it’s wrong.

Worked example
An escrow / coin-flip bet:
• Alice and Bob each send $100 of tokens into a contract that says “hold $200, then pay it all to the winner once the agreed outcome is decided.”
• The contract now holds the funds — neither can grab them early; the rules are fixed on-chain.
• The outcome is determined; the contract checks it and automatically sends $200 to the winner. No referee, no trusting the loser to pay.
• The catch: if the code can be tricked into paying both, or a bug locks the funds forever, that happens automatically too — with no appeal.

Where the trust moves

A smart contract removes the human middleman, but it does not remove trust — it relocates it to the correctness of the code. You’re now trusting that the program does what it claims and has no exploitable bug. That’s why audited, open-source, battle-tested contracts matter, and why the verify-don’t- trust habit from lesson 5 applies here too: you can read what a contract actually does before sending it a cent. Same discipline, new target — from “trust this company” to “check this code.”

An everyday analogy

A vending machine is a tiny contract made of mechanism. Put in the right coins, press B4, and it will give you the snack — no shopkeeper, no negotiation, no changing its mind. The rules are fixed and anyone can predict them. But if it’s mis-built so B4 drops two snacks for one coin, it’ll keep doing that until it’s empty, and there’s no clerk to stop it. A smart contract is a vending machine for money and tokens: wonderfully automatic, and exactly as flawed as it was built.

Worked example
Why “the chain is secure” doesn’t mean “the contract is safe”:
1. A contract is deployed to hold and swap users’ tokens. The blockchain itself is rock-solid — consensus, hashing, the works.
2. But the contract’s code has a logic bug: under a certain sequence of calls, it lets a caller withdraw more than they deposited.
3. An attacker triggers exactly that sequence. Every node faithfully runs the buggy code and agrees on the result — the withdrawal is “valid” by the rules as written.
4. The funds drain, irreversibly. The chain did its job perfectly; the code was wrong.
5. Lesson: the network guarantees the contract runs as written and can’t be tampered with — not that what was written is correct. Those are different guarantees.

This is the reading. The interactive version — active-recall quiz, a hands-on experiment you run in your own AI, and an earned mastery check — is free in the app.

Start this lesson free →