Smart Order Router

The Smart Order Router enables optimal trade execution by finding the most efficient trades across all available liquidity pools on the StoryHunt protocol.

Overview

  • AlphaRouter: The core routing engine responsible for pathfinding, quote simulation, gas estimation, and execution preparation.

  • SwapAlphaRouter: A singleton wrapper around the low-level AlphaRouter algorithm which is a public interface to query optimal swap routes.


graph TD
A[SwapAlphaRouter]
B[AlphaRouter]
A --> B
B --> C[On-chain Providers]
B --> D[Subgraph Pool Providers]
B --> E[Gas Price Estimator]
B --> F[Simulators]
B --> G[Route Cache]

Installation & Setup

Ensure the project has access to the required SDK:

npm install @storyhunt/smart-order-router
npm install @storyhunt/sdk-core

SwapAlphaRouter

SwapAlphaRouter acts as a singleton utility class to interact with the router logic.

Initialization

Core Function: Get Swap Route

Sample Output

Path Discovery: getPaths

Returns all possible candidate token paths (via pools) between two tokens. Useful for visualizing or debugging router behavior.

AlphaRouter: Parameters

The AlphaRouter uses a comprehensive set of providers to accurately compute the best route.

Each of these components can be extended or replaced, enabling a high degree of modularity for advanced use cases like simulation, cache control, or risk-adjusted routing.

Full Example

Notes

  • ExactIn/ExactOut: The router supports both exactIn and exactOut trade types.

  • Simulation: By enabling simulate: true, a backend simulation can be triggered to return more accurate gas and slippage data.

  • Calldata: The router returns the calldata necessary to execute the trade via a smart contract.


Debugging Tips

  • Use debugRouting: true in development to see intermediate routing decisions.

  • Use getPaths() to check for missing pools or broken liquidity routes.

  • Ensure RPC and Subgraph URLs are up to date and synced.

Last updated