Simulation
Simulate interactions with any protocol.
Last updated
Simulate interactions with any protocol.
Last updated
Tycho Simulation is a Rust crate that provides powerful tools for interacting with protocol states, calculating spot prices, and simulating token swaps.
The repository is available here.
ProtocolSim
is the trait that all protocols need to implement (see definition here).
The main methods of the ProtocolSim
trait are the following:
The spot_price
method retrieves the current spot price between two tokens.
The get_amount_out
method simulates token swaps, returning comprehensive swap details.
The GetAmountOutResult
is defined as follows:
It contains the swap's output amount
, the gas
cost, and the new state
of the protocol after the swap. The new state is particularly useful for performing chained swaps, allowing you to simulate multiple consecutive swaps in the same protocol.
Please refer to the in-code documentation of the ProtocolSim
trait and its methods for more in-depth information.
You can find an example of a price printer using the tycho-simulation
crate in the examples/price_printer
directory of the repository here. This price printer example serves as a deeper way to explore the power of simulation in Ethereum - it’s one of many possible use cases that you can build on top of Tycho simulation.
In order to run it, you must clone the repo, then perform the following actions in your terminal:
You will see the following UI screen appear, with instructions for how to navigate pool prices on the screen. The program prints logs automatically to a file in the logs
directory in the repo.
Simulate interactions with any protocol off-chain using either a virtual machine (VM), or the protocol's native implementation Our simulations are divided into two protocol categories: native protocols and VM protocols.
Native protocols indicate that the protocol's math is ported to Rust. On the other hand, VM protocols execute the protocols bytecode locally, allowing the simulation of protocol actions. Currently, the tycho-simulation
crate focuses on token exchange protocols, but it is not necessarily limited to this.
Currently, the following protocols are implemented natively:
Uniswap V2 and Forks
Uniswap V3
The the following VM protocols are implemented:
Balancer V2
Curve
Interested in adding protocol support? Refer to the Tycho Simulation for DEXs documentation for implementation guidelines.
We have benchmarked how fast it takes to perform one swap on each of the following protocols:
Uniswap V2: ~0.02ms
Uniswap V3: ~0.02ms
Balancer V2: ~0.8ms
Curve: ~1.7ms