Smart contracts: code that enforces the rules
A smart contract is a program that runs on a blockchain under fixed, public rules — no middleman needed. 'If enough people chip in, the campaign is funded' becomes code that enforces itself.
The big idea
A smart contract is code with rules that execute automatically when conditions are met, with its logic visible to everyone.
See it in code
At heart a smart contract is a rule that runs by itself. Here's the crowdfunding rule alone: if the amount raised reaches the goal, mark it funded — no human decides:
# A smart contract is code that enforces a rule automatically.
goal = 100
raised = 40
if raised >= goal:
status = "FUNDED"
else:
status = "OPEN"
print("Raised:", raised, "/", goal)
print("Status:", status)Raised: 40 / 100 Status: OPEN
The if is the contract. It didn't ask permission — it just checked the condition. But a real campaign takes many contributions, so the total needs to grow.
So let contributions add up, re-checking the rule each time. Two backers chip in, and the status flips the instant the running total crosses the goal:
# The contract re-checks its rule after every contribution.
goal = 100
raised = 0
raised += 40 # Alice contributes
status = "FUNDED" if raised >= goal else "OPEN"
print("After Alice:", raised, "/", goal, "->", status)
raised += 70 # Bob contributes
status = "FUNDED" if raised >= goal else "OPEN"
print("After Bob: ", raised, "/", goal, "->", status)After Alice: 40 / 100 -> OPEN After Bob: 110 / 100 -> FUNDED
No one flipped the switch — crossing goal did, automatically. Wrapping this in a reusable function that also tracks who backed it gives the full contract.
Now wrap it in a reusable contribute function that updates the total, records each backer, and flips the status — automatically, no human approval:
# A smart contract runs under fixed, public rules.
def contribute(state, who, amount):
state["raised"] += amount
state["backers"].append(who)
if state["raised"] >= state["goal"]:
state["status"] = "FUNDED"
return state
campaign = {"goal": 100, "raised": 0, "backers": [], "status": "OPEN"}
campaign = contribute(campaign, "Alice", 40)
campaign = contribute(campaign, "Bob", 70)
print("Raised:", campaign["raised"], "/", campaign["goal"])
print("Backers:", campaign["backers"])
print("Status:", campaign["status"])Raised: 110 / 100 Backers: ['Alice', 'Bob'] Status: FUNDED
No one decided to mark it funded — the rule did, the instant raised crossed goal. On a real blockchain this code would be public and run by the network, so every backer can see and trust exactly what will happen. The logic is the agreement.
Rules-as-code that run without a trusted middleman power a lot of ideas: automated escrow, token swaps, voting, royalty splits. The broader concept — encode an agreement so it executes objectively — is why people call them 'contracts', even though they're really just programs.
Try it yourself
Add a rule that rejects contributions once the campaign is FUNDED. Then add a refund rule that only triggers if the goal isn't met by a deadline — real contracts handle the failure cases too.
The common mistake
Assuming 'smart' means safe. A smart contract does exactly what its code says — bugs and all — and once deployed it often can't be changed. Real contracts have lost fortunes to logic errors, which is why they demand extremely careful, well-tested code.
What it unlocks
Smart contracts build on functions, conditionals, and dictionaries, and underpin DeFi.