myco-bonding-curve/docs/04-elliptical-clp.md

81 lines
3.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 04: Elliptic Concentrated Liquidity Pool (E-CLP)
## Source
- **Protocol**: Gyroscope
- **Files**: `GyroECLPMath.sol` (53KB), `eclp_float.py`, `eclp_prec_implementation.py`
- **Repos**: `gyrostable/gyro-pools`, `balancer/balancer-v3-monorepo`
- **Paper**: `gyrostable/technical-papers/E-CLP/E-CLP Mathematics.pdf`
## Rationale for MYCO
The E-CLP is the most mathematically sophisticated AMM curve in production. It adds two degrees of freedom beyond the 2-CLP:
1. **Rotation (φ)**: Tilts the price curve, useful when the target price ratio isn't 1:1
2. **Stretching (λ)**: Controls how elongated the ellipse is — higher λ = more concentrated near the peg, like a flattened oval
For MYCO:
1. The A-matrix transformation is the **key abstraction** that generalizes to N dimensions
2. Rotation handles non-unit price targets (e.g., ETH/USDC at $3000)
3. Stretching controls how much slippage increases as reserves deviate from target
4. Production-tested on Balancer V3 with formal security review and technical paper
5. The 5 parameters (α, β, c, s, λ) provide fine-grained curve shaping
## Invariant
$$|A(v - \text{offset})|^2 = r^2$$
where:
- $v = (x, y)$ are reserve balances
- $\text{offset} = (a, b)$ are virtual offsets (functions of $r$)
- $A$ is the 2×2 transformation matrix mapping ellipse → unit circle
- $r$ is the scalar invariant (liquidity parameter)
**A-matrix:**
$$A = \begin{pmatrix} c/\lambda & -s/\lambda \\ s & c \end{pmatrix}$$
**5 Parameters:**
| Param | Meaning | Range |
|-------|---------|-------|
| $\alpha$ | Lower price bound | (0, 1) typically |
| $\beta$ | Upper price bound | (1, ∞) typically |
| $c$ | $\cos(-\phi)$ — rotation | [0, 1] |
| $s$ | $\sin(-\phi)$ — rotation | [0, 1] |
| $\lambda$ | Stretching factor | [1, 10^8] |
Constraint: $c^2 + s^2 = 1$
**Derived parameters** (computed once, stored as immutables):
- $\tau(\alpha), \tau(\beta)$ — unit circle endpoints at 38-decimal precision
- $u, v, w, z$ — decomposition of $A \cdot \chi$ (center direction)
- $d_{Sq}$ — error correction for $c^2 + s^2$
## Swap Math
Given new x, solve for y on the ellipse via quadratic:
$$(A_{01}^2 + A_{11}^2) \cdot v^2 + 2(A_{00} A_{01} + A_{10} A_{11}) \cdot u \cdot v + (A_{00}^2 + A_{10}^2) \cdot u^2 = r^2$$
where $u = x - a$, $v = y - b$, solved with standard quadratic formula.
## Properties
- **Elliptical iso-invariant curves**: Level sets are ellipses (not circles or hyperbolas)
- **Concentrated + rotated**: Liquidity concentrated along the ellipse's long axis
- **Homogeneous degree 1**: $r(k \cdot v) = k \cdot r(v)$ — compatible with BPT math
- **Degenerates to 2-CLP**: When $\lambda = 1$ and $\phi = 0$, the ellipse becomes a circle
## MYCO Application
The E-CLP is the **2-asset specialization** of the full MYCO bonding surface. For any pair of reserve assets within the N-D surface, the local geometry is essentially an ellipse — the N-D surface is an N-D generalization of this.
The A-matrix pattern is the key insight: by encoding geometry as a linear transform, we can:
1. Compute invariants efficiently (quadratic formula, not iterative)
2. Parameterize curves with intuitive knobs (rotation, stretch)
3. Generalize to N dimensions naturally (N×N matrix)
## Implementation
See `src/primitives/elliptical_clp.py`