|
| 1 | +use cosmwasm_std::{entry_point, Decimal, DepsMut, Env, IbcChannel, Response, Timestamp}; |
| 2 | +use cw2::set_contract_version; |
| 3 | +use cw_storage_plus::Item; |
| 4 | +use cw_utils::nonpayable; |
| 5 | +use mesh_apis::price_feed_api::SudoMsg; |
| 6 | +use sylvia::types::{InstantiateCtx, QueryCtx}; |
| 7 | +use sylvia::{contract, schemars}; |
| 8 | + |
| 9 | +use mesh_apis::price_feed_api::{self, PriceFeedApi, PriceResponse}; |
| 10 | + |
| 11 | +use crate::error::ContractError; |
| 12 | +use crate::ibc::{make_ibc_packet, AUTH_ENDPOINT}; |
| 13 | +use crate::msg::AuthorizedEndpoint; |
| 14 | +use crate::price_keeper::PriceKeeper; |
| 15 | +use crate::scheduler::{Action, Scheduler}; |
| 16 | +use crate::state::TradingPair; |
| 17 | + |
| 18 | +pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME"); |
| 19 | +pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); |
| 20 | + |
| 21 | +pub struct RemotePriceFeedContract { |
| 22 | + pub channel: Item<'static, IbcChannel>, |
| 23 | + pub trading_pair: Item<'static, TradingPair>, |
| 24 | + pub price_keeper: PriceKeeper, |
| 25 | + pub scheduler: Scheduler<Box<dyn Action>>, |
| 26 | +} |
| 27 | + |
| 28 | +impl Default for RemotePriceFeedContract { |
| 29 | + fn default() -> Self { |
| 30 | + Self::new() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +#[cfg_attr(not(feature = "library"), sylvia::entry_points)] |
| 35 | +#[contract] |
| 36 | +#[error(ContractError)] |
| 37 | +#[messages(price_feed_api as PriceFeedApi)] |
| 38 | +impl RemotePriceFeedContract { |
| 39 | + pub fn new() -> Self { |
| 40 | + Self { |
| 41 | + channel: Item::new("channel"), |
| 42 | + trading_pair: Item::new("tpair"), |
| 43 | + price_keeper: PriceKeeper::new(), |
| 44 | + // TODO: the indirection can be removed once Sylvia supports |
| 45 | + // generics. The constructor can then probably be constant. |
| 46 | + // |
| 47 | + // Stable existential types would be even better! |
| 48 | + // https://github.com/rust-lang/rust/issues/63063 |
| 49 | + scheduler: Scheduler::new(Box::new(query_twap)), |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + #[msg(instantiate)] |
| 54 | + pub fn instantiate( |
| 55 | + &self, |
| 56 | + mut ctx: InstantiateCtx, |
| 57 | + trading_pair: TradingPair, |
| 58 | + auth_endpoint: AuthorizedEndpoint, |
| 59 | + epoch_in_secs: u64, |
| 60 | + price_info_ttl_in_secs: u64, |
| 61 | + ) -> Result<Response, ContractError> { |
| 62 | + nonpayable(&ctx.info)?; |
| 63 | + |
| 64 | + set_contract_version(ctx.deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; |
| 65 | + self.trading_pair.save(ctx.deps.storage, &trading_pair)?; |
| 66 | + |
| 67 | + self.price_keeper |
| 68 | + .init(&mut ctx.deps, price_info_ttl_in_secs)?; |
| 69 | + self.scheduler.init(&mut ctx.deps, epoch_in_secs)?; |
| 70 | + |
| 71 | + AUTH_ENDPOINT.save(ctx.deps.storage, &auth_endpoint)?; |
| 72 | + |
| 73 | + Ok(Response::new()) |
| 74 | + } |
| 75 | + |
| 76 | + pub(crate) fn update_twap( |
| 77 | + &self, |
| 78 | + deps: DepsMut, |
| 79 | + time: Timestamp, |
| 80 | + twap: Decimal, |
| 81 | + ) -> Result<(), ContractError> { |
| 82 | + Ok(self.price_keeper.update(deps, time, twap)?) |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[contract] |
| 87 | +#[messages(price_feed_api as PriceFeedApi)] |
| 88 | +impl PriceFeedApi for RemotePriceFeedContract { |
| 89 | + type Error = ContractError; |
| 90 | + |
| 91 | + /// Return the price of the foreign token. That is, how many native tokens |
| 92 | + /// are needed to buy one foreign token. |
| 93 | + #[msg(query)] |
| 94 | + fn price(&self, ctx: QueryCtx) -> Result<PriceResponse, Self::Error> { |
| 95 | + Ok(self |
| 96 | + .price_keeper |
| 97 | + .price(ctx.deps, &ctx.env) |
| 98 | + .map(|rate| PriceResponse { |
| 99 | + native_per_foreign: rate, |
| 100 | + })?) |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 105 | +pub fn sudo(deps: DepsMut, env: Env, msg: SudoMsg) -> Result<Response, ContractError> { |
| 106 | + let contract = RemotePriceFeedContract::new(); |
| 107 | + |
| 108 | + match msg { |
| 109 | + SudoMsg::HandleEpoch {} => contract.scheduler.trigger(deps, &env), |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +pub fn query_twap(deps: DepsMut, env: &Env) -> Result<Response, ContractError> { |
| 114 | + let contract = RemotePriceFeedContract::new(); |
| 115 | + let TradingPair { |
| 116 | + pool_id, |
| 117 | + base_asset, |
| 118 | + quote_asset, |
| 119 | + } = contract.trading_pair.load(deps.storage)?; |
| 120 | + |
| 121 | + let channel = contract |
| 122 | + .channel |
| 123 | + .may_load(deps.storage)? |
| 124 | + .ok_or(ContractError::IbcChannelNotOpen)?; |
| 125 | + |
| 126 | + let packet = mesh_apis::ibc::RemotePriceFeedPacket::QueryTwap { |
| 127 | + pool_id, |
| 128 | + base_asset, |
| 129 | + quote_asset, |
| 130 | + }; |
| 131 | + let msg = make_ibc_packet(&env.block.time, channel, packet)?; |
| 132 | + |
| 133 | + Ok(Response::new().add_message(msg)) |
| 134 | +} |
| 135 | + |
| 136 | +#[cfg(test)] |
| 137 | +mod tests { |
| 138 | + use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; |
| 139 | + |
| 140 | + use super::*; |
| 141 | + |
| 142 | + #[test] |
| 143 | + fn instantiation() { |
| 144 | + let mut deps = mock_dependencies(); |
| 145 | + let env = mock_env(); |
| 146 | + let info = mock_info("sender", &[]); |
| 147 | + let contract = RemotePriceFeedContract::new(); |
| 148 | + |
| 149 | + let trading_pair = TradingPair { |
| 150 | + pool_id: 1, |
| 151 | + base_asset: "base".to_string(), |
| 152 | + quote_asset: "quote".to_string(), |
| 153 | + }; |
| 154 | + let auth_endpoint = AuthorizedEndpoint { |
| 155 | + connection_id: "connection".to_string(), |
| 156 | + port_id: "port".to_string(), |
| 157 | + }; |
| 158 | + |
| 159 | + contract |
| 160 | + .instantiate( |
| 161 | + InstantiateCtx { |
| 162 | + deps: deps.as_mut(), |
| 163 | + env, |
| 164 | + info, |
| 165 | + }, |
| 166 | + trading_pair, |
| 167 | + auth_endpoint, |
| 168 | + 10, |
| 169 | + 50, |
| 170 | + ) |
| 171 | + .unwrap(); |
| 172 | + } |
| 173 | +} |
0 commit comments