#![allow(clippy::module_inception)]
use std::fmt;
use std::fmt::Display;
use cosmwasm_schema::{cw_serde, QueryResponses};
use cosmwasm_std::{Deps, StdResult, Timestamp, Uint64};
use cw_ownable::{cw_ownable_execute, cw_ownable_query};
#[cw_serde]
pub struct InstantiateMsg {
pub owner: String,
pub epoch_config: EpochConfig,
}
#[cw_ownable_execute]
#[cw_serde]
pub enum ExecuteMsg {
UpdateConfig {
epoch_config: Option<EpochConfig>,
},
}
#[cw_ownable_query]
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
#[returns(ConfigResponse)]
Config {},
#[returns(EpochResponse)]
CurrentEpoch {},
#[returns(EpochResponse)]
Epoch {
id: u64,
},
}
#[cw_serde]
pub struct MigrateMsg {}
#[cw_serde]
#[derive(Default)]
pub struct Epoch {
pub id: u64,
pub start_time: Timestamp,
}
impl Epoch {
pub fn to_epoch_response(self) -> EpochResponse {
EpochResponse { epoch: self }
}
}
impl Display for Epoch {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"Epoch {{ id: {}, start_time: {} }}",
self.id, self.start_time,
)
}
}
#[cw_serde]
pub struct EpochConfig {
pub duration: Uint64,
pub genesis_epoch: Uint64,
}
impl Display for EpochConfig {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"EpochConfig {{ epoch_duration: {}, genesis_epoch: {}, }}",
self.duration, self.genesis_epoch
)
}
}
pub type ConfigResponse = Config;
#[cw_serde]
pub struct Config {
pub epoch_config: EpochConfig,
}
#[cw_serde]
pub struct EpochResponse {
pub epoch: Epoch,
}
pub fn get_current_epoch(deps: Deps, epoch_manager_addr: String) -> StdResult<Epoch> {
let epoch_response: EpochResponse = deps
.querier
.query_wasm_smart(epoch_manager_addr, &QueryMsg::CurrentEpoch {})?;
Ok(epoch_response.epoch)
}