dexter_stable_pool/
state.rs

1use std::collections::HashMap;
2
3use cosmwasm_schema::cw_serde;
4use cosmwasm_std::{StdResult, Storage, Uint128, Addr, Decimal256, Decimal};
5use cw_storage_plus::{Item, Map};
6use dexter::asset::AssetInfo;
7use dexter::pool::Config;
8
9/// ## Description
10/// Stores config at the given key
11pub const CONFIG: Item<Config> = Item::new("config");
12/// Stores extra config for stableswap at the given key
13pub const STABLESWAP_CONFIG: Item<StableSwapConfig> = Item::new("stableswap_config");
14
15///  Stores Twap prices for the tokens supported by the pool
16pub const TWAPINFO: Item<Twap> = Item::new("twap");
17
18/// Stores custom config at the given key which can be different between different dexter pools
19pub const MATHCONFIG: Item<MathConfig> = Item::new("math_config");
20/// ## Description
21/// This struct describes the main math config of pool.
22#[cw_serde]
23pub struct MathConfig {
24    // This is the current amplification used in the pool
25    pub init_amp: u64,
26    // This is the start time when amplification starts to scale up or down
27    pub init_amp_time: u64,
28    // This is the target amplification to reach at `next_amp_time`
29    pub next_amp: u64,
30    // This is the timestamp when the current pool amplification should be `next_amp`
31    pub next_amp_time: u64,
32    /// The greatest precision of assets in the pool
33    pub greatest_precision: u8,
34}
35
36/// Stablswap config. New version removes the unused max_allowed_spread field.
37#[cw_serde]
38pub struct StableSwapConfig {
39    /// If this is true, then the scaling factors can be updated by the scaling_factor_manager.
40    pub supports_scaling_factors_update: bool,
41    /// The vector of scaling factors for each asset in the pool.
42    /// The scaling factor is used to scale the volume of the asset in the pool for the stableswap invariant calculations.
43    pub scaling_factors: Vec<AssetScalingFactor>,
44    // This address is allowed to update scaling factors. This address is required if support_scaling_factors_update is true.
45    pub scaling_factor_manager: Option<Addr>,
46}
47
48#[cw_serde]
49pub struct StableSwapConfigV1 {
50    /// Max allowed spread between the price of the asset and the price of the pool.
51    /// If the spread is greater than this value, the swap will fail.
52    /// This value is configurable by the Pool Manager.
53    /// Max allowed spread is in the range (0, 1) non-inclusive.
54    pub max_allowed_spread: Decimal,
55    /// If this is true, then the scaling factors can be updated by the scaling_factor_manager.
56    pub supports_scaling_factors_update: bool,
57    /// The vector of scaling factors for each asset in the pool.
58    /// The scaling factor is used to scale the volume of the asset in the pool for the stableswap invariant calculations.
59    pub scaling_factors: Vec<AssetScalingFactor>,
60    // This address is allowed to update scaling factors. This address is required if support_scaling_factors_update is true.
61    pub scaling_factor_manager: Option<Addr>,
62}
63
64impl StableSwapConfig {
65    pub fn scaling_factors(&self) -> HashMap<AssetInfo, Decimal256> {
66        let mut scaling_factors = HashMap::new();
67        for scaling_factor in &self.scaling_factors {
68            scaling_factors.insert(scaling_factor.asset_info.clone(), scaling_factor.scaling_factor);
69        }
70        scaling_factors
71    }
72
73    pub fn get_scaling_factor_for(&self, asset_info: &AssetInfo) -> Option<Decimal256> {
74        for scaling_factor in &self.scaling_factors {
75            if scaling_factor.asset_info == *asset_info {
76                return Some(scaling_factor.scaling_factor);
77            }
78        }
79        None
80    }
81}
82
83/// ## Description
84/// This struct which stores the TWAP calcs related info for the pool
85#[cw_serde]
86pub struct Twap {
87    /// The vector contains cumulative prices for each pair of assets in the pool
88    pub cumulative_prices: Vec<(AssetInfo, AssetInfo, Uint128)>,
89    /// The latest timestamp when TWAP prices of asset pairs were last updated.
90    /// Although it seems same as the param inside CONFIG, but it is different. As the TWAP price
91    /// accumulation not always succeeds, so this might be different than the one in config.
92    /// So, better to keep it here.
93    pub block_time_last: u64,
94}
95
96/// This struct holds stableswap pool parameters.
97#[cw_serde]
98pub struct StablePoolParams {
99    /// The current stableswap pool amplification
100    pub amp: u64,
101    /// Support scaling factors update
102    pub supports_scaling_factors_update: bool,
103    /// Scaling factors
104    /// The scaling factor is used to scale the volume of the asset in the pool for the stableswap invariant calculations.
105    /// This allows to support assets which are not equal in value but are highly correlated in price.
106    /// The usecases of this are:
107    /// 1. Liquid staking tokens (e.g. stkATOM, stkXPRT, etc.) which differ in price to the base asset by a C-Ratio accruing the rewards earned from staking.
108    /// 2. Yield bearing tokens (e.g. aTokens, cTokens, etc.) which differ in price to the base asset by a C-Ratio accruing the interest earned from lending.
109    /// 3. Related synthetic assets which follow the price of the base assets by a defined mechanism.
110    pub scaling_factors: Vec<AssetScalingFactor>,
111    /// Scaling factor manager
112    pub scaling_factor_manager: Option<Addr>,
113}
114
115#[cw_serde]
116pub struct AssetScalingFactor {
117    pub asset_info: AssetInfo,
118    pub scaling_factor: Decimal256,
119}
120
121impl AssetScalingFactor {
122    pub fn new(asset_info: AssetInfo, scaling_factor: Decimal256) -> Self {
123        Self {
124            asset_info,
125            scaling_factor,
126        }
127    }
128}
129
130/// This enum stores the options available to start and stop changing a stableswap pool's amplification.
131#[cw_serde]
132pub enum StablePoolUpdateParams {
133    StartChangingAmp { next_amp: u64, next_amp_time: u64 },
134    StopChangingAmp {},
135    UpdateScalingFactorManager { manager: Addr },
136    UpdateScalingFactor { asset_info: AssetInfo, scaling_factor: Decimal256 }
137}
138
139// ----------------x----------------x----------------x----------------
140// ----------------x      PRESISION : Store and getter fns     x------
141// ----------------x----------------x----------------x----------------
142
143/// Stores map of AssetInfo (as String) -> precision
144pub const PRECISIONS: Map<String, u8> = Map::new("precisions");
145
146/// ## Description
147/// Loads precision of the given asset info.
148pub(crate) fn get_precision(storage: &dyn Storage, asset_info: &AssetInfo) -> StdResult<u8> {
149    PRECISIONS.load(storage, asset_info.to_string())
150}