# Aggregator Operation

### **Aggregator Initialization**

When the [Aggregator](https://github.com/soroswap/aggregator/) 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.

<figure><img src="/files/TiYzqrNvDon4Twmd1OpT" alt=""><figcaption><p><strong>Figure 1: Aggregator <code>initialize</code> function operations diagram.</strong></p></figcaption></figure>

### **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.

<figure><img src="/files/3Jw6Mkh7OXYQUw1MmUpZ" alt=""><figcaption><p>Figure 2: Aggregator <code>swap</code> functions operations diagram.</p></figcaption></figure>

### **Distribution Calculation**

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

$$
\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
$$

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`.

$$
(\text{dist}\_i.\text{parts})  is  the fraction  assigned  to  protocol  (i).
$$

$$
(Z)  represents   the  set  of  integers, and  (1 \leq i < n)  specifies
$$

$$
that  (i)   is  within  the  range  of  protocols  involved  in  the  swap.
$$

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**.

<figure><img src="/files/wCSDOQtJxVX8pLL2N3ku" alt=""><figcaption><p><strong>Figure 3: Adapter swap functions and data fetchers diagram.</strong></p></figcaption></figure>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.soroswap.finance/smart-contracts/soroswap-aggregator/technical-reference/aggregator-operation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
