# M10 — ABI and Contract Calls: hands-on lab

## 1. Bài toán
A token balance is not accessible by reading an ordinary database.

## 2. Vấn đề
A naïve implementation may silently return stale/wrong-chain data or double-process blockchain state.

## 3. Giải pháp
Build eth_call calldata using the ERC-20 balanceOf selector and decode 32-byte ABI integers.

## 4. Lý thuyết
Separate blockchain protocol data from business state. Chain ID distinguishes networks; ABI encodes calls; transaction nonce is per sender; event logs can be duplicated or orphaned; service validation belongs at boundaries.

## 5. Lab chi tiết
1. Open `solution/Lab.java` and identify inputs, validation, and assertions.
2. Run `bash solution/run.sh` with Java 21 and `javac` installed.
3. Copy the specification into `starter/Lab.java` and implement independently.
4. Add a failing/negative case and rerun.
5. For real RPC usage, configure `RPC_URL` through environment variables; never commit keys.

## 6. Expected output
A single `PASS ...` line and exit code 0. No external network needed.

## 7. Exercises
- Add invalid input and provider failure tests.
- Explain which state belongs on chain vs PostgreSQL.
- Add one recovery strategy for process restarts.

## 8. Interview
Q: What does this lab simulate vs production?
A: It tests one narrow boundary offline. Production needs real Web3j signing, persistent nonce reservations, RPC retries, finality policy, and secrets management.
