mars_params/types/
hls.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{Addr, Api, Decimal};
3use mars_utils::helpers::validate_native_denom;
4
5use crate::error::ContractResult;
6
7#[cw_serde]
8pub enum HlsAssetType<T> {
9    Coin {
10        denom: String,
11    },
12    Vault {
13        addr: T,
14    },
15}
16
17impl From<HlsAssetType<Addr>> for HlsAssetType<String> {
18    fn from(t: HlsAssetType<Addr>) -> Self {
19        match t {
20            HlsAssetType::Coin {
21                denom,
22            } => HlsAssetType::Coin {
23                denom,
24            },
25            HlsAssetType::Vault {
26                addr,
27            } => HlsAssetType::Vault {
28                addr: addr.to_string(),
29            },
30        }
31    }
32}
33
34#[cw_serde]
35pub struct HlsParamsBase<T> {
36    pub max_loan_to_value: Decimal,
37    pub liquidation_threshold: Decimal,
38    /// Given this asset is debt, correlations are the only allowed collateral
39    /// which are permitted to fulfill the HLS strategy
40    pub correlations: Vec<HlsAssetType<T>>,
41}
42
43pub type HlsParams = HlsParamsBase<Addr>;
44pub type HlsParamsUnchecked = HlsParamsBase<String>;
45
46impl From<HlsParams> for HlsParamsUnchecked {
47    fn from(hls: HlsParams) -> Self {
48        Self {
49            max_loan_to_value: hls.max_loan_to_value,
50            liquidation_threshold: hls.liquidation_threshold,
51            correlations: hls.correlations.into_iter().map(Into::into).collect(),
52        }
53    }
54}
55
56impl HlsParamsUnchecked {
57    pub fn check(&self, api: &dyn Api) -> ContractResult<HlsParams> {
58        Ok(HlsParamsBase {
59            max_loan_to_value: self.max_loan_to_value,
60            liquidation_threshold: self.liquidation_threshold,
61            correlations: self
62                .correlations
63                .iter()
64                .map(|c| match c {
65                    HlsAssetType::Coin {
66                        denom,
67                    } => {
68                        validate_native_denom(denom)?;
69                        Ok(HlsAssetType::Coin {
70                            denom: denom.clone(),
71                        })
72                    }
73                    HlsAssetType::Vault {
74                        addr,
75                    } => Ok(HlsAssetType::Vault {
76                        addr: api.addr_validate(addr)?,
77                    }),
78                })
79                .collect::<ContractResult<Vec<_>>>()?,
80        })
81    }
82}