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 .
To use the simulation tools with Ethereum Virtual Machine (EVM) chains, add the optional evm
feature flag to your dependency configuration:
Add this to your project's Cargo.toml
file.
spot_price
returns the pool's current marginal price.
get_amount_out
simulates token swaps.
You receive a GetAmountOutResult
, which is defined as follows:
new state
allows you to, for example, simulate 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.
fee
returns the fee of the protocol as a ratio.
For example if the fee is 1%, the value returned would be 0.01.
get_limits
returns a tuple containing the maximum amount in and out that can be traded between two tokens.
To maintain up-to-date states of the protocols you wish to simulate over, you can use a Tycho Indexer stream. Such a stream can be set up in 2 easy steps:
It is necessary to collect all tokens you are willing to support/swap over as this must be set on the stream builder in step 2. You can either set up custom logic to define this, or use the Tycho Indexer RPC to fetch and filter tokens for you. To simplify this, a util function called load_all_tokens
is supplied and can be used as follows:
The stream created emits BlockUpdate messages which consist of:
block number
- the block this update message refers to
new_pairs
- new components witnessed (either recently created or newly meeting filter criteria)
removed_pairs
- components no longer tracked (either deleted due to a reorg or no longer meeting filter criteria)
states
- the updated ProtocolSim
states for all components modified in this block
The first message received will contain states for all protocol components registered to. Thereafter, further block updates will only contain data for updated or new components.
Note: For efficiency,
ProtocolSim
states contain simulation-critical data only. Reference data such as protocol names and token information is provided in theProtocolComponent
objects within thenew_pairs
field. Consider maintaining a store of these components if you need this metadata.
Clone the repo, then run:
You will see a UI where you can select any pool, press enter, and simulate different trade amounts on the pool.
The program prints logs automatically to a file in the logs
directory in the repo.
The tycho-simulation
package will soon be available on . Until then, you can import it directly from our GitHub repository.
Note: Replace x.y.z
with the latest version number from our . Using the latest release ensures you have the most up-to-date features and bug fixes.
All protocols implement the ProtocolSim
trait (see definition ). It has the main methods:
You can use the to easily set up and manage multiple protocols within one stream. An example of creating such a stream with Uniswap V2 and Balancer V2 protocols is as follows:
For a full list of supported protocols and the simulation state implementations they use, see .
You can find an example of a price printer .
You'll need an RPC to fetch some static protocol info. You can use any RPC provider – e.g. set one up with .