Skip to content

Commit

Permalink
add contract for cron module
Browse files Browse the repository at this point in the history
  • Loading branch information
joldie777 committed Sep 4, 2024
1 parent 2589bbd commit d921c65
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 36 deletions.
84 changes: 48 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions contracts/cron/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "cron"
version = "0.1.0"
authors = ["joldie777 <[email protected]>"]
edition = "2021"

exclude = [
# Those files are rust-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication.
"contract.wasm",
"hash.txt",
]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
crate-type = ["cdylib", "rlib"]

[features]
library = []

[dependencies]
cosmwasm-std = { workspace = true }
cw2 = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
neutron-sdk = { workspace = true }
cw-storage-plus = { workspace = true }

[dev-dependencies]
cosmwasm-schema = { workspace = true }
64 changes: 64 additions & 0 deletions contracts/cron/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::state::{BEGIN_BLOCKER_SHEDULES, END_BLOCKER_SHEDULES};
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
};
use cw2::set_contract_version;

const CONTRACT_NAME: &str = concat!("crates.io:neutron-contracts__", env!("CARGO_PKG_NAME"));
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[entry_point]
pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> StdResult<Response> {
deps.api.debug("WASMDEBUG: instantiate");
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Ok(Response::default())
}

#[entry_point]
pub fn execute(deps: DepsMut, _: Env, _: MessageInfo, msg: ExecuteMsg) -> StdResult<Response> {
deps.api
.debug(format!("WASMDEBUG: execute: received msg: {:?}", msg).as_str());

match msg {
ExecuteMsg::BeginBlocker { name } => {
let counter = BEGIN_BLOCKER_SHEDULES
.load(deps.storage, name.clone())?
.checked_add(1)
.unwrap_or_default();

BEGIN_BLOCKER_SHEDULES.save(deps.storage, name, &counter)?;

Ok(Response::default())
}
ExecuteMsg::EndBlocker { name } => {
let counter = END_BLOCKER_SHEDULES
.load(deps.storage, name.clone())?
.checked_add(1)
.unwrap_or_default();

END_BLOCKER_SHEDULES.save(deps.storage, name, &counter)?;

Ok(Response::default())
}
}
}

#[entry_point]
pub fn query(deps: Deps, _: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetBeginBlockerScheduleCounter { name } => {
let res = BEGIN_BLOCKER_SHEDULES.load(deps.storage, name)?;
to_json_binary(&res)
}
QueryMsg::GetEndBlockerScheduleCounter { name } => {
let res = END_BLOCKER_SHEDULES.load(deps.storage, name)?;
to_json_binary(&res)
}
}
}
19 changes: 19 additions & 0 deletions contracts/cron/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2022 Neutron
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#![warn(clippy::unwrap_used, clippy::expect_used)]

pub mod contract;
pub mod msg;
pub mod state;
19 changes: 19 additions & 0 deletions contracts/cron/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct InstantiateMsg {}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ExecuteMsg {
BeginBlocker { name: String },
EndBlocker { name: String },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
GetBeginBlockerScheduleCounter { name: String },
GetEndBlockerScheduleCounter { name: String },
}
4 changes: 4 additions & 0 deletions contracts/cron/src/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
use cw_storage_plus::Map;

pub const BEGIN_BLOCKER_SHEDULES: Map<String, u64> = Map::new("begin_blocker_shedules");
pub const END_BLOCKER_SHEDULES: Map<String, u64> = Map::new("end_blocker_shedules");

0 comments on commit d921c65

Please sign in to comment.