Skip to main content

eth_prices/router/
mod.rs

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