Skip to main content

Algorithm

Trait Algorithm 

Source
pub trait Algorithm: Send + Sync {
    type GraphType: Send + Sync;
    type GraphManager: GraphManager<Self::GraphType> + Default;

    // Required methods
    fn name(&self) -> &str;
    async fn find_best_route(
        &self,
        graph: &Self::GraphType,
        market: SharedMarketDataRef,
        derived: Option<SharedDerivedDataRef>,
        order: &Order,
    ) -> Result<RouteResult, AlgorithmError>;
    fn computation_requirements(&self) -> ComputationRequirements;
    fn timeout(&self) -> Duration;
}
Expand description

Trait for route-finding algorithms.

Algorithms are generic over their preferred graph type G, allowing them to:

  • Use different graph crates (petgraph, custom, etc.)
  • Leverage built-in algorithms from graph libraries
  • Optimize their graph representation for their specific needs

§Implementation Notes

  • Algorithms should respect the timeout from timeout()
  • They should use graph for path finding (BFS/etc)
  • They should use market to read component states for simulation
  • They should NOT modify the graph or market data

Required Associated Types§

Source

type GraphType: Send + Sync

The graph type this algorithm uses.

Source

type GraphManager: GraphManager<Self::GraphType> + Default

The graph manager type for this algorithm. This allows the solver to automatically create the appropriate graph manager.

Required Methods§

Source

fn name(&self) -> &str

Returns the algorithm’s name.

Source

async fn find_best_route( &self, graph: &Self::GraphType, market: SharedMarketDataRef, derived: Option<SharedDerivedDataRef>, order: &Order, ) -> Result<RouteResult, AlgorithmError>

Finds the best route for the given order.

§Arguments
  • graph - The algorithm’s preferred graph type (e.g., petgraph::Graph)
  • market - Shared reference to market data for state lookups (algorithms acquire their own locks)
  • derived - Optional shared reference to derived data (token prices, etc.)
  • order - The order to solve
§Returns

The best route and its gas-adjusted net output amount, or an error if no route could be found.

Source

fn computation_requirements(&self) -> ComputationRequirements

Returns the derived data computation requirements for this algorithm.

Algorithms declare freshness requirements for derived data:

  • require_fresh: Data must be from the current block (same as SharedMarketData)
  • allow_stale: Data can be from any past block, as long as it exists

Workers use this to determine when they can safely solve.

Default implementation returns no requirements - algorithm works without any derived data.

Source

fn timeout(&self) -> Duration

Returns the timeout for solving.

Workers use this to set the maximum time to wait for derived data before failing a solve request.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§