Soroswap.Finance Docs
  • Welcome 👋🏼
    • What is Soroswap Finance?
    • Getting Started
      • Wallet Setup and Connection
      • How to Swap
      • Provide Liquidity
      • How the Aggregator Works
  • Concepts
    • AMM
    • Liquidity Pools
    • Swap
    • Fees
    • Slippage
    • Router
    • SDEX
    • Aggregator
    • Trustlines
    • Advanced Topics
      • Pricing
      • Understanding Returns
      • Security
      • Research
  • Soroswap AMM (DEX)
    • How Soroswap AMM works
    • Ecosystem Participants
    • Audits
    • Technical Reference
      • Smart contracts
        • SoroswapPair
        • SoroswapFactory
        • SoroswapRouter
        • SoroswapLibrary
      • Deployed Addresses
      • Error Codes
      • Using Soroswap with TypeScript
      • Smart Contract Integration
      • Deploy Soroswap Yourself
        • Setup your environment.
        • Experiment the Pair contract
        • Experiment the Factory Contract.
        • Deployments.
        • Using the Soroswap Testnet
    • Glossary
  • Soroswap Aggregator
    • Supported AMMs
    • Audits
    • Technical Reference
      • How Soroswap Aggregator works
      • Design
      • Technical Overview
      • Aggregator Operation
      • Smart Contracts
        • SoroswapAggregator
        • Adapter Trait
        • SoroswapAdapter
      • Inspirations
        • 1inch
      • Other AMMs in Soroban
        • Phoenix
    • Disclaimer
  • Swap Route API
  • Soroswap Info
  • Tutorials
    • Installing Freighter
    • Soroswap sections
    • Adding Liquidity
    • Doing Swap
    • Remove Liquidity
    • Using Stellar Classic Assets
      • Wrapping Stellar Classic Assets
      • Swap Stellar Classic Assets
      • Test Stellar Classic Assets
    • Bridge using Pendulum
    • Conclusions
  • Partnerships
    • Collaboration with Mercury and SubQuery
    • Business Partnerships
  • Support & Resources
    • About Us
    • General FAQ
    • Additional Resources
Powered by GitBook
On this page
  • Router Function
  • How It Works
  • Router Methods
  • Token Validation
Edit on GitHub
  1. Concepts

Router

PreviousSlippageNextSDEX

Last updated 7 months ago

The is a key component of the protocol that facilitates interactions with multiple Liquidity Pools are always formed between two tokens, creating what are known as Pairs. For example, a pool might be between Token A and Token B, forming the pair A:B.

Router Function

The Router allows for swaps between two tokens, even if there is no direct pair between them. This is where the Router's flexibility comes into play. Instead of performing a direct swap between Token A and Token C, the Router can find the best route using one or more intermediate pairs. For example:

  • Direct Route: If a direct pair A:C exists, the Router can perform the swap directly between A and C.

  • Indirect Route: If no direct pair is available, the Router can use a multi-hop route, such as A:B followed by B:C.

How It Works

  1. Retrieve Pools: The Router first retrieves data on all available pools involving the tokens you wish to swap. This data is sourced from the backend database.

  2. Calculate Routes: Using the pool data, the Router calculates all possible routes for the swap, considering a maximum number of hops allowed between pairs.

  3. Optimize Route: The Router breaks down possible routes into segments and calculates the optimal swap for each segment. It then combines these routes and validates which offers the best overall exchange rate.

  4. Execute Swap: Finally, the Router selects the best route based on the amount of tokens to be received or sent and executes the swap.

The Router provides two main methods:

  • getPools(tokenIn, tokenOut): Retrieves information on available pools that include the specified tokens.

  • getBestRoute(tokenIn, tokenOut, TradeType, ...options): Calculates and returns the best route for the swap between the specified tokens, considering trade type and additional options.

Token Validation

To ensure security, the Router uses a list of known and validated tokens. This prevents the use of malicious tokens in swap routes. Tokens used in a transaction are checked against this list to ensure they are reliable.

Router in Soroswap Finance
Liquidity Pools.
Router Methods
Page cover image