From ef8255da755362602317fe15ec28a1fafd0a6193 Mon Sep 17 00:00:00 2001 From: ccamel Date: Sat, 7 Oct 2023 10:26:26 +0200 Subject: [PATCH 1/3] feat(law-stone): implement program_code query --- contracts/okp4-law-stone/src/contract.rs | 13 +++++++++++++ contracts/okp4-law-stone/src/msg.rs | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/contracts/okp4-law-stone/src/contract.rs b/contracts/okp4-law-stone/src/contract.rs index a6af8464..ee6f363b 100644 --- a/contracts/okp4-law-stone/src/contract.rs +++ b/contracts/okp4-law-stone/src/contract.rs @@ -123,6 +123,7 @@ pub fn query(deps: Deps<'_, LogicCustomQuery>, _env: Env, msg: QueryMsg) -> StdR match msg { QueryMsg::Ask { query } => to_binary(&query::ask(deps, query)?), QueryMsg::Program => to_binary(&query::program(deps)?), + QueryMsg::ProgramCode => to_binary(&query::program_code(deps)?), } } @@ -139,6 +140,18 @@ pub mod query { Ok(program) } + pub fn program_code(deps: Deps<'_, LogicCustomQuery>) -> StdResult { + let ObjectRef { + storage_address, + object_id, + } = PROGRAM.load(deps.storage)?.law; + + deps.querier.query_wasm_smart::( + storage_address, + &StorageQuery::ObjectData { id: object_id }, + ) + } + pub fn ask(deps: Deps<'_, LogicCustomQuery>, query: String) -> StdResult { let stone = PROGRAM.load(deps.storage)?; if stone.broken { diff --git a/contracts/okp4-law-stone/src/msg.rs b/contracts/okp4-law-stone/src/msg.rs index e4c436c4..ddba3a6f 100644 --- a/contracts/okp4-law-stone/src/msg.rs +++ b/contracts/okp4-law-stone/src/msg.rs @@ -38,6 +38,11 @@ pub enum QueryMsg { /// If not broken, returns the law program location information. #[returns(ProgramResponse)] Program, + + /// # ProgramCode + /// ProgramCode returns the law program code. + #[returns(Binary)] + ProgramCode, } /// # ProgramResponse From a8a784e3e2e7068f5ce6d9d80295d3d3f8cc6c95 Mon Sep 17 00:00:00 2001 From: ccamel Date: Mon, 16 Oct 2023 20:51:58 +0200 Subject: [PATCH 2/3] test(law-stone): test program_code query --- contracts/okp4-law-stone/src/contract.rs | 44 ++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/contracts/okp4-law-stone/src/contract.rs b/contracts/okp4-law-stone/src/contract.rs index ee6f363b..94bfc177 100644 --- a/contracts/okp4-law-stone/src/contract.rs +++ b/contracts/okp4-law-stone/src/contract.rs @@ -398,6 +398,50 @@ mod tests { assert_eq!(storage_addr, result.storage_address); } + #[test] + fn program_code() { + const CONTRACT_ID: &str = "okp41ffzp0xmjhwkltuxcvccl0z9tyfuu7txp5ke0tpkcjpzuq9fcj3pqrteqt3"; + const OBJECT_ID: &str = "4cbe36399aabfcc7158ee7a66cbfffa525bb0ceab33d1ff2cff08759fe0a9b05"; + const A_PROGRAM: &str = "foo(_) :- true."; + + let mut deps = + mock_dependencies_with_logic_handler(|_| SystemResult::Err(SystemError::Unknown {})); + deps.querier.update_wasm(move |query| match query { + WasmQuery::Smart { contract_addr, msg } if contract_addr == CONTRACT_ID => { + let data = to_binary(&A_PROGRAM).unwrap(); + let storage_query: StorageQuery = from_binary(msg).unwrap(); + + assert!( + matches!(storage_query, StorageQuery::ObjectData { id } if id == OBJECT_ID) + ); + + SystemResult::Ok(ContractResult::Ok(to_binary(&data).unwrap())) + } + _ => { + panic!("UnsupportedRequest: query_wasm"); + } + }); + + PROGRAM + .save( + deps.as_mut().storage, + &LawStone { + broken: false, + law: ObjectRef { + object_id: OBJECT_ID.to_string(), + storage_address: CONTRACT_ID.to_string(), + }, + }, + ) + .unwrap(); + + let result = query(deps.as_ref(), mock_env(), QueryMsg::ProgramCode {}).unwrap(); + let data: Binary = from_binary(&result).unwrap(); + let program: String = from_binary(&data).unwrap(); + + assert_eq!(A_PROGRAM, program); + } + fn custom_logic_handler_with_query( query: String, program: ObjectRef, From 33ba9398b0341d6190882e001385d1081df63f62 Mon Sep 17 00:00:00 2001 From: ccamel Date: Tue, 17 Oct 2023 09:42:12 +0200 Subject: [PATCH 3/3] docs(law-stone): update generated documentation --- docs/okp4-law-stone.md | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/docs/okp4-law-stone.md b/docs/okp4-law-stone.md index d1a628a8..8297613c 100644 --- a/docs/okp4-law-stone.md +++ b/docs/okp4-law-stone.md @@ -56,6 +56,14 @@ If not broken, returns the law program location information. |-------| |`"program"`| +### QueryMsg::ProgramCode + +ProgramCode returns the law program code. + +|literal| +|-------| +|`"program_code"`| + ## Responses ### ask @@ -77,6 +85,16 @@ ProgramResponse carry elements to locate the program in a `okp4-objectarium` con |`object_id`|*(Required.) * **string**. The program object id in the `okp4-objectarium` contract.| |`storage_address`|*(Required.) * **string**. The `okp4-objectarium` contract address on which the law program is stored.| +### program_code + +Binary is a wrapper around Vec<u8> to add base64 de/serialization with serde. It also adds some helper methods to help encode inline. + +This is only needed as serde-json-{core,wasm} has a horrible encoding for Vec<u8>. See also <https://github.com/CosmWasm/cosmwasm/blob/main/docs/MESSAGE_TYPES.md>. + +|type| +|----| +|**string**.| + ## Definitions ### Answer @@ -126,4 +144,4 @@ A string containing Base64-encoded data. --- -*Rendered by [Fadroma](https://fadroma.tech) ([@fadroma/schema 1.1.0](https://www.npmjs.com/package/@fadroma/schema)) from `okp4-law-stone.json` (`023b72150b485c6b`)* \ No newline at end of file +*Rendered by [Fadroma](https://fadroma.tech) ([@fadroma/schema 1.1.0](https://www.npmjs.com/package/@fadroma/schema)) from `okp4-law-stone.json` (`092608edf6c36d25`)* \ No newline at end of file