Skip to content

Commit

Permalink
feat(law): add broken flag in state
Browse files Browse the repository at this point in the history
  • Loading branch information
amimart committed Mar 24, 2023
1 parent f31e905 commit f82073f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
32 changes: 19 additions & 13 deletions contracts/cw-law-stone/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub mod query {
use crate::state::PROGRAM;

pub fn program(deps: Deps<'_, LogicCustomQuery>) -> StdResult<ProgramResponse> {
let program = PROGRAM.load(deps.storage)?;
let program = PROGRAM.load(deps.storage)?.law;
Ok(ProgramResponse::from(program))
}
}
Expand All @@ -91,7 +91,7 @@ pub fn reply(
pub mod reply {
use super::*;
use crate::helper::{ask_response_to_objects, get_reply_event_attribute};
use crate::state::{Object, DEPENDENCIES, PROGRAM};
use crate::state::{LawStone, Object, DEPENDENCIES, PROGRAM};
use url::Url;

pub fn store_program_reply(
Expand All @@ -113,25 +113,28 @@ pub mod reply {
))
})
})
.map(|obj_id| Object {
object_id: obj_id,
storage_address: context.clone(),
.map(|obj_id| LawStone {
broken: false,
law: Object {
object_id: obj_id,
storage_address: context.clone(),
},
})
.and_then(|program| -> Result<Vec<SubMsg>, ContractError> {
.and_then(|stone| -> Result<Vec<SubMsg>, ContractError> {
PROGRAM
.save(deps.storage, &program)
.save(deps.storage, &stone)
.map_err(ContractError::from)?;

// Clean instantiate context
INSTANTIATE_CONTEXT.remove(deps.storage);

let req = build_source_files_query(program.clone())?.into();
let req = build_source_files_query(stone.law.clone())?.into();
let res = deps.querier.query(&req).map_err(ContractError::from)?;

let objects = ask_response_to_objects(res, "Files".to_string())?;
let mut msgs = Vec::with_capacity(objects.len());
for obj in objects {
if obj.object_id == program.object_id {
if obj.object_id == stone.object_id {
continue;
}
DEPENDENCIES.save(deps.storage, obj.object_id.as_str(), &obj)?;
Expand Down Expand Up @@ -169,7 +172,7 @@ pub mod reply {
mod tests {
use super::*;
use crate::msg::ProgramResponse;
use crate::state::{Object, DEPENDENCIES, PROGRAM};
use crate::state::{LawStone, Object, DEPENDENCIES, PROGRAM};
use cosmwasm_std::testing::{mock_env, mock_info, MockQuerierCustomHandlerResult};
use cosmwasm_std::{
from_binary, to_binary, CosmosMsg, Event, Order, SubMsgResponse, SubMsgResult, SystemError,
Expand Down Expand Up @@ -281,9 +284,12 @@ mod tests {
PROGRAM
.save(
deps.as_mut().storage,
&Object {
object_id: object_id.clone(),
storage_address: storage_addr.clone(),
&LawStone {
broken: false,
law: Object {
object_id: object_id.clone(),
storage_address: storage_addr.clone(),
},
},
)
.unwrap();
Expand Down
8 changes: 7 additions & 1 deletion contracts/cw-law-stone/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ use url::Url;
/// State to store context during contract instantiation
pub const INSTANTIATE_CONTEXT: Item<'_, String> = Item::new("instantiate");

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct LawStone {
pub broken: bool,
pub law: Object,
}

/// Represent a link to an Object stored in the `cw-storage` contract.
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
pub struct Object {
Expand Down Expand Up @@ -96,7 +102,7 @@ impl TryInto<Url> for Object {
}
}

pub const PROGRAM: Item<'_, Object> = Item::new("program");
pub const PROGRAM: Item<'_, LawStone> = Item::new("program");

pub const DEPENDENCIES: Map<'_, &str, Object> = Map::new("dependencies");

Expand Down

0 comments on commit f82073f

Please sign in to comment.