BC7: Writing smart contracts in Solidity
What a smart contract is, and isn't
🎯You'll be able to
Explain what a smart contract can and can't do.
🏠The everyday picture
A vending machine: put in the right coins, press a button, and it gives the item by its fixed rules, for anyone, without a shopkeeper. It can't decide to be kind, and it can't fetch things from outside itself.
📘What it really is
- Code and storage at an address: a contract is a program deployed to the chain, with its own storage and balance.
- Runs when called: by a transaction (which can change state and costs gas) or a call (read-only and free). It can't wake itself.
-
Deterministic: every node runs it and must get the same result, so it can't read the internet, a clock beyond the block's timestamp, or true randomness by itself. Outside data comes through oracles, which bring their own trust.
-
Public and final: anyone can read the code and call any public function. Deployed code doesn't change.
- Not a legal contract: it enforces what the code says, not what people meant. If the two differ, the code wins on the chain.
📝A worked example
Input: the group wants "pay each member 2% interest on 1 January".
Steps:
- The contract can't wake up on 1 January; someone must call it.
- It can check
block.timestampis after 1 January when called. - A loop over all members could run out of gas (BC5), so each member claims their own interest instead.
Expected output: a design that fits what contracts can do: called, checked, and bounded.
🌍Where it's used
- Tokens following standards such as ERC-20.
- Escrow releasing funds when conditions are met.
- Registries of names, certificates or hashes.
📝Case scenario
A team built a contract that paid out when the price of a crop reached a level, reading the price from one website through a single oracle. The website showed a wrong price for an hour, and the contract paid out by mistake. Using several independent price sources would have reduced the risk.
🖱️Try it
For each, say whether a contract can do it alone: hold ether; send a reminder SMS; check that a caller is a member; pay interest at midnight automatically. Success: yes, no, yes, no.
⚠️Common mistakes
- Expecting contracts to act on their own. Sign: "it will pay on Friday". Fix: something has to call it.
- Trusting one data source. Sign: a single oracle. Fix: several sources, or don't depend on outside data.
- Treating code as the agreement. Sign: no plain-language terms. Fix: write the agreement down too.
🛠️For your project
SakhiFund in Project 1 holds deposits and pays withdrawals when members call it; it does nothing on its own.
✅Quick check
Why can't a contract fetch data from a website by itself?
Show the answer
Every node must get the same result, so contracts can't read outside data directly.
What wins on the chain if the code and the intended agreement differ?
Show the answer
The code.
