penumbra_sdk_dex/
params.rs

1use penumbra_sdk_asset::{asset, STAKING_TOKEN_ASSET_ID};
2use penumbra_sdk_proto::penumbra::core::component::dex::v1 as pb;
3use penumbra_sdk_proto::DomainType;
4use serde::{Deserialize, Serialize};
5
6#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
7#[serde(try_from = "pb::DexParameters", into = "pb::DexParameters")]
8/// The configuration parameters for the DEX component.
9pub struct DexParameters {
10    pub is_enabled: bool,
11    pub fixed_candidates: Vec<asset::Id>,
12    pub max_hops: u32,
13    pub max_positions_per_pair: u32,
14    pub max_execution_budget: u32,
15}
16
17impl DomainType for DexParameters {
18    type Proto = pb::DexParameters;
19}
20
21impl TryFrom<pb::DexParameters> for DexParameters {
22    type Error = anyhow::Error;
23
24    fn try_from(msg: pb::DexParameters) -> anyhow::Result<Self> {
25        Ok(DexParameters {
26            is_enabled: msg.is_enabled,
27            fixed_candidates: msg
28                .fixed_candidates
29                .into_iter()
30                .map(|id| id.try_into())
31                .collect::<Result<_, _>>()?,
32            max_hops: msg.max_hops,
33            max_positions_per_pair: msg.max_positions_per_pair,
34            max_execution_budget: msg.max_execution_budget,
35        })
36    }
37}
38
39impl From<DexParameters> for pb::DexParameters {
40    fn from(params: DexParameters) -> Self {
41        pb::DexParameters {
42            is_enabled: params.is_enabled,
43            fixed_candidates: params
44                .fixed_candidates
45                .into_iter()
46                .map(Into::into)
47                .collect(),
48            max_hops: params.max_hops,
49            max_positions_per_pair: params.max_positions_per_pair,
50            max_execution_budget: params.max_execution_budget,
51        }
52    }
53}
54
55#[allow(clippy::unwrap_used)]
56impl Default for DexParameters {
57    fn default() -> Self {
58        // This will get used for generating default chain parameters; put some
59        // test assets in there.
60        let cache = asset::Cache::with_known_assets();
61        Self {
62            is_enabled: true,
63            fixed_candidates: vec![
64                *STAKING_TOKEN_ASSET_ID,
65                cache.get_unit("test_usd").unwrap().id(),
66                cache.get_unit("gm").unwrap().id(),
67                cache.get_unit("gn").unwrap().id(),
68                cache.get_unit("test_atom").unwrap().id(),
69                cache.get_unit("test_osmo").unwrap().id(),
70                cache.get_unit("test_btc").unwrap().id(),
71            ],
72            max_hops: 4,
73            max_positions_per_pair: 1_000,
74            max_execution_budget: 64,
75        }
76    }
77}