Skip to content

Commit

Permalink
Merge pull request #490 from nolus-protocol/release-query
Browse files Browse the repository at this point in the history
Release query
  • Loading branch information
gmanev7 authored Jan 30, 2025
2 parents 9980452 + bc67660 commit 0edd2dd
Show file tree
Hide file tree
Showing 28 changed files with 320 additions and 41 deletions.
2 changes: 2 additions & 0 deletions platform/contracts/admin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@ serde = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }

[dev-dependencies]
platform = { workspace = true, features = ["testing"] }

schema = { workspace = true }
serde_test = { workspace = true }
19 changes: 19 additions & 0 deletions platform/contracts/admin/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@ pub fn query(deps: Deps<'_>, env: Env, msg: QueryMsg) -> ContractResult<Binary>
.and_then(|ref protocol| {
cosmwasm_std::to_json_binary::<ProtocolQueryResponse>(protocol).map_err(Into::into)
}),
QueryMsg::PlatformPackageRelease {} => {
cosmwasm_std::to_json_binary(&CURRENT_RELEASE).map_err(Into::into)
}
}
}

Expand Down Expand Up @@ -321,3 +324,19 @@ fn check_release_label(

Ok(())
}

#[cfg(test)]
mod test {

use platform::tests as platform_tests;

use super::QueryMsg;

#[test]
fn release() {
assert_eq!(
Ok(QueryMsg::PlatformPackageRelease {}),
platform_tests::ser_de(&versioning::query::PlatformPackage::Release {}),
);
}
}
7 changes: 6 additions & 1 deletion platform/contracts/admin/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,15 @@ where
Uint64: Into<CodeId>,
CodeId: Into<Uint64>,
{
InstantiateAddress { code_id: Uint64, protocol: String },
InstantiateAddress {
code_id: Uint64,
protocol: String,
},
Protocols {},
Platform {},
Protocol(String),
/// Implementation of [versioning::query::PlatformPackage::Release]
PlatformPackageRelease {},
}

pub type ProtocolsQueryResponse = Vec<String>;
Expand Down
3 changes: 3 additions & 0 deletions platform/contracts/timealarms/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ pub fn query(deps: Deps<'_>, env: Env, msg: QueryMsg) -> ContractResult<Binary>
QueryMsg::AlarmsStatus {} => Ok(to_json_binary(
&TimeAlarms::new(deps.storage).try_any_alarm(env.block.time)?,
)?),
QueryMsg::PlatformPackageRelease {} => {
cosmwasm_std::to_json_binary(&CURRENT_RELEASE).map_err(Into::into)
}
}
.inspect_err(platform_error::log(deps.api))
}
Expand Down
19 changes: 19 additions & 0 deletions platform/contracts/timealarms/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,13 @@ pub enum ExecuteMsg {
pub enum SudoMsg {}

#[derive(Serialize, Deserialize)]
#[cfg_attr(any(test, feature = "testing"), derive(Debug, PartialEq, Eq))]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub enum QueryMsg {
ContractVersion {},
AlarmsStatus {},
/// Implementation of [versioning::query::PlatformPackage::Release]
PlatformPackageRelease {},
}

#[derive(Serialize, Deserialize, Clone, Copy, Eq, PartialEq, JsonSchema)]
Expand All @@ -63,3 +66,19 @@ pub struct DispatchAlarmsResponse(pub AlarmsCount);
pub struct AlarmsStatusResponse {
pub remaining_alarms: bool,
}

#[cfg(test)]
mod test {

use platform::tests as platform_tests;

use super::QueryMsg;

#[test]
fn release() {
assert_eq!(
Ok(QueryMsg::PlatformPackageRelease {}),
platform_tests::ser_de(&versioning::query::PlatformPackage::Release {}),
);
}
}
1 change: 1 addition & 0 deletions platform/contracts/treasury/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ serde = { workspace = true, features = ["derive"] }
currency = { workspace = true, features = ["testing"] }
lpp-platform = { workspace = true, features = ["testing"] }
oracle-platform = { workspace = true, features = ["testing"] }
platform = { workspace = true, features = ["testing"] }

