feat: native Curve StableSwap 2-token pool simulation#557
feat: native Curve StableSwap 2-token pool simulation#557sunce86 wants to merge 1 commit intopropeller-heads:mainfrom
Conversation
4c7d453 to
0a50a6d
Compare
Add a native Rust implementation of the Curve StableSwap invariant for
2-token plain pools (e.g., DAI/USDC, USDS/USDC). This provides ~100x
faster swap simulation compared to the VM/revm fallback.
Implementation:
- Newton-Raphson solvers (get_d, get_y) ported from Curve's Vyper source
- ProtocolSim trait with get_amount_out, spot_price, get_limits, delta_transition
- Decoder for Tycho indexer snapshots (reserve0, reserve1, A, fee attributes)
- 19 unit tests covering math convergence, swaps, roundtrips, symmetry,
delta transitions, decoding, immutability, and split-vs-single behavior
Usage:
.exchange::<CurveStableSwapState>("curve_stableswap", filter, None)
Scope: 2-token plain pools only. MetaPools and Tricrypto can follow in
separate PRs.
0a50a6d to
6222013
Compare
|
Hey team, I want to flag that this PR is premature and incomplete as-is. Since opening it, I've been working on curve-math, a standalone Rust crate that implements the complete math for all Curve pool types (StableSwap, CryptoSwap, MetaPools, Tricrypto, etc.), not just 2-token plain pools. It's currently in alpha while I validate against on-chain results across all pool types and chains. The right approach would be to use curve-math as the foundation and implement the full ProtocolSim trait on top of it, rather than shipping partial coverage that only handles one pool type. I also have a native Curve Tycho substream adapter prepared that is compatible with curve-math and could be contributed alongside it. My question to the team: would you be interested in me contributing this once curve-math moves to beta? I'd like to gauge interest before investing time in the integration work. |
Summary
Native Rust implementation of the Curve StableSwap invariant for 2-token plain pools (e.g., DAI/USDC, FRAX/USDC, USDS/USDC). This replaces the VM/revm fallback for these pool types, providing ~100x faster swap simulation.
Motivation
StableSwap pools are among the highest-volume DeFi pools. The current VM fallback executes Curve bytecode in revm at ~100-200μs per simulation. A native implementation runs at ~900ns (benchmarked in a separate project), enabling significantly better routing performance when evaluating thousands of pool candidates.
Implementation
Math (
math.rs):get_d()— Newton-Raphson solver for StableSwap invariant Dget_y()— Newton-Raphson solver for output balance given input and Dspot_price_raw()— Analytical marginal price from invariant derivative (no simulation needed)State (
state.rs):ProtocolSimimplementation withget_amount_out,spot_price,get_limits,delta_transitionDecoder (
decoder.rs):reserve0,reserve1,A,feefrom indexer attributesall_tokensfor rate computationA()to internal precision (A * A_PRECISION)Verification
On-chain verification against FRAX/USDC pool (
0xDcEF968d416a41Cdac0ED8702fAC8128A64241A2) on Ethereum mainnet:assert_eq!— bit-for-bit identical to on-chainget_dy(), zero difference30 unit tests covering:
Usage
Scope & Limitations
reserve0,reserve1,A,fee). The current Curve substreams module may send raw storage slots for the VM adapter instead. An indexer update to emit parsed attributes would be needed.query_pool_swapnot yet implemented (returns error, consistent with V4/VM)Follow-up
query_pool_swapimplementation for target-price solving