Skip to content

Commit

Permalink
testcases submessage errors fixed for contracts
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek-1857 committed Dec 17, 2024
1 parent adb8e7b commit 9be64e5
Show file tree
Hide file tree
Showing 139 changed files with 17,880 additions and 3,628 deletions.
404 changes: 219 additions & 185 deletions Cargo.lock

Large diffs are not rendered by default.

7 changes: 3 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ exclude = ["ci/configs/", "wasmvm/libwasmvm"]
members = [
"./contracts/dao-dao-core",
"./contracts/external/cw-admin-factory",
"./contracts/external/snip20-reference-impl",
"./contracts/external/snip20-base",
"./contracts/external/snip721-reference-impl",
"./contracts/external/snip721-roles/",
"./contracts/external/snip721-roles-impl/",
Expand Down Expand Up @@ -136,10 +136,9 @@ secret-toolkit = { version = "0.10.0", default-features = false, features = [

secret-cw-controllers = { path = "./packages/controllers/",default-features = false }
secret-storage-plus = { git = "https://github.com/securesecrets/secret-plus-utils", version = "0.13.4", branch = "main"}
secret-multi-test = { git = "https://github.com/securesecrets/secret-plus-utils", version = "0.13.4", branch = "main"}
secret-multi-test = { git = "https://github.com/kent-3/secret-plus-utils", branch = "protobuf-fix"}
secret-utils = { git = "https://github.com/securesecrets/secret-plus-utils", version = "0.13.4", branch = "main" }
secret-cw2 = { git = "https://github.com/securesecrets/secret-plus-utils", version = "1.0.1", branch = "main"}

cosmwasm-std = { package = "secret-cosmwasm-std", version = "1.1.11", features = [
"stargate",
"staking",
Expand All @@ -152,7 +151,7 @@ schemars = {version = "0.8.12",default-features = false}
serde = { version = "1.0.158", default-features = false, features = ["derive"] }
thiserror = { version = "1.0.21" ,default-features = false}
cosmwasm-schema = { git = "https://github.com/scrtlabs/cosmwasm/", branch = "secret",default-features = false }
snip20-reference-impl = { path = "./contracts/external/snip20-reference-impl/",default-features = false }
snip20-base = { path = "./contracts/external/snip20-base",default-features = false }
snip721-reference-impl = { path = "./contracts/external/snip721-reference-impl/" ,default-features = false}
snip721-roles-impl = { path = "./contracts/external/snip721-roles-impl/" ,default-features = false}
cosmos-sdk-proto = { version = "0.20.0", default-features = false }
Expand Down
79 changes: 79 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# .PHONY: compile
# compile:
# docker run --rm -v .:/code \
# --mount type=volume,source="optimizer_cache",target=/target \
# --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
# cosmwasm/optimizer:0.16.0

.PHONY: check
check:
cargo check

.PHONY: clippy
clippy:
cargo clippy

PHONY: test
test: unit-test

.PHONY: unit-test
unit-test:
cargo unit-test

# This is a local build with debug-prints activated. Debug prints only show up
# in the local development chain (see the `start-server` command below)
# and mainnet won't accept contracts built with the feature enabled.
.PHONY: build _build
build: _build compress-wasm
_build:
RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown --features="debug-print"

# This is a build suitable for uploading to mainnet.
# Calls to `debug_print` get removed by the compiler.
.PHONY: build-mainnet _build-mainnet
build-mainnet: _build-mainnet compress-wasm
_build-mainnet:
RUSTFLAGS='-C link-arg=-s' cargo build --release --target wasm32-unknown-unknown

# like build-mainnet, but slower and more deterministic
.PHONY: build-mainnet-reproducible
build-mainnet-reproducible:
docker run --rm -v "$$(pwd)":/contract \
--mount type=volume,source="$$(basename "$$(pwd)")_cache",target=/code/target \
--mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
mr7uca/wasm-contract-optimizer:0.0.10

.PHONY: compress-wasm
compress-wasm:
cp ./target/wasm32-unknown-unknown/release/*.wasm ./contract.wasm
@## The following line is not necessary, may work only on linux (extra size optimization)
@# wasm-opt -Os ./contract.wasm -o ./contract.wasm
cat ./contract.wasm | gzip -9 > ./contract.wasm.gz

.PHONY: schema
schema:
cargo run --example schema

# Run local development chain with four funded accounts (named a, b, c, and d)
.PHONY: start-server
start-server: # CTRL+C to stop
docker run -it --rm \
-p 26657:26657 -p 26656:26656 -p 1317:1317 -p 5000:5000 \
-v "$$(pwd)":/root/code \
--name localsecret ghcr.io/scrtlabs/localsecret:v1.6.0

# This relies on running `start-server` in another console
# You can run other commands on the secretcli inside the dev image
# by using `docker exec localsecret secretcli`.
.PHONY: store-contract-local
store-contract-local:
docker exec localsecret secretcli tx compute store -y --from a --gas auto --gas-prices 0.0125uscrt --gas-adjustment 1.3 -y -b sync /root/code/optimized-wasm/factory.wasm.gz

.PHONY: integration-test
integration-test:
npx ts-node tests/integration.ts

.PHONY: clean
clean:
cargo clean
-rm -f ./contract.wasm ./contract.wasm.gz
7 changes: 3 additions & 4 deletions contracts/dao-dao-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dao-dao-macros = { workspace = true }
cw-paginate-storage = { workspace = true }
cw-core-v1 = { workspace = true, features = ["library"] }
secret-toolkit = { workspace = true }
snip20-reference-impl = { workspace = true }
snip20-base = { workspace = true }
snip721-reference-impl = { workspace = true }
schemars = { workspace = true }
serde = { workspace = true }
Expand All @@ -37,13 +37,12 @@ shade-protocol ={ workspace = true }
dao-utils = {workspace = true}
cw4 ={ workspace = true }
query_auth ={ workspace = true }
dao-voting-cw4 = { workspace = true }
cw4-group = { workspace = true }
dao-proposal-sudo ={ workspace = true }
dao-voting-snip20-balance ={ workspace = true }


[dev-dependencies]
secret-multi-test = { workspace = true, features = ["stargate"] }
# dao-proposal-sudo = { workspace = true }
# dao-voting-snip20-balance = { workspace = true }
dao-voting-snip20-balance = { workspace = true }
dao-voting-cw4 = { workspace = true }
89 changes: 35 additions & 54 deletions contracts/dao-dao-core/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
from_binary, to_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Empty, Env, MessageInfo, Reply,
Response, StdError, StdResult, SubMsg, SubMsgResult, Uint128,
from_binary, from_slice, to_binary, Addr, Binary, CosmosMsg, Deps, DepsMut, Empty, Env, MessageInfo, Reply, Response, StdError, StdResult, SubMsg, SubMsgResult, Uint128
};
use dao_interface::replies::parse_reply_address_from_event;
use dao_interface::state::AnyContractInfo;
Expand All @@ -21,20 +20,21 @@ use dao_interface::{
};
use dao_utils::msg::GroupContract;
use dao_utils::msg::NftRolesContract;
use dao_utils::query::get_contract_code_hash;
use secret_cw2::{get_contract_version, set_contract_version, ContractVersion};
use secret_toolkit::utils::InitCallback;
use secret_toolkit::{serialization::Json, storage::Keymap, utils::HandleCallback};
use secret_utils::Duration;
use shade_protocol::basic_staking::Auth;
use shade_protocol::utils::asset::RawContract;
use shade_protocol::Contract;
use snip20_reference_impl::msg::ExecuteAnswer;
use snip20_base::msg::ExecuteAnswer;

use crate::query_auth_init::QueryAuthInstantiateMsg;
use crate::state::{
ACTIVE_PROPOSAL_MODULE_COUNT, ADMIN, CONFIG, ITEMS, NOMINATED_ADMIN, PAUSED, PROPOSAL_MODULES,
QUERY_AUTH, REPLY_IDS, SNIP20_CODE_HASH, SNIP20_LIST, SNIP721_CODE_HASH, SNIP721_LIST,
SUBDAO_LIST, TOKEN_VIEWING_KEY, TOTAL_PROPOSAL_MODULE_COUNT, VOTING_MODULE,
QUERY_AUTH, REPLY_IDS, SNIP20_LIST, SNIP721_LIST, SUBDAO_LIST, TOKEN_VIEWING_KEY,
TOTAL_PROPOSAL_MODULE_COUNT, VOTING_MODULE,
};
use crate::{error::ContractError, snip20_msg};

Expand Down Expand Up @@ -79,7 +79,6 @@ pub fn instantiate(
ReplyEvent::InstantiateQueryAuth {
voting_module_instantiate_info: msg.voting_module_instantiate_info.clone(),
proposal_modules_instantiate_info: msg.proposal_modules_instantiate_info,
code_hash: msg.query_auth_code_hash.clone(),
},
)?;

Expand Down Expand Up @@ -108,8 +107,6 @@ pub fn instantiate(

TOTAL_PROPOSAL_MODULE_COUNT.save(deps.storage, &0)?;
ACTIVE_PROPOSAL_MODULE_COUNT.save(deps.storage, &0)?;
SNIP20_CODE_HASH.save(deps.storage, &msg.snip20_code_hash)?;
SNIP721_CODE_HASH.save(deps.storage, &msg.snip721_code_hash)?;

Ok(Response::new()
.add_attribute("action", "instantiate")
Expand Down Expand Up @@ -342,12 +339,7 @@ pub fn execute_update_voting_module(
}

let wasm = module.clone().to_cosmos_msg(env.contract.address);
let reply_id = REPLY_IDS.add_event(
deps.storage,
ReplyEvent::VotingModuleInstantiate {
code_hash: module.code_hash,
},
)?;
let reply_id = REPLY_IDS.add_event(deps.storage, ReplyEvent::VotingModuleInstantiate {})?;
let submessage = SubMsg::reply_on_success(wasm, reply_id);

Ok(Response::default()
Expand Down Expand Up @@ -400,12 +392,7 @@ pub fn execute_update_proposal_modules(
.map(|info| {
let wasm = info.clone().into_wasm_msg(env.contract.address.clone());
let reply_id = REPLY_IDS
.add_event(
deps.storage,
ReplyEvent::ProposalModuleInstantiate {
code_hash: info.code_hash,
},
)
.add_event(deps.storage, ReplyEvent::ProposalModuleInstantiate {})
.unwrap();
SubMsg::reply_on_success(wasm, reply_id)
})
Expand Down Expand Up @@ -462,8 +449,8 @@ pub fn execute_update_snip20_list(
let viewing_key = TOKEN_VIEWING_KEY
.get(deps.storage, addr)
.unwrap_or_default();
let snip20_code_hash = SNIP20_CODE_HASH.load(deps.storage)?;
let _info: snip20_reference_impl::msg::QueryAnswer = deps.querier.query_wasm_smart(
let snip20_code_hash = get_contract_code_hash(deps.querier, addr.to_string())?;
let _info: snip20_base::msg::QueryAnswer = deps.querier.query_wasm_smart(
snip20_code_hash,
addr,
&secret_toolkit::snip20::QueryMsg::Balance {
Expand All @@ -487,7 +474,7 @@ pub fn execute_update_snip721_list(
return Err(ContractError::Unauthorized {});
}
do_update_addr_list(deps, &SNIP721_LIST, to_add, to_remove, |addr, deps| {
let snip721_code_hash = SNIP721_CODE_HASH.load(deps.storage)?;
let snip721_code_hash = get_contract_code_hash(deps.querier, addr.to_string())?;
let _info: secret_toolkit::snip721::query::ContractInfo = deps.querier.query_wasm_smart(
snip721_code_hash,
addr,
Expand Down Expand Up @@ -567,8 +554,7 @@ pub fn execute_receive_snip20(
sender: Addr,
_wrapper: Snip20ReceiveMsg,
) -> Result<Response, ContractError> {
println!("here");
let code_hash = SNIP20_CODE_HASH.load(deps.storage)?;
let code_hash = get_contract_code_hash(deps.querier, sender.to_string())?;
let viewing_key = TOKEN_VIEWING_KEY
.get(deps.storage, &sender)
.unwrap_or_default();
Expand Down Expand Up @@ -994,21 +980,21 @@ pub fn query_cw20_balances(
let balances: StdResult<Vec<Snip20BalanceResponse>> = res
.into_iter()
.map(|addr| {
let snip20_code_hash = SNIP20_CODE_HASH.load(deps.storage)?;
let snip20_code_hash = get_contract_code_hash(deps.querier, addr.clone())?;
let viewing_key = TOKEN_VIEWING_KEY
.get(deps.storage, &deps.api.addr_validate(&addr)?)
.unwrap_or_default();
let mut balance_amount = Uint128::zero();
let balance: snip20_reference_impl::msg::QueryAnswer = deps.querier.query_wasm_smart(
let balance: snip20_base::msg::QueryAnswer = deps.querier.query_wasm_smart(
snip20_code_hash.clone(),
addr.clone(),
&snip20_reference_impl::msg::QueryMsg::Balance {
&snip20_base::msg::QueryMsg::Balance {
address: env.contract.address.to_string(),
key: viewing_key,
},
)?;
match balance {
snip20_reference_impl::msg::QueryAnswer::Balance { amount } => {
snip20_base::msg::QueryAnswer::Balance { amount } => {
balance_amount = amount;
}
_ => (),
Expand Down Expand Up @@ -1101,10 +1087,11 @@ pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result<Response, C
pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractError> {
let reply_event = REPLY_IDS.get_event(deps.storage, msg.id)?;
match reply_event {
ReplyEvent::ProposalModuleInstantiate { code_hash } => match msg.result {
ReplyEvent::ProposalModuleInstantiate {} => match msg.result {
SubMsgResult::Err(err) => Err(ContractError::Std(StdError::GenericErr { msg: err })),
SubMsgResult::Ok(res) => {
let address = parse_reply_address_from_event(res.clone());
let code_hash = get_contract_code_hash(deps.querier, address.clone())?;

let total_module_count = TOTAL_PROPOSAL_MODULE_COUNT.load(deps.storage)?;

Expand Down Expand Up @@ -1140,11 +1127,11 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractEr
.add_messages(callback_msgs))
}
},
ReplyEvent::VotingModuleInstantiate { code_hash } => match msg.result {
ReplyEvent::VotingModuleInstantiate {} => match msg.result {
SubMsgResult::Err(err) => Err(ContractError::Std(StdError::GenericErr { msg: err })),
SubMsgResult::Ok(res) => {
let address = parse_reply_address_from_event(res.clone());

let code_hash = get_contract_code_hash(deps.querier, address.clone())?;
let voting_module = VotingModuleInfo {
code_hash,
addr: deps.api.addr_validate(&address.clone())?,
Expand All @@ -1168,24 +1155,27 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractEr
ReplyEvent::Snip20ModuleCreateViewingKey { contract_address } => match msg.result {
SubMsgResult::Ok(res) => {
let token_addr = deps.api.addr_validate(&contract_address)?;
let data: snip20_reference_impl::msg::ExecuteAnswer =
from_binary(&res.data.unwrap())?;
let mut viewing_key = String::new();
if let ExecuteAnswer::CreateViewingKey { key } = data {
viewing_key = key;
}
TOKEN_VIEWING_KEY.insert(deps.storage, &token_addr, &viewing_key)?;
let raw_data = res.data.ok_or_else(|| StdError::generic_err("No data returned"))?;

// Extract JSON payload starting from the '{' character
let json_data = &raw_data.0[raw_data.0.iter().position(|&b| b == b'{').unwrap_or(0)..];
let key = match from_slice::<ExecuteAnswer>(json_data)? {
ExecuteAnswer::CreateViewingKey { key } => key,
_ => return Err(ContractError::Std(StdError::generic_err("Unexpected response type"))),
};

TOKEN_VIEWING_KEY.insert(deps.storage, &token_addr, &key)?;
Ok(Response::new().add_attribute("action", "create_token_viewing_key"))
}
SubMsgResult::Err(_) => Err(ContractError::TokenExecuteError {}),
SubMsgResult::Err(_) => Err(ContractError::Std(StdError::generic_err("Token execution error"))),
},
ReplyEvent::InstantiateQueryAuth {
voting_module_instantiate_info,
proposal_modules_instantiate_info,
code_hash,
} => match msg.result {
SubMsgResult::Ok(res) => {
let address = parse_reply_address_from_event(res);
let code_hash = get_contract_code_hash(deps.querier, address.clone())?;
let msg = update_query_auth(
voting_module_instantiate_info.clone(),
RawContract {
Expand All @@ -1194,12 +1184,8 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractEr
},
env.contract.address.clone().to_string(),
)?;
let reply_id = REPLY_IDS.add_event(
deps.storage,
ReplyEvent::VotingModuleInstantiate {
code_hash: voting_module_instantiate_info.code_hash,
},
)?;
let reply_id =
REPLY_IDS.add_event(deps.storage, ReplyEvent::VotingModuleInstantiate {})?;
let vote_module_msg: SubMsg<Empty> = SubMsg::reply_on_success(msg, reply_id);

let proposal_module_msgs: Vec<SubMsg<Empty>> = proposal_modules_instantiate_info
Expand All @@ -1216,12 +1202,7 @@ pub fn reply(deps: DepsMut, env: Env, msg: Reply) -> Result<Response, ContractEr
.unwrap();

let reply_id = REPLY_IDS
.add_event(
deps.storage,
ReplyEvent::ProposalModuleInstantiate {
code_hash: info.code_hash,
},
)
.add_event(deps.storage, ReplyEvent::ProposalModuleInstantiate {})
.unwrap();
SubMsg::reply_on_success(msg, reply_id)
})
Expand Down Expand Up @@ -1318,7 +1299,7 @@ pub(crate) fn update_query_auth(
}

//Dao voting snip20 balance
if let Ok(msg) = from_binary::<dao_voting_snip20_balance::msg::InstantiateMsg>(&info.msg) {
if let Ok(msg) = from_binary::<dao_utils::msg::DaoVotingSnip20BalanceInstantiateMsg>(&info.msg) {
return msg.to_cosmos_msg(Some(admin), info.label, info.code_id, info.code_hash, None);
}

Expand Down
3 changes: 0 additions & 3 deletions contracts/dao-dao-core/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,3 @@ pub const TOKEN_VIEWING_KEY: Keymap<Addr, String, Json> = Keymap::new(b"token_vi
pub const REPLY_IDS: ReplyIds = ReplyIds::new(b"reply_ids", b"reply_ids_count");

pub const QUERY_AUTH: Item<AnyContractInfo> = Item::new("query_auth");

pub const SNIP20_CODE_HASH: Item<String> = Item::new("snip20_code_hash");
pub const SNIP721_CODE_HASH: Item<String> = Item::new("snip721_code_hash");
Loading

0 comments on commit 9be64e5

Please sign in to comment.