From 6b7523bc72d72d00fb2f806e3b053673128d584a Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 12 Aug 2020 23:18:24 +0200 Subject: [PATCH 1/3] Config -> AdminList in cw1-whitelist --- contracts/cw1-subkeys/examples/schema.rs | 4 +-- contracts/cw1-subkeys/src/contract.rs | 18 +++++------ contracts/cw1-whitelist/examples/schema.rs | 8 ++--- ...response.json => admin_list_response.json} | 2 +- ...le_msg_for__empty.json => handle_msg.json} | 2 +- contracts/cw1-whitelist/schema/query_msg.json | 4 +-- contracts/cw1-whitelist/src/contract.rs | 32 +++++++++---------- contracts/cw1-whitelist/src/msg.rs | 4 +-- contracts/cw1-whitelist/src/state.rs | 20 ++++++------ 9 files changed, 47 insertions(+), 47 deletions(-) rename contracts/cw1-whitelist/schema/{config_response.json => admin_list_response.json} (92%) rename contracts/cw1-whitelist/schema/{handle_msg_for__empty.json => handle_msg.json} (99%) diff --git a/contracts/cw1-subkeys/examples/schema.rs b/contracts/cw1-subkeys/examples/schema.rs index 6ef22a424..9438669e1 100644 --- a/contracts/cw1-subkeys/examples/schema.rs +++ b/contracts/cw1-subkeys/examples/schema.rs @@ -5,7 +5,7 @@ use cosmwasm_schema::{export_schema, 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(); @@ -17,5 +17,5 @@ fn main() { export_schema(&schema_for!(HandleMsg), &out_dir); 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); } diff --git a/contracts/cw1-subkeys/src/contract.rs b/contracts/cw1-subkeys/src/contract.rs index 2fcab540d..3d70cc4ed 100644 --- a/contracts/cw1-subkeys/src/contract.rs +++ b/contracts/cw1-subkeys/src/contract.rs @@ -8,7 +8,7 @@ use cosmwasm_std::{ use cw1_whitelist::{ contract::{handle_freeze, handle_update_admins, init as whitelist_init, query_config}, msg::InitMsg, - state::config_read, + state::admin_list_read, }; use cw20::Expiration; @@ -56,7 +56,7 @@ pub fn handle_execute( 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) { @@ -108,7 +108,7 @@ pub fn handle_increase_allowance( 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)?; @@ -152,7 +152,7 @@ pub fn handle_decrease_allowance( 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)?; @@ -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( @@ -321,7 +321,7 @@ mod tests { let config = query_config(&deps).unwrap(); assert_eq!( config, - ConfigResponse { + AdminListResponse { admins: initial_admins.clone(), mutable: true, } @@ -339,7 +339,7 @@ mod tests { println!("config: {:#?}", config); assert_eq!( config, - ConfigResponse { + AdminListResponse { admins: new_admins, mutable: true, } @@ -356,7 +356,7 @@ mod tests { println!("config: {:#?}", config); assert_eq!( config, - ConfigResponse { + AdminListResponse { admins: vec![admin3.clone()], mutable: true, } @@ -384,7 +384,7 @@ mod tests { println!("config: {:#?}", config); assert_eq!( config, - ConfigResponse { + AdminListResponse { admins: vec![admin3, owner], mutable: true, } diff --git a/contracts/cw1-whitelist/examples/schema.rs b/contracts/cw1-whitelist/examples/schema.rs index 66b19130f..b1a6e2004 100644 --- a/contracts/cw1-whitelist/examples/schema.rs +++ b/contracts/cw1-whitelist/examples/schema.rs @@ -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(); @@ -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); } diff --git a/contracts/cw1-whitelist/schema/config_response.json b/contracts/cw1-whitelist/schema/admin_list_response.json similarity index 92% rename from contracts/cw1-whitelist/schema/config_response.json rename to contracts/cw1-whitelist/schema/admin_list_response.json index 5682f83b5..4173be545 100644 --- a/contracts/cw1-whitelist/schema/config_response.json +++ b/contracts/cw1-whitelist/schema/admin_list_response.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "title": "ConfigResponse", + "title": "AdminListResponse", "type": "object", "required": [ "admins", diff --git a/contracts/cw1-whitelist/schema/handle_msg_for__empty.json b/contracts/cw1-whitelist/schema/handle_msg.json similarity index 99% rename from contracts/cw1-whitelist/schema/handle_msg_for__empty.json rename to contracts/cw1-whitelist/schema/handle_msg.json index 00e018fda..c26ab5056 100644 --- a/contracts/cw1-whitelist/schema/handle_msg_for__empty.json +++ b/contracts/cw1-whitelist/schema/handle_msg.json @@ -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", diff --git a/contracts/cw1-whitelist/schema/query_msg.json b/contracts/cw1-whitelist/schema/query_msg.json index 166d3e351..fa774fc91 100644 --- a/contracts/cw1-whitelist/schema/query_msg.json +++ b/contracts/cw1-whitelist/schema/query_msg.json @@ -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" } } diff --git a/contracts/cw1-whitelist/src/contract.rs b/contracts/cw1-whitelist/src/contract.rs index 6cb0efd0e..438bac49e 100644 --- a/contracts/cw1-whitelist/src/contract.rs +++ b/contracts/cw1-whitelist/src/contract.rs @@ -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( deps: &mut Extern, _env: Env, msg: InitMsg, ) -> StdResult { - 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()) } @@ -55,7 +55,7 @@ pub fn handle_execute( 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 { @@ -70,12 +70,12 @@ pub fn handle_freeze( deps: &mut Extern, env: Env, ) -> StdResult { - 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")]; @@ -88,12 +88,12 @@ pub fn handle_update_admins( env: Env, admins: Vec, ) -> StdResult { - 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")]; @@ -106,15 +106,15 @@ pub fn query( msg: QueryMsg, ) -> StdResult { match msg { - QueryMsg::Config {} => to_binary(&query_config(deps)?), + QueryMsg::AdminList {} => to_binary(&query_config(deps)?), } } pub fn query_config( deps: &Extern, -) -> StdResult { - let cfg = config_read(&deps.storage).load()?; - Ok(ConfigResponse { +) -> StdResult { + let cfg = admin_list_read(&deps.storage).load()?; + Ok(AdminListResponse { admins: map_human(&deps.api, &cfg.admins)?, mutable: cfg.mutable, }) @@ -147,7 +147,7 @@ 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, }; @@ -172,7 +172,7 @@ 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, }; @@ -189,7 +189,7 @@ 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, }; diff --git a/contracts/cw1-whitelist/src/msg.rs b/contracts/cw1-whitelist/src/msg.rs index a2c8be078..b82933639 100644 --- a/contracts/cw1-whitelist/src/msg.rs +++ b/contracts/cw1-whitelist/src/msg.rs @@ -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, pub mutable: bool, } diff --git a/contracts/cw1-whitelist/src/state.rs b/contracts/cw1-whitelist/src/state.rs index ca2a18274..972575641 100644 --- a/contracts/cw1-whitelist/src/state.rs +++ b/contracts/cw1-whitelist/src/state.rs @@ -5,12 +5,12 @@ use cosmwasm_std::{CanonicalAddr, ReadonlyStorage, Storage}; use cosmwasm_storage::{singleton, singleton_read, ReadonlySingleton, Singleton}; #[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug, Default)] -pub struct Config { +pub struct AdminList { pub admins: Vec, pub mutable: bool, } -impl Config { +impl AdminList { /// returns true if the address is a registered admin pub fn is_admin(&self, addr: &CanonicalAddr) -> bool { self.admins.iter().any(|a| a == addr) @@ -22,15 +22,15 @@ impl Config { } } -pub const CONFIG_KEY: &[u8] = b"config"; +pub const ADMIN_LIST_KEY: &[u8] = b"admin_list"; // config is all config information -pub fn config(storage: &mut S) -> Singleton { - singleton(storage, CONFIG_KEY) +pub fn admin_list(storage: &mut S) -> Singleton { + singleton(storage, ADMIN_LIST_KEY) } -pub fn config_read(storage: &S) -> ReadonlySingleton { - singleton_read(storage, CONFIG_KEY) +pub fn admin_list_read(storage: &S) -> ReadonlySingleton { + singleton_read(storage, ADMIN_LIST_KEY) } #[cfg(test)] @@ -46,7 +46,7 @@ mod tests { .into_iter() .map(|name| api.canonical_address(&HumanAddr::from(name)).unwrap()) .collect(); - let config = Config { + let config = AdminList { admins: admins.clone(), mutable: false, }; @@ -64,7 +64,7 @@ mod tests { let bob = api.canonical_address(&HumanAddr::from("bob")).unwrap(); // admin can modify mutable contract - let config = Config { + let config = AdminList { admins: vec![bob.clone()], mutable: true, }; @@ -72,7 +72,7 @@ mod tests { assert!(config.can_modify(&bob)); // no one can modify an immutable contract - let config = Config { + let config = AdminList { admins: vec![alice.clone()], mutable: false, }; From 75ce281fd88815f910e7a85dc98cae71f71d995d Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 12 Aug 2020 23:21:31 +0200 Subject: [PATCH 2/3] Update cw1-subkeys to use same as cw1-whitelist --- contracts/cw1-subkeys/examples/schema.rs | 4 ++-- ...config_response.json => admin_list_response.json} | 2 +- .../{handle_msg_for__empty.json => handle_msg.json} | 2 +- contracts/cw1-subkeys/schema/query_msg.json | 6 +++--- contracts/cw1-subkeys/src/contract.rs | 12 ++++++------ contracts/cw1-subkeys/src/msg.rs | 4 ++-- contracts/cw1-whitelist/src/contract.rs | 10 +++++----- 7 files changed, 20 insertions(+), 20 deletions(-) rename contracts/cw1-subkeys/schema/{config_response.json => admin_list_response.json} (92%) rename contracts/cw1-subkeys/schema/{handle_msg_for__empty.json => handle_msg.json} (99%) diff --git a/contracts/cw1-subkeys/examples/schema.rs b/contracts/cw1-subkeys/examples/schema.rs index 9438669e1..ea09b60ed 100644 --- a/contracts/cw1-subkeys/examples/schema.rs +++ b/contracts/cw1-subkeys/examples/schema.rs @@ -1,7 +1,7 @@ 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; @@ -14,7 +14,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!(Allowance), &out_dir); export_schema(&schema_for!(AdminListResponse), &out_dir); diff --git a/contracts/cw1-subkeys/schema/config_response.json b/contracts/cw1-subkeys/schema/admin_list_response.json similarity index 92% rename from contracts/cw1-subkeys/schema/config_response.json rename to contracts/cw1-subkeys/schema/admin_list_response.json index 5682f83b5..4173be545 100644 --- a/contracts/cw1-subkeys/schema/config_response.json +++ b/contracts/cw1-subkeys/schema/admin_list_response.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "title": "ConfigResponse", + "title": "AdminListResponse", "type": "object", "required": [ "admins", diff --git a/contracts/cw1-subkeys/schema/handle_msg_for__empty.json b/contracts/cw1-subkeys/schema/handle_msg.json similarity index 99% rename from contracts/cw1-subkeys/schema/handle_msg_for__empty.json rename to contracts/cw1-subkeys/schema/handle_msg.json index 9c682d3a6..8f9f5c9dd 100644 --- a/contracts/cw1-subkeys/schema/handle_msg_for__empty.json +++ b/contracts/cw1-subkeys/schema/handle_msg.json @@ -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", diff --git a/contracts/cw1-subkeys/schema/query_msg.json b/contracts/cw1-subkeys/schema/query_msg.json index 67c8ff37d..2de08c415 100644 --- a/contracts/cw1-subkeys/schema/query_msg.json +++ b/contracts/cw1-subkeys/schema/query_msg.json @@ -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" } } diff --git a/contracts/cw1-subkeys/src/contract.rs b/contracts/cw1-subkeys/src/contract.rs index 3d70cc4ed..da653d727 100644 --- a/contracts/cw1-subkeys/src/contract.rs +++ b/contracts/cw1-subkeys/src/contract.rs @@ -6,7 +6,7 @@ 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::admin_list_read, }; @@ -196,7 +196,7 @@ pub fn query( msg: QueryMsg, ) -> StdResult { 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)?), } } @@ -318,7 +318,7 @@ 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, AdminListResponse { @@ -335,7 +335,7 @@ 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, @@ -352,7 +352,7 @@ 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, @@ -380,7 +380,7 @@ 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, diff --git a/contracts/cw1-subkeys/src/msg.rs b/contracts/cw1-subkeys/src/msg.rs index ece1fd529..2b8763bc8 100644 --- a/contracts/cw1-subkeys/src/msg.rs +++ b/contracts/cw1-subkeys/src/msg.rs @@ -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 }, diff --git a/contracts/cw1-whitelist/src/contract.rs b/contracts/cw1-whitelist/src/contract.rs index 438bac49e..63edc7a9d 100644 --- a/contracts/cw1-whitelist/src/contract.rs +++ b/contracts/cw1-whitelist/src/contract.rs @@ -106,11 +106,11 @@ pub fn query( msg: QueryMsg, ) -> StdResult { match msg { - QueryMsg::AdminList {} => to_binary(&query_config(deps)?), + QueryMsg::AdminList {} => to_binary(&query_admin_list(deps)?), } } -pub fn query_config( +pub fn query_admin_list( deps: &Extern, ) -> StdResult { let cfg = admin_list_read(&deps.storage).load()?; @@ -151,7 +151,7 @@ mod tests { 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 { @@ -176,7 +176,7 @@ mod tests { 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, &[]); @@ -193,7 +193,7 @@ mod tests { 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 { From 11f1d5583d1529f912ead2a0494c076cd730dc11 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 12 Aug 2020 23:30:16 +0200 Subject: [PATCH 3/3] Rename Meta to TokenInfo in cw20 contracts --- contracts/cw20-base/examples/schema.rs | 4 +- contracts/cw20-base/schema/query_msg.json | 6 +- ...response.json => token_info_response.json} | 2 +- contracts/cw20-base/src/allowances.rs | 14 ++-- contracts/cw20-base/src/contract.rs | 74 +++++++++---------- contracts/cw20-base/src/msg.rs | 4 +- contracts/cw20-base/src/state.rs | 28 +++---- packages/cw20/README.md | 4 +- packages/cw20/examples/schema.rs | 6 +- packages/cw20/schema/cw20_query_msg.json | 6 +- ...response.json => token_info_response.json} | 2 +- packages/cw20/src/helpers.rs | 7 +- packages/cw20/src/lib.rs | 2 +- packages/cw20/src/query.rs | 6 +- 14 files changed, 78 insertions(+), 87 deletions(-) rename contracts/cw20-base/schema/{meta_response.json => token_info_response.json} (94%) rename packages/cw20/schema/{meta_response.json => token_info_response.json} (94%) diff --git a/contracts/cw20-base/examples/schema.rs b/contracts/cw20-base/examples/schema.rs index 588cdf122..c91ee4343 100644 --- a/contracts/cw20-base/examples/schema.rs +++ b/contracts/cw20-base/examples/schema.rs @@ -3,7 +3,7 @@ use std::fs::create_dir_all; use cosmwasm_schema::{export_schema, remove_schemas, schema_for}; -use cw20::{BalanceResponse, MetaResponse}; +use cw20::{BalanceResponse, TokenInfoResponse}; use cw20_base::msg::{HandleMsg, InitMsg, QueryMsg}; fn main() { @@ -16,5 +16,5 @@ fn main() { export_schema(&schema_for!(HandleMsg), &out_dir); export_schema(&schema_for!(QueryMsg), &out_dir); export_schema(&schema_for!(BalanceResponse), &out_dir); - export_schema(&schema_for!(MetaResponse), &out_dir); + export_schema(&schema_for!(TokenInfoResponse), &out_dir); } diff --git a/contracts/cw20-base/schema/query_msg.json b/contracts/cw20-base/schema/query_msg.json index 68cdf1490..501eb6f51 100644 --- a/contracts/cw20-base/schema/query_msg.json +++ b/contracts/cw20-base/schema/query_msg.json @@ -23,13 +23,13 @@ } }, { - "description": "Returns metadata on the contract - name, decimals, supply, etc. Return type: MetaResponse.", + "description": "Returns metadata on the contract - name, decimals, supply, etc. Return type: TokenInfoResponse.", "type": "object", "required": [ - "meta" + "token_info" ], "properties": { - "meta": { + "token_info": { "type": "object" } } diff --git a/contracts/cw20-base/schema/meta_response.json b/contracts/cw20-base/schema/token_info_response.json similarity index 94% rename from contracts/cw20-base/schema/meta_response.json rename to contracts/cw20-base/schema/token_info_response.json index 211cde46f..effb03ea0 100644 --- a/contracts/cw20-base/schema/meta_response.json +++ b/contracts/cw20-base/schema/token_info_response.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "title": "MetaResponse", + "title": "TokenInfoResponse", "type": "object", "required": [ "decimals", diff --git a/contracts/cw20-base/src/allowances.rs b/contracts/cw20-base/src/allowances.rs index f0d70bc9f..8284be5c8 100644 --- a/contracts/cw20-base/src/allowances.rs +++ b/contracts/cw20-base/src/allowances.rs @@ -4,7 +4,7 @@ use cosmwasm_std::{ }; use cw20::{AllowanceResponse, Cw20ReceiveMsg, Expiration}; -use crate::state::{allowance_remove, allowances, allowances_read, balances, meta}; +use crate::state::{allowances, allowances_read, balances, token_info}; pub fn handle_increase_allowance( deps: &mut Extern, @@ -67,7 +67,7 @@ pub fn handle_decrease_allowance( } bucket.save(spender_raw.as_slice(), &allowance)?; } else { - allowance_remove(&mut deps.storage, owner_raw, spender_raw); + allowances(&mut deps.storage, owner_raw).remove(spender_raw.as_slice()); } let res = HandleResponse { @@ -173,7 +173,7 @@ pub fn handle_burn_from( balance.unwrap_or_default() - amount })?; // reduce total_supply - meta(&mut deps.storage).update(|mut meta| { + token_info(&mut deps.storage).update(|mut meta| { meta.total_supply = (meta.total_supply - amount)?; Ok(meta) })?; @@ -265,9 +265,9 @@ mod tests { use cosmwasm_std::testing::{mock_dependencies, mock_env}; use cosmwasm_std::{coins, CosmosMsg, StdError, WasmMsg}; - use cw20::MetaResponse; + use cw20::TokenInfoResponse; - use crate::contract::{handle, init, query_balance, query_meta}; + use crate::contract::{handle, init, query_balance, query_token_info}; use crate::msg::{HandleMsg, InitMsg, InitialBalance}; fn get_balance>( @@ -282,7 +282,7 @@ mod tests { deps: &mut Extern, addr: &HumanAddr, amount: Uint128, - ) -> MetaResponse { + ) -> TokenInfoResponse { let init_msg = InitMsg { name: "Auto Gen".to_string(), symbol: "AUTO".to_string(), @@ -295,7 +295,7 @@ mod tests { }; let env = mock_env(&HumanAddr("creator".to_string()), &[]); init(deps, env, init_msg).unwrap(); - query_meta(&deps).unwrap() + query_token_info(&deps).unwrap() } #[test] diff --git a/contracts/cw20-base/src/contract.rs b/contracts/cw20-base/src/contract.rs index 5cd7ee1a1..18e0bbf39 100644 --- a/contracts/cw20-base/src/contract.rs +++ b/contracts/cw20-base/src/contract.rs @@ -2,21 +2,21 @@ use cosmwasm_std::{ log, to_binary, Api, Binary, Env, Extern, HandleResponse, HumanAddr, InitResponse, Querier, StdError, StdResult, Storage, Uint128, }; -use cw20::{BalanceResponse, Cw20ReceiveMsg, MetaResponse, MinterResponse}; +use cw20::{BalanceResponse, Cw20ReceiveMsg, MinterResponse, TokenInfoResponse}; use crate::allowances::{ handle_burn_from, handle_decrease_allowance, handle_increase_allowance, handle_send_from, handle_transfer_from, query_allowance, }; use crate::msg::{HandleMsg, InitMsg, InitialBalance, QueryMsg}; -use crate::state::{balances, balances_read, meta, meta_read, Meta, MinterData}; +use crate::state::{balances, balances_read, token_info, token_info_read, MinterData, TokenInfo}; pub fn init( deps: &mut Extern, _env: Env, msg: InitMsg, ) -> StdResult { - // check valid meta-data + // check valid token info msg.validate()?; // create initial accounts let total_supply = create_accounts(deps, &msg.initial_balances)?; @@ -35,15 +35,15 @@ pub fn init( None => None, }; - // store metadata - let data = Meta { + // store token info + let data = TokenInfo { name: msg.name, symbol: msg.symbol, decimals: msg.decimals, total_supply, mint, }; - meta(&mut deps.storage).save(&data)?; + token_info(&mut deps.storage).save(&data)?; Ok(InitResponse::default()) } @@ -143,9 +143,9 @@ pub fn handle_burn( balance.unwrap_or_default() - amount })?; // reduce total_supply - meta(&mut deps.storage).update(|mut meta| { - meta.total_supply = (meta.total_supply - amount)?; - Ok(meta) + token_info(&mut deps.storage).update(|mut info| { + info.total_supply = (info.total_supply - amount)?; + Ok(info) })?; let res = HandleResponse { @@ -166,7 +166,7 @@ pub fn handle_mint( recipient: HumanAddr, amount: Uint128, ) -> StdResult { - let mut config = meta_read(&deps.storage).load()?; + let mut config = token_info_read(&deps.storage).load()?; if config.mint.is_none() || config.mint.as_ref().unwrap().minter != deps.api.canonical_address(&env.message.sender)? @@ -181,7 +181,7 @@ pub fn handle_mint( return Err(StdError::generic_err("Minting cannot exceed the cap")); } } - meta(&mut deps.storage).save(&config)?; + token_info(&mut deps.storage).save(&config)?; // add amount to recipient balance let rcpt_raw = deps.api.canonical_address(&recipient)?; @@ -250,7 +250,7 @@ pub fn query( ) -> StdResult { match msg { QueryMsg::Balance { address } => to_binary(&query_balance(deps, address)?), - QueryMsg::Meta {} => to_binary(&query_meta(deps)?), + QueryMsg::TokenInfo {} => to_binary(&query_token_info(deps)?), QueryMsg::Minter {} => to_binary(&query_minter(deps)?), QueryMsg::Allowance { owner, spender } => { to_binary(&query_allowance(deps, owner, spender)?) @@ -269,15 +269,15 @@ pub fn query_balance( Ok(BalanceResponse { balance }) } -pub fn query_meta( +pub fn query_token_info( deps: &Extern, -) -> StdResult { - let meta = meta_read(&deps.storage).load()?; - let res = MetaResponse { - name: meta.name, - symbol: meta.symbol, - decimals: meta.decimals, - total_supply: meta.total_supply, +) -> StdResult { + let info = token_info_read(&deps.storage).load()?; + let res = TokenInfoResponse { + name: info.name, + symbol: info.symbol, + decimals: info.decimals, + total_supply: info.total_supply, }; Ok(res) } @@ -285,7 +285,7 @@ pub fn query_meta( pub fn query_minter( deps: &Extern, ) -> StdResult> { - let meta = meta_read(&deps.storage).load()?; + let meta = token_info_read(&deps.storage).load()?; let minter = match meta.mint { Some(m) => Some(MinterResponse { minter: deps.api.human_address(&m.minter)?, @@ -318,7 +318,7 @@ mod tests { amount: Uint128, minter: &HumanAddr, cap: Option, - ) -> MetaResponse { + ) -> TokenInfoResponse { _do_init( deps, addr, @@ -335,7 +335,7 @@ mod tests { deps: &mut Extern, addr: &HumanAddr, amount: Uint128, - ) -> MetaResponse { + ) -> TokenInfoResponse { _do_init(deps, addr, amount, None) } @@ -345,7 +345,7 @@ mod tests { addr: &HumanAddr, amount: Uint128, mint: Option, - ) -> MetaResponse { + ) -> TokenInfoResponse { let init_msg = InitMsg { name: "Auto Gen".to_string(), symbol: "AUTO".to_string(), @@ -360,10 +360,10 @@ mod tests { let res = init(deps, env, init_msg).unwrap(); assert_eq!(0, res.messages.len()); - let meta = query_meta(&deps).unwrap(); + let meta = query_token_info(&deps).unwrap(); assert_eq!( meta, - MetaResponse { + TokenInfoResponse { name: "Auto Gen".to_string(), symbol: "AUTO".to_string(), decimals: 3, @@ -394,8 +394,8 @@ mod tests { assert_eq!(0, res.messages.len()); assert_eq!( - query_meta(&deps).unwrap(), - MetaResponse { + query_token_info(&deps).unwrap(), + TokenInfoResponse { name: "Cash Token".to_string(), symbol: "CASH".to_string(), decimals: 9, @@ -429,8 +429,8 @@ mod tests { assert_eq!(0, res.messages.len()); assert_eq!( - query_meta(&deps).unwrap(), - MetaResponse { + query_token_info(&deps).unwrap(), + TokenInfoResponse { name: "Cash Token".to_string(), symbol: "CASH".to_string(), decimals: 9, @@ -582,8 +582,8 @@ mod tests { assert_eq!(0, res.messages.len()); assert_eq!( - query_meta(&deps).unwrap(), - MetaResponse { + query_token_info(&deps).unwrap(), + TokenInfoResponse { name: "Bash Shell".to_string(), symbol: "BASH".to_string(), decimals: 6, @@ -603,7 +603,7 @@ mod tests { let expected = do_init(&mut deps, &addr1, amount1); // check meta query - let loaded = query_meta(&deps).unwrap(); + let loaded = query_token_info(&deps).unwrap(); assert_eq!(expected, loaded); // check balance query (full) @@ -676,7 +676,7 @@ mod tests { let remainder = (amount1 - transfer).unwrap(); assert_eq!(get_balance(&deps, &addr1), remainder); assert_eq!(get_balance(&deps, &addr2), transfer); - assert_eq!(query_meta(&deps).unwrap().total_supply, amount1); + assert_eq!(query_token_info(&deps).unwrap().total_supply, amount1); } #[test] @@ -697,7 +697,7 @@ mod tests { StdError::Underflow { .. } => {} e => panic!("Unexpected error: {}", e), } - assert_eq!(query_meta(&deps).unwrap().total_supply, amount1); + assert_eq!(query_token_info(&deps).unwrap().total_supply, amount1); // valid burn reduces total supply let env = mock_env(addr1.clone(), &[]); @@ -707,7 +707,7 @@ mod tests { let remainder = (amount1 - burn).unwrap(); assert_eq!(get_balance(&deps, &addr1), remainder); - assert_eq!(query_meta(&deps).unwrap().total_supply, remainder); + assert_eq!(query_token_info(&deps).unwrap().total_supply, remainder); } #[test] @@ -768,6 +768,6 @@ mod tests { let remainder = (amount1 - transfer).unwrap(); assert_eq!(get_balance(&deps, &addr1), remainder); assert_eq!(get_balance(&deps, &contract), transfer); - assert_eq!(query_meta(&deps).unwrap().total_supply, amount1); + assert_eq!(query_token_info(&deps).unwrap().total_supply, amount1); } } diff --git a/contracts/cw20-base/src/msg.rs b/contracts/cw20-base/src/msg.rs index 4ec723d9f..8b56c30b7 100644 --- a/contracts/cw20-base/src/msg.rs +++ b/contracts/cw20-base/src/msg.rs @@ -128,8 +128,8 @@ pub enum QueryMsg { /// Return type: BalanceResponse. Balance { address: HumanAddr }, /// Returns metadata on the contract - name, decimals, supply, etc. - /// Return type: MetaResponse. - Meta {}, + /// Return type: TokenInfoResponse. + TokenInfo {}, /// Only with "mintable" extension. /// Returns who can mint and how much. /// Return type: MinterResponse. diff --git a/contracts/cw20-base/src/state.rs b/contracts/cw20-base/src/state.rs index 7121ad1f0..adea77c5b 100644 --- a/contracts/cw20-base/src/state.rs +++ b/contracts/cw20-base/src/state.rs @@ -3,14 +3,14 @@ use serde::{Deserialize, Serialize}; use cosmwasm_std::{CanonicalAddr, ReadonlyStorage, Storage, Uint128}; use cosmwasm_storage::{ - bucket, bucket_read, singleton, singleton_read, Bucket, PrefixedStorage, ReadonlyBucket, - ReadonlySingleton, Singleton, + bucket, bucket_read, singleton, singleton_read, Bucket, ReadonlyBucket, ReadonlySingleton, + Singleton, }; use cw20::AllowanceResponse; #[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] #[serde(rename_all = "snake_case")] -pub struct Meta { +pub struct TokenInfo { pub name: String, pub symbol: String, pub decimals: u8, @@ -25,23 +25,23 @@ pub struct MinterData { pub cap: Option, } -impl Meta { +impl TokenInfo { pub fn get_cap(&self) -> Option { self.mint.as_ref().and_then(|v| v.cap) } } -const META_KEY: &[u8] = b"meta"; +const TOKEN_INFO_KEY: &[u8] = b"token_info"; const PREFIX_BALANCE: &[u8] = b"balance"; const PREFIX_ALLOWANCE: &[u8] = b"allowance"; // meta is the token definition as well as the total_supply -pub fn meta(storage: &mut S) -> Singleton { - singleton(storage, META_KEY) +pub fn token_info(storage: &mut S) -> Singleton { + singleton(storage, TOKEN_INFO_KEY) } -pub fn meta_read(storage: &S) -> ReadonlySingleton { - singleton_read(storage, META_KEY) +pub fn token_info_read(storage: &S) -> ReadonlySingleton { + singleton_read(storage, TOKEN_INFO_KEY) } /// balances are state of the erc20 tokens @@ -70,13 +70,3 @@ pub fn allowances_read<'a, S: ReadonlyStorage>( ) -> ReadonlyBucket<'a, S, AllowanceResponse> { ReadonlyBucket::multilevel(&[PREFIX_ALLOWANCE, owner.as_slice()], storage) } - -// we delete the allowance (TODO: expose this in Bucket for simpler API) -pub fn allowance_remove( - storage: &mut S, - owner: &CanonicalAddr, - spender: &CanonicalAddr, -) { - PrefixedStorage::multilevel(&[PREFIX_ALLOWANCE, owner.as_slice()], storage) - .remove(spender.as_slice()); -} diff --git a/packages/cw20/README.md b/packages/cw20/README.md index 6b69c6a33..d4c8a3de3 100644 --- a/packages/cw20/README.md +++ b/packages/cw20/README.md @@ -36,8 +36,8 @@ and reduce `total_supply` by the same amount. Returns "0" if the address is unknown to the contract. Return type is `BalanceResponse{balance}`. -`Meta{}` - Returns the meta-data of the contract. Return type is -`MetaData{name, symbol, decimal, total_supply}`. +`TokenInfo{}` - Returns the token info of the contract. Return type is +`TokenInfoResponse{name, symbol, decimal, total_supply}`. ### Receiver diff --git a/packages/cw20/examples/schema.rs b/packages/cw20/examples/schema.rs index a72b4a245..afef25c27 100644 --- a/packages/cw20/examples/schema.rs +++ b/packages/cw20/examples/schema.rs @@ -4,8 +4,8 @@ use std::fs::create_dir_all; use cosmwasm_schema::{export_schema, remove_schemas, schema_for}; use cw20::{ - AllowanceResponse, BalanceResponse, Cw20HandleMsg, Cw20QueryMsg, Cw20ReceiveMsg, MetaResponse, - MinterResponse, + AllowanceResponse, BalanceResponse, Cw20HandleMsg, Cw20QueryMsg, Cw20ReceiveMsg, + MinterResponse, TokenInfoResponse, }; fn main() { @@ -19,6 +19,6 @@ fn main() { export_schema(&schema_for!(Cw20ReceiveMsg), &out_dir); export_schema(&schema_for!(AllowanceResponse), &out_dir); export_schema(&schema_for!(BalanceResponse), &out_dir); - export_schema(&schema_for!(MetaResponse), &out_dir); + export_schema(&schema_for!(TokenInfoResponse), &out_dir); export_schema(&schema_for!(MinterResponse), &out_dir); } diff --git a/packages/cw20/schema/cw20_query_msg.json b/packages/cw20/schema/cw20_query_msg.json index 9c901fd5a..95aa90682 100644 --- a/packages/cw20/schema/cw20_query_msg.json +++ b/packages/cw20/schema/cw20_query_msg.json @@ -23,13 +23,13 @@ } }, { - "description": "Returns metadata on the contract - name, decimals, supply, etc. Return type: MetaResponse.", + "description": "Returns metadata on the contract - name, decimals, supply, etc. Return type: TokenInfoResponse.", "type": "object", "required": [ - "meta" + "token_info" ], "properties": { - "meta": { + "token_info": { "type": "object" } } diff --git a/packages/cw20/schema/meta_response.json b/packages/cw20/schema/token_info_response.json similarity index 94% rename from packages/cw20/schema/meta_response.json rename to packages/cw20/schema/token_info_response.json index 211cde46f..effb03ea0 100644 --- a/packages/cw20/schema/meta_response.json +++ b/packages/cw20/schema/token_info_response.json @@ -1,6 +1,6 @@ { "$schema": "http://json-schema.org/draft-07/schema#", - "title": "MetaResponse", + "title": "TokenInfoResponse", "type": "object", "required": [ "decimals", diff --git a/packages/cw20/src/helpers.rs b/packages/cw20/src/helpers.rs index 26ed1861c..ad711bdb0 100644 --- a/packages/cw20/src/helpers.rs +++ b/packages/cw20/src/helpers.rs @@ -7,7 +7,8 @@ use cosmwasm_std::{ }; use crate::{ - AllowanceResponse, BalanceResponse, Cw20HandleMsg, Cw20QueryMsg, MetaResponse, MinterResponse, + AllowanceResponse, BalanceResponse, Cw20HandleMsg, Cw20QueryMsg, MinterResponse, + TokenInfoResponse, }; /// Cw20Contract is a wrapper around HumanAddr that provides a lot of helpers @@ -52,8 +53,8 @@ impl Cw20Contract { /// Get metadata from the contract. This is a good check that the address /// is a valid Cw20 contract. - pub fn meta(&self, querier: &Q) -> StdResult { - let msg = Cw20QueryMsg::Meta {}; + pub fn meta(&self, querier: &Q) -> StdResult { + let msg = Cw20QueryMsg::TokenInfo {}; let query = WasmQuery::Smart { contract_addr: self.addr(), msg: to_binary(&msg)?, diff --git a/packages/cw20/src/lib.rs b/packages/cw20/src/lib.rs index b101ad089..669a264d1 100644 --- a/packages/cw20/src/lib.rs +++ b/packages/cw20/src/lib.rs @@ -6,7 +6,7 @@ mod receiver; pub use crate::helpers::{Cw20CanonicalContract, Cw20Contract}; pub use crate::msg::{Cw20HandleMsg, Expiration}; pub use crate::query::{ - AllowanceResponse, BalanceResponse, Cw20QueryMsg, MetaResponse, MinterResponse, + AllowanceResponse, BalanceResponse, Cw20QueryMsg, MinterResponse, TokenInfoResponse, }; pub use crate::receiver::Cw20ReceiveMsg; diff --git a/packages/cw20/src/query.rs b/packages/cw20/src/query.rs index e7fccf985..637ca226e 100644 --- a/packages/cw20/src/query.rs +++ b/packages/cw20/src/query.rs @@ -12,8 +12,8 @@ pub enum Cw20QueryMsg { /// Return type: BalanceResponse. Balance { address: HumanAddr }, /// Returns metadata on the contract - name, decimals, supply, etc. - /// Return type: MetaResponse. - Meta {}, + /// Return type: TokenInfoResponse. + TokenInfo {}, /// Only with "allowance" extension. /// Returns how much spender can use from owner account, 0 if unset. /// Return type: AllowanceResponse. @@ -33,7 +33,7 @@ pub struct BalanceResponse { } #[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] -pub struct MetaResponse { +pub struct TokenInfoResponse { pub name: String, pub symbol: String, pub decimals: u8,