schema = { workspace = true }
sdk = { workspace = true, default-features = false, features = ["testing"] }
3 changes: 3 additions & 0 deletions platform/contracts/treasury/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ pub fn query(deps: Deps<'_>, env: Env, msg: QueryMsg) -> ContractResult<Binary>
}
QueryMsg::CalculateRewards {} => query_reward_apr(deps.storage, deps.querier, &env)
.and_then(|ref apr| to_json_binary(apr).map_err(ContractError::Serialize)),
QueryMsg::PlatformPackageRelease {} => {
cosmwasm_std::to_json_binary(&CURRENT_RELEASE).map_err(Into::into)
}
}
.inspect_err(platform_error::log(deps.api))
}
Expand Down
18 changes: 18 additions & 0 deletions platform/contracts/treasury/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub enum SudoMsg {
pub enum QueryMsg {
Config {},
CalculateRewards {},
/// Implementation of [versioning::query::PlatformPackage::Release]
PlatformPackageRelease {},
}

// We define a custom struct for each query response
Expand All @@ -51,3 +53,19 @@ pub struct ConfigResponse {
}

pub type RewardScaleResponse = RewardScale;

#[cfg(test)]
mod test {

use platform::tests as platform_tests;

use super::QueryMsg;

#[test]
fn release() {
assert_eq!(
Ok(QueryMsg::PlatformPackageRelease {}),
platform_tests::ser_de(&versioning::query::PlatformPackage::Release {}),
);
}
}
14 changes: 12 additions & 2 deletions platform/packages/platform/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use sdk::cosmwasm_std::{from_json, Binary, Event};
use serde::de::DeserializeOwned;
use std::fmt::Debug;

use sdk::cosmwasm_std::{self, from_json, Binary, Event, StdError};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

pub trait EventSource<'a> {
type EventIter: Iterator<Item = &'a Event>;
Expand Down Expand Up @@ -34,3 +36,11 @@ where
resp.as_ref()
.map(|data| from_json(data).expect("deserialization succeed"))
}

pub fn ser_de<T, R>(obj: &T) -> Result<R, StdError>
where
T: Serialize,
R: Debug + for<'a> Deserialize<'a> + PartialEq,
{
cosmwasm_std::from_json(cosmwasm_std::to_json_vec(obj).expect("serialization succeed"))
}
4 changes: 2 additions & 2 deletions platform/packages/versioning/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ pub use crate::{
error::Error,
protocol::Release as ProtocolRelease,
release::{
Id as ReleaseId, PlatformPackageRelease, ProtocolPackageRelease, ProtocolPackageReleaseId,
UpdatablePackage,
query, Id as ReleaseId, PlatformPackageRelease, ProtocolPackageRelease,
ProtocolPackageReleaseId, UpdatablePackage,
},
software::{PackageRelease as SoftwarePackageRelease, VersionSegment},
};
Expand Down
4 changes: 4 additions & 0 deletions platform/packages/versioning/src/release/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use self::id::Id;
#[cfg(feature = "protocol_contract")]
mod current;
mod id;
pub mod query;

pub trait UpdatablePackage
where
Expand All @@ -26,12 +27,15 @@ where
}

pub type PlatformPackageRelease = SoftwarePackageRelease;
#[derive(Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub struct ProtocolPackageRelease {
software: SoftwarePackageRelease,
protocol: ProtocolRelease,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub struct ProtocolPackageReleaseId {
software: Id,
protocol: Id,
Expand Down
52 changes: 52 additions & 0 deletions platform/packages/versioning/src/release/query.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use serde::Serialize;
use thiserror::Error;

use sdk::cosmwasm_std::{Addr, QuerierWrapper, StdError};

use super::{PlatformPackageRelease, ProtocolPackageRelease};

/// A common versioning API of each platform package
#[derive(Serialize)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub enum PlatformPackage {
/// Query the platform package for its release.
///
/// The result is [versioning::PlatformPackageRelease]
#[serde(rename = "platform_package_release")]
Release {},
}

/// A common versioning API of each protocol package
#[derive(Serialize)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub enum ProtocolPackage {
/// Query the protocol package for its release.
///
/// The result is [versioning::ProtocolPackageRelease]
#[serde(rename = "protocol_package_release")]
Release {},
}

#[derive(Error, Debug, PartialEq)]
pub enum Error {
#[error("[Versioning][Query] {0}")]
Transmission(StdError),
}

pub fn platform_release(
contract: Addr,
querier: QuerierWrapper<'_>,
) -> Result<PlatformPackageRelease, Error> {
querier
.query_wasm_smart(contract, &PlatformPackage::Release {})
.map_err(Error::Transmission)
}

pub fn protocol_release(
contract: Addr,
querier: QuerierWrapper<'_>,
) -> Result<ProtocolPackageRelease, Error> {
querier
.query_wasm_smart(contract, &ProtocolPackage::Release {})
.map_err(Error::Transmission)
}
4 changes: 2 additions & 2 deletions protocol/contracts/lease/examples/lease_schema.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use lease::api::{open::NewLeaseForm, query::StateQuery, ExecuteMsg};
use lease::api::{open::NewLeaseForm, query::QueryMsg, ExecuteMsg};
use sdk::cosmwasm_schema::{export_schema, schema_for};

