diff --git a/common/cosmwasm-smart-contracts/coconut-dkg-contract/src/msg.rs b/common/cosmwasm-smart-contracts/coconut-dkg-contract/src/msg.rs index 235575ede8..2bc27300a0 100644 --- a/common/cosmwasm-smart-contracts/coconut-dkg-contract/src/msg.rs +++ b/common/cosmwasm-smart-contracts/coconut-dkg-contract/src/msg.rs @@ -23,7 +23,9 @@ pub enum ExecuteMsg { #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] -pub enum QueryMsg {} +pub enum QueryMsg { + GetCurrentEpoch {}, +} #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)] #[serde(rename_all = "snake_case")] diff --git a/contracts/coconut-dkg/src/epoch/queries.rs b/contracts/coconut-dkg/src/epoch/queries.rs index 87d5c392c8..e20e2df730 100644 --- a/contracts/coconut-dkg/src/epoch/queries.rs +++ b/contracts/coconut-dkg/src/epoch/queries.rs @@ -1,2 +1,11 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 + +use super::storage; +use crate::ContractError; +use coconut_dkg_common::types::Epoch; +use cosmwasm_std::Storage; + +pub(crate) fn query_current_epoch(storage: &dyn Storage) -> Result { + storage::current_epoch(storage) +} diff --git a/contracts/coconut-dkg/src/epoch/storage.rs b/contracts/coconut-dkg/src/epoch/storage.rs index 3ba73a58c5..c1c7b7e631 100644 --- a/contracts/coconut-dkg/src/epoch/storage.rs +++ b/contracts/coconut-dkg/src/epoch/storage.rs @@ -1,7 +1,15 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use crate::ContractError; use coconut_dkg_common::types::Epoch; +use cosmwasm_std::Storage; use cw_storage_plus::Item; -pub(crate) const CURRENT_EPOCH: Item = Item::new("current_epoch"); +pub(crate) const CURRENT_EPOCH: Item<'_, Epoch> = Item::new("current_epoch"); + +pub(crate) fn current_epoch(storage: &dyn Storage) -> Result { + CURRENT_EPOCH + .load(storage) + .map_err(|_| ContractError::EpochNotInitialised) +} diff --git a/contracts/coconut-dkg/src/error.rs b/contracts/coconut-dkg/src/error.rs index 5679cee0d3..14a02125d0 100644 --- a/contracts/coconut-dkg/src/error.rs +++ b/contracts/coconut-dkg/src/error.rs @@ -44,4 +44,7 @@ pub enum ContractError { #[error("This sender is already a dealer for the epoch")] AlreadyADealer, + + #[error("Epoch hasn't been correctly initialised!")] + EpochNotInitialised, } diff --git a/contracts/coconut-dkg/src/lib.rs b/contracts/coconut-dkg/src/lib.rs index 37158efa3d..d213963367 100644 --- a/contracts/coconut-dkg/src/lib.rs +++ b/contracts/coconut-dkg/src/lib.rs @@ -1,10 +1,13 @@ // Copyright 2022 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use crate::epoch::queries::query_current_epoch; use crate::error::ContractError; use coconut_dkg_common::msg::{ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg}; use coconut_dkg_common::types::{Epoch, EpochState}; -use cosmwasm_std::{entry_point, Deps, DepsMut, Env, MessageInfo, QueryResponse, Response}; +use cosmwasm_std::{ + entry_point, to_binary, Deps, DepsMut, Env, MessageInfo, QueryResponse, Response, +}; use epoch::storage as epoch_storage; mod constants; @@ -63,12 +66,12 @@ pub fn execute( } #[entry_point] -pub fn query(_deps: Deps<'_>, _env: Env, msg: QueryMsg) -> Result { - match msg { - _ => (), - } +pub fn query(deps: Deps<'_>, _env: Env, msg: QueryMsg) -> Result { + let response = match msg { + QueryMsg::GetCurrentEpoch {} => to_binary(&query_current_epoch(deps.storage)?)?, + }; - Ok(Default::default()) + Ok(response) } #[entry_point]