dexter_weighted_pool/
state.rs

1use cosmwasm_schema::cw_serde;
2use cosmwasm_std::{Decimal, DepsMut, StdResult, Storage, Uint128};
3use cw_storage_plus::{Item, Map};
4
5use dexter::asset::{Asset, AssetInfo};
6use dexter::pool::Config;
7
8/// ## Description
9/// Stores config at the given key
10pub const CONFIG: Item<Config> = Item::new("config");
11
12/// Stores custom Twap at the given key which can be different between different dexter pools
13pub const TWAPINFO: Item<Twap> = Item::new("twap");
14
15/// Stores custom config at the given key which can be different between different dexter pools
16pub const MATHCONFIG: Item<MathConfig> = Item::new("math_config");
17
18/// Stores map of AssetInfo (as String) -> precision
19pub const PRECISIONS: Map<String, u8> = Map::new("precisions");
20
21/// Stores map of AssetInfo (as String) -> weight
22pub const WEIGHTS: Map<String, Decimal> = Map::new("weights");
23
24/// ## Description
25/// This struct describes the main math config of pool.
26#[cw_serde]
27pub struct MathConfig {
28    /// Exit fee in % charged when liquidity is withdrawn from the pool
29    pub exit_fee: Option<Decimal>,
30    /// The greatest precision of assets in the pool
31    pub greatest_precision: u8,
32}
33
34/// ## Description
35/// This struct which stores the TWAP calcs related info for the pool
36#[cw_serde]
37pub struct Twap {
38    /// The vector contains cumulative prices for each pair of assets in the pool
39    pub cumulative_prices: Vec<(AssetInfo, AssetInfo, Uint128)>,
40    pub block_time_last: u64,
41}
42
43// ----------------x----------------x----------------x----------------
44// ----------------x      PRESISION : Store and getter fns     x------
45// ----------------x----------------x----------------x----------------
46
47/// ## Description
48/// Loads precision of the given asset info.
49pub(crate) fn get_precision(storage: &dyn Storage, asset_info: &AssetInfo) -> StdResult<u8> {
50    PRECISIONS.load(storage, asset_info.to_string())
51}
52
53// ----------------x----------------x----------------x----------------
54// ----------------x      WEIGHTS : Store and getter fns     x------
55// ----------------x----------------x----------------x----------------
56
57/// ## Description
58/// Store all token weights
59pub(crate) fn store_weights(
60    deps: DepsMut,
61    asset_weights: Vec<(AssetInfo, Decimal)>,
62) -> StdResult<()> {
63    for (asset_info, weight) in asset_weights.iter() {
64        WEIGHTS.save(deps.storage, asset_info.to_string(), weight)?;
65    }
66
67    Ok(())
68}
69
70/// ## Description
71/// Loads precision of the given asset info.
72pub(crate) fn get_weight(storage: &dyn Storage, asset_info: &AssetInfo) -> StdResult<Decimal> {
73    WEIGHTS.load(storage, asset_info.to_string())
74}
75
76/// ## Description - This struct describes a asset (native or CW20) and its normalized weight
77#[cw_serde]
78pub struct WeightedAsset {
79    /// Information about an asset stored in a [`Asset`] struct
80    pub asset: Asset,
81    /// The weight of the asset
82    pub weight: Decimal,
83}
84
85#[cw_serde]
86pub struct WeightedParams {
87    pub weights: Vec<Asset>,
88    pub exit_fee: Option<Decimal>,
89}