Skip to content

Commit

Permalink
feat(law): implements break stone exec msg
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 24, 2023
1 parent eb3570e commit c28535f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 13 deletions.
78 changes: 69 additions & 9 deletions contracts/cw-law-stone/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::ContractError::NotImplemented;
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Reply, Response, StdError, StdResult,
SubMsg, WasmMsg,
};
use cw2::set_contract_version;
use cw_storage::msg::ExecuteMsg as StorageMsg;
use cw_utils::parse_reply_execute_data;
use cw_storage::msg::{
ExecuteMsg as StorageMsg, ObjectPinsResponse, PageInfo, QueryMsg as StorageQuery,
};
use logic_bindings::LogicCustomQuery;

use crate::error::ContractError;
Expand Down Expand Up @@ -62,13 +62,75 @@ pub fn execute(

pub mod execute {
use super::*;
use crate::state::{Object, DEPENDENCIES, PROGRAM};
use cosmwasm_std::Order;

pub fn break_stone(
_deps: DepsMut<'_>,
_env: Env,
_info: MessageInfo,
deps: DepsMut<'_>,
env: Env,
info: MessageInfo,
) -> Result<Response, ContractError> {
Err(NotImplemented {})
match deps
.querier
.query_wasm_contract_info(env.contract.address)?
.admin
{
Some(admin_addr) if admin_addr != info.sender.to_string() => {
Err(ContractError::Unauthorized {})
}
_ => Ok({}),
}?;

let resp = Response::new().add_attribute("action", "break_stone");

let mut stone = PROGRAM.load(deps.storage)?;
if stone.broken {
return Ok(resp);
}
stone.broken = true;
PROGRAM.save(deps.storage, &stone)?;

let law_release_msg = match deps
.querier
.query_wasm_smart::<ObjectPinsResponse>(
stone.law.storage_address.clone(),
&StorageQuery::ObjectPins {
id: stone.law.object_id.clone(),
first: Some(1u32),
after: None,
},
)?
.page_info
.has_next_page
{
true => StorageMsg::UnpinObject {
id: stone.law.object_id,
},
_ => StorageMsg::ForgetObject {
id: stone.law.object_id,
},
};

Ok(resp
.add_message(WasmMsg::Execute {
contract_addr: stone.law.storage_address,
msg: to_binary(&law_release_msg)?,
funds: vec![],
})
.add_messages(
DEPENDENCIES
.range(deps.storage, None, None, Order::Ascending)
.map(|res: StdResult<(String, Object)>| {
res.and_then(|(_, obj)| {
Ok(WasmMsg::Execute {
contract_addr: obj.storage_address,
msg: to_binary(&StorageMsg::UnpinObject { id: obj.object_id })?,
funds: vec![],
})
})
})
.collect::<StdResult<Vec<WasmMsg>>>()?,
))
}
}

Expand Down Expand Up @@ -195,8 +257,6 @@ mod tests {
from_binary, to_binary, ContractInfoResponse, ContractResult, CosmosMsg, Event, Order,
SubMsgResponse, SubMsgResult, SystemError, SystemResult, WasmQuery,
};
use cw_storage::msg::{ObjectPinsResponse, PageInfo};
use cw_storage::msg::{ObjectResponse, QueryMsg as StorageQuery};
use logic_bindings::testing::mock::{
mock_dependencies_with_logic_and_balance, mock_dependencies_with_logic_handler,
};
Expand Down
5 changes: 1 addition & 4 deletions contracts/cw-law-stone/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ pub enum ContractError {
#[error("{0}")]
Std(#[from] StdError),

#[error("Not implemented")]
NotImplemented {},

#[error("{0}")]
Parse(#[from] ParseReplyError),

Expand All @@ -30,7 +27,7 @@ impl ContractError {
ContractError::LogicLoadUri { error, uri }
}
}
#[derive(Error, Debug)]
#[derive(Error, Debug, PartialEq)]
pub enum UriError {
#[error("{0}")]
Parse(#[from] ParseError),
Expand Down

0 comments on commit c28535f

Please sign in to comment.