penumbra_sdk_dex/component/router/
params.rs

1use std::sync::Arc;
2
3use penumbra_sdk_asset::asset;
4use penumbra_sdk_num::fixpoint::U128x128;
5
6use crate::DexParameters;
7
8#[derive(Debug, Clone)]
9pub struct RoutingParams {
10    pub price_limit: Option<U128x128>,
11    pub fixed_candidates: Arc<Vec<asset::Id>>,
12    pub max_hops: usize,
13}
14
15impl RoutingParams {
16    pub fn with_extra_candidates(self, iter: impl IntoIterator<Item = asset::Id>) -> Self {
17        let mut fixed_candidates: Vec<_> = (*self.fixed_candidates).clone();
18        fixed_candidates.extend(iter);
19
20        Self {
21            fixed_candidates: Arc::new(fixed_candidates),
22            ..self
23        }
24    }
25
26    /// Clamps the spill price to the price limit and returns whether or not it was clamped.
27    pub fn clamp_to_limit(&self, spill_price: Option<U128x128>) -> (Option<U128x128>, bool) {
28        match (spill_price, self.price_limit) {
29            (Some(spill_price), Some(price_limit)) => {
30                if spill_price > price_limit {
31                    (Some(price_limit), true)
32                } else {
33                    (Some(spill_price), false)
34                }
35            }
36            (Some(spill_price), None) => (Some(spill_price), false),
37            (None, Some(price_limit)) => (Some(price_limit), true),
38            (None, None) => (None, false),
39        }
40    }
41}
42
43impl From<DexParameters> for RoutingParams {
44    fn from(
45        DexParameters {
46            fixed_candidates,
47            max_hops,
48            ..
49        }: DexParameters,
50    ) -> Self {
51        Self {
52            fixed_candidates: Arc::new(fixed_candidates),
53            max_hops: max_hops as usize,
54            price_limit: None,
55        }
56    }
57}