Skip to content

Commit

Permalink
feat(template): update template with new from cosmwasm
Browse files Browse the repository at this point in the history
  • Loading branch information
bdeneux committed Oct 18, 2022
1 parent 4486496 commit 9656cca
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 160 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ cw2 = "0.15.1"
schemars = "0.8.11"
serde = { version = "1.0.144", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.37" }
cosmwasm-schema = "1.1.4"
cosmwasm-schema = "1.1.3"
cw-multi-test = "0.15.1"
6 changes: 3 additions & 3 deletions contracts/cw-template/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[package]
authors = ["OKP4"]
edition = "2021"
edition = "2022"
name = "cw-template"
version = "0.1.0"
version = "0.2.0"

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.
Expand All @@ -27,6 +27,7 @@ panic = 'abort'
rpath = false

[dependencies]
cosmwasm-schema.workspace = true
cosmwasm-std.workspace = true
cosmwasm-storage.workspace = true
cw-storage-plus.workspace = true
Expand All @@ -36,7 +37,6 @@ serde.workspace = true
thiserror.workspace = true

[dev-dependencies]
cosmwasm-schema.workspace = true
cw-multi-test.workspace = true

[features]
Expand Down
20 changes: 0 additions & 20 deletions contracts/cw-template/examples/schema.rs

This file was deleted.

12 changes: 0 additions & 12 deletions contracts/cw-template/schema/count_response.json

This file was deleted.

33 changes: 0 additions & 33 deletions contracts/cw-template/schema/execute_msg.json

This file was deleted.

12 changes: 0 additions & 12 deletions contracts/cw-template/schema/instantiate_msg.json

This file was deleted.

16 changes: 0 additions & 16 deletions contracts/cw-template/schema/query_msg.json

This file was deleted.

21 changes: 0 additions & 21 deletions contracts/cw-template/schema/state.json

This file was deleted.

11 changes: 11 additions & 0 deletions contracts/cw-template/src/bin/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use cosmwasm_schema::write_api;

use cw_template::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

fn main() {
write_api! {
instantiate: InstantiateMsg,
execute: ExecuteMsg,
query: QueryMsg,
}
}
62 changes: 35 additions & 27 deletions contracts/cw-template/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use cosmwasm_std::{to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response,
use cw2::set_contract_version;

use crate::error::ContractError;
use crate::msg::{CountResponse, ExecuteMsg, InstantiateMsg, QueryMsg};
use crate::msg::{ExecuteMsg, GetCountResponse, InstantiateMsg, QueryMsg};
use crate::state::{State, STATE};

// version info for migration info
const CONTRACT_NAME: &str = "crates.io:cw-template";
const CONTRACT_NAME: &str = "crates.io:template-contract";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");

#[cfg_attr(not(feature = "library"), entry_point)]
Expand Down Expand Up @@ -39,41 +39,49 @@ pub fn execute(
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Increment {} => try_increment(deps),
ExecuteMsg::Reset { count } => try_reset(deps, info, count),
ExecuteMsg::Increment {} => execute::increment(deps),
ExecuteMsg::Reset { count } => execute::reset(deps, info, count),
}
}

pub fn try_increment(deps: DepsMut) -> Result<Response, ContractError> {
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
state.count += 1;
Ok(state)
})?;
pub mod execute {
use super::*;

Ok(Response::new().add_attribute("method", "try_increment"))
}
pub fn increment(deps: DepsMut) -> Result<Response, ContractError> {
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
state.count += 1;
Ok(state)
})?;

pub fn try_reset(deps: DepsMut, info: MessageInfo, count: i32) -> Result<Response, ContractError> {
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
if info.sender != state.owner {
return Err(ContractError::Unauthorized {});
}
state.count = count;
Ok(state)
})?;
Ok(Response::new().add_attribute("method", "reset"))
Ok(Response::new().add_attribute("action", "increment"))
}

