Simulation
To enable simulations for a newly added protocol, it must first be integrated into the Tycho Simulation repository. Please submit a pull request to the repository to include it.
Native Integration
In order to add a new native protocol, you will need to complete the following high-level steps:
Create a protocol state struct that contains the state of the protocol, and implements the
ProtocolSim
trait (see here).Create a tycho decoder for the protocol state: i.e. implement
TryFromWithBlock
forComponentWithState
to your new protocol state.
Each native protocol should have its own module under tycho-simulation/src/evm/protocol
.
VM Integration
To create a VM integration, provide a manifest file and an implementation of the corresponding adapter interface. Tycho Protocol SDK is a library to integrate DEXs and other onchain liquidity protocols into Tycho.
Example Implementations
The following exchanges are integrated with the VM approach:
Balancer V2 (see code here)
Install prerequisites
Install Foundry, start by downloading and installing the Foundry installer:
then start a new terminal session and run
Clone the Tycho Protocol SDK:
Install dependencies:
Understanding the ISwapAdapter
Read the documentation of the Ethereum Solidity interface. It describes the functions that need to be implemented and the manifest file.
Additionally, read through the docstring of the ISwapAdapter.sol interface and the ISwapAdapterTypes.sol interface, which defines the data types and errors the adapter interface uses. You can also generate the documentation locally and look at the generated documentation in the ./docs
folder:
Implementing the ISwapAdapter interface
Your integration should be in a separate directory in the evm/src
folder. Start by cloning the template directory:
Implement the ISwapAdapter
interface in the ./evm/src/<your-adapter-name>.sol
file. See Balancer V2 implementation for reference.
Testing your implementation
Set up test files:
Copy
evm/test/TemplateSwapAdapter.t.sol
Rename to
<your-adapter-name>.t.sol
Write comprehensive tests:
Test all implemented functions.
Use fuzz testing (see Foundry test guide, especially the chapter for Fuzz testing)
Reference existing test files:
BalancerV2SwapAdapter.t.sol
Configure fork testing (run a local mainnet fork against actual contracts and data):
Set
ETH_RPC_URL
environment variableUse your own Ethereum node or services like Infura
Run the tests with
Add implementation to Tycho simulation
Once you have the swap adapter implemented for the new protocol, you will need to:
Generate the adapter runtime file by running the
evm/scripts/buildRuntime.sh
script in our SDK repository with the proper input parameters. For example, in order to build theBalancer V2
runtime, the following command can be run:Add the associated adapter runtime file to
tycho-simulations/src/protocol/vm/assets
. Make sure to name the file according to the protocol name used by Tycho Indexer in the following format:<Protocol><Version>Adapter.evm.runtime
. For example:vm:balancer_v2
will beBalancerV2Adapter.evm.runtime
. Following this naming format is important as we use an automated name resolution for these files.
Filtering
If your implementation does not support all pools indexed for a protocol, you can create a filter function to handle this. This filter can then be used when registering an exchange in the ProtocolStreamBuilder
. See here for example implementations.
Last updated