Skip to content

Commit

Permalink
Update the contract.rs file with code for a barebones code
Browse files Browse the repository at this point in the history
  • Loading branch information
emperorjm committed Oct 11, 2024
1 parent 79bb302 commit f311a76
Showing 1 changed file with 22 additions and 139 deletions.
161 changes: 22 additions & 139 deletions blank/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,158 +1,41 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{% raw %}{{% endraw %}{% unless version == "minimal" %}to_binary, {% endunless %}Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
{% if version == "minimal" %}// {% endif %}use cw2::set_contract_version;
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult};
// use cw2::set_contract_version;

use crate::error::ContractError;
use crate::msg::{ExecuteMsg, {% unless version == "minimal" %}GetCountResponse, {% endunless %}InstantiateMsg, QueryMsg};
{% unless version == "minimal" %}use crate::state::{State, STATE};
{% endunless %}
{% if version == "minimal" %}/*
{% endif %}// version info for migration info
const CONTRACT_NAME: &str = "crates.io:{{project-name}}";
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};

/*
// version info for migration info
const CONTRACT_NAME: &str = "crates.io:blank";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
{% if version == "minimal" %}*/
{% endif %}
*/

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
{% if version == "minimal" %}_{% endif %}deps: DepsMut,
_deps: DepsMut,
_env: Env,
{% if version == "minimal" %}_{% endif %}info: MessageInfo,
{% if version == "minimal" %}_{% endif %}msg: InstantiateMsg,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> Result<Response, ContractError> {
{% if version == "minimal" %}unimplemented!(){% else %}let state = State {
count: msg.count,
owner: info.sender.clone(),
};
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
STATE.save(deps.storage, &state)?;

Ok(Response::new()
.add_attribute("method", "instantiate")
.add_attribute("owner", info.sender)
.add_attribute("count", msg.count.to_string())){% endif %}
unimplemented!()
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
{% if version == "minimal" %}_{% endif %}deps: DepsMut,
_deps: DepsMut,
_env: Env,
{% if version == "minimal" %}_{% endif %}info: MessageInfo,
{% if version == "minimal" %}_{% endif %}msg: ExecuteMsg,
_info: MessageInfo,
_msg: ExecuteMsg,
) -> Result<Response, ContractError> {
{% if version == "minimal" %}unimplemented!(){% else %}match msg {
ExecuteMsg::Increment {} => execute::increment(deps),
ExecuteMsg::Reset { count } => execute::reset(deps, info, count),
}{% endif %}
}{% unless version == "minimal" %}

pub mod execute {
use super::*;

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

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"))
}
}{% endunless %}
unimplemented!()
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query({% if version == "minimal" %}_{% endif %}deps: Deps, _env: Env, {% if version == "minimal" %}_{% endif %}msg: QueryMsg) -> StdResult<Binary> {
{% if version == "minimal" %}unimplemented!(){% else %}match msg {
QueryMsg::GetCount {} => to_binary(&query::count(deps)?),
}{% endif %}
}{% unless version == "minimal" %}

pub mod query {
use super::*;

pub fn count(deps: Deps) -> StdResult<GetCountResponse> {
let state = STATE.load(deps.storage)?;
Ok(GetCountResponse { count: state.count })
}
}{% endunless %}
pub fn query(_deps: Deps, _env: Env, _msg: QueryMsg) -> StdResult<Binary> {
unimplemented!()
}

#[cfg(test)]
mod tests {% raw %}{{% endraw %}{% unless version == "minimal" %}
use super::*;
use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info};
use cosmwasm_std::{coins, from_binary};

#[test]
fn proper_initialization() {
let mut deps = mock_dependencies();

let msg = InstantiateMsg { count: 17 };
let info = mock_info("creator", &coins(1000, "earth"));

// we can just call .unwrap() to assert this was a success
let res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();
assert_eq!(0, res.messages.len());

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

#[test]
fn increment() {
let mut deps = mock_dependencies();

let msg = InstantiateMsg { count: 17 };
let info = mock_info("creator", &coins(2, "token"));
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

// beneficiary can release it
let info = mock_info("anyone", &coins(2, "token"));
let msg = ExecuteMsg::Increment {};
let _res = execute(deps.as_mut(), mock_env(), info, msg).unwrap();

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

#[test]
fn reset() {
let mut deps = mock_dependencies();

let msg = InstantiateMsg { count: 17 };
let info = mock_info("creator", &coins(2, "token"));
let _res = instantiate(deps.as_mut(), mock_env(), info, msg).unwrap();

// beneficiary can release it
let unauth_info = mock_info("anyone", &coins(2, "token"));
let msg = ExecuteMsg::Reset { count: 5 };
let res = execute(deps.as_mut(), mock_env(), unauth_info, msg);
match res {
Err(ContractError::Unauthorized {}) => {}
_ => panic!("Must return unauthorized error"),
}

// only the original creator can reset the counter
let auth_info = mock_info("creator", &coins(2, "token"));
let msg = ExecuteMsg::Reset { count: 5 };
let _res = execute(deps.as_mut(), mock_env(), auth_info, msg).unwrap();

// should now be 5
let res = query(deps.as_ref(), mock_env(), QueryMsg::GetCount {}).unwrap();
let value: GetCountResponse = from_binary(&res).unwrap();
assert_eq!(5, value.count);
}
{% endunless %}}
mod tests {}

0 comments on commit f311a76

Please sign in to comment.