Skip to main content

eth_prices/router/
route.rs

1use alloy::primitives::U256;
2use tracing::info;
3
4use crate::{
5    Result,
6    asset::AssetIdentifier,
7    network::NetworkInstant,
8    quoter::{AnyQuoter, RateDirection},
9};
10
11#[derive(Debug, Clone)]
12pub struct RouteStep {
13    pub quoter: AnyQuoter,
14    pub direction: RateDirection,
15}
16
17#[derive(Debug, Clone)]
18pub struct Route {
19    pub path: Vec<RouteStep>,
20    pub input_token: AssetIdentifier,
21    pub output_token: AssetIdentifier,
22}
23
24impl Route {
25    /// calculate a quote for a given route
26    pub async fn quote(&self, networks: &NetworkInstant, amount_in: U256) -> Result<U256> {
27        let mut amount_out = amount_in;
28
29        for step in self.path.iter() {
30            let quoter_slug = step.quoter.to_string();
31
32            info!(
33                target: "router::quote_start",
34                quoter_slug,
35                amount_in = %amount_in,
36                direction = %step.direction,
37            );
38
39            let rate = step
40                .quoter
41                .rate(amount_out, step.direction, networks)
42                .await?;
43            amount_out = rate;
44
45            info!(
46                target: "router::quote_end",
47                quoter_slug,
48                amount_in = %amount_in,
49                direction = %step.direction,
50                amount_out = %amount_out,
51            );
52        }
53
54        Ok(U256::from(amount_out))
55    }
56}