Skip to content

Commit

Permalink
minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jhernandezb committed Mar 27, 2024
1 parent eeb9b90 commit b55a074
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 19 deletions.
14 changes: 8 additions & 6 deletions contracts/name-minter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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() {
Expand Down
6 changes: 1 addition & 5 deletions contracts/whitelist-updatable-flatrate/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 5 additions & 5 deletions contracts/whitelist-updatable-flatrate/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -19,7 +19,7 @@ impl WhitelistUpdatableFlatrateContract {
}

pub fn call<T: Into<ExecuteMsg>>(&self, msg: T) -> StdResult<CosmosMsg> {
let msg = to_binary(&msg.into())?;
let msg = to_json_binary(&msg.into())?;
Ok(WasmMsg::Execute {
contract_addr: self.addr().into(),
msg,
Expand All @@ -37,15 +37,15 @@ impl WhitelistUpdatableFlatrateContract {
pub fn includes(&self, querier: &QuerierWrapper, address: String) -> StdResult<bool> {
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)
}

pub fn config(&self, querier: &QuerierWrapper) -> StdResult<Config> {
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)
Expand All @@ -54,7 +54,7 @@ impl WhitelistUpdatableFlatrateContract {
pub fn mint_discount_amount(&self, querier: &QuerierWrapper) -> StdResult<Option<u64>> {
querier.query(&QueryRequest::Wasm(WasmQuery::Smart {
contract_addr: self.addr().into(),
msg: to_binary(&QueryMsg::MintDiscountAmount {})?,
msg: to_json_binary(&QueryMsg::MintDiscountAmount {})?,
}))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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()],
};
Expand Down

0 comments on commit b55a074

Please sign in to comment.