pub fn reset(deps: DepsMut, info: MessageInfo, count: i32) -> Result<Response, ContractError> {
STATE.update(deps.storage, |mut state| -> Result<_, ContractError> {
if info.sender != state.owner {
return Err(ContractError::Unauthorized {});
}
state.count = count;
Ok(state)
})?;
Ok(Response::new().add_attribute("action", "reset"))
}
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::GetCount {} => to_binary(&query_count(deps)?),
QueryMsg::GetCount {} => to_binary(&query::count(deps)?),
}
}

fn query_count(deps: Deps) -> StdResult<CountResponse> {
let state = STATE.load(deps.storage)?;
Ok(CountResponse { count: state.count })
pub mod query {
use super::*;

pub fn count(deps: Deps) -> StdResult<GetCountResponse> {
let state = STATE.load(deps.storage)?;
Ok(GetCountResponse { count: state.count })
}
}

#[cfg(test)]
Expand All @@ -95,7 +103,7 @@ mod tests {

// it worked, let's query the state
let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
let value: CountResponse = from_binary(&res).unwrap();
let value: GetCountResponse = from_binary(&res).unwrap();
assert_eq!(17, value.count);
}

Expand All @@ -114,7 +122,7 @@ mod tests {

// should increase counter by 1
let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
let value: CountResponse = from_binary(&res).unwrap();
let value: GetCountResponse = from_binary(&res).unwrap();
assert_eq!(18, value.count);
}

Expand Down Expand Up @@ -142,7 +150,7 @@ mod tests {

// should now be 5
let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
let value: CountResponse = from_binary(&res).unwrap();
let value: GetCountResponse = from_binary(&res).unwrap();
assert_eq!(5, value.count);
}
}
3 changes: 0 additions & 3 deletions contracts/cw-template/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ pub enum ContractError {

#[error("Unauthorized")]
Unauthorized {},

#[error("Custom Error val: {val:?}")]
CustomError { val: String },
// Add any other custom errors you like here.
// Look at https://docs.rs/thiserror/1.0.21/thiserror/ for details.
}
6 changes: 3 additions & 3 deletions contracts/cw-template/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use cosmwasm_std::{
to_binary, Addr, CosmosMsg, CustomQuery, Querier, QuerierWrapper, StdResult, WasmMsg, WasmQuery,
};

use crate::msg::{CountResponse, ExecuteMsg, QueryMsg};
use crate::msg::{ExecuteMsg, GetCountResponse, QueryMsg};

/// CwTemplateContract is a wrapper around Addr that provides a lot of helpers
/// for working with this.
Expand All @@ -28,7 +28,7 @@ impl CwTemplateContract {
}

/// Get Count
pub fn count<Q, T, CQ>(&self, querier: &Q) -> StdResult<CountResponse>
pub fn count<Q, T, CQ>(&self, querier: &Q) -> StdResult<GetCountResponse>
where
Q: Querier,
T: Into<String>,
Expand All @@ -40,7 +40,7 @@ impl CwTemplateContract {
msg: to_binary(&msg)?,
}
.into();
let res: CountResponse = QuerierWrapper::<CQ>::new(querier).query(&query)?;
let res: GetCountResponse = QuerierWrapper::<CQ>::new(querier).query(&query)?;
Ok(res)
}
}
17 changes: 8 additions & 9 deletions contracts/cw-template/src/msg.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use cosmwasm_schema::{cw_serde, QueryResponses};

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

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
pub enum ExecuteMsg {
Increment {},
Reset { count: i32 },
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
#[cw_serde]
#[derive(QueryResponses)]
pub enum QueryMsg {
// GetCount returns the current count as a json-encoded number
#[returns(GetCountResponse)]
GetCount {},
}

// We define a custom struct for each query response
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct CountResponse {
#[cw_serde]
pub struct GetCountResponse {
pub count: i32,
}

0 comments on commit 9656cca

Please sign in to comment.