# M08-L04 — Detailed Lab: Collateral and liquidation

## Objective
Simulate collateral ratios. Final observable result: **Undercollateralized position liquidates**.

## Environment
Java 21/Maven for Java simulations; Foundry/Anvil for Solidity/EVM; BSC Testnet only for explicitly network-oriented lessons. No real funds.

## Steps
1. Read the theory and define an invariant that must never be violated.
2. Create a new working directory under `starter/`; record tool versions and configuration in `lab-notes.md`.
3. Implement **Simulate collateral ratios** using the relevant runtime. Separate domain state from network IO. Avoid hidden global state and unbounded retries.
4. Write a positive-path test that demonstrates: **Undercollateralized position liquidates**.
5. Write a negative test: invalid input, insufficient balance, wrong nonce/chain, unauthorized caller, or duplicate event (choose what applies).
6. Add one failure-injection test: RPC timeout, revert, restart, competing write, or stale block (choose what applies).
7. Run the appropriate command: `mvn test` for Java, `forge test -vvv` for Solidity; save real stdout to `results/test-output.txt`.
8. Explain why observed state is correct and how it differs from the naive design. Document reproducible steps and test seed where applicable.

## Acceptance criteria
- The implementation is deterministic in local tests.
- The happy path and at least two negative paths have assertions.
- **Undercollateralized position liquidates** is demonstrated by actual logs/assertions.
- No secrets or real private keys are committed.

## Submission
Source code, automated tests, `lab-notes.md`, captured output, and a short architecture explanation.

> This is a guided lab specification. For runnable reference code, consult `labs/` and `IMPLEMENTATION_STATUS.md`.
