# On Chain Registry

## Summary

Stores a registry of Balancer Pool addresses for a given token address pair. Pools can be sorted in order of liquidity and queried via view functions. Used in combination with the [Exchange Proxy](https://balancer.gitbook.io/balancer-v2/v1/smart-contracts/exchange-proxy) swaps can be sourced and exectured entirely on-chain.

## API

### **Adding Pools To Registry**

#### **addPoolPair**

`addPoolPair(address pool, address token1, address token2)`

Adds a single pool address for token pair.

#### addPools

`addPools(address[] calldata pools, address token1, address token2)`

Adds an array of pool addresses for token pair.

### **Sorting Pools**

#### **sortPools**

`sortPools(address[] calldata tokens, uint256 lengthLimit)`

Sorts pools in order of liquidity. lengthLimit can be used to limit the number of pools sorted.

#### sortPoolsWithPurge

`sortPoolsWithPurge(address[] calldata tokens, uint256 lengthLimit)`

Sorts pools in order of liquidity and removes any pools with <10% of total liquidity.

### **Retrieving Pools**

#### **getBestPools**

`getBestPools(address fromToken, address destToken)`

Retrieve array of pool addresses for token pair. Ordered by liquidity if previously sorted. Max of 32 pools returned.

#### getBestPoolsWithLimit

`getBestPoolsWithLimit(address fromToken, address destToken, uint256 limit)`

Retrieve array of pool addresses for token pair. Ordered by liquidity if previously sorted. Max of n pools returned where n=limit.

#### getPoolsWithLimit

`getPoolsWithLimit(address fromToken, address destToken, uint256 offset, uint256 limit)`

Retrieve array of pool addresses using an offset starting position.


---

# 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/v1/smart-contracts/on-chain-registry.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.
