Tycho Quickstart

A quickstart to simulate token swaps. By the end of this quickstart, you will:

  • Fetch real-time market data from Tycho Indexer.

  • Simulate swaps between token pairs to calculate spot prices and output amounts.

This quickstart is a step-by-step introduction to get you up and running with Tycho Simulation, enabling you to build more advanced solutions for your solver.

Run the Quickstart

Clone the Tycho Simulation repository; you'll find the quickstart there.

To run the quickstart, run the following commands:

export RPC_URL=<your-eth-rpc-url>
cargo run --release --example quickstart

If you don't have an RPC URL, here is a list of public ones.

What it does

The quickstart fetches all protocol state and simulates a swap of 1 USDC to WETH on a Uniswap v2 pool.

You should see an output like the following:

==================== Received block 21422546 ====================
Using only pools that were updated this block...
Calculations for pool "0xb4e16d0168e52d35cacd2c6185b44281ec28c9dc" with tokens "USDC/WETH"
Spot price "USDC/WETH": 0.0002501568463534588
Amount out for trading 1 "USDC" -> "WETH": 249406369008335 (takes 120000 gas)

Logs

If you would like to see all the Tycho Indexer and Simulation logs run:

RUST_LOG=info cargo run --release --example quickstart

How the Quickstart works

The quickstart is a minimal example of how to:

  1. Setup and load necessary data.

  2. Connect to the Tycho Indexer to fetch on-chain protocol data (e.g., Uniswap V2, Balancer V2) and build a Protocol Stream that streams updates (like new pools, states) in real-time.

  3. Simulate price and swap (incl. gas) on the fetched state (swapping 1 USDC to WETH on a Uniswap v2 pool)

1. Set up

To run Tycho Indexer, set up the following environment variables:

  • URL (by default "tycho-beta.propellerheads.xyz")

  • API key (by default, the test key is sampletoken)

  • TVL threshold: A filter to only get snapshot data for pools with TVL greater than a specified threshold (in ETH). Here the default is 10,000 ETH to limit the data pulled.

The Indexer stream or the Simulation does not manage tokens; the user must manage them. To simplify this, we provide the load_all_tokens function that queries the Tycho Indexer RPC and retrieves all token information at the current moment.

2. Connect to Tycho Indexer

The protocol stream connects to Tycho Indexer to fetch the real-time state of protocols.

let mut protocol_stream = ProtocolStreamBuilder::new(&tycho_url, Chain::Ethereum)
    .exchange::<UniswapV2State>("uniswap_v2", tvl_filter.clone(), None)
    .exchange::<EVMPoolState<PreCachedDB>>(
        "vm:balancer_v2",
        tvl_filter.clone(),
        Some(balancer_pool_filter),
    )
    .auth_key(Some(tycho_api_key.clone()))
    .set_tokens(all_tokens.clone())
    .await
    .build()
    .await
    .expect("Failed building protocol stream");

Here you are subscribing to Uniswap V2 and Balancer V2 only. To include additional protocols like Uniswap V3, simply add:

.exchange::<UniswapV3State>("uniswap_v3", tvl_filter.clone(), None)

3. Simulate price and swap

Within the print_calculations function, we use Tycho Simulation to calculate spot prices and simulate swaps. To keep things simple, we limit this to the first 10 protocols updated in the current block (i.e. those with balance changes).

a. Calculating spot price

let spot_price = state.spot_price(&tokens[0], &tokens[1])

b. Simulating token swaps

let result = state.get_amount_out(amount_in, &tokens[0], &tokens[1])

result is a GetAmountOutResult that has information on the amount out, gas cost and the new state of the protocol. For example, if you want to do another swap after this one you could do

let other_result = result.new_state.get_amount_out(other_amount_in, &tokens[0], &tokens[1])

Run this quickstart independently

If you want to run this quickstart outside of the Tycho Simulation repository, please add to your project's Cargo.toml the following requirements:

tycho-simulation = { git = "https://github.com/propeller-heads/tycho-simulation.git", package = "tycho-simulation", features = ["evm"] }
tokio = "1.42.0"
futures = "0.3.31"
tracing-subscriber = "0.3.19"
num-bigint = "0.4.6"

Recap

In this quickstart, you explored how to use Tycho to:

  1. Connect to the Tycho Indexer: Retrieve real-time protocol data filtered by TVL.

  2. Fetch Token and Pool Data: Load all token details and process protocol updates.

  3. Calculate Spot Prices: Determine the current price between token pairs based on protocol states.

  4. Simulate Token Swaps: Compute the output amount, gas cost, and updated protocol state for a swap.

What's next?

To dive deeper:

  • Integrate with your Solver: Add more liquidity to your solver, starting from this guide.

  • Learn more about Tycho Simulation: Explore advanced features like custom filters, protocol-specific simulations, and state transitions.

  • Explore Tycho Indexer: To add or modify the data Tyho indexes or build custom tooling than swap simulations on it.

Last updated