Skip to content
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

Unique singleton names #39

Merged
merged 3 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions contracts/cw1-subkeys/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};
use cosmwasm_schema::{export_schema, export_schema_with_title, remove_schemas, schema_for};

use cw1_subkeys::msg::{HandleMsg, QueryMsg};
use cw1_subkeys::state::Allowance;
use cw1_whitelist::msg::{ConfigResponse, InitMsg};
use cw1_whitelist::msg::{AdminListResponse, InitMsg};

fn main() {
let mut out_dir = current_dir().unwrap();
Expand All @@ -14,8 +14,8 @@ fn main() {
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InitMsg), &out_dir);
export_schema(&schema_for!(HandleMsg), &out_dir);
export_schema_with_title(&mut schema_for!(HandleMsg), &out_dir, "HandleMsg");
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(Allowance), &out_dir);
export_schema(&schema_for!(ConfigResponse), &out_dir);
export_schema(&schema_for!(AdminListResponse), &out_dir);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ConfigResponse",
"title": "AdminListResponse",
"type": "object",
"required": [
"admins",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "HandleMsg_for_Empty",
"title": "HandleMsg",
"anyOf": [
{
"description": "Execute requests the contract to re-dispatch all these messages with the contract's address as sender. Every implementation has it's own logic to determine in",
Expand Down
6 changes: 3 additions & 3 deletions contracts/cw1-subkeys/schema/query_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
"title": "QueryMsg",
"anyOf": [
{
"description": "Shows all admins and whether or not it is mutable Returns cw1-whitelist::ConfigResponse",
"description": "Shows all admins and whether or not it is mutable Returns cw1-whitelist::AdminListResponse",
"type": "object",
"required": [
"config"
"admin_list"
],
"properties": {
"config": {
"admin_list": {
"type": "object"
}
}
Expand Down
30 changes: 15 additions & 15 deletions contracts/cw1-subkeys/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use cosmwasm_std::{
HumanAddr, InitResponse, Querier, StdError, StdResult, Storage,
};
use cw1_whitelist::{
contract::{handle_freeze, handle_update_admins, init as whitelist_init, query_config},
contract::{handle_freeze, handle_update_admins, init as whitelist_init, query_admin_list},
msg::InitMsg,
state::config_read,
state::admin_list_read,
};
use cw20::Expiration;

Expand Down Expand Up @@ -56,7 +56,7 @@ pub fn handle_execute<S: Storage, A: Api, Q: Querier, T>(
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
let cfg = config_read(&deps.storage).load()?;
let cfg = admin_list_read(&deps.storage).load()?;
let owner_raw = &deps.api.canonical_address(&env.message.sender)?;
// this is the admin behavior (same as cw1-whitelist)
if cfg.is_admin(owner_raw) {
Expand Down Expand Up @@ -108,7 +108,7 @@ pub fn handle_increase_allowance<S: Storage, A: Api, Q: Querier, T>(
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
let cfg = config_read(&deps.storage).load()?;
let cfg = admin_list_read(&deps.storage).load()?;
let spender_raw = &deps.api.canonical_address(&spender)?;
let owner_raw = &deps.api.canonical_address(&env.message.sender)?;

Expand Down Expand Up @@ -152,7 +152,7 @@ pub fn handle_decrease_allowance<S: Storage, A: Api, Q: Querier, T>(
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
let cfg = config_read(&deps.storage).load()?;
let cfg = admin_list_read(&deps.storage).load()?;
let spender_raw = &deps.api.canonical_address(&spender)?;
let owner_raw = &deps.api.canonical_address(&env.message.sender)?;

Expand Down Expand Up @@ -196,7 +196,7 @@ pub fn query<S: Storage, A: Api, Q: Querier>(
msg: QueryMsg,
) -> StdResult<Binary> {
match msg {
QueryMsg::Config {} => to_binary(&query_config(deps)?),
QueryMsg::AdminList {} => to_binary(&query_admin_list(deps)?),
QueryMsg::Allowance { spender } => to_binary(&query_allowance(deps, spender)?),
}
}
Expand All @@ -219,7 +219,7 @@ mod tests {
use crate::balance::Balance;
use cosmwasm_std::testing::{mock_dependencies, mock_env, MOCK_CONTRACT_ADDR};
use cosmwasm_std::{coin, coins};
use cw1_whitelist::msg::ConfigResponse;
use cw1_whitelist::msg::AdminListResponse;

// this will set up the init for other tests
fn setup_test_case<S: Storage, A: Api, Q: Querier>(
Expand Down Expand Up @@ -318,10 +318,10 @@ mod tests {
setup_test_case(&mut deps, &env, &initial_admins, &vec![], &vec![], &vec![]);

// Verify
let config = query_config(&deps).unwrap();
let config = query_admin_list(&deps).unwrap();
assert_eq!(
config,
ConfigResponse {
AdminListResponse {
admins: initial_admins.clone(),
mutable: true,
}
Expand All @@ -335,11 +335,11 @@ mod tests {
handle(&mut deps, env.clone(), msg).unwrap();

// Verify
let config = query_config(&deps).unwrap();
let config = query_admin_list(&deps).unwrap();
println!("config: {:#?}", config);
assert_eq!(
config,
ConfigResponse {
AdminListResponse {
admins: new_admins,
mutable: true,
}
Expand All @@ -352,11 +352,11 @@ mod tests {
handle(&mut deps, env.clone(), msg).unwrap();

// Verify admin3 is now the sole admin
let config = query_config(&deps).unwrap();
let config = query_admin_list(&deps).unwrap();
println!("config: {:#?}", config);
assert_eq!(
config,
ConfigResponse {
AdminListResponse {
admins: vec![admin3.clone()],
mutable: true,
}
Expand All @@ -380,11 +380,11 @@ mod tests {
handle(&mut deps, env.clone(), msg).unwrap();

// Verify
let config = query_config(&deps).unwrap();
let config = query_admin_list(&deps).unwrap();
println!("config: {:#?}", config);
assert_eq!(
config,
ConfigResponse {
AdminListResponse {
admins: vec![admin3, owner],
mutable: true,
}
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw1-subkeys/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ where
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
/// Shows all admins and whether or not it is mutable
/// Returns cw1-whitelist::ConfigResponse
Config {},
/// Returns cw1-whitelist::AdminListResponse
AdminList {},
/// Get the current allowance for the given subkey (how much it can spend)
/// Returns crate::state::Allowance
Allowance { spender: HumanAddr },
Expand Down
8 changes: 4 additions & 4 deletions contracts/cw1-whitelist/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::env::current_dir;
use std::fs::create_dir_all;

use cosmwasm_schema::{export_schema, remove_schemas, schema_for};
use cosmwasm_schema::{export_schema, export_schema_with_title, remove_schemas, schema_for};

use cw1_whitelist::msg::{ConfigResponse, HandleMsg, InitMsg, QueryMsg};
use cw1_whitelist::msg::{AdminListResponse, HandleMsg, InitMsg, QueryMsg};

fn main() {
let mut out_dir = current_dir().unwrap();
Expand All @@ -12,7 +12,7 @@ fn main() {
remove_schemas(&out_dir).unwrap();

export_schema(&schema_for!(InitMsg), &out_dir);
export_schema(&schema_for!(HandleMsg), &out_dir);
export_schema_with_title(&mut schema_for!(HandleMsg), &out_dir, "HandleMsg");
export_schema(&schema_for!(QueryMsg), &out_dir);
export_schema(&schema_for!(ConfigResponse), &out_dir);
export_schema(&schema_for!(AdminListResponse), &out_dir);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "ConfigResponse",
"title": "AdminListResponse",
"type": "object",
"required": [
"admins",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "HandleMsg_for_Empty",
"title": "HandleMsg",
"anyOf": [
{
"description": "Execute requests the contract to re-dispatch all these messages with the contract's address as sender. Every implementation has it's own logic to determine in",
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw1-whitelist/schema/query_msg.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"description": "Shows all admins and whether or not it is mutable",
"type": "object",
"required": [
"config"
"admin_list"
],
"properties": {
"config": {
"admin_list": {
"type": "object"
}
}
Expand Down
40 changes: 20 additions & 20 deletions contracts/cw1-whitelist/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ use cosmwasm_std::{
HumanAddr, InitResponse, Querier, StdError, StdResult, Storage,
};

use crate::msg::{ConfigResponse, HandleMsg, InitMsg, QueryMsg};
use crate::state::{config, config_read, Config};
use crate::msg::{AdminListResponse, HandleMsg, InitMsg, QueryMsg};
use crate::state::{admin_list, admin_list_read, AdminList};

pub fn init<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
_env: Env,
msg: InitMsg,
) -> StdResult<InitResponse> {
let cfg = Config {
let cfg = AdminList {
admins: map_canonical(&deps.api, &msg.admins)?,
mutable: msg.mutable,
};
config(&mut deps.storage).save(&cfg)?;
admin_list(&mut deps.storage).save(&cfg)?;
Ok(InitResponse::default())
}

Expand Down Expand Up @@ -55,7 +55,7 @@ pub fn handle_execute<S: Storage, A: Api, Q: Querier, T>(
where
T: Clone + fmt::Debug + PartialEq + JsonSchema,
{
let cfg = config_read(&deps.storage).load()?;
let cfg = admin_list_read(&deps.storage).load()?;
if !cfg.is_admin(&deps.api.canonical_address(&env.message.sender)?) {
Err(StdError::unauthorized())
} else {
Expand All @@ -70,12 +70,12 @@ pub fn handle_freeze<S: Storage, A: Api, Q: Querier>(
deps: &mut Extern<S, A, Q>,
env: Env,
) -> StdResult<HandleResponse> {
let mut cfg = config_read(&deps.storage).load()?;
let mut cfg = admin_list_read(&deps.storage).load()?;
if !cfg.can_modify(&deps.api.canonical_address(&env.message.sender)?) {
Err(StdError::unauthorized())
} else {
cfg.mutable = false;
config(&mut deps.storage).save(&cfg)?;
admin_list(&mut deps.storage).save(&cfg)?;

let mut res = HandleResponse::default();
res.log = vec![log("action", "freeze")];
Expand All @@ -88,12 +88,12 @@ pub fn handle_update_admins<S: Storage, A: Api, Q: Querier>(
env: Env,
admins: Vec<HumanAddr>,
) -> StdResult<HandleResponse> {
let mut cfg = config_read(&deps.storage).load()?;
let mut cfg = admin_list_read(&deps.storage).load()?;
if !cfg.can_modify(&deps.api.canonical_address(&env.message.sender)?) {
Err(StdError::unauthorized())
} else {
cfg.admins = map_canonical(&deps.api, &admins)?;
config(&mut deps.storage).save(&cfg)?;
admin_list(&mut deps.storage).save(&cfg)?;

let mut res = HandleResponse::default();
res.log = vec![log("action", "update_admins")];
Expand All @@ -106,15 +106,15 @@ pub fn query<S: Storage, A: Api, Q: Querier>(
msg: QueryMsg,
) -> StdResult<Binary> {
match msg {
QueryMsg::Config {} => to_binary(&query_config(deps)?),
QueryMsg::AdminList {} => to_binary(&query_admin_list(deps)?),
}
}

pub fn query_config<S: Storage, A: Api, Q: Querier>(
pub fn query_admin_list<S: Storage, A: Api, Q: Querier>(
deps: &Extern<S, A, Q>,
) -> StdResult<ConfigResponse> {
let cfg = config_read(&deps.storage).load()?;
Ok(ConfigResponse {
) -> StdResult<AdminListResponse> {
let cfg = admin_list_read(&deps.storage).load()?;
Ok(AdminListResponse {
admins: map_human(&deps.api, &cfg.admins)?,
mutable: cfg.mutable,
})
Expand Down Expand Up @@ -147,11 +147,11 @@ mod tests {
init(&mut deps, env, init_msg).unwrap();

// ensure expected config
let expected = ConfigResponse {
let expected = AdminListResponse {
admins: vec![alice.clone(), bob.clone(), carl.clone()],
mutable: true,
};
assert_eq!(query_config(&deps).unwrap(), expected);
assert_eq!(query_admin_list(&deps).unwrap(), expected);

// anyone cannot modify the contract
let msg = HandleMsg::UpdateAdmins {
Expand All @@ -172,11 +172,11 @@ mod tests {
handle(&mut deps, env, msg).unwrap();

// ensure expected config
let expected = ConfigResponse {
let expected = AdminListResponse {
admins: vec![alice.clone(), bob.clone()],
mutable: true,
};
assert_eq!(query_config(&deps).unwrap(), expected);
assert_eq!(query_admin_list(&deps).unwrap(), expected);

// carl cannot freeze it
let env = mock_env(&carl, &[]);
Expand All @@ -189,11 +189,11 @@ mod tests {
// but bob can
let env = mock_env(&bob, &[]);
handle(&mut deps, env, HandleMsg::Freeze {}).unwrap();
let expected = ConfigResponse {
let expected = AdminListResponse {
admins: vec![alice.clone(), bob.clone()],
mutable: false,
};
assert_eq!(query_config(&deps).unwrap(), expected);
assert_eq!(query_admin_list(&deps).unwrap(), expected);

// and now alice cannot change it again
let msg = HandleMsg::UpdateAdmins {
Expand Down
4 changes: 2 additions & 2 deletions contracts/cw1-whitelist/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ where
#[serde(rename_all = "snake_case")]
pub enum QueryMsg {
/// Shows all admins and whether or not it is mutable
Config {},
AdminList {},
}

#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
pub struct ConfigResponse {
pub struct AdminListResponse {
pub admins: Vec<HumanAddr>,
pub mutable: bool,
}
Loading