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
graphfor path finding (BFS/etc) - They should use
marketto read component states for simulation - They should NOT modify the graph or market data
Required Associated Types§
Sourcetype GraphManager: GraphManager<Self::GraphType> + Default
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§
Sourceasync fn find_best_route(
&self,
graph: &Self::GraphType,
market: SharedMarketDataRef,
derived: Option<SharedDerivedDataRef>,
order: &Order,
) -> Result<RouteResult, AlgorithmError>
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.
Sourcefn computation_requirements(&self) -> ComputationRequirements
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.
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.