Introduction to Stage 4
DeFi Engineering. AMMs, routing, oracles, lending, perpetuals, and MEV—from mathematics to code.
The questions this stage addresses
This stage implements the financial mechanisms from Part A's third stage one by one.
Moving from mathematics to code reveals details invisible at the conceptual level: where fees enter the formula, how precision loss accumulates, why liquidations fall behind rapid price declines, where prices come from, and how much it costs to manipulate them.
The final chapter covers MEV: who sees your transaction before inclusion, and why transaction order itself is a commodity.
Six chapters
What does x·y=k look like in code?
How many layers does one swap pass through?
How does a chain know the outside price?
When must the liquidation engine outrun the market?
How do margin, funding rates, and a risk engine form a system?
Who saw your transaction before it entered a block?
After this stage
You can implement a trading or lending system with slippage protection, fees, and liquidation logic.
Stage artifact: A working AMM and router with comprehensive tests.
Study suggestions
- Read alongside Part A chapters 14–18. One explains why; the other explains how.
- T21 on oracles has the highest concentration of risk in this stage. Most economic attacks begin by manipulating the price source.
- For every mechanism you implement, calculate how much it would cost to attack. This habit becomes essential in stage 5.