> For the complete documentation index, see [llms.txt](https://txnlab.gitbook.io/haystack-dev/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://txnlab.gitbook.io/haystack-dev/haystack-router/architecture/order-routing-algorithm.md).

# Order Routing Algorithm

The value of any token can change quickly at any point in time. Therefore, the best priced swap is not always on one DEX and may involve a combination of swaps across many DEXes.

Haystack Order Router connects the AMM pools of every DEX it aggregates, together with liquid-staking (tALGO, xALGO) and lending (Folks Finance) markets, and searches direct and atomic multi-hop routes between any two tokens in the Algorand ecosystem. Routes are compared after pool fees and network fees, and the route that delivers the most is returned.

#### Swap Splitting

Haystack Order Router will split your swap into smaller swap sizes. For example, if you want to make a 100 USDC-ALGO swap, it may distribute your swap into a 30 USDC-ALGO swap on Tinyman V2 and a 70 USDC-ALGO swap on Pact. The % distribution of the swaps is dynamically computed to give you the best price. Swap splitting helps get better prices for large size swaps and swaps with tokens where there is shallow liquidity spread out across a number of DEXes.

<figure><img src="https://4237715784-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ff7CoS3B3CCJggIxecPe2%2Fuploads%2Fgit-blob-3eccd43bbd094be619dcf78c918e9d3b3e0b1fb3%2Fimage%20(1).png?alt=media" alt=""><figcaption><p>Split swap route example</p></figcaption></figure>

#### Multi-hop Routes

In some cases where a token pair has low liquidity across all sources in the ecosystem, Haystack Order Router may find the best price by routing through an intermediary token, e.g. USDC-ALGO-DEFLY instead of USDC-DEFLY. This may be due to price inefficiencies and/or volatility in the various markets that Haystack Order Router routes through. Multi-hop routes can be part of split swaps, and a multi-hop route can include split swaps.

<figure><img src="https://4237715784-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Ff7CoS3B3CCJggIxecPe2%2Fuploads%2Fgit-blob-fff3f010230b6cac6b953164e160177d5a3301be%2Fimage.png?alt=media" alt=""><figcaption><p>Multi-hop route example</p></figcaption></figure>

##
