Router Function
The Router allows for swaps between two tokens, even if there is no direct pair between them. This is where the Router’s flexibility comes into play. Instead of performing a direct swap between Token A and Token C, the Router can find the best route using one or more intermediate pairs. For example:- Direct Route: If a direct pair A:C exists, the Router can perform the swap directly between A and C.
- Indirect Route: If no direct pair is available, the Router can use a multi-hop route, such as A:B followed by B:C.
How It Works
- Retrieve Pools: The Router first retrieves data on all available pools involving the tokens you wish to swap. This data is sourced from the backend database.
- Calculate Routes: Using the pool data, the Router calculates all possible routes for the swap, considering a maximum number of hops allowed between pairs.
- Optimize Route: The Router breaks down possible routes into segments and calculates the optimal swap for each segment. It then combines these routes and validates which offers the best overall exchange rate.
- Execute Swap: Finally, the Router selects the best route based on the amount of tokens to be received or sent and executes the swap.
Router Methods
The Router provides two main methods:getPools(tokenIn, tokenOut): Retrieves information on available pools that include the specified tokens.getBestRoute(tokenIn, tokenOut, TradeType, ...options): Calculates and returns the best route for the swap between the specified tokens, considering trade type and additional options.