pyth_sdk_cw/
lib.rs

1pub mod error;
2pub mod testing;
3
4pub use pyth_sdk::{
5    Price,
6    PriceFeed,
7    PriceIdentifier,
8    UnixTimestamp,
9};
10use {
11    cosmwasm_schema::{
12        cw_serde,
13        QueryResponses,
14    },
15    cosmwasm_std::{
16        to_binary,
17        Addr,
18        Binary,
19        Coin,
20        QuerierWrapper,
21        QueryRequest,
22        StdResult,
23        WasmQuery,
24    },
25    std::time::Duration,
26};
27
28#[derive(Eq)]
29#[cw_serde]
30pub enum ExecuteMsg {
31    UpdatePriceFeeds { data: Vec<Binary> },
32    ExecuteGovernanceInstruction { data: Binary },
33}
34
35#[cw_serde]
36#[derive(QueryResponses)]
37pub enum QueryMsg {
38    #[returns(PriceFeedResponse)]
39    PriceFeed { id: PriceIdentifier },
40    #[returns(Coin)]
41    GetUpdateFee { vaas: Vec<Binary> },
42    #[cfg(feature = "osmosis")]
43    #[returns(Coin)]
44    GetUpdateFeeForDenom { denom: String, vaas: Vec<Binary> },
45    #[returns(Duration)]
46    GetValidTimePeriod,
47}
48
49#[cw_serde]
50pub struct PriceFeedResponse {
51    pub price_feed: PriceFeed,
52}
53
54/// Queries the price on-chain
55pub fn query_price_feed(
56    querier: &QuerierWrapper,
57    contract_addr: Addr,
58    id: PriceIdentifier,
59) -> StdResult<PriceFeedResponse> {
60    let price_feed_response = querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
61        contract_addr: contract_addr.into_string(),
62        msg:           to_binary(&QueryMsg::PriceFeed { id })?,
63    }))?;
64    Ok(price_feed_response)
65}
66
67/// Get the fee required in order to update the on-chain state with the provided
68/// `price_update_vaas`.
69pub fn get_update_fee(
70    querier: &QuerierWrapper,
71    contract_addr: Addr,
72    price_update_vaas: &[Binary],
73) -> StdResult<Coin> {
74    querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
75        contract_addr: contract_addr.into_string(),
76        msg:           to_binary(&QueryMsg::GetUpdateFee {
77            vaas: price_update_vaas.to_vec(),
78        })?,
79    }))
80}
81
82#[cfg(feature = "osmosis")]
83/// Get the fee required in order to update the on-chain state with the provided
84/// `price_update_vaas`.
85pub fn get_update_fee_for_denom(
86    querier: &QuerierWrapper,
87    contract_addr: Addr,
88    price_update_vaas: &[Binary],
89    denom: String,
90) -> StdResult<Coin> {
91    querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
92        contract_addr: contract_addr.into_string(),
93        msg:           to_binary(&QueryMsg::GetUpdateFeeForDenom {
94            vaas: price_update_vaas.to_vec(),
95            denom,
96        })?,
97    }))
98}
99
100/// Get the default length of time for which a price update remains valid.
101pub fn get_valid_time_period(querier: &QuerierWrapper, contract_addr: Addr) -> StdResult<Duration> {
102    querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
103        contract_addr: contract_addr.into_string(),
104        msg:           to_binary(&QueryMsg::GetValidTimePeriod)?,
105    }))
106}