Skip to content

Commit 7aecef8

Browse files
committed
feat(dataverse): implement instantiate msg
1 parent 2cb181c commit 7aecef8

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

contracts/okp4-dataverse/src/contract.rs

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
#[cfg(not(feature = "library"))]
22
use cosmwasm_std::entry_point;
3-
use cosmwasm_std::{Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult};
3+
use cosmwasm_std::{
4+
instantiate2_address, to_binary, Binary, CodeInfoResponse, Deps, DepsMut, Env, MessageInfo,
5+
Response, StdError, StdResult, WasmMsg,
6+
};
47
use cw2::set_contract_version;
58

69
use crate::error::ContractError;
710
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg};
11+
use crate::state::{Dataverse, DATAVERSE};
812

913
// version info for migration info
1014
const CONTRACT_NAME: &str = concat!("crates.io:", env!("CARGO_PKG_NAME"));
@@ -13,13 +17,42 @@ const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
1317
#[cfg_attr(not(feature = "library"), entry_point)]
1418
pub fn instantiate(
1519
deps: DepsMut<'_>,
16-
_env: Env,
20+
env: Env,
1721
_info: MessageInfo,
18-
_msg: InstantiateMsg,
22+
msg: InstantiateMsg,
1923
) -> Result<Response, ContractError> {
2024
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
2125

22-
Err(StdError::generic_err("Not implemented").into())
26+
let creator = deps.api.addr_canonicalize(env.contract.address.as_str())?;
27+
let CodeInfoResponse { checksum, .. } = deps
28+
.querier
29+
.query_wasm_code_info(msg.triplestore_config.code_id.u64())?;
30+
let salt = Binary::from(msg.name.as_bytes());
31+
32+
let triplestore_address = deps
33+
.api
34+
.addr_humanize(&instantiate2_address(&checksum, &creator, &salt)?)?;
35+
36+
DATAVERSE.save(
37+
deps.storage,
38+
&Dataverse {
39+
name: msg.name.clone(),
40+
triplestore_address: triplestore_address.to_string(),
41+
},
42+
)?;
43+
44+
Ok(Response::new()
45+
.add_attribute("triplestore_address", triplestore_address)
46+
.add_message(WasmMsg::Instantiate2 {
47+
admin: Some(env.contract.address.to_string()),
48+
code_id: msg.triplestore_config.code_id.u64(),
49+
label: format!("{}_triplestore", msg.name),
50+
msg: to_binary(&okp4_cognitarium::msg::InstantiateMsg {
51+
limits: msg.triplestore_config.limits,
52+
})?,
53+
funds: vec![],
54+
salt,
55+
}))
2356
}
2457

2558
#[cfg_attr(not(feature = "library"), entry_point)]

contracts/okp4-dataverse/src/error.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
use cosmwasm_std::StdError;
1+
use cosmwasm_std::{Instantiate2AddressError, StdError};
22
use thiserror::Error;
33

44
#[derive(Error, Debug, PartialEq)]
55
pub enum ContractError {
66
#[error("{0}")]
77
Std(#[from] StdError),
8+
9+
#[error("{0}")]
10+
Instantiate2Address(#[from] Instantiate2AddressError),
811
}

0 commit comments

Comments
 (0)