Intermediate · Lesson 8
Smart contracts and dApps: code that cannot apologise
A smart contract does exactly what it says, including the part nobody read. This lesson is about reading the part.
A smart contract is a program that lives at an address on a chain. It has storage, functions and a balance. When you send a transaction to it, every node that validates the chain runs the same code on the same inputs and must reach the same result. That is the whole promise, and the whole problem: the result is not negotiable.
What actually runs
A contract is not a web service. It cannot call an API, read a file or ask a human for confirmation. It can read its own storage, the current block and data that someone else has already written on-chain. Anything from outside — a price, a sports result, a bank balance — arrives through an oracle, a separate contract or service with its own trust assumptions.
Execution is paid for in gas: a unit of computation priced by the network. A transaction declares a gas limit; if the code runs out, it reverts. Reverting undoes the state changes but not the fee. The work was done and the network charges for the attempt.
That explains a lot of user pain: a failed swap is not free, a failed mint is not free, and a transaction that reverts after consuming most of its gas is an expensive way to learn that a condition was not met.
A call, step by step
Most token interactions are two calls, not one:
approve(router, amount) // allow the router to move your tokens swap(tokenIn, tokenOut, amount, minOut)
The first writes an allowance into the token's storage. The second asks the router to pull the tokens and send something back. If the second call fails, the allowance may remain — which is why revoking unused approvals is a habit, not a hobby.
The minOut argument is the slippage guard. If the pool moves before your transaction is included, the contract reverts rather than filling at a worse price. Reverting is safer than being filled at any price, but it still costs gas.
Worked example: what the call costs
Suppose the effective gas price is 20 gwei and ether is worth 3,000 dollars. One gwei is one billionth of an ether, so 20 gwei is 0.00000002 ETH per gas.
- Approval: 46,000 gas × 20 gwei = 920,000 gwei = 0.00092 ETH ≈ 2.76 dollars.
- Swap: 150,000 gas × 20 gwei = 3,000,000 gwei = 0.003 ETH ≈ 9.00 dollars.
- Total: 196,000 gas = 0.00392 ETH ≈ 11.76 dollars.
If the gas price rises to 120 gwei, the same two calls cost 196,000 × 120 = 23,520,000 gwei = 0.02352 ETH ≈ 70.56 dollars. The code did not change; the queue for block space did.
And if the swap reverts after 80,000 gas, you pay 80,000 × 20 = 1,600,000 gwei = 0.0016 ETH ≈ 4.80 dollars, and the token balances are exactly where they started.
These numbers are illustrative: gas depends on the contract, the state and whether a storage slot is warm or cold.
What an audit does not cover
An audit is a review of a particular version of the code by a team at a point in time. It can find a reentrancy bug, a missing access check or an arithmetic error. It cannot promise that the economics make sense, that the admin key is safe, that the front end serves the real contract, or that the next upgrade will not remove the check it just approved.
Many contracts are upgradeable behind a proxy: the address you bookmark stays the same while the logic changes. If an admin can replace the implementation, the trust question is not «is the code audited?» but «who can change it, and how long do I have to leave before they do?» A timelock and a multisig are not decoration; they are the difference between a rule and a promise.
An audit also says nothing about the parameters. A lending market with a sensible contract and a reckless collateral factor is still reckless. A stablecoin wrapper with a perfect contract and an unverifiable reserve is still a claim on a reserve.
The front end is not the protocol
A dApp is a website that talks to a contract. The website can be cloned, taken down or replaced. The contract address is the identity that matters. A drainer does not need to break the contract; it only needs you to sign a transaction the real contract never required.
Three habits reduce that risk: open the dApp from a saved link rather than a search result, check the contract address against a second source, and read the wallet prompt before signing. If the prompt asks for an approval you did not intend, close it. If the site insists that a «validation» transaction is needed to claim something, be sceptical: claiming usually costs gas, not an unlimited allowance.
Who can change the rules
Most contracts have an administration surface, even when the documentation calls them decentralised. Look for roles that can pause transfers, mint supply, change fees, upgrade logic or move the treasury. Then ask who holds them: a single key, a multisig, a DAO vote, or nobody because the contract is immutable.
Immutability is a trade-off too. A contract that cannot be fixed cannot be fixed after a bug. The useful question is not «is it immutable?» but «what can go wrong, who can respond, and what can they do to my position while responding?»
Check yourself
- A transaction reverts after using 80,000 gas. What did you pay, and what changed on-chain?
- Why is «audited» a statement about the past, not about your position?
- You reach a dApp through a bookmark. Which address should you verify, and against what?
The terms in this lesson — gas, smart contract, dApp, oracle — are in the glossary. Smart contract risk turns this into a checklist, and AMMs shows a contract that prices assets with a formula instead of an order book.