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
  • Aggregator Initialization
  • Token Swap Process
  • Distribution Calculation
  • Administrative Functions
  • Adapter Interface and Protocols
Edit on GitHub
  1. Soroswap Aggregator
  2. Technical Reference

Aggregator Operation

PreviousTechnical OverviewNextSmart Contracts

Last updated 8 months ago

Aggregator Initialization

When the contract is deployed, the user who initializes it becomes the protocol administrator. During initialization, compatible underlying protocols are registered in the contract's storage, as shown in Figure 1.

Token Swap Process

The Aggregator facilitates token swaps through two primary functions:

  • swap_exact_tokens_for_tokens (when you want to have an exact input)

  • swap_tokens_for_exact_tokens (when you want ot have an exact output)

These functions require users to specify:

  • The input token (token_in).

  • The output token (token_out).

  • The amounts involved.

  • A distribution vector (DexDistribution).

This vector contains information on how the swap should be split across different protocols and the paths to be followed for the operation.

Distribution Calculation

To calculate the amounts to be swapped in each protocol, the calculate_distribution_amounts function is used. The mathematical formula applied is:

This formula calculates the amount to be swapped for each protocol (i), where:

  • total_amount is the total amount to be swapped.

  • total_parts is the sum of all parts specified in DexDistribution.

This formula ensures that the total amount to be swapped is correctly distributed among the selected protocols.

Administrative Functions

The Aggregator includes administrative functions that allow management of the contract, such as:

  • Initializing the contract.

  • Registering, pausing, and removing protocols.

  • Modifying the administrator address.

These functions ensure that the administrator has control over the protocols used in swaps and that only trusted protocols are utilized.

Adapter Interface and Protocols

The Aggregator communicates with protocols via adapters, which implement the SoroswapAggregatorAdapterTrait interface. This interface defines standard functions such as:

  • initialize: Sets up the adapter with necessary information.

  • swap_exact_tokens_for_tokens: Executes swaps specifying the minimum token amounts.

  • swap_tokens_for_exact_tokens: Executes swaps specifying the maximum input tokens.

Each adapter handles the specifics of its respective protocol, as shown in Figure 3.

amounti=⌊total_amount×disti.partstotal_parts⌋;∀i∈Z s.t. 1≤i<n \text{amount}_i = \left\lfloor \frac{\text{total\_amount} \times \text{dist}_i.\text{parts}}{\text{total\_parts}} \right\rfloor ; \quad \forall i \in Z \text{ s.t. } 1 \leq i < n amounti​=⌊total_partstotal_amount×disti​.parts​⌋;∀i∈Z s.t. 1≤i<n
(disti.parts) is thefraction assigned to protocol (i). (\text{dist}_i.\text{parts})  is  the fraction  assigned  to  protocol  (i).  (disti​.parts) is thefraction assigned to protocol (i). 
 (Z) represents the set of integers,and (1≤i<n) specifies  (Z)  represents   the  set  of  integers, and  (1 \leq i < n)  specifies  (Z) represents the set of integers,and (1≤i<n) specifies 
that (i) is within the range of protocols involved in the swap.that  (i)   is  within  the  range  of  protocols  involved  in  the  swap.that (i) is within the range of protocols involved in the swap.
Aggregator
Figure 1: Aggregator initialize function operations diagram.
Figure 2: Aggregator swap functions operations diagram.
Figure 3: Adapter swap functions and data fetchers diagram.
Page cover image