Skip to content

Commit

Permalink
feat: pausable mock
Browse files Browse the repository at this point in the history
  • Loading branch information
ratik committed Jul 10, 2024
1 parent 0406eae commit 8b62597
Show file tree
Hide file tree
Showing 11 changed files with 220 additions and 0 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions contracts/pausable/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[alias]
wasm = "build --release --target wasm32-unknown-unknown"
wasm-debug = "build --target wasm32-unknown-unknown"
unit-test = "test --lib"
integration-test = "test --test integration"
schema = "run --example pausable-schema"
33 changes: 33 additions & 0 deletions contracts/pausable/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "pausable"
version = "0.1.0"
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]
# for quicker tests, cargo test --lib
library = []

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


[dev-dependencies]
cosmwasm-schema = { workspace = true }
5 changes: 5 additions & 0 deletions contracts/pausable/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Description

Simple contract for integration tests.

Can be paused and unpaused by anybody
30 changes: 30 additions & 0 deletions contracts/pausable/examples/pausable-schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// 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.

use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};

use pausable::msg::{ExecuteMsg, InstantiateMsg};

fn main() {
let mut out_dir = current_dir().unwrap();
out_dir.push("schema");
create_dir_all(&out_dir).unwrap();
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InstantiateMsg), &out_dir);
export_schema(&schema_for!(ExecuteMsg), &out_dir);
}
30 changes: 30 additions & 0 deletions contracts/pausable/schema/execute_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ExecuteMsg",
"oneOf": [
{
"type": "object",
"required": [
"pause"
],
"properties": {
"pause": {
"type": "object"
}
},
"additionalProperties": false
},
{
"type": "object",
"required": [
"unpause"
],
"properties": {
"unpause": {
"type": "object"
}
},
"additionalProperties": false
}
]
}
5 changes: 5 additions & 0 deletions contracts/pausable/schema/instantiate_msg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "InstantiateMsg",
"type": "object"
}
57 changes: 57 additions & 0 deletions contracts/pausable/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::{
msg::{ExecuteMsg, InstantiateMsg, QueryMsg},
state::PAUSED,
};
use cosmwasm_std::{
entry_point, to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
};
use cw2::set_contract_version;
use neutron_sdk::bindings::msg::NeutronMsg;
use neutron_sdk::NeutronResult;

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<NeutronMsg>> {
match msg {
ExecuteMsg::Pause {} => execute_update_pause(deps, true),
ExecuteMsg::Unpause {} => execute_update_pause(deps, false),
}
}

fn execute_update_pause(deps: DepsMut, paused: bool) -> StdResult<Response<NeutronMsg>> {
PAUSED.save(deps.storage, &paused)?;
Ok(Response::default())
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _: Env, msg: QueryMsg) -> NeutronResult<Binary> {
deps.api
.debug(format!("WASMDEBUG: execute: received msg: {:?}", msg).as_str());
match msg {
QueryMsg::State {} => query_paused(deps),
}
}

fn query_paused(deps: Deps) -> NeutronResult<Binary> {
let paused = PAUSED.load(deps.storage)?;
Ok(to_json_binary(&paused)?)
}
19 changes: 19 additions & 0 deletions contracts/pausable/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;
18 changes: 18 additions & 0 deletions contracts/pausable/src/msg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 {
Pause {},
Unpause {},
}

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

pub const PAUSED: Item<bool> = Item::new("paused");

0 comments on commit 8b62597

Please sign in to comment.