myco-bonding-curve/docs/01-weighted-product.md

65 lines
3.0 KiB
Markdown

# 01: Weighted Constant Product
## Source
- **Protocol**: Balancer V2/V3
- **Files**: `WeightedMath.sol`, `LogExpMath.sol`
- **Repo**: `balancer/balancer-v2-monorepo`, `balancer/balancer-v3-monorepo`
## Rationale for MYCO
The weighted constant product is the **simplest N-asset invariant** and serves as:
1. The baseline against which more sophisticated curves are compared
2. A fallback pricing mechanism when parameters for elliptical curves aren't calibrated
3. The foundation for understanding how invariant-based bonding works before adding concentration, rotation, and tranching
4. Production-proven at scale ($B+ TVL on Balancer)
For MYCO specifically: if the reserve starts with 2-3 assets and grows to N, the weighted product provides immediate N-asset support with intuitive parameter tuning (just set weights).
## Invariant
$$I = \prod_{i=0}^{N} b_i^{w_i}$$
where:
- $b_i$ = balance of token $i$ (all positive)
- $w_i$ = normalized weight of token $i$ ($\sum w_i = 1$, each $w_i \in [0.01, 0.99]$)
**Key property — homogeneous of degree 1:**
$$I(k \cdot b_1, \ldots, k \cdot b_n) = k \cdot I(b_1, \ldots, b_n)$$
This means scaling all balances by $k$ scales the invariant by $k$, which is required for Balancer V3's `BasePoolMath` to compute unbalanced liquidity operations generically.
## Swap Math
**Exact input (compute output):**
$$a_{out} = b_{out} \cdot \left(1 - \left(\frac{b_{in}}{b_{in} + a_{in}}\right)^{w_{in}/w_{out}}\right)$$
**Exact output (compute input):**
$$a_{in} = b_{in} \cdot \left(\left(\frac{b_{out}}{b_{out} - a_{out}}\right)^{w_{out}/w_{in}} - 1\right)$$
**Spot price** of token $i$ in terms of token $j$:
$$p_{ij} = \frac{b_j / w_j}{b_i / w_i}$$
## Parameters
| Parameter | Range | Effect |
|-----------|-------|--------|
| $w_i$ | [0.01, 0.99] | Relative value share. Higher weight = more price-inert to that token's balance changes |
| N tokens | [2, 100] | Number of reserve assets (limited by min weight 1%) |
**MYCO application**: Initial weights could be 50/50 for 2-asset launch, or 80/10/10 for a primary reserve + two satellite assets.
## Properties
- **Convexity**: The level sets (iso-invariant curves) are convex — swaps always move along a convex surface
- **No impermanent loss bounds**: Unlike concentrated LPs, the curve extends to infinity in all directions
- **Self-rebalancing**: As external prices change, arbitrageurs rebalance the pool to match, maintaining value shares equal to weights
- **Slippage**: Proportional to swap size relative to balance — large swaps relative to pool size incur significant price impact
## MYCO Application
In the composed system, weighted product serves as the **outer envelope** of the bonding surface. When reserves are far from target ratios (outside the concentrated region of the ellipsoid), pricing falls back to weighted-product-like behavior. This provides continuous pricing even when the elliptical concentration region is exhausted.
## Implementation
See `src/primitives/weighted_product.py`