Custom Algorithm
The Algorithm trait
Algorithm traitImplement the trait
/// A custom algorithm that wraps [`MostLiquidAlgorithm`].
///
/// Replace the delegation in [`Algorithm::find_best_route`] with your own routing
/// logic to use a fully custom algorithm.
struct MyAlgorithm {
inner: MostLiquidAlgorithm,
}
impl MyAlgorithm {
fn new(config: AlgorithmConfig) -> Self {
let inner =
MostLiquidAlgorithm::with_config(config).expect("invalid algorithm configuration");
Self { inner }
}
}
impl Algorithm for MyAlgorithm {
// Reuse the built-in graph type and manager so the worker infrastructure
// (graph initialisation, event handling, edge weight updates) works unchanged.
type GraphType = <MostLiquidAlgorithm as Algorithm>::GraphType;
type GraphManager = <MostLiquidAlgorithm as Algorithm>::GraphManager;
fn name(&self) -> &str {
"my_custom_algo"
}
async fn find_best_route(
&self,
graph: &Self::GraphType,
market: SharedMarketDataRef,
derived: Option<SharedDerivedDataRef>,
order: &Order,
) -> Result<RouteResult, AlgorithmError> {
// Delegate to the inner algorithm. Replace this with custom logic.
self.inner
.find_best_route(graph, market, derived, order)
.await
}
fn computation_requirements(&self) -> ComputationRequirements {
self.inner.computation_requirements()
}
fn timeout(&self) -> Duration {
self.inner.timeout()
}
}Wire it up
Run the example
Prerequisites
Run
Last updated