astroport_oracle/
state.rs

1use cosmwasm_schema::cw_serde;
2
3use astroport::asset::{AssetInfo, PairInfo};
4use cosmwasm_std::{Addr, Decimal256, DepsMut, StdResult, Storage, Uint128};
5use cw_storage_plus::{Item, Map};
6
7/// Stores the contract config at the given key
8pub const CONFIG: Item<Config> = Item::new("config");
9/// Stores the latest cumulative and average prices at the given key
10pub const PRICE_LAST: Item<PriceCumulativeLast> = Item::new("price_last");
11
12/// This structure stores the latest cumulative and average token prices for the target pool
13#[cw_serde]
14pub struct PriceCumulativeLast {
15    /// The vector contains last cumulative prices for each pair of assets in the pool
16    pub cumulative_prices: Vec<(AssetInfo, AssetInfo, Uint128)>,
17    /// The vector contains average prices for each pair of assets in the pool
18    pub average_prices: Vec<(AssetInfo, AssetInfo, Decimal256)>,
19    /// The last timestamp block in pool
20    pub block_timestamp_last: u64,
21}
22
23/// Global configuration for the contract
24#[cw_serde]
25pub struct Config {
26    /// The address that's allowed to change contract parameters
27    pub owner: Addr,
28    /// The factory contract address
29    pub factory: Addr,
30    /// The assets in the pool. Each asset is described using a [`AssetInfo`]
31    pub asset_infos: Vec<AssetInfo>,
32    /// Information about the pair (LP token address, pair type etc)
33    pub pair: PairInfo,
34}
35
36/// Stores map of AssetInfo (as String) -> precision
37const PRECISIONS: Map<String, u8> = Map::new("precisions");
38
39/// Store all token precisions and return the greatest one.
40pub(crate) fn store_precisions(
41    deps: DepsMut,
42    asset_info: &AssetInfo,
43    factory_contract: &Addr,
44) -> StdResult<()> {
45    let precision = asset_info.decimals(&deps.querier, factory_contract)?;
46    PRECISIONS.save(deps.storage, asset_info.to_string(), &precision)?;
47
48    Ok(())
49}
50
51/// Loads precision of the given asset info.
52pub(crate) fn get_precision(storage: &dyn Storage, asset_info: &AssetInfo) -> StdResult<u8> {
53    PRECISIONS.load(storage, asset_info.to_string())
54}