Quickstart
A quickstart to swap onchain with Tycho. 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 using Tycho Simulation.
Encode the best trade, for a certain token pair, and simulate or execute it using Tycho Execution.
Want to chat with our docs? Download an LLM-friendly text file of the full Tycho docs.
Run the Quickstart
Clone the Tycho Simulation repository; you'll find the quickstart code there.
To run the quickstart, run the following commands:
If you don't have an RPC URL, you can explore a list of public ones for Ethereum Mainnet and Base.
What it does
The quickstart fetches all protocol states and returns you the best amount out (best price) for a given token pair (by default, 1 WETH to USDC).
Additionally, it returns calldata to execute the swap on this pool with the Tycho Router.
You should see an output like the following:
If you would like to see the results for a different token, amount, or chain, you can set additional flags:
This example would look for the best swap for 10 USDC -> WETH on Base.
Logs
If you would like to see all the Tycho Indexer and Simulation logs run with RUST_LOG=info
:
How the Quickstart works
The quickstart is a minimal example of how to:
Setup and load necessary data (such as available tokens).
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.
Simulate swaps on all available pools for a specified pair (e.g. USDC, WETH), and print out the most USDC you can get for 1 WETH.
Encode a swap to swap 1 WETH against the best pool.
Execute the swap against the Tycho Router.
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 the specified threshold (in ETH). Here, the default is 10,000 ETH to limit the data you pull.
The Indexer stream or the Simulation does not manage tokens; you must manage them.
To simplify this, load_all_tokens
gets all current token information from Tycho Indexer RPC for you.
2. Connect to Tycho Indexer
The protocol stream connects to Tycho Indexer to fetch the real-time state of protocols.
Here, you subscribe to Uniswap V2 and Balancer V2 only. To include additional protocols like Uniswap V3, simply add:
For a full list of supported protocols and which simulation state (like UniswapV3State
) to use for them, see Supported Protocols.
Note: The protocol stream will supply all protocol states in the first BlockUpdate
object. All subsequent BlockUpdates
contain only new and changed protocol states (i.e., deltas).
3. Simulate swap
get_best_swap
uses Tycho Simulation to simulate swaps and calculate buy amounts. We inspect all protocols updated in the current block (i.e. those with balance changes).
a. Simulating token swaps
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
By inspecting each of the amount outs, we then choose the protocol component that gives us the highest amount out.
4. Encode a swap
After choosing the best swap, we can proceed to use Tycho Execution to encode it.
a. Create encoder
b. Create a solution object
Knowing the best protocol component (i.e., pool), we can now put the swap into the expected input format for our encoder. For more info about the Swap
and Solution
models, please have a look here.
c. Encode swap
5. Simulate or execute the best swap
This step allows you to test or perform real transactions based on the best available swap options. To be able to do this step, you need to pass your wallet's private key in the run command. Handle it securely and never expose it publicly.
You'll encounter the following prompt:
You have three options:
Simulate the swap: Tests the swap without executing it on-chain. It will simulate an approval (for permit2) and a swap transaction on the node. You will see something similar to this:
If status is false
, the simulation has failed. You can print the full simulation output for detailed failure information.
execute
: Performs the swap on-chain using your real funds. The process involves performing an approval (for permit2) and a swap transaction. You'll receive transaction hashes and statuses like:
If status is false
, the execution failed. Use the transaction hash on platforms like Etherscan or Tenderly to investigate the failure.
skip
: Ignores this swap and the program resumes listening for blocks.
Market conditions can change rapidly. Delays in making your decision might lead to transaction reverts, especially if you've set parameters like minimum amount out or slippage. Always ensure you're comfortable with the potential risks before executing swaps.
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:
Recap
In this quickstart, you explored how to use Tycho to:
Connect to the Tycho Indexer: Retrieve real-time protocol data filtered by TVL.
Fetch Token and Pool Data: Load all token details and process protocol updates.
Simulate Token Swaps: Compute the output amount, gas cost, and updated protocol state for a swap.
Encode a Swap: Create a solution from the best pool state and retrieve calldata to execute against a Tycho router.
Execute a Swap: Execute the best trade using the Tycho Router.
What's next?
To dive deeper:
Integrate with your Solver: Add Tycho pool liquidity to your solver, starting from this guide.
Learn more about Tycho Execution: Read more about the datatypes necessary to encode an execution against a Tycho router or executor.
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 Tycho indexes.
Last updated