Seventeen lessons, seventeen pieces. In this lesson every piece from lessons 1 to 17 is put together into one project: Raka's Shirt Shop. The result is a full dApp, an app whose rules and important data live in smart contracts, used through the user's wallet, shown by a frontend, with big files on IPFS and the history summed up by an indexer. It feels like the shop's opening day. There is a till whose rules can't be changed (the contract), a member card in the customer's wallet (the wallet), the shop window (the frontend), the warehouse for catalogue photos (IPFS), the monthly summary book (the indexer), and a back office for things that must not be shown in the public book, such as delivery addresses (your own server).
Raka's Shirt Shop's architecture
| Piece | What it holds | Tools |
|---|---|---|
| Smart contracts | RakaPoints (ERC-20, 1 PTS per $1, 100 PTS = $5 off) and CoffeeEditionShirt (ERC-721, at most 100) | Solidity, OpenZeppelin 5.6.1, Hardhat 3.19.0 |
| Wallet | the customer's key; signs redeem |
MetaMask or another EIP-6963 wallet |
| Frontend | the "your PTS" page and the redeem button | React, wagmi 3.7.7, viem 2.57.4 |
| IPFS | COFFEE's metadata and images, pointed at with ipfs:// |
kubo, a pinning service |
| Indexer | PTS holder rankings, Transfer history, COFFEE owners | a subgraph (The Graph) or Ponder |
| Your own server (off-chain) | delivery addresses, notifications, caching | an ordinary backend, never holding users' private keys |
The flow: the frontend reads through RPC or the indexer, the wallet signs transactions, the contracts change state and emit events, the indexer reads those events, and the frontend shows the result.
The checklist from lesson 1 to 17
| # | Lesson slug | What this project uses from it |
|---|---|---|
| 1 | blockchain-fundamentals |
on-chain data is public and permanent, so personal data stays off-chain |
| 2 | ethereum-smart-contracts |
accounts, gas, the local chain 31337 and Sepolia 11155111 |
| 3 | wallet-metamask-integration |
connecting a wallet, switching chains, SIWE login |
| 4 | solidity-basics |
state, events, custom errors in RakaPoints |
| 5 | ethers-js-viem |
clients, calldata, parseUnits('100', 18) |
| 6 | reading-blockchain-data |
eth_call, getLogs, latest vs finalized |
| 7 | sending-transactions |
receipts, a revert still pays gas, simulate first |
| 8 | erc20-token |
the ERC-20 RakaPoints, approve only what is needed |
| 9 | erc721-nft |
CoffeeEditionShirt, _safeMint, SoldOut() at #101 |
| 10 | dapp-frontend-wagmi-react |
the "your PTS" component with wagmi |
| 11 | smart-contract-testing-hardhat |
11 tests, fixtures, coverage |
| 12 | ipfs-decentralized-storage |
the coffee/ metadata folder and its CID in _baseURI |
| 13 | defi-basics-untuk-devs |
not used by this shop yet; needed if PTS goes into an AMM pool (slippage, oracles) |
| 14 | gas-optimization |
measuring gas, the optimizer for deploys |
| 15 | smart-contract-security |
access control, Slither, audits |
| 16 | the-graph-indexing |
a subgraph for Account, Transfer and Shirt |
| 17 | l2-scaling-solutions |
the option of deploying to a cheaper L2 |
The order to build in
- Contracts + tests on the local chain (lessons 4, 8, 9, 11):
npx hardhat testmust be green, failure paths included. - Metadata to IPFS (lesson 12): upload the
coffee/folder, note its CID, write it into_baseURI. - Local deploy + the shop script (lesson 7): the next section.
- Frontend against the local chain (lesson 10), with contract addresses read from the deploy output file.
- Indexer (lesson 16) starting at the deploy block.
- Testnet: Sepolia, or an L2 testnet such as OP Sepolia (lesson 17), then source code verification.
- Mainnet preparation: the section "Before mainnet".
Why IPFS before the deploy? _baseURI in CoffeeEditionShirt is a pure function that returns a fixed string, and a deployed contract can't be edited. A CID you only get after the deploy can't be put in; Raka has to deploy again, at a new address.
Run everything on the local chain
// scripts/deploy.ts: deploy RakaPoints and CoffeeEditionShirt, then save their addresses for scripts, the frontend and the subgraph
import { writeFileSync } from "node:fs";
import { network } from "hardhat";
import { getAddress } from "viem";
const { viem, networkName } = await network.getOrCreate();
const publicClient = await viem.getPublicClient();
const [raka] = await viem.getWalletClients();
const points = await viem.sendDeploymentTransaction("RakaPoints");
const pointsReceipt = await publicClient.waitForTransactionReceipt({ hash: points.deploymentTransaction.hash });
const shirts = await viem.sendDeploymentTransaction("CoffeeEditionShirt");
const shirtsReceipt = await publicClient.waitForTransactionReceipt({ hash: shirts.deploymentTransaction.hash });
console.log(`network ${networkName}, chain ID ${await publicClient.getChainId()}, deployer ${getAddress(raka.account.address)}`);
console.log(`RakaPoints ${points.contract.address} block ${pointsReceipt.blockNumber} gas ${pointsReceipt.gasUsed}`);
console.log(`CoffeeEditionShirt ${shirts.contract.address} block ${shirtsReceipt.blockNumber} gas ${shirtsReceipt.gasUsed}`);
writeFileSync(
`deployments/${networkName}.json`,
JSON.stringify({ RakaPoints: points.contract.address, CoffeeEditionShirt: shirts.contract.address, startBlock: Number(pointsReceipt.blockNumber) }, null, 2),
);
A second script, scripts/shop.ts, reads deployments/localhost.json, then runs one day at the shop: four purchases (earn), COFFEE #1 minted for Dinda and #2 for Yoga, Dinda redeeming 100 PTS, and Yoga trying to redeem 200 through simulate (so a transaction that is sure to fail is never sent). This is real output from Hardhat 3.19.0 on a fresh local chain (public test accounts, no real money):
$ npx hardhat test
11 passing (2 solidity, 9 nodejs)
$ npx hardhat node # another terminal: the local chain 31337
$ npx hardhat run scripts/deploy.ts --network localhost
network localhost, chain ID 31337, deployer 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
RakaPoints 0x5FbDB2315678afecb367f032d93F642f64180aa3 block 1 gas 1229240
CoffeeEditionShirt 0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512 block 2 gas 1999101
$ npx hardhat run scripts/shop.ts --network localhost
block 3 earn(Dinda, 9500) Coffee Print Shirt gas 73204 success
block 4 earn(Yoga, 7500) Plain Black Shirt gas 56092 success
block 5 earn(Yoga, 4000) Raka Cap gas 38992 success
block 6 earn(Dinda, 6500) Coffee Print Mug gas 39004 success
block 7 mint COFFEE #1 for Dinda gas 96559 success
block 8 mint COFFEE #2 for Yoga gas 79447 success
block 9 Dinda redeems 100 PTS gas 35679 success
Error: ERC20InsufficientBalance(address sender, uint256 balance, uint256 needed)
(0x3C44CdDdB6a900fa2b585dd299e03d12FA4293BC, 115000000000000000000, 200000000000000000000)
PTS Dinda 60, Yoga 115, totalSupply 175
COFFEE #1 owned by 0x70997970C51812dc3A010C7d01b50e0d17dc79C8 (Dinda)
tokenURI(1) = ipfs://bafybeicske33e77e3uujpvrkwi6ynpy3dpo7hbkaelixuxkaxfvebus37q/1
Transfer RakaPoints: 5 logs, head block 9
- The addresses match the earlier lessons because a contract address depends only on the deployer and its nonce: Raka's transactions 0 and 1.
redeemburns 100 PTS, sototalSupplydrops from 275 to 175. It costs 35,679 gas, not the 33,946 of the first RakaPoints, because the code differs (OpenZeppelin).- Yoga fails in the simulation, before any gas is spent. In the frontend, the button just shows that message.
- The 5 Transfer logs (4 mints + 1 burn) are what the indexer works from.
Frontend, indexer, and L2
The frontend uses the "your PTS" component from the dapp-frontend-wagmi-react lesson as it is; only the chain list and the addresses change:
import { createConfig, http } from 'wagmi'
import { hardhat, sepolia, optimismSepolia } from 'wagmi/chains'
import { injected } from 'wagmi/connectors'
export const config = createConfig({
chains: [hardhat, sepolia, optimismSepolia],
connectors: [injected()],
transports: { [hardhat.id]: http(), [sepolia.id]: http(), [optimismSepolia.id]: http() },
})
The subgraph from the the-graph-indexing lesson gets one more data source for COFFEE (address 0xe7f1...0512, startBlock: 2, event Transfer(indexed address,indexed address,indexed uint256)) with a Shirt { id, owner, mintedAtBlock } entity; its graph build and Matchstick test pass. For an L2, add an opSepolia network to hardhat.config.ts (the l2-scaling-solutions lesson), run npx hardhat run scripts/deploy.ts --network opSepolia, and deployments/opSepolia.json gets its addresses. Keep addresses per chain ID, not in one constant.
When you need your own server
Contracts suit the rules everyone must trust: who has how many PTS, who owns COFFEE #1. An ordinary (off-chain) server is still needed for:
- Data that doesn't need to be on-chain or mustn't be public. Dinda's delivery address and phone number live in the shop's database, not in a contract.
- Notifications. The server listens for
Earnedevents, then sends a "your PTS went up" message. - Caching and fast queries. Indexer or RPC results are kept for a while so the page is quick.
What it must not do: store users' private keys or sign on their behalf. That is the wallet's job.
Before mainnet
This tutorial stops at testnets. If these contracts ever hold real value:
- Tests and review: failure paths tested, coverage checked, Slither clean, then an audit by an outside party (the
smart-contract-securitylesson). - Source code verification:
npx hardhat verify --network sepolia <address>(the Etherscan API key from the keystore), so anyone can read the code that was really deployed. - The owner key: the key that calls
earnandmintmust not sit in a.envfile on a laptop. Keep it in a hardware wallet, or move ownership to a multisig (Safe, for example) withtransferOwnership. - Separate keys: a testnet key is never used on mainnet, and the other way round.
- Monitoring and a response plan: alerts for odd events, emergency contacts, and a bug bounty.
- If you use an L2: check its stage on L2BEAT and its bridge's risks (the
l2-scaling-solutionslesson).
⚠️ Mistakes you only find after the deploy
- Deploying COFFEE before the metadata is ready.
_baseURIcan't be changed; the CID must exist before the deploy. - A contract address hardcoded for one chain. A frontend that moves to Sepolia still calls the local address. Keep addresses per chain ID from the deploy output file.
- The owner key in a committed
.env. Usenpx hardhat keystore setfor testnets, a hardware wallet or a multisig for mainnet. - Sending a transaction without simulating it. Yoga redeeming 200 PTS would pay gas for a revert; simulate first and show the message.
TL;DR: Raka's Shirt Shop's full dApp is smart contracts (RakaPoints ERC-20 and CoffeeEditionShirt ERC-721) as the trusted backend, a wallet to sign, a React + wagmi frontend, metadata on IPFS, an indexer for rankings and history, and an ordinary server for data that doesn't need to be on-chain, notifications and caching (never holding users' private keys). The order: contracts + tests, IPFS metadata (the CID must exist before the deploy because _baseURI can't change), local deploy + script, frontend, indexer, testnet (Sepolia or an L2 testnet) + verification. On the local chain: 11 tests pass, RakaPoints at 0x5FbD...0aa3, CoffeeEditionShirt at 0xe7f1...0512, Dinda left with 60 PTS after her redeem, Yoga refused with ERC20InsufficientBalance in the simulation. Before mainnet: an audit, source verification, the owner key in a hardware wallet or a multisig.
⚡ One dApp, six pieces
Meaning: Smart contracts hold the trusted rules and data, the wallet signs, the frontend shows, IPFS keeps big files, an indexer sums up the history, and your own server keeps what doesn't need to be on-chain.
⚡ IPFS first, then deploy
Guess first: why must the metadata go to IPFS before CoffeeEditionShirt is deployed?
_baseURI is a fixed string, and a deployed contract can't be edited. A CID you only get after the deploy can't be put in, so Raka would have to deploy again, at a new address.
Meaning: The order: contracts + tests, IPFS metadata, local deploy + script, frontend, indexer, then testnet.
⚡ A day at the shop, on the local chain
Write:
npx hardhat run scripts/deploy.ts --network localhost
npx hardhat run scripts/shop.ts --network localhost
Meaning: Four purchases, COFFEE #1 for Dinda, Dinda redeems 100 PTS and has 60 left, while Yoga is refused with ERC20InsufficientBalance in the simulation, before any gas is spent.
⚡ An ordinary server is still needed
Meaning: For data that doesn't need to be on-chain or mustn't be public (delivery addresses), notifications and caching. The server must never store users' private keys or sign on their behalf.