Run the stack
About 2408 wordsAbout 8 min
Week 5 · Part 1 — Run the stack
Core question — what actually happens between typing a command and seeing a value from the blockchain?
In Week 3 you wrote, deployed and called a contract inside Remix. Remix did a lot for you: it held the code, compiled it, picked a network and showed you buttons.
No programming background beyond Foundation is assumed. From now on you will work with code in a folder on your laptop, tools in a terminal and scripts that talk to the chain. This Part introduces those tools and shows how their pieces connect; you do not need to arrive comfortable with a terminal or JavaScript.
Picture a restaurant kitchen
Remix is a meal kit: everything arrives measured and boxed. A real kitchen has separate stations (prep, stove, pass) and you need to know which one does what.
Where the picture stops: a kitchen keeps working if you forget where the salt is. A project does not. One missing setting, such as the network address in .env, stops everything, so you learn exactly where each setting is read.
The hard skill
Run a real Web3 project locally, and trace how a script gets from your terminal to a value on the chain.
After this Part you can:
- clone, install, compile and test a Foundry + TypeScript project;
- run a TypeScript script that reads live Sepolia data;
- point to the file and line where the RPC address enters the code;
- explain why a setting lives in
.envand not in the code.
Core / reference material
Before you run the stack
These names identify the stations you are about to connect. This is an orientation, not a second installation guide.
Time note: The 35-minute Part estimate assumes the required tools are already installed. First-time Node.js, Foundry or Windows/WSL setup may take additional time. It is one-time environment setup, not required task work.
| Tool or file | What it does here |
|---|---|
| Terminal | Runs text commands inside a current folder on your laptop. A command is what you type; output is what the tool prints back. |
| Git | Tracks project changes and shares them through GitHub. The Git CLI is its terminal-based form. |
| Node.js | Runs JavaScript and TypeScript tools on your laptop. |
| npm | Installs JavaScript packages and runs project shortcuts. |
package.json | Lists project packages and the shortcuts available through npm run. |
| TypeScript | JavaScript with optional type labels that help tools catch mistakes. |
Foundry / forge | Foundry is the Solidity toolkit; forge compiles and tests contracts. |
| viem | A TypeScript library that talks to Ethereum through an RPC endpoint. |
| Vite | Runs and builds the React frontend. |
.env | A local file for settings such as an RPC URL; it is not committed. |
| RPC endpoint | The network address a program uses to ask a blockchain node for data or submit a request. |
| Application Binary Interface (ABI) | The machine-readable description of a contract's functions and events, including how inputs and results are encoded. |
Terminal: command, folder, output
Commands act in the current folder, the directory your terminal is currently working in. Use these to check or move between folders:
pwd # show the current folder
ls # list its files
cd academy-builder-starter # move into the project folder
cd .. # move up one folderThe first command assumes you kept Git's default clone-folder name. If you chose a different name when cloning, move into that folder instead.
If a command starts a server and keeps the terminal occupied, press Ctrl+C to stop that process. On Windows, use Microsoft's official WSL installation guide to set up the Linux-compatible terminal environment these tools expect.
Git CLI: the terminal form of GitHub collaboration
These commands cover the operations used in Builder; you do not need advanced Git:
| Command | Meaning |
|---|---|
git clone --recurse-submodules <url> | Copy the project and its test-library submodule to your laptop. |
git status | Show which files changed. |
git switch -c <name> | Create and move to your own branch. |
git fetch | Download the latest remote history without merging it into your branch. |
git add <file> | Select a changed file for the next saved change. |
git commit -m "message" | Save the selected change in your local history. |
git push -u origin <name> | Upload your branch to GitHub. |
You can clone a public GitHub repository and run it locally without a GitHub account or WCB. Create an account when you need to push your own branch or repository, or publish project evidence. If you skipped the optional Week 0 setup, set up an account before your first GitHub push.
The same action crosses several of these stations. The visual below is an explanatory map of the path; the later file trace gives the exact code.
In plain English: npm finds the shortcut, tsx starts the script, the script uses viem to form a request, the RPC endpoint carries it to a Sepolia node, and the node answers from the contract's current state. The response then travels back through the same client and is printed by the script. A failure at any station is useful evidence about which layer to inspect next.
The starter, folder by folder
The Builder starter is the project you use for all of Week 5 and Week 6.
| Folder or file | What lives there | Tool that uses it |
|---|---|---|
contracts/src/ | The Solidity contract (Registry.sol) | Foundry (forge) |
contracts/test/ | Foundry tests | forge test |
out/ | Compiled contract and ABI, created by forge build | Scripts, export step |
shared/contract.ts | Address, chain and ABI in one file, generated from out/ | Scripts and frontend |
scripts/ | TypeScript scripts that talk to Sepolia | tsx, run by npm run … |
src/ | The React frontend | Vite (npm run dev) |
.env | Your local settings, such as RPC_URL | Read by scripts/client.ts |
package.json | The npm run shortcuts | npm |
Four commands you will use every day
| Command | What it does |
|---|---|
npm run compile | forge build, then exports the ABI into shared/contract.ts |
forge test | Runs the Solidity tests on a local, throwaway chain. No Sepolia, no gas |
npm run read | Runs scripts/read.ts against Sepolia |
npm run dev | Starts the frontend on your laptop |
Open package.json and find the "scripts" section. Every npm run x is a shortcut for the command written next to x. When something breaks, that is the first place to look.
Settings live in .env
.env is configuration, not magic. When scripts/client.ts starts, it explicitly calls process.loadEnvFile('.env'). Node then loads the file's NAME=value entries into the process environment, and the script reads process.env.RPC_URL. Node does not automatically load .env in every project; this starter chooses to load it explicitly. An RPC address can contain a private API key, and different people use different providers, so it is never written into the code. Instead:
.env.exampleis committed and shows which settings exist.- You copy it to
.envand fill in your own values. .gitignorehelps prevent an untracked.envfrom being added through normal Git workflows. It does not erase a secret that was already committed, and a file can still be force-added.scripts/client.tsexplicitly loads the file and reads the value when it starts.
Some values are sensitive. .env.example documents names and safe defaults; it must not contain your private key or a provider credential.
Programming survival kit
You do not need to memorise syntax or become a TypeScript developer. You need to follow where data comes from, which function is called, what goes in, what comes back and where an error is handled. For example:
const learner = { name: 'Ari' }
function greeting(name: string): string {
if (!name) return 'Hello'
return `Hello, ${name}`
}
const message = greeting(learner.name)
console.log(message)- A variable (
learner) stores a value. An object groups named values;learner.namereads itsnameproperty. - A function is reusable behaviour. Calling
greeting(...)passes an argument (learner.name) into its parameter (name); the return value is the result it gives back.ifchooses what to do when a condition is true. : stringis a TypeScript type label saying this value should be text.- An array is an ordered list.
.map()is a common way to turn each item into a displayed row; you will recognise it in the frontend Parts. importuses code another file exports.asyncmarks a function that can wait for a slow operation;awaitwaits for it.try … catchhandles an error rather than letting it stop the whole script.
You only need to read and adapt these patterns. For npm run read, for example, follow the address and RPC into the function, see what it returns and notice which catch reports a failure. Ask AI to explain unfamiliar syntax, then run and verify the result.
Worked example: trace npm run read
Run it first, from the repository root, after cp .env.example .env:
npm run readNetwork: Sepolia (11155111)
Contract: 0xf3eea9aa5a43846490a638f1b2bebb29ac2938b1
Owner: 0xE234F67EaB4638Cb6C7f93D1bfcD0bb9DCE09252
Record read from Sepolia: Hello from NTU Blockchain Builder LabThis is illustrative output from a checked run, not a permanent promise about the owner's record. The contract address and network are the useful fixed context; live state can change.
Now follow it through the files.
1. package.json maps read to tsx scripts/read.ts. tsx runs a TypeScript file directly.
2. scripts/read.ts starts with two imports:
import { address, abi, chain } from '../shared/contract.js'
import { client, requireSepolia } from './client.js'So the address, ABI and chain come from the generated shared/contract.ts, and the connection comes from client.ts.
3. scripts/client.ts is where the settings enter:
if (existsSync('.env')) process.loadEnvFile('.env')
const rpcUrl = process.env.RPC_URL
if (!rpcUrl) throw new Error('Set RPC_URL in .env first (copy .env.example).')
export const client = createPublicClient({ chain, transport: http(rpcUrl) })It loads .env, takes RPC_URL, and builds a viem public client: an object that sends read requests to that RPC.
4. Back in read.ts, main() uses that client in order: requireSepolia() asks the RPC for its chain ID, getCode checks a contract exists at the address, then two readContract calls read owner and that owner's records entry. Each one is a request to the RPC, and each answer comes from Sepolia's current state.
Prove it by breaking it. Empty RPC_URL in .env and run again:
/…/scripts/client.ts:7
if (!rpcUrl) throw new Error('Set RPC_URL in .env first (copy .env.example).')
Error: Set RPC_URL in .env first (copy .env.example).The error names the exact file and line where the setting is read. Now set RPC_URL to an Ethereum mainnet RPC such as https://ethereum-rpc.publicnode.com:
RPC_URL must point to Ethereum Sepolia (11155111).The connection worked, but requireSepolia() caught the wrong network before any contract read. Put the Sepolia value back before you continue.
The execution path in one line: npm run read → tsx → scripts/read.ts → imports shared/contract.ts (address, ABI, chain) and scripts/client.ts (reads RPC_URL from .env) → viem → RPC → Sepolia → answer printed.
Hands-on task
- Run the stack. Clone the starter (follow its README), then run
npm run compile,forge testandnpm run read. - Read one more value. Add one line to
scripts/read.tsthat prints a new piece of live Sepolia data, for example the latest block number or the owner's ETH balance. Use a function from the viem public client. Run it and commit the change. - Explain the path. In five to eight sentences, explain what happens when you run
npm run read, from the command to the printed value. Name the file and line whereRPC_URLenters, and say why it is in.envrather than in the code.
Expected result
forge test compiles the Solidity contract and reports 6 passed, 0 failed. These tests run locally on a throwaway chain; they do not send a Sepolia transaction or use test ETH. Timing and other terminal formatting may differ.
Out of scope: deploying anything, changing the contract and changing the frontend. Those come in Parts 2 to 4.
If something fails
Read the error first. Most first-run problems are the wrong Node version (the starter needs Node 22: run nvm use), a missing .env, or forgetting --recurse-submodules when cloning (fix it with git submodule update --init --recursive).
Evidence required
- Terminal output of
forge testand of your changednpm run read. - A link to your commit that adds the new line.
- Your short explanation of the execution path.
Completion and revision
This Part is worth 100 points in the Academy progression. Academy participants submit it for a binary Complete or Revise review against the criteria above. Independent readers may use those criteria as a self-check; formal points are tracked through organised Academy participation. There is no partial-score rubric.
Further exploration — optional, not assessed
- Open the browser developer tools while
npm run devis running and compare how the frontend reaches its settings with how the script does. - Run
forge test -vvvvand read the trace of one test. - Swap the public RPC in
.envfor one from a provider account, and compare the speed ofnpm run read.
Sources and attribution
- Foundry Book — Projects — Link, referenced only
- viem — Getting started — Link, referenced only
- Node.js — process.loadEnvFile — Link, referenced only
- Blockchain@NTU Academy Builder starter — Reuse (MIT), code excerpts from
scripts/read.tsandscripts/client.ts
Changelog
f0652-docs(curriculum): migrate Academy to 10-week structure (#34)on4fee9-docs(curriculum): close prerequisite gaps across Foundation and Deep Dive (#33)on69ce3-docs(builder): improve Deep Dive teaching clarity and engineering guidance (#31)one1f77-docs(track): add Builder W5 Parts 1 to 4 and 6 (#27)on