DexRouting Docs
  • Welcome
  • What is Dex Routing?
  • Features
    • Dynamic Routing
    • Our pathfinding algorithm
  • Active Chains
    • 🟒Canto
  • Support
    • Help
Powered by GitBook
On this page
  1. Features

Our pathfinding algorithm

PreviousDynamic RoutingNextCanto

Last updated 8 months ago

Mathematical Optimization for Maximum Efficiency

We don’t just rely on intuition; our algorithms are backed by solid math to ensure precision:

  • Max Output Equation: Our core algorithm employs a sophisticated equation to maximize output. Given two tokens A and B with liquidity pools ( L_A ) and ( L_B ), the equation is:

    MaxΒ Output=max⁑(LAΓ—PAβ†’BLB+Ξ”LB)\text{Max Output} = \max \left( \frac{L_A \times P_{A \rightarrow B}}{L_B + \Delta L_B} \right)MaxΒ Output=max(LB​+Ξ”LB​LA​×PAβ†’B​​)

    where PA β†’ B is the price of token A in terms of token B, and Ξ” LB represents the change in liquidity for token B due to the swap.

  • Optimal Pathfinding: We use a pathfinding algorithm inspired by Brent’s method to ensure the best route is selected. Our implementation involves:

    xn+1=xnβˆ’f(xn)β‹…(xnβˆ’xnβˆ’1)f(xn)βˆ’f(xnβˆ’1)x_{n+1} = x_n - \frac{f(x_n) \cdot (x_n - x_{n-1})}{f(x_n) - f(x_{n-1})}xn+1​=xnβ€‹βˆ’f(xn​)βˆ’f(xnβˆ’1​)f(xn​)β‹…(xnβ€‹βˆ’xnβˆ’1​)​

    where xn is the current estimate, xn-1 is the previous estimate, and f(x) represents the function that calculates the output of the swap. This approach combines Brent’s algorithm with inverse parabolic interpolation to speed up convergence and optimize routing decisions.

  • Arbitrage Efficiency Function: To minimize scalar deviations and optimize arbitrage opportunities, we use:

    Earb=βˆ‘i=1n(Pi,buyβˆ’Pi,sellPi,sell)β‹…Wiβˆ‘i=1nWiE_{\text{arb}} = \frac{\sum_{i=1}^n \left( \frac{P_{i, \text{buy}} - P_{i, \text{sell}}}{P_{i, \text{sell}}} \right) \cdot W_i}{\sum_{i=1}^n W_i}Earb​=βˆ‘i=1n​Wiβ€‹βˆ‘i=1n​(Pi,sell​Pi,buyβ€‹βˆ’Pi,sell​​)β‹…Wi​​

    where Pi, buy and Pi, sell are the buy and sell prices of token i, Wi represents the weight of each price opportunity, and Earb is the efficiency score for the arbitrage opportunity. This formula helps in evaluating the best routes for exploiting price differences across multiple exchanges.