tefi_oracle/
querier.rs

1use cosmwasm_std::{to_binary, Addr, QuerierWrapper, QueryRequest, StdResult, WasmQuery};
2
3use crate::hub::{HubQueryMsg, PriceResponse};
4use crate::proxy::{ProxyBaseQuery, ProxyPriceResponse, ProxyQueryMsg};
5
6/// ## Description
7/// Queries an asset token price from the orcle proxy contract, price is given in the base denomination
8/// ## Parameters
9/// * `proxy_addr` - Proxy contract address
10/// * `symbol` - Symbol of the asset
11pub fn query_proxy_symbol_price(
12    querier: &QuerierWrapper,
13    proxy_addr: &Addr,
14    symbol: String,
15) -> StdResult<ProxyPriceResponse> {
16    let res: ProxyPriceResponse = querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
17        contract_addr: String::from(proxy_addr),
18        msg: to_binary(&ProxyBaseQuery::Base(ProxyQueryMsg::Price { symbol }))?,
19    }))?;
20
21    Ok(res)
22}
23
24/// ## Description
25/// Queries an asstet token price from hub. Hub contract will redirect the query to the corresponding price source.
26/// ## Parameters
27/// * `oracle_hub_addr` - Oracle hub contract address
28/// * `asset_token` - Asset token address. Native assets are not supported
29/// * `timeframe` - (optional) Valid price timeframe in seconds
30pub fn query_asset_price(
31    querier: &QuerierWrapper,
32    oracle_hub_addr: &Addr,
33    asset_token: &Addr,
34    timeframe: Option<u64>,
35) -> StdResult<PriceResponse> {
36    let res: PriceResponse = querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
37        contract_addr: String::from(oracle_hub_addr),
38        msg: to_binary(&HubQueryMsg::Price {
39            asset_token: String::from(asset_token),
40            timeframe,
41        })?,
42    }))?;
43
44    Ok(res)
45}