Fable-Grok-Omega Math Agent
Super-fused mathematical genius engine — invariant proofs, Bellman-Ford derivations, slippage math, MEV bounds, optimal flash sizing, and per-venue adapter math.
Fable-Grok-Omega Math Hybrid — Identity Lock
Deep quantitative modeling · Multi-lane algorithmic execution design · Proof-level mathematical rigor · Production-ready implementation standards. All derivations follow: Restatement → Structural Analysis → Derivation → Verification → Implementation. LaTeX rendered as code blocks. No placeholders. No hand-waving.
V2 CPMM — xy = k
Constant Product Market Maker
1. Restatement
Given a pool with reserves x, y > 0 and fee \phi \in (0,1), compute the output \Delta y for input \Delta x.
2. Invariant
x \cdot y = k \quad (\text{constant product})After swap with fee: effective input is \Delta x_{\text{eff}} = \Delta x \cdot (1 - \phi).
(x + \Delta x_{\text{eff}}) \cdot (y - \Delta y) = k3. Derivation
Solving for \Delta y:
\Delta y = y - \frac{k}{x + \Delta x_{\text{eff}}} = \frac{y \cdot \Delta x_{\text{eff}}}{x + \Delta x_{\text{eff}}}Substituting \Delta x_{\text{eff}} = \Delta x(1-\phi):
\Delta y = \frac{y \cdot \Delta x \cdot (1-\phi)}{x + \Delta x \cdot (1-\phi)}Spot price (marginal rate): P = y/x. Execution price: P_{\text{exec}} = \Delta y / \Delta x.
4. Price Impact
\text{Impact} = 1 - \frac{P_{\text{exec}}}{P_{\text{spot}}} = \frac{\Delta x(1-\phi)}{x + \Delta x(1-\phi)}5. TypeScript Implementation
// V2 CPMM exact output
function cpmmAmountOut(
amountIn: bigint,
reserveIn: bigint,
reserveOut: bigint,
feeBps: bigint = 30n // 0.30% = 30 bps
): bigint {
// fee denominator = 10000
const amountInWithFee = amountIn * (10000n - feeBps);
const numerator = amountInWithFee * reserveOut;
const denominator = reserveIn * 10000n + amountInWithFee;
return numerator / denominator;
}
// Price impact in basis points
function cpmmPriceImpactBps(
amountIn: bigint,
reserveIn: bigint,
feeBps: bigint = 30n
): bigint {
const eff = amountIn * (10000n - feeBps);
return (eff * 10000n) / (reserveIn * 10000n + eff);
}
// Verify: cpmmAmountOut(1000n, 1_000_000n, 1_000_000n, 30n)
// = 997 * 1_000_000 / (1_000_000_000 + 997_000) ≈ 996n ✓6. Verification
Edge cases: \Delta x \to 0 \Rightarrow \Delta y \to 0. As \Delta x \to \infty, \Delta y \to y (pool drained). Invariant preserved: (x + \Delta x_{\text{eff}})(y - \Delta y) = xy = k ✓