Skip to content

Commit

Permalink
refactor using ensure
Browse files Browse the repository at this point in the history
  • Loading branch information
yubrew committed Aug 25, 2023
1 parent 3b37448 commit 981f1ce
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 40 deletions.
33 changes: 15 additions & 18 deletions contracts/minters/vending-minter-wl-flex/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,24 +395,22 @@ pub fn execute_set_whitelist(
},
..
} = config.clone();
if admin != info.sender {
return Err(ContractError::Unauthorized(
"Sender is not an admin".to_owned(),
));
};
ensure!(
info.sender == admin,
ContractError::Unauthorized("Sender is not an admin".to_owned(),)
);

if env.block.time >= start_time {
return Err(ContractError::AlreadyStarted {});
}
ensure!(
env.block.time < start_time,
ContractError::AlreadyStarted {}
);

if let Some(wl) = existing_whitelist {
let res: WhitelistConfigResponse = deps
.querier
.query_wasm_smart(wl, &WhitelistQueryMsg::Config {})?;

if res.is_active {
return Err(ContractError::WhitelistAlreadyStarted {});
}
ensure!(!res.is_active, ContractError::WhitelistAlreadyStarted {});
}

let new_wl = deps.api.addr_validate(whitelist)?;
Expand All @@ -426,9 +424,7 @@ pub fn execute_set_whitelist(
.querier
.query_wasm_smart(new_wl, &WhitelistQueryMsg::Config {})?;

if wl_is_active {
return Err(ContractError::WhitelistAlreadyStarted {});
}
ensure!(!wl_is_active, ContractError::WhitelistAlreadyStarted {});

// Whitelist could be free, while factory minimum is not
let ParamsResponse {
Expand All @@ -441,12 +437,13 @@ pub fn execute_set_whitelist(
.querier
.query_wasm_smart(factory, &Sg2QueryMsg::Params {})?;

if factory_min_mint_price.amount > wl_mint_price.amount {
return Err(ContractError::InsufficientWhitelistMintPrice {
ensure!(
factory_min_mint_price.amount <= wl_mint_price.amount,
ContractError::InsufficientWhitelistMintPrice {
expected: factory_min_mint_price.amount.into(),
got: wl_mint_price.amount.into(),
});
}
}
);

// Whitelist denom should match factory mint denom
ensure!(
Expand Down
42 changes: 20 additions & 22 deletions contracts/minters/vending-minter/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,24 +408,22 @@ pub fn execute_set_whitelist(
},
..
} = config.clone();
if admin != info.sender {
return Err(ContractError::Unauthorized(
"Sender is not an admin".to_owned(),
));
};
ensure!(
admin == info.sender,
ContractError::Unauthorized("Sender is not an admin".to_owned())
);

if env.block.time >= start_time {
return Err(ContractError::AlreadyStarted {});
}
ensure!(
env.block.time < start_time,
ContractError::AlreadyStarted {}
);

if let Some(wl) = existing_whitelist {
let res: WhitelistConfigResponse = deps
.querier
.query_wasm_smart(wl, &WhitelistQueryMsg::Config {})?;

if res.is_active {
return Err(ContractError::WhitelistAlreadyStarted {});
}
ensure!(!res.is_active, ContractError::WhitelistAlreadyStarted {});
}

let new_wl = deps.api.addr_validate(whitelist)?;
Expand All @@ -439,16 +437,15 @@ pub fn execute_set_whitelist(
.querier
.query_wasm_smart(new_wl, &WhitelistQueryMsg::Config {})?;

if wl_is_active {
return Err(ContractError::WhitelistAlreadyStarted {});
}
ensure!(!wl_is_active, ContractError::WhitelistAlreadyStarted {});

if wl_mint_price.denom != config.mint_price.denom {
return Err(ContractError::InvalidDenom {
ensure!(
wl_mint_price.denom == config.mint_price.denom,
ContractError::InvalidDenom {
expected: config.mint_price.denom,
got: wl_mint_price.denom,
});
}
}
);

// Whitelist could be free, while factory minimum is not
let ParamsResponse {
Expand All @@ -461,12 +458,13 @@ pub fn execute_set_whitelist(
.querier
.query_wasm_smart(factory, &Sg2QueryMsg::Params {})?;

if factory_min_mint_price.amount > wl_mint_price.amount {
return Err(ContractError::InsufficientWhitelistMintPrice {
ensure!(
factory_min_mint_price.amount <= wl_mint_price.amount,
ContractError::InsufficientWhitelistMintPrice {
expected: factory_min_mint_price.amount.into(),
got: wl_mint_price.amount.into(),
});
}
}
);

// Whitelist denom should match factory mint denom
ensure!(
Expand Down

0 comments on commit 981f1ce

Please sign in to comment.