fn main() {
let out_dir = schema::prep_out_dir().expect("The output directory should be valid");
export_schema(&schema_for!(NewLeaseForm), &out_dir);
export_schema(&schema_for!(ExecuteMsg), &out_dir);
export_schema(&schema_for!(StateQuery), &out_dir);
export_schema(&schema_for!(QueryMsg), &out_dir);
}
34 changes: 30 additions & 4 deletions protocol/contracts/lease/src/api/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,23 @@ use super::{DownpaymentCoin, LeaseAssetCurrencies, LeaseCoin};
pub use opened::ClosePolicy;

#[derive(Deserialize, JsonSchema)]
#[cfg_attr(any(test, feature = "testing"), derive(Clone, Debug, Serialize))]
#[cfg_attr(
any(test, feature = "testing"),
derive(Clone, Debug, PartialEq, Serialize)
)]
#[serde(deny_unknown_fields, rename_all = "snake_case")]
pub struct StateQuery {
pub enum QueryMsg {
/// Ask for estimation of the due and overdue amounts and periods in that point of time
///
/// Return a [StateResponse]
///
/// The value is meaningfull only if the lease is in Opened state.
#[serde(default, rename = "due_projection_secs")]
pub due_projection: Seconds,
State {
#[serde(default, rename = "due_projection_secs")]
due_projection: Seconds,
},
/// Implementation of [versioning::query::ProtocolPackage::Release]
ProtocolPackageRelease {},
}

#[derive(Serialize)]
Expand Down Expand Up @@ -181,3 +190,20 @@ pub(crate) mod paid {
TransferInFinish,
}
}

#[cfg(test)]
mod test {
use platform::tests as platform_tests;

use super::QueryMsg;
#[test]
fn release() {
assert_eq!(
Ok(QueryMsg::ProtocolPackageRelease {}),
platform_tests::ser_de(&versioning::query::ProtocolPackage::Release {}),
);

platform_tests::ser_de::<_, QueryMsg>(&versioning::query::PlatformPackage::Release {})
.unwrap_err();
}
}
27 changes: 15 additions & 12 deletions protocol/contracts/lease/src/contract/endpoins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use versioning::{
};

use crate::{
api::{open::NewLeaseContract, query::StateQuery, ExecuteMsg, MigrateMsg},
api::{open::NewLeaseContract, query::QueryMsg, ExecuteMsg, MigrateMsg},
contract::api::Contract,
error::{ContractError, ContractResult},
};
Expand Down Expand Up @@ -93,17 +93,20 @@ pub fn sudo(deps: DepsMut<'_>, env: Env, msg: SudoMsg) -> ContractResult<CwRespo
}

#[entry_point]
pub fn query(deps: Deps<'_>, env: Env, msg: StateQuery) -> ContractResult<Binary> {
state::load(deps.storage)
.and_then(|state| {
state.state(
env.block.time,
Duration::from_secs(msg.due_projection),
deps.querier,
)
})
.and_then(|resp| to_json_binary(&resp).map_err(Into::into))
.inspect_err(platform_error::log(deps.api))
pub fn query(deps: Deps<'_>, env: Env, msg: QueryMsg) -> ContractResult<Binary> {
match msg {
QueryMsg::State { due_projection } => state::load(deps.storage)
.and_then(|state| {
state.state(
env.block.time,
Duration::from_secs(due_projection),
deps.querier,
)
})
.and_then(|resp| to_json_binary(&resp).map_err(Into::into)),
QueryMsg::ProtocolPackageRelease {} => to_json_binary(&CURRENT_RELEASE).map_err(Into::into),
}
.inspect_err(platform_error::log(deps.api))
}

fn process_lease<ProcFn>(
Expand Down
1 change: 1 addition & 0 deletions protocol/contracts/leaser/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ pub fn sudo(deps: DepsMut<'_>, _env: Env, msg: SudoMsg) -> ContractResult<Respon
pub fn query(deps: Deps<'_>, _env: Env, msg: QueryMsg) -> ContractResult<Binary> {
match msg {
QueryMsg::Config {} => to_json_binary(&Leaser::new(deps).config()?),
QueryMsg::ProtocolPackageRelease {} => to_json_binary(&CURRENT_RELEASE),
QueryMsg::Quote {
downpayment,
lease_asset,
Expand Down
Loading

0 comments on commit 0edd2dd

Please sign in to comment.