Skip to content

Commit c59c003

Browse files
author
Dev Kalra
authored
add query helper functions (#587)
1 parent ca1a03b commit c59c003

File tree

1 file changed

+42
-0
lines changed
  • target_chains/cosmwasm/pyth-sdk-cw/src

1 file changed

+42
-0
lines changed

target_chains/cosmwasm/pyth-sdk-cw/src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ use {
1212
QueryResponses,
1313
},
1414
cosmwasm_std::{
15+
to_binary,
16+
Addr,
1517
Binary,
1618
Coin,
19+
QuerierWrapper,
20+
QueryRequest,
21+
StdResult,
22+
WasmQuery,
1723
},
1824
std::time::Duration,
1925
};
@@ -40,3 +46,39 @@ pub enum QueryMsg {
4046
pub struct PriceFeedResponse {
4147
pub price_feed: PriceFeed,
4248
}
49+
50+
/// Queries the price on-chain
51+
pub fn query_price_feed(
52+
querier: &QuerierWrapper,
53+
contract_addr: Addr,
54+
id: PriceIdentifier,
55+
) -> StdResult<PriceFeedResponse> {
56+
let price_feed_response = querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
57+
contract_addr: contract_addr.into_string(),
58+
msg: to_binary(&QueryMsg::PriceFeed { id })?,
59+
}))?;
60+
Ok(price_feed_response)
61+
}
62+
63+
/// Get the fee required in order to update the on-chain state with the provided
64+
/// `price_update_vaas`.
65+
pub fn get_update_fee(
66+
querier: &QuerierWrapper,
67+
contract_addr: Addr,
68+
price_update_vaas: &[Binary],
69+
) -> StdResult<Coin> {
70+
querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
71+
contract_addr: contract_addr.into_string(),
72+
msg: to_binary(&QueryMsg::GetUpdateFee {
73+
vaas: price_update_vaas.to_vec(),
74+
})?,
75+
}))
76+
}
77+
78+
/// Get the default length of time for which a price update remains valid.
79+
pub fn get_valid_time_period(querier: &QuerierWrapper, contract_addr: Addr) -> StdResult<Duration> {
80+
querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
81+
contract_addr: contract_addr.into_string(),
82+
msg: to_binary(&QueryMsg::GetValidTimePeriod)?,
83+
}))
84+
}

0 commit comments

Comments
 (0)