# Smart Order Router

## Overview

The Smart Order Router (SOR) finds the best prices for Balancer traders. For given input and output tokens, the SOR finds the optimal trades whether that is a direct swap in one pool, or a combination of trades hopping through multiple pools.

![](https://3750263814-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlH9xKbfiul3CI0OXaQ%2Fuploads%2Fo2lDagtxw9HEGmH9hAh2%2Fsor.png?alt=media\&token=3dd2ecc3-0e26-4b9e-8be9-7729ed32857c)

## Utilize All the Liquidity!

As the variety of [Balancer Pools](https://balancer.gitbook.io/balancer-v2/products/broken-reference) grows, the SOR grows too! The SOR keeps expanding as  new pool types that use different math under the hood are added. This ensures that all pools in the Balancer ecosystem can support trades. By integrating with the SOR, any custom pool built on Balancer can benefit from all the other Balancer liquidity. All you need to integrate a pool is first and second order differentiable `spotPriceAfterSwap` functions (differentiable either numerically or analytically).

## Taking Gas Into Account

In an ideal world in which gas costs are negligible, a trade between two tokens would involve each Balancer pool that contains that pair. This would utilize all the available liquidity for the trader and maintain equal prices across all pools. Such a scenario would be an *arbitrage-free state*, in which no value could be extracted from the pools' price differences.

Since there *are* incremental gas costs for each swap added to a batch, additional pools are added to the path only when they provide enough of a price difference to make up for the gas. Since only a subset of pools are considered, this can create arbitrage opportunities across Balancer pools.

## How It Works

The optimization mechanism finds the path through a set of Balancer Pools with the greatest output (after gas costs).

![](https://3750263814-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlH9xKbfiul3CI0OXaQ%2Fuploads%2FfsAIK6DNg5BW35L1mD71%2FSORrevised4.png?alt=media\&token=c2e566f6-2937-4214-9f5d-f737a794bae3)

### Multiple Pools, Same Spot Price

In order to get the best price for a trader, the SOR is designed to create an arbitrage-free state between the paths it's using. In order to achieve this, **each path the SOR routes through needs to provide the same spot price after the swap has completed.**&#x20;

![](https://3750263814-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MlH9xKbfiul3CI0OXaQ%2Fuploads%2F1QSPHH78IQ5s19dkcclp%2FtargetSP%20\(1\).png?alt=media\&token=9c1aabc3-2b2d-4f21-aa51-6ff0e0d0fe09)


---

# 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://balancer.gitbook.io/balancer-v2/products/smart-order-router.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.
