Smart contracts & Ethereum
A smart contract is a program whose execution the whole network verifies β which makes it the first credible way to automate money-moving agreements like insurance payouts without an operator in the middle. Here you learn the machine, the language, and the eight ways it bites.
L1The EVM: a world computer with a meter
Ethereum is one giant, replicated state machine. Every node executes every transaction through the Ethereum Virtual Machine (EVM) and must arrive at the identical result β that redundancy is what makes the outcome trustworthy, and what makes computation expensive.
- Two account types. Externally owned accounts (EOAs β a keypair, e.g. your MetaMask) and contract accounts (code + storage at an address). Only EOAs start transactions; contracts react.
- Gas. Every EVM instruction has a price. You attach a gas budget to a transaction; run out and everything reverts (but the fee is spent). Gas is the spam-defence and the reason on-chain code stays lean β heavy logic and data belong off-chain.
- Transactions are atomic. A transaction either fully succeeds or fully reverts. There is no "half-paid claim" state β a genuinely useful property for financial workflows.
L2Solidity anatomy β HelloPolicy, line by line
Solidity is the dominant contract language: statically typed, compiled to EVM bytecode. Here is a deliberately small but honest insurance contract β one product, one insured, fixed sums. You will deploy it in the lab below.
// SPDX-License-Identifier: MIT pragma solidity ^0.8.24; /// Minimal fixed-benefit cover β learning scaffold, not production. contract HelloPolicy { address public insurer; // deployer = the carrier uint256 public constant PREMIUM = 0.01 ether; uint256 public constant PAYOUT = 0.05 ether; struct Policy { address holder; uint64 expiry; bool paid; bool claimed; } mapping(address => Policy) public policies; // on-chain "policy admin table" event PolicyIssued(address indexed holder, uint64 expiry); event ClaimPaid(address indexed holder, uint256 amount); error NotInsurer(); error NoActivePolicy(); error AlreadyClaimed(); modifier onlyInsurer() { if (msg.sender != insurer) revert NotInsurer(); _; } constructor() payable { insurer = msg.sender; } // fund it on deploy: claims reserve /// Customer pays exactly the premium; cover runs 30 days. function buyPolicy() external payable { require(msg.value == PREMIUM, "premium is 0.01 ETH"); uint64 expiry = uint64(block.timestamp + 30 days); policies[msg.sender] = Policy(msg.sender, expiry, true, false); emit PolicyIssued(msg.sender, expiry); } /// Insurer approves a claim β contract itself pays out. Checks-Effects-Interactions. function approveClaim(address holder) external onlyInsurer { Policy storage p = policies[holder]; if (!p.paid || block.timestamp > p.expiry) revert NoActivePolicy(); if (p.claimed) revert AlreadyClaimed(); p.claimed = true; // EFFECT firstβ¦ (bool ok, ) = holder.call{value: PAYOUT}(""); // β¦INTERACTION last require(ok, "transfer failed"); emit ClaimPaid(holder, PAYOUT); } }
Read it as an insurance engineer:
mapping(address β Policy)is your policy-admin table;publicauto-generates a free read function.- Events (
PolicyIssued,ClaimPaid) are the integration bus β indexed, cheap, subscribable. Contract events : Ethereum :: ledger entries β Kafka : Galileo. - Custom errors + modifiers encode authority ("only the insurer approves claims") in code that a regulator can read.
- The checks-effects-interactions ordering in
approveClaimis the canonical reentrancy defence β flag before paying. - Notice what's missing: underwriting, KYC, variable pricing, privacy. On a public chain, everything here is world-readable β a core reason enterprise insurance work moved to permissioned platforms (Module 03) or hybrid designs (public anchor + private data).
L3Tokens in one lesson
Interchangeable units
The standard interface (transfer, approve, balanceOf) behind stablecoins β which is exactly what Aon used to settle premiums in 2026 β and tokenized deposits (Module 09). If your product touches money movement, ERC-20 is the plumbing.
Unique items
One token = one unique asset. Insurance readings: a policy as an NFT (transferable proof of cover β Etherisc does this), or a cat-bond note as a token (Module 10's ILS tokenization).
L4Oracles β the bridge that decides parametric claims
Contracts cannot call APIs; the chain only trusts what's already on it. An oracle brings external facts (wind speed, flight status, an official index) on-chain β and instantly becomes the most attackable part of any parametric design. Chainlink, the dominant network, answers with decentralised oracle committees: many independent nodes fetch, aggregate, sign.
L5Security β the eight classic wounds
| # | Vulnerability | One-line mechanics | Insurance flavour of the risk |
|---|---|---|---|
| 1 | Reentrancy | External call re-enters before state updates ("The DAO", 2016) | Claim paid twice before "claimed" flag set β CEI ordering / guards |
| 2 | Access control | Missing/wrong onlyRole checks | Anyone can approve claims or change premiums |
| 3 | Oracle manipulation | Feed spoofed, stale, or thin-market price pushed | Fake weather = fake payouts; use multi-source, signed, time-bounded feeds |
| 4 | Integer edge cases | Overflow (pre-0.8), rounding, unit slips (wei vs ether) | Premium/payout math off by 10ΒΉβΈ |
| 5 | Front-running / MEV | Public mempool lets others reorder around your tx | Racing a trigger to buy cover just before a known payout |
| 6 | Denial of service | Unbounded loops; a reverting receiver blocks everyone | Payout loop over all holders bricks the book β use pull payments |
| 7 | Bad randomness | Block variables are predictable | Any lottery-like selection needs VRF, not block.timestamp |
| 8 | Upgrade/admin risk | Proxy patterns move "immutable" into an admin key | Who can change the terms of in-force cover? Governance, timelocks, multisig |
Contract code is public, immutable-ish, and adversarially probed with real money on the line. Treat deployment like releasing firmware for a pacemaker: reviews, tests, audits, and the smallest possible surface. In 2016 The DAO lost ~$60 M to wound #1 and forced Ethereum's fork β the industry's founding security story.
L6Lab β deploy HelloPolicy in 15 minutes
Lab 2.1 Β· Remix, zero install
browser only- Open Remix
Go to remix.ethereum.org. In File Explorer create
HelloPolicy.soland paste the contract from L2 (copy button above). - Compile
Solidity Compiler tab β version 0.8.24+ β Compile. Green check = bytecode ready.
- Deploy funded
Deploy & Run tab β environment Remix VM. Set VALUE = 1 ether (the claims reserve!) then Deploy. You are now the
insurer. - Buy cover as a customer
Switch the ACCOUNT dropdown to a second address. Set VALUE = 0.01 ether (10000000 gwei Γ· 1000 β easier: choose "ether" unit, type 0.01) and click
buyPolicy. Readpolicies(yourAddress)β there's your record. - Approve a claim as the insurer
Switch back to account #1, call
approveClaim(customerAddress). Watch the customer's balance jump by 0.05 ETH and find theClaimPaidevent in the transaction log. - Now break it (the real lesson)
Try: buying with 0.02 ether (revert message?), approving twice (which custom error?), approving from the customer account (which error?). Reverts-with-reasons are your friend.
Optional stretch: redeploy on the Sepolia testnet via MetaMask (M00 safety rules apply) β the exact flow is in Lab M06 where it matters. Prefer local tooling? The Foundry appendix lives in M06 too.
Map the vocabulary: Galileo's /command APIs β state-changing contract functions; its /query APIs β free view reads; ledger-then-Kafka β transaction-then-events; DB-updater workers β your event-subscribed backend. Same architecture instincts β different trust boundary. What the EVM adds is enforced shared execution: no single party (not even the insurer) can skip the rules once deployed. What it costs is privacy and gas.
βΆWatch / go deeper
The definitive free deep-dive when you want mastery, with repo.
Python-flavoured alternative path.
Collins' current home β updated Solidity, security and auditing tracks.
Sources: Antonopoulos & Wood, Mastering Ethereum (EVM, wallets, security); Mastering Solidity (supplementary); Chainlink docs for the oracle pattern. DAO figures: contemporaneous reporting, 2016.