-
Notifications
You must be signed in to change notification settings - Fork 19
Feat/dataverse instantiate #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
0a129c5
b3de779
490e0e4
1c05850
f5fade2
3d9d514
561c673
2e4560c
0b0c635
0498747
c142976
31340f2
89a55ee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,14 @@ | ||
| #[cfg(not(feature = "library"))] | ||
| use cosmwasm_std::entry_point; | ||
| use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult}; | ||
| use cosmwasm_std::{ | ||
| instantiate2_address, to_binary, Binary, CodeInfoResponse, Deps, DepsMut, Env, MessageInfo, | ||
| Response, StdError, StdResult, WasmMsg, | ||
| }; | ||
| use cw2::set_contract_version; | ||
|
|
||
| use crate::error::ContractError; | ||
| use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; | ||
| use crate::state::{Dataverse, DATAVERSE}; | ||
|
|
||
| // version info for migration info | ||
| const CONTRACT_NAME: &str = concat!("crates.io:", env!("CARGO_PKG_NAME")); | ||
|
|
@@ -13,13 +17,50 @@ const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); | |
| #[cfg_attr(not(feature = "library"), entry_point)] | ||
| pub fn instantiate( | ||
| deps: DepsMut<'_>, | ||
| _env: Env, | ||
| env: Env, | ||
| _info: MessageInfo, | ||
| _msg: InstantiateMsg, | ||
| msg: InstantiateMsg, | ||
| ) -> Result<Response, ContractError> { | ||
| set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; | ||
|
|
||
| Err(StdError::generic_err("Not implemented").into()) | ||
| let creator = deps.api.addr_canonicalize(env.contract.address.as_str())?; | ||
| let CodeInfoResponse { checksum, .. } = deps | ||
| .querier | ||
| .query_wasm_code_info(msg.triplestore_config.code_id.u64())?; | ||
| let salt = Binary::from(msg.name.as_bytes()); | ||
|
|
||
| /// Necessary stuff for testing purposes, see: https://github.com/CosmWasm/cosmwasm/issues/1648 | ||
| #[allow(unused)] | ||
| let triplestore_address = instantiate2_address(&checksum, &creator, &salt)?; | ||
| let triplestore_address = { | ||
| #[cfg(not(test))] | ||
| { | ||
| deps.api.addr_humanize(&triplestore_address)? | ||
| } | ||
| #[cfg(test)] | ||
| cosmwasm_std::Addr::unchecked("predicted address") | ||
| }; | ||
|
|
||
| DATAVERSE.save( | ||
| deps.storage, | ||
| &Dataverse { | ||
| name: msg.name.clone(), | ||
| triplestore_address: triplestore_address.to_string(), | ||
| }, | ||
| )?; | ||
|
|
||
| Ok(Response::new() | ||
| .add_attribute("triplestore_address", triplestore_address) | ||
| .add_message(WasmMsg::Instantiate2 { | ||
| admin: Some(env.contract.address.to_string()), | ||
| code_id: msg.triplestore_config.code_id.u64(), | ||
| label: format!("{}_triplestore", msg.name), | ||
| msg: to_binary(&okp4_cognitarium::msg::InstantiateMsg { | ||
| limits: msg.triplestore_config.limits.into(), | ||
| })?, | ||
| funds: vec![], | ||
| salt, | ||
| })) | ||
| } | ||
|
amimart marked this conversation as resolved.
|
||
|
|
||
| #[cfg_attr(not(feature = "library"), entry_point)] | ||
|
|
@@ -42,4 +83,72 @@ pub fn query(_deps: Deps<'_>, _env: Env, _msg: QueryMsg) -> StdResult<Binary> { | |
| pub mod query {} | ||
|
|
||
| #[cfg(test)] | ||
| mod tests {} | ||
| mod tests { | ||
| use super::*; | ||
| use crate::msg::{TripleStoreConfig, TripleStoreLimitsInput}; | ||
| use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; | ||
| use cosmwasm_std::{ | ||
| Attribute, ContractResult, HexBinary, SubMsg, SystemError, SystemResult, Uint128, Uint64, | ||
| WasmQuery, | ||
| }; | ||
|
|
||
| #[test] | ||
| fn proper_instantiate() { | ||
| let mut deps = mock_dependencies(); | ||
| deps.querier.update_wasm(|query| match query { | ||
| WasmQuery::CodeInfo { code_id, .. } => { | ||
| let resp = CodeInfoResponse::new( | ||
| code_id.clone(), | ||
| "creator".to_string(), | ||
| HexBinary::from_hex( | ||
| "3B94AAF0B7D804B5B458DED0D20CACF95D2A1C8DF78ED3C89B61291760454AEC", | ||
| ) | ||
| .unwrap(), | ||
| ); | ||
| SystemResult::Ok(ContractResult::Ok(to_binary(&resp).unwrap())) | ||
| } | ||
| _ => SystemResult::Err(SystemError::Unknown {}), | ||
| }); | ||
|
|
||
| let store_limits = TripleStoreLimitsInput { | ||
| max_byte_size: Some(Uint128::from(50000u128)), | ||
| ..Default::default() | ||
| }; | ||
|
|
||
| let msg = InstantiateMsg { | ||
| name: "my-dataverse".to_string(), | ||
| triplestore_config: TripleStoreConfig { | ||
| code_id: Uint64::from(17u64), | ||
| limits: store_limits.clone(), | ||
| }, | ||
| }; | ||
|
|
||
| let res = instantiate(deps.as_mut(), mock_env(), mock_info("creator", &[]), msg).unwrap(); | ||
|
|
||
| assert_eq!( | ||
| res.attributes, | ||
| vec![Attribute::new("triplestore_address", "predicted address")] | ||
| ); | ||
| assert_eq!( | ||
| res.messages, | ||
| vec![SubMsg::new(WasmMsg::Instantiate2 { | ||
| admin: Some("cosmos2contract".to_string()), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The - admin: Some("cosmos2contract".to_string()),
+ admin: Some(env.contract.address.to_string()),---end hunk 2--- |
||
| code_id: 17, | ||
| label: "my-dataverse_triplestore".to_string(), | ||
| msg: to_binary(&okp4_cognitarium::msg::InstantiateMsg { | ||
| limits: store_limits.into(), | ||
| }) | ||
| .unwrap(), | ||
| funds: vec![], | ||
| salt: Binary::from("my-dataverse".as_bytes()), | ||
| })] | ||
| ); | ||
| assert_eq!( | ||
| DATAVERSE.load(&deps.storage).unwrap(), | ||
| Dataverse { | ||
| name: "my-dataverse".to_string(), | ||
| triplestore_address: "predicted address".to_string(), | ||
| } | ||
| ) | ||
| } | ||
|
amimart marked this conversation as resolved.
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,11 @@ | ||
| use cosmwasm_std::StdError; | ||
| use cosmwasm_std::{Instantiate2AddressError, StdError}; | ||
| use thiserror::Error; | ||
|
|
||
| #[derive(Error, Debug, PartialEq)] | ||
| pub enum ContractError { | ||
| #[error("{0}")] | ||
| Std(#[from] StdError), | ||
|
|
||
| #[error("{0}")] | ||
| Instantiate2Address(#[from] Instantiate2AddressError), | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,10 @@ | ||
| use cw_storage_plus::Item; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| pub const DATAVERSE: Item<'_, Dataverse> = Item::new("dataverse"); | ||
|
|
||
| #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] | ||
| pub struct Dataverse { | ||
| pub name: String, | ||
| pub triplestore_address: String, | ||
| } | ||
|
Comment on lines
+4
to
+11
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The implementation of the #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
pub struct Dataverse {
pub name: String,
pub triplestore_address: Addr, // Use Addr to ensure the address is valid
} |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
#[allow(unused)]attribute should be removed iftriplestore_addressis being used, as it seems to be in this case.- #[allow(unused)]