# How the Aggregator Works

### Step 1: Collect liquidity pool data

<mark style="color:$info;">It gathers data from connected AMMs:</mark>

* Direct pools (TokenA–TokenB)
* Intermediate pools (via WETH, USDC, etc.)

<mark style="color:$info;">Supported:</mark>

* **V2 Pools** — constant product (x \* y = k)
* **V3 Pools** — concentrated liquidity
* **Order Book** — e.g., Kuru

### Step 2: Build the route graph

* **Nodes** = tokens
* **Edges** = liquidity pools

### Step 3: Find optimal paths

Uses a modified Bellman-Ford algorithm to find up to 4-hop paths and selects the one with the best output.

### Step 4: Simulate and calculate

Each route is simulated considering pool fee, liquidity, and slippage.

***

## Route Graph Visualization <a href="#route-graph-visualization" id="route-graph-visualization"></a>

Here's how the aggregator maps available liquidity:

```
text    [USDT] ----Pool1---- [WMON]
          |                     |
        Pool2                 Pool3
          |                     |
        [USDC]  ----Pool4---- [MON]
```

<mark style="color:$info;">Graph Structure:</mark>

* **Nodes:** USDT, WMON, USDC, MON (tokens)
* **Edges:** Pool1, Pool2, Pool3, Pool4 (liquidity pools)

<mark style="color:$info;">Possible Routes (USDT → DAI):</mark>

1. **Direct:** USDT → Pool2 → USDC → Pool4 → MON
2. **Multi-hop:** USDT → Pool1 → WMON → Pool3 → MON
3. **Complex:** USDT → Pool1 → WMON → Pool3 → MON + USDT → Pool2 → USDC → Pool4 → MON (split route)

***

## Route Types <a href="#route-types" id="route-types"></a>

### <mark style="color:$info;">1. Direct Route</mark>

Single pool: `[USDT] → [WMON]`

### <mark style="color:$info;">2. Multi-hop Route</mark>

Through intermediates: `[TokenA] → [WMON] → [TokenB]`

### <mark style="color:$info;">3. Split Route</mark>

Split across pools: 60% Kuru, 40% Bean

***

## Route Building Process <a href="#route-building-process" id="route-building-process"></a>

1. **Pool discovery** (5–10 ms)
2. **Graph construction** (10–20 ms)
3. **Path search** (50–200 ms)
4. **Alternative search** (30–100 ms)

### <mark style="color:$info;">Optimization</mark>

**Weights assigned based on efficiency**

**Bonuses/penalties:**

* **+15%** for direct routes
* **+5%** for V3
* **−2%** per extra hop

**Limits:**

* Max 60% per route
* Min 5% per route

### <mark style="color:$info;">Convert to smart contract weights</mark>

**Example:**

* Route 1: 60% → 6000
* Route 2: 25% → 2500
* Route 3: 15% → 1500
* **Total: 10000** ✅

***

## Optimization and Distribution <a href="#optimization-and-distribution" id="optimization-and-distribution"></a>

<mark style="color:$info;">Three strategies compared:</mark>

1. Single best route
2. Multiple direct routes
3. Multi-hop combinations

**The system picks the strategy with the highest token output.**

### <mark style="color:$info;">Slippage example</mark>

* **Expected:** 1000 tokens
* **Fee 0.2%** = -2
* **Slippage 0.5%** = -5
* **→ Minimum = 993 tokens**


---

# 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://dirolprotocols-organization.gitbook.io/dirol/how-the-aggregator-works.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.
