Tycho
  • Quickstart
  • Overview
  • Motivation
  • Concepts
  • How to Contribute
    • Bounties
  • For Solvers
    • Indexer
      • Tycho RPC
      • Tycho Client
        • Binary / CLI
        • Rust Client
        • Python Client
    • Simulation
    • Execution
      • Encoding
      • Executing
      • Contract Addresses
      • Execution Venues
    • Hosted Endpoints
    • Supported Protocols
  • For DEXs
    • Protocol Integration
      • Indexing
        • 1. Setup
        • 2. Implementation
        • 3. Testing
          • How to Run
        • Common Problems & Patterns
          • Tracking Components
          • Tracking Contract Storage
          • Normalizing relative ERC20 Balances
          • Tracking Contract Balances
          • Custom protobuf models
        • Best Practices
        • Reserved Attributes
      • Simulation
        • Ethereum: Solidity
      • Execution
        • Code Architecture
      • Contributing guidelines
Powered by GitBook
On this page
  • Native Integration
  • VM Integration
  • Example Implementations
  • Install prerequisites
  • Understanding the ISwapAdapter
  • Implementing the ISwapAdapter interface
  • Testing your implementation
  • Add implementation to Tycho simulation
  • Filtering
Export as PDF
  1. For DEXs
  2. Protocol Integration

Simulation

PreviousReserved AttributesNextEthereum: Solidity

Last updated 3 months ago

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 to include it.

Native Integration

In order to add a new native protocol, you will need to complete the following high-level steps:

  1. Create a protocol state struct that contains the state of the protocol, and implements the ProtocolSim trait (see ).

  2. Create a tycho decoder for the protocol state: i.e. implement TryFromWithBlock for ComponentWithState 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. 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 )

Install prerequisites

  1. Install , start by downloading and installing the Foundry installer:

    curl -L https://foundry.paradigm.xyz | bash

    then start a new terminal session and run

    foundryup
  2. Clone the Tycho Protocol SDK:

    git clone https://github.com/propeller-heads/tycho-protocol-lib
  3. Install dependencies:

    cd ./tycho-protocol-lib/evm/
    forge install

Understanding the ISwapAdapter

cd ./evm/
forge doc

Implementing the ISwapAdapter interface

Your integration should be in a separate directory in the evm/src folder. Start by cloning the template directory:

cp ./evm/src/template ./evm/src/<your-adapter-name>

Implement the ISwapAdapter interface in the ./evm/src/<your-adapter-name>.sol file. See Balancer V2 implementation for reference.

Testing your implementation

  1. Set up test files:

    • Copy evm/test/TemplateSwapAdapter.t.sol

    • Rename to <your-adapter-name>.t.sol

  2. Write comprehensive tests:

    • Test all implemented functions.

    • Reference existing test files: BalancerV2SwapAdapter.t.sol

  3. Configure fork testing (run a local mainnet fork against actual contracts and data):

    • Set ETH_RPC_URL environment variable

  4. Run the tests with

    cd ./evm
    forge test

Add implementation to Tycho simulation

Once you have the swap adapter implemented for the new protocol, you will need to:

  1. >>> cd evm
    >>> ./scripts/buildRuntime.sh -c “BalancerV2SwapAdapter” -s “constructor(address)” -a “0xBA12222222228d8Ba445958a75a0704d566BF2C8”

  2. 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 be BalancerV2Adapter.evm.runtime. Following this naming format is important as we use an automated name resolution for these files.

Filtering

Read the documentation of the interface. It describes the functions that need to be implemented and the manifest file.

Additionally, read through the docstring of the interface and the 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:

Use fuzz testing (see , especially the chapter for )

Use your own Ethereum node or services like

Generate the adapter runtime file by running the script in our SDK repository with the proper input parameters. For example, in order to build the Balancer V2 runtime, the following command can be run:

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 for example implementations.

repository
here
Tycho Protocol SDK
here
Foundry
Ethereum Solidity
ISwapAdapter.sol
ISwapAdapterTypes.sol
Foundry test guide
Fuzz testing
Infura
evm/scripts/buildRuntime.sh
here