From b55a074b543f3f136a8e8bf8db4b2d9463db42c0 Mon Sep 17 00:00:00 2001 From: jhernandezb Date: Wed, 27 Mar 2024 08:08:26 -0600 Subject: [PATCH 1/4] minor updates --- contracts/name-minter/src/contract.rs | 14 ++++++++------ .../whitelist-updatable-flatrate/src/contract.rs | 6 +----- .../whitelist-updatable-flatrate/src/helpers.rs | 10 +++++----- .../src/integration_tests.rs | 6 +++--- 4 files changed, 17 insertions(+), 19 deletions(-) diff --git a/contracts/name-minter/src/contract.rs b/contracts/name-minter/src/contract.rs index de228354..2812cd80 100644 --- a/contracts/name-minter/src/contract.rs +++ b/contracts/name-minter/src/contract.rs @@ -177,8 +177,6 @@ pub fn execute_mint_and_list( .transpose()? .unwrap_or(None); - println!("discount: {:?}", discount); - let price = validate_payment(name.len(), &info, params.base_price.u128(), discount)?; if price.clone().is_some() { charge_fees( @@ -343,10 +341,14 @@ fn validate_payment( }) .into(); - if discount.is_some() && amount.ge(&Uint128::from(discount.unwrap())) { - amount = amount - .checked_sub(Uint128::from(discount.unwrap())) - .unwrap(); + if let Some(discount_value) = discount { + let discount_amount = Uint128::from(discount_value); + // TODO: should we handle the case where discount > amount (eg 1,000 discount but buying a 100 name) + if amount.ge(&discount_amount) { + amount = amount + .checked_sub(discount_amount) + .map_err(|_| StdError::generic_err("invalid discount amount"))?; + } } if amount.is_zero() { diff --git a/contracts/whitelist-updatable-flatrate/src/contract.rs b/contracts/whitelist-updatable-flatrate/src/contract.rs index 9a4751b3..e48bb21b 100644 --- a/contracts/whitelist-updatable-flatrate/src/contract.rs +++ b/contracts/whitelist-updatable-flatrate/src/contract.rs @@ -135,11 +135,7 @@ pub fn execute_add_addresses( for address in addresses.into_iter() { let addr = deps.api.addr_validate(&address.clone())?; - if WHITELIST.has(deps.storage, addr.clone()) { - return Err(ContractError::AddressAlreadyExists { - addr: addr.to_string(), - }); - } else { + if !WHITELIST.has(deps.storage, addr.clone()) { WHITELIST.save(deps.storage, addr, &0u32)?; count += 1; } diff --git a/contracts/whitelist-updatable-flatrate/src/helpers.rs b/contracts/whitelist-updatable-flatrate/src/helpers.rs index ee52ea47..37cdb1b6 100644 --- a/contracts/whitelist-updatable-flatrate/src/helpers.rs +++ b/contracts/whitelist-updatable-flatrate/src/helpers.rs @@ -1,6 +1,6 @@ use cosmwasm_schema::cw_serde; use cosmwasm_std::{ - to_binary, Addr, Decimal, QuerierWrapper, QueryRequest, StdResult, WasmMsg, WasmQuery, + to_json_binary, Addr, QuerierWrapper, QueryRequest, StdResult, WasmMsg, WasmQuery, }; use sg_std::CosmosMsg; @@ -19,7 +19,7 @@ impl WhitelistUpdatableFlatrateContract { } pub fn call>(&self, msg: T) -> StdResult { - let msg = to_binary(&msg.into())?; + let msg = to_json_binary(&msg.into())?; Ok(WasmMsg::Execute { contract_addr: self.addr().into(), msg, @@ -37,7 +37,7 @@ impl WhitelistUpdatableFlatrateContract { pub fn includes(&self, querier: &QuerierWrapper, address: String) -> StdResult { let includes: bool = querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_binary(&QueryMsg::IncludesAddress { address })?, + msg: to_json_binary(&QueryMsg::IncludesAddress { address })?, }))?; Ok(includes) } @@ -45,7 +45,7 @@ impl WhitelistUpdatableFlatrateContract { pub fn config(&self, querier: &QuerierWrapper) -> StdResult { let res: Config = querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_binary(&QueryMsg::Config {})?, + msg: to_json_binary(&QueryMsg::Config {})?, }))?; Ok(res) @@ -54,7 +54,7 @@ impl WhitelistUpdatableFlatrateContract { pub fn mint_discount_amount(&self, querier: &QuerierWrapper) -> StdResult> { querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_binary(&QueryMsg::MintDiscountAmount {})?, + msg: to_json_binary(&QueryMsg::MintDiscountAmount {})?, })) } } diff --git a/contracts/whitelist-updatable-flatrate/src/integration_tests.rs b/contracts/whitelist-updatable-flatrate/src/integration_tests.rs index 710a5ff1..0392f1e2 100644 --- a/contracts/whitelist-updatable-flatrate/src/integration_tests.rs +++ b/contracts/whitelist-updatable-flatrate/src/integration_tests.rs @@ -244,7 +244,7 @@ mod tests { ], }; let res = app.execute_contract(Addr::unchecked(OTHER_ADMIN), wl_addr.clone(), &msg, &[]); - assert!(res.is_err()); + assert!(res.is_ok()); let res: bool = app .wrap() .query_wasm_smart( @@ -254,12 +254,12 @@ mod tests { }), ) .unwrap(); - assert!(!res); + assert!(res); let res: u64 = app .wrap() .query_wasm_smart(&wl_addr, &(QueryMsg::AddressCount {})) .unwrap(); - assert_eq!(res, 5); + assert_eq!(res, 6); let msg = ExecuteMsg::AddAddresses { addresses: vec!["addr0007".to_string(), "addr0006".to_string()], }; From 721e86a1e42b66871cabffc12e88e1169050bd4f Mon Sep 17 00:00:00 2001 From: jhernandezb Date: Wed, 27 Mar 2024 08:15:35 -0600 Subject: [PATCH 2/4] ignore lint --- .../src/contract.rs | 22 +++++++++++-------- .../src/helpers.rs | 10 ++++----- contracts/whitelist-updatable/src/helpers.rs | 11 +++++----- packages/sg-name/src/lib.rs | 8 +++---- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/contracts/whitelist-updatable-flatrate/src/contract.rs b/contracts/whitelist-updatable-flatrate/src/contract.rs index e48bb21b..564d1bc8 100644 --- a/contracts/whitelist-updatable-flatrate/src/contract.rs +++ b/contracts/whitelist-updatable-flatrate/src/contract.rs @@ -2,7 +2,7 @@ use crate::state::{Config, CONFIG, TOTAL_ADDRESS_COUNT, WHITELIST}; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - ensure, to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, Event, MessageInfo, Order, + ensure, to_json_binary, Addr, Binary, Deps, DepsMut, Empty, Env, Event, MessageInfo, Order, StdError, StdResult, }; use cw2::set_contract_version; @@ -271,14 +271,18 @@ pub fn execute_purge(deps: DepsMut, info: MessageInfo) -> Result StdResult { match msg { - QueryMsg::Config {} => to_binary(&query_config(deps)?), - QueryMsg::IncludesAddress { address } => to_binary(&query_includes_address(deps, address)?), - QueryMsg::MintCount { address } => to_binary(&query_mint_count(deps, address)?), - QueryMsg::Admins {} => to_binary(&query_admins(deps)?), - QueryMsg::AddressCount {} => to_binary(&query_address_count(deps)?), - QueryMsg::PerAddressLimit {} => to_binary(&query_per_address_limit(deps)?), - QueryMsg::IsProcessable { address } => to_binary(&query_is_processable(deps, address)?), - QueryMsg::MintDiscountAmount {} => to_binary(&query_mint_discount_amount(deps)?), + QueryMsg::Config {} => to_json_binary(&query_config(deps)?), + QueryMsg::IncludesAddress { address } => { + to_json_binary(&query_includes_address(deps, address)?) + } + QueryMsg::MintCount { address } => to_json_binary(&query_mint_count(deps, address)?), + QueryMsg::Admins {} => to_json_binary(&query_admins(deps)?), + QueryMsg::AddressCount {} => to_json_binary(&query_address_count(deps)?), + QueryMsg::PerAddressLimit {} => to_json_binary(&query_per_address_limit(deps)?), + QueryMsg::IsProcessable { address } => { + to_json_binary(&query_is_processable(deps, address)?) + } + QueryMsg::MintDiscountAmount {} => to_json_binary(&query_mint_discount_amount(deps)?), } } diff --git a/contracts/whitelist-updatable-flatrate/src/helpers.rs b/contracts/whitelist-updatable-flatrate/src/helpers.rs index 37cdb1b6..ee52ea47 100644 --- a/contracts/whitelist-updatable-flatrate/src/helpers.rs +++ b/contracts/whitelist-updatable-flatrate/src/helpers.rs @@ -1,6 +1,6 @@ use cosmwasm_schema::cw_serde; use cosmwasm_std::{ - to_json_binary, Addr, QuerierWrapper, QueryRequest, StdResult, WasmMsg, WasmQuery, + to_binary, Addr, Decimal, QuerierWrapper, QueryRequest, StdResult, WasmMsg, WasmQuery, }; use sg_std::CosmosMsg; @@ -19,7 +19,7 @@ impl WhitelistUpdatableFlatrateContract { } pub fn call>(&self, msg: T) -> StdResult { - let msg = to_json_binary(&msg.into())?; + let msg = to_binary(&msg.into())?; Ok(WasmMsg::Execute { contract_addr: self.addr().into(), msg, @@ -37,7 +37,7 @@ impl WhitelistUpdatableFlatrateContract { pub fn includes(&self, querier: &QuerierWrapper, address: String) -> StdResult { let includes: bool = querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_json_binary(&QueryMsg::IncludesAddress { address })?, + msg: to_binary(&QueryMsg::IncludesAddress { address })?, }))?; Ok(includes) } @@ -45,7 +45,7 @@ impl WhitelistUpdatableFlatrateContract { pub fn config(&self, querier: &QuerierWrapper) -> StdResult { let res: Config = querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_json_binary(&QueryMsg::Config {})?, + msg: to_binary(&QueryMsg::Config {})?, }))?; Ok(res) @@ -54,7 +54,7 @@ impl WhitelistUpdatableFlatrateContract { pub fn mint_discount_amount(&self, querier: &QuerierWrapper) -> StdResult> { querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_json_binary(&QueryMsg::MintDiscountAmount {})?, + msg: to_binary(&QueryMsg::MintDiscountAmount {})?, })) } } diff --git a/contracts/whitelist-updatable/src/helpers.rs b/contracts/whitelist-updatable/src/helpers.rs index 459b9ea5..97aac1e6 100644 --- a/contracts/whitelist-updatable/src/helpers.rs +++ b/contracts/whitelist-updatable/src/helpers.rs @@ -1,6 +1,7 @@ use cosmwasm_schema::cw_serde; use cosmwasm_std::{ - to_binary, Addr, Decimal, QuerierWrapper, QueryRequest, StdResult, WasmMsg, WasmQuery, + to_json_binary, to_json_binary, Addr, Decimal, QuerierWrapper, QueryRequest, StdResult, + WasmMsg, WasmQuery, }; use sg_std::CosmosMsg; @@ -19,7 +20,7 @@ impl WhitelistUpdatableContract { } pub fn call>(&self, msg: T) -> StdResult { - let msg = to_binary(&msg.into())?; + let msg = to_json_binary(&msg.into())?; Ok(WasmMsg::Execute { contract_addr: self.addr().into(), msg, @@ -37,7 +38,7 @@ impl WhitelistUpdatableContract { pub fn includes(&self, querier: &QuerierWrapper, address: String) -> StdResult { let includes: bool = querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_binary(&QueryMsg::IncludesAddress { address })?, + msg: to_json_binary(&QueryMsg::IncludesAddress { address })?, }))?; Ok(includes) } @@ -45,7 +46,7 @@ impl WhitelistUpdatableContract { pub fn config(&self, querier: &QuerierWrapper) -> StdResult { let res: Config = querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_binary(&QueryMsg::Config {})?, + msg: to_json_binary(&QueryMsg::Config {})?, }))?; Ok(res) @@ -54,7 +55,7 @@ impl WhitelistUpdatableContract { pub fn mint_discount_percent(&self, querier: &QuerierWrapper) -> StdResult> { querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_binary(&QueryMsg::MintDiscountPercent {})?, + msg: to_json_binary(&QueryMsg::MintDiscountPercent {})?, })) } } diff --git a/packages/sg-name/src/lib.rs b/packages/sg-name/src/lib.rs index 3276b7f2..dee4d3eb 100644 --- a/packages/sg-name/src/lib.rs +++ b/packages/sg-name/src/lib.rs @@ -1,5 +1,5 @@ use cosmwasm_schema::{cw_serde, QueryResponses}; -use cosmwasm_std::{to_vec, Addr}; +use cosmwasm_std::{to_json_vec, Addr}; pub const MAX_TEXT_LENGTH: u32 = 512; @@ -13,7 +13,7 @@ pub struct NFT { impl NFT { pub fn into_json_string(self: &NFT) -> String { - String::from_utf8(to_vec(&self).unwrap_or_default()).unwrap_or_default() + String::from_utf8(to_json_vec(&self).unwrap_or_default()).unwrap_or_default() } } @@ -34,7 +34,7 @@ impl TextRecord { } pub fn into_json_string(self: &TextRecord) -> String { - String::from_utf8(to_vec(&self).unwrap_or_default()).unwrap_or_default() + String::from_utf8(to_json_vec(&self).unwrap_or_default()).unwrap_or_default() } } @@ -53,7 +53,7 @@ impl Metadata { // and represent it as a type. Note that we have to use the CosmWasm fork // of serde_json to avoid floats. pub fn into_json_string(self: &Metadata) -> String { - String::from_utf8(to_vec(&self).unwrap_or_default()).unwrap_or_default() + String::from_utf8(to_json_vec(&self).unwrap_or_default()).unwrap_or_default() } } From a25429fa27632ce54c288e2d824f4b8bb249ed5d Mon Sep 17 00:00:00 2001 From: jhernandezb Date: Wed, 27 Mar 2024 08:18:07 -0600 Subject: [PATCH 3/4] ignore lint --- .../src/contract.rs | 22 ++++++++----------- contracts/whitelist-updatable/src/helpers.rs | 11 +++++----- packages/sg-name/src/lib.rs | 8 +++---- 3 files changed, 18 insertions(+), 23 deletions(-) diff --git a/contracts/whitelist-updatable-flatrate/src/contract.rs b/contracts/whitelist-updatable-flatrate/src/contract.rs index 564d1bc8..e48bb21b 100644 --- a/contracts/whitelist-updatable-flatrate/src/contract.rs +++ b/contracts/whitelist-updatable-flatrate/src/contract.rs @@ -2,7 +2,7 @@ use crate::state::{Config, CONFIG, TOTAL_ADDRESS_COUNT, WHITELIST}; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; use cosmwasm_std::{ - ensure, to_json_binary, Addr, Binary, Deps, DepsMut, Empty, Env, Event, MessageInfo, Order, + ensure, to_binary, Addr, Binary, Deps, DepsMut, Empty, Env, Event, MessageInfo, Order, StdError, StdResult, }; use cw2::set_contract_version; @@ -271,18 +271,14 @@ pub fn execute_purge(deps: DepsMut, info: MessageInfo) -> Result StdResult { match msg { - QueryMsg::Config {} => to_json_binary(&query_config(deps)?), - QueryMsg::IncludesAddress { address } => { - to_json_binary(&query_includes_address(deps, address)?) - } - QueryMsg::MintCount { address } => to_json_binary(&query_mint_count(deps, address)?), - QueryMsg::Admins {} => to_json_binary(&query_admins(deps)?), - QueryMsg::AddressCount {} => to_json_binary(&query_address_count(deps)?), - QueryMsg::PerAddressLimit {} => to_json_binary(&query_per_address_limit(deps)?), - QueryMsg::IsProcessable { address } => { - to_json_binary(&query_is_processable(deps, address)?) - } - QueryMsg::MintDiscountAmount {} => to_json_binary(&query_mint_discount_amount(deps)?), + QueryMsg::Config {} => to_binary(&query_config(deps)?), + QueryMsg::IncludesAddress { address } => to_binary(&query_includes_address(deps, address)?), + QueryMsg::MintCount { address } => to_binary(&query_mint_count(deps, address)?), + QueryMsg::Admins {} => to_binary(&query_admins(deps)?), + QueryMsg::AddressCount {} => to_binary(&query_address_count(deps)?), + QueryMsg::PerAddressLimit {} => to_binary(&query_per_address_limit(deps)?), + QueryMsg::IsProcessable { address } => to_binary(&query_is_processable(deps, address)?), + QueryMsg::MintDiscountAmount {} => to_binary(&query_mint_discount_amount(deps)?), } } diff --git a/contracts/whitelist-updatable/src/helpers.rs b/contracts/whitelist-updatable/src/helpers.rs index 97aac1e6..459b9ea5 100644 --- a/contracts/whitelist-updatable/src/helpers.rs +++ b/contracts/whitelist-updatable/src/helpers.rs @@ -1,7 +1,6 @@ use cosmwasm_schema::cw_serde; use cosmwasm_std::{ - to_json_binary, to_json_binary, Addr, Decimal, QuerierWrapper, QueryRequest, StdResult, - WasmMsg, WasmQuery, + to_binary, Addr, Decimal, QuerierWrapper, QueryRequest, StdResult, WasmMsg, WasmQuery, }; use sg_std::CosmosMsg; @@ -20,7 +19,7 @@ impl WhitelistUpdatableContract { } pub fn call>(&self, msg: T) -> StdResult { - let msg = to_json_binary(&msg.into())?; + let msg = to_binary(&msg.into())?; Ok(WasmMsg::Execute { contract_addr: self.addr().into(), msg, @@ -38,7 +37,7 @@ impl WhitelistUpdatableContract { pub fn includes(&self, querier: &QuerierWrapper, address: String) -> StdResult { let includes: bool = querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_json_binary(&QueryMsg::IncludesAddress { address })?, + msg: to_binary(&QueryMsg::IncludesAddress { address })?, }))?; Ok(includes) } @@ -46,7 +45,7 @@ impl WhitelistUpdatableContract { pub fn config(&self, querier: &QuerierWrapper) -> StdResult { let res: Config = querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_json_binary(&QueryMsg::Config {})?, + msg: to_binary(&QueryMsg::Config {})?, }))?; Ok(res) @@ -55,7 +54,7 @@ impl WhitelistUpdatableContract { pub fn mint_discount_percent(&self, querier: &QuerierWrapper) -> StdResult> { querier.query(&QueryRequest::Wasm(WasmQuery::Smart { contract_addr: self.addr().into(), - msg: to_json_binary(&QueryMsg::MintDiscountPercent {})?, + msg: to_binary(&QueryMsg::MintDiscountPercent {})?, })) } } diff --git a/packages/sg-name/src/lib.rs b/packages/sg-name/src/lib.rs index dee4d3eb..3276b7f2 100644 --- a/packages/sg-name/src/lib.rs +++ b/packages/sg-name/src/lib.rs @@ -1,5 +1,5 @@ use cosmwasm_schema::{cw_serde, QueryResponses}; -use cosmwasm_std::{to_json_vec, Addr}; +use cosmwasm_std::{to_vec, Addr}; pub const MAX_TEXT_LENGTH: u32 = 512; @@ -13,7 +13,7 @@ pub struct NFT { impl NFT { pub fn into_json_string(self: &NFT) -> String { - String::from_utf8(to_json_vec(&self).unwrap_or_default()).unwrap_or_default() + String::from_utf8(to_vec(&self).unwrap_or_default()).unwrap_or_default() } } @@ -34,7 +34,7 @@ impl TextRecord { } pub fn into_json_string(self: &TextRecord) -> String { - String::from_utf8(to_json_vec(&self).unwrap_or_default()).unwrap_or_default() + String::from_utf8(to_vec(&self).unwrap_or_default()).unwrap_or_default() } } @@ -53,7 +53,7 @@ impl Metadata { // and represent it as a type. Note that we have to use the CosmWasm fork // of serde_json to avoid floats. pub fn into_json_string(self: &Metadata) -> String { - String::from_utf8(to_json_vec(&self).unwrap_or_default()).unwrap_or_default() + String::from_utf8(to_vec(&self).unwrap_or_default()).unwrap_or_default() } } From 1e8e4a3949ed29aa9a04ae927645ea724ecf709e Mon Sep 17 00:00:00 2001 From: jhernandezb Date: Wed, 27 Mar 2024 08:32:35 -0600 Subject: [PATCH 4/4] update schema --- contracts/name-minter/schema/name-minter.json | 27 ------------------- 1 file changed, 27 deletions(-) diff --git a/contracts/name-minter/schema/name-minter.json b/contracts/name-minter/schema/name-minter.json index 886cfa02..a7b7ce15 100644 --- a/contracts/name-minter/schema/name-minter.json +++ b/contracts/name-minter/schema/name-minter.json @@ -156,16 +156,6 @@ "properties": { "address": { "type": "string" - }, - "whitelist_type": { - "anyOf": [ - { - "$ref": "#/definitions/WhitelistType" - }, - { - "type": "null" - } - ] } }, "additionalProperties": false @@ -188,16 +178,6 @@ "properties": { "address": { "type": "string" - }, - "whitelist_type": { - "anyOf": [ - { - "$ref": "#/definitions/WhitelistType" - }, - { - "type": "null" - } - ] } }, "additionalProperties": false @@ -252,13 +232,6 @@ "Uint64": { "description": "A thin wrapper around u64 that is using strings for JSON encoding/decoding, such that the full u64 range can be used for clients that convert JSON numbers to floats, like JavaScript and jq.\n\n# Examples\n\nUse `from` to create instances of this and `u64` to get the value out:\n\n``` # use cosmwasm_std::Uint64; let a = Uint64::from(42u64); assert_eq!(a.u64(), 42);\n\nlet b = Uint64::from(70u32); assert_eq!(b.u64(), 70); ```", "type": "string" - }, - "WhitelistType": { - "type": "string", - "enum": [ - "Updatable", - "Flatrate" - ] } } },