whitelist_immutable/
contract.rs1use crate::state::{Config, CONFIG, TOTAL_ADDRESS_COUNT, WHITELIST};
2#[cfg(not(feature = "library"))]
3use cosmwasm_std::entry_point;
4use cosmwasm_std::{to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, StdResult};
5use cw2::set_contract_version;
6
7use crate::error::ContractError;
8use crate::msg::{ConfigResponse, ExecuteMsg, InstantiateMsg, QueryMsg};
9use cw_utils::nonpayable;
10use sg_std::Response;
11
12const CONTRACT_NAME: &str = "crates.io:whitelist-immutable";
14const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
15
16#[cfg_attr(not(feature = "library"), entry_point)]
17pub fn instantiate(
18 mut deps: DepsMut,
19 _env: Env,
20 info: MessageInfo,
21 mut msg: InstantiateMsg,
22) -> Result<Response, ContractError> {
23 nonpayable(&info)?;
24 set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
25 let config = Config {
26 admin: info.sender,
27 per_address_limit: msg.per_address_limit,
28 mint_discount_bps: msg.mint_discount_bps,
29 };
30
31 msg.addresses.sort_unstable();
32 msg.addresses.dedup();
33 let count = update_whitelist(&mut deps, msg)?;
34 validate_nonempty_whitelist(count)?;
35 TOTAL_ADDRESS_COUNT.save(deps.storage, &count)?;
36 CONFIG.save(deps.storage, &config)?;
37
38 Ok(Response::default()
39 .add_attribute("action", "instantiate")
40 .add_attribute("contract_name", CONTRACT_NAME)
41 .add_attribute("contract_version", CONTRACT_VERSION))
42}
43
44fn update_whitelist(deps: &mut DepsMut, msg: InstantiateMsg) -> Result<u64, ContractError> {
45 let mut count = 0u64;
46 for address in msg.addresses.into_iter() {
47 WHITELIST.save(deps.storage, &address, &true)?;
48 count += 1;
49 }
50 Ok(count)
51}
52
53fn validate_nonempty_whitelist(count: u64) -> Result<bool, ContractError> {
54 if count < 1 {
55 return Err(ContractError::EmptyWhitelist {});
56 }
57 Ok(true)
58}
59
60#[cfg_attr(not(feature = "library"), entry_point)]
61pub fn execute(
62 _deps: DepsMut,
63 _env: Env,
64 _info: MessageInfo,
65 _msg: ExecuteMsg,
66) -> Result<Response, ContractError> {
67 Ok(Response::new())
68}
69
70#[cfg_attr(not(feature = "library"), entry_point)]
71pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
72 match msg {
73 QueryMsg::Config {} => to_json_binary(&query_config(deps)?),
74 QueryMsg::IncludesAddress { address } => {
75 to_json_binary(&query_includes_address(deps, address)?)
76 }
77 QueryMsg::Admin {} => to_json_binary(&query_admin(deps)?),
78 QueryMsg::AddressCount {} => to_json_binary(&query_address_count(deps)?),
79 QueryMsg::PerAddressLimit {} => to_json_binary(&query_per_address_limit(deps)?),
80 }
81}
82
83pub fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
84 let config = CONFIG.load(deps.storage)?;
85 Ok(ConfigResponse { config })
86}
87
88pub fn query_includes_address(deps: Deps, address: String) -> StdResult<bool> {
89 Ok(WHITELIST.has(deps.storage, &address))
90}
91
92pub fn query_admin(deps: Deps) -> StdResult<String> {
93 let config = CONFIG.load(deps.storage)?;
94 Ok(config.admin.to_string())
95}
96
97pub fn query_address_count(deps: Deps) -> StdResult<u64> {
98 TOTAL_ADDRESS_COUNT.load(deps.storage)
99}
100
101pub fn query_per_address_limit(deps: Deps) -> StdResult<u32> {
102 let config = CONFIG.load(deps.storage)?;
103 Ok(config.per_address_limit)
104}