An HTTP service that brokers Tycho protocol streams into on-demand quote simulations.
The server ingests Tycho protocol updates, keeps an in-memory view of pool state, and exposes an HTTP API for requesting swap quotes or checking readiness. It runs on Axum atop Tokio and relies on the tycho-simulation crate for all pricing logic.
- Background ingestion of Tycho protocol streams with TVL-based filtering.
POST /simulateendpoint that returns laddered amount-out simulated quotes with rich metadata.GET /statusendpoint for readiness polling (block height, pool count).- Structured logging with
tracing. - Fully asynchronous execution with Tokio.
- Rust (latest stable toolchain)
- Cargo
- Clone the repository:
git clone https://github.com/dewiz-xyz/tycho-simulation-server.git cd tycho-simulation-server - Configure the environment:
Populate
cp .env.example .env
.envwith your Tycho credentials and optional tuning knobs. - Build the binary:
cargo build --release
The following environment variables are read at startup:
TYCHO_URL– Tycho API base URL (default:tycho-beta.propellerheads.xyz)TYCHO_API_KEY– API key for authenticated Tycho access (required)TVL_THRESHOLD– Minimum TVL (in native units) for adding a pool to the stream (default:300)TVL_KEEP_RATIO– Fraction ofTVL_THRESHOLDused to decide when to keep/remove pools (default:0.2)PORT– HTTP port (default:3000)HOST– Bind address (default:127.0.0.1)RUST_LOG– Logging filter (default:info)QUOTE_TIMEOUT_MS– Wall-clock timeout for an entire quote request (default:75)POOL_TIMEOUT_NATIVE_MS– Per-pool timeout for native integrations (default:5)POOL_TIMEOUT_VM_MS– Per-pool timeout for VM-backed integrations (default:25)REQUEST_TIMEOUT_MS– Request-level guard applied at handler, router adds +250ms headroom (default:1800)TOKEN_REFRESH_TIMEOUT_MS– Timeout for refreshing token metadata from Tycho (default:200)
JSON body:
curl -X POST "http://localhost:3000/simulate" \
-H "Content-Type: application/json" \
-d '{
"request_id": "req-123",
"token_in": "0x6b175474e89094c44da98b954eedeac495271d0f",
"token_out": "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
"amounts": ["1000000000000000000", "5000000000000000000"]
}'Response body:
{
"request_id": "req-123",
"data": [
{
"pool": "uniswapv3-1",
"pool_name": "UniswapV3::DAI/USDC",
"pool_address": "0x...",
"amounts_out": ["999000000", "4985000000"],
"gas_used": [210000, 210000],
"block_number": 19876543
}
],
"meta": {
"status": "ready",
"block_number": 19876543,
"matching_pools": 4,
"candidate_pools": 4,
"total_pools": 412,
"failures": []
}
}QuoteMeta.status communicates warm-up, validation, and partial-failure states—HTTP status codes remain 200 OK.
Timeout behavior:
- Handler-level timeout returns
200 OKwithPartialFailure, includesrequest_id,block_number,total_pools, and aTimeoutfailure. - Router-level timeout (rare fallback, applied only to
/simulate) returns200 OKwithPartialFailure,request_idmay be empty,block_numberis0, andtotal_poolsis unset. Logs includescope="router_timeout"./statusis not subject to router-level timeouts.
Returns readiness information for health checks and pollers:
{
"status": "ready",
"block": 19876543,
"pools": 412
}If the service is still ingesting initial state the endpoint responds with 503 Service Unavailable and "status": "warming_up".
Launch the server after exporting the necessary environment variables:
cargo run --releaseThe application binds to the configured HOST:PORT and begins streaming protocol data before serving HTTP requests.
tycho-simulation– protocol stream and simulator clienttokio– asynchronous runtimeaxum– HTTP router and extractorsserde/serde_json– serializationanyhow– error propagationtracing&tracing-subscriber– structured logging
src/api– router wiring for public endpointssrc/config– environment loading and logging setupsrc/handlers– HTTP handlers (quote,status) and stream ingestersrc/models– request/response models and shared statesrc/services– stream builder and quote enginesrc/main.rs– application entrypoint
MIT