Section 10 of 10
Ship Your Temporal Function AMM
Final Build
Submit your complete protocol and run the full test suite. Earn the "tfa-builder" badge on completion.
Ship Your Temporal Function AMM
What You Have Built
Over the past four sections, you constructed a complete QuantAMM-style temporal function AMM from scratch. Here is everything in your protocol:
WeightStorage contract. Multi-slot packing system that stores 4 weights per 256-bit storage slot using 32-bit fixed-point encoding. Pack and unpack functions with bit shifting and masking. Normalization with dust collection. Guard rail clamping that limits per-update weight movement. Block-by-block interpolation in getNormalizedWeights() for smooth, MEV-resistant weight transitions.
TimeAdjustments contract. The temporal function engine. EMA calculation with configurable lambda for price smoothing. Block multiplier computation for per-block interpolation. The momentum update rule that computes target weights from price gradients: w_new = w_prev + kappa * (gradient - normFactor). Last interpolation time detection that identifies when the first weight would breach its guard rail.
TFAPool contract. Multi-asset pool supporting 2 to 8 tokens. Weighted product invariant product(balance_i ^ weight_i) = k with a log-exp power function. onSwap() that reads block-interpolated weights for every trade. Three levels of guard rails: absolute weight clamp, velocity constraint (epsilon max) with proportional scaling, and trade size limit. Full performUpdate() flow from prices through to stored multipliers.
Security properties across the system:
- Reentrancy protection on all state-changing functions
- Minimum weight floor (1%) prevents any token from being priced out
- Maximum weight ceiling (99%) prevents single-token concentration
- Block-by-block interpolation prevents sandwich attacks on weight updates
- Velocity constraint prevents multi-block MEV exploitation
- Trade size limits prevent reserve draining in single swaps
- EMA smoothing prevents reaction to single-block price manipulation
- Weight normalization invariant (sum = 1e18) maintained through dust collection
How This Differs from What You Have Seen Before
If you also completed the CFAMM or CLM specialization, notice the key differences:
The CFAMM uses a fixed invariant. Pricing is deterministic given reserves. There is no concept of time in the swap math. The CLM uses fixed tick-based ranges. Liquidity is allocated statically within a range.
The TFA introduces time as a first-class variable. Every swap reads the current block number to interpolate weights. The pool's behavior changes block by block, even without any transactions. This creates a fundamentally different security model: you must reason about multi-block attack vectors, not just single-transaction exploits.
Validation
Paste your complete protocol code below (all three contracts). The test suite verifies that every major component is present: storage packing, EMA smoothing, block multipliers, weighted swap math, velocity constraints, trade limits, guard rails, and reentrancy protection.