|
| 1 | +#[cfg(not(feature = "library"))] |
| 2 | +use cosmwasm_std::{ |
| 3 | + coin, entry_point, to_binary, Addr, BankMsg, Binary, CosmosMsg, Deps, DepsMut, Env, |
| 4 | + MessageInfo, Response, StdResult, Uint128, |
| 5 | +}; |
| 6 | +use cw_storage_plus::Item; |
| 7 | + |
| 8 | +use crate::error::ContractError; |
| 9 | +use crate::msg::{ConfigQueryResponse, ExecuteMsg, InstantiateMsg, QueryMsg}; |
| 10 | +use crate::state::{BALANCES, OWNER, THRESHOLD}; |
| 11 | +use cw_utils::must_pay; |
| 12 | + |
| 13 | +pub const DENOM: &str = "uawesome"; |
| 14 | +pub const TOP_DEPOSITOR: Item<Addr> = Item::new("address"); |
| 15 | + |
| 16 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 17 | +pub fn instantiate( |
| 18 | + deps: DepsMut, |
| 19 | + _env: Env, |
| 20 | + _info: MessageInfo, |
| 21 | + msg: InstantiateMsg, |
| 22 | +) -> Result<Response, ContractError> { |
| 23 | + OWNER.save(deps.storage, &deps.api.addr_validate(&msg.owner)?)?; |
| 24 | + |
| 25 | + THRESHOLD.save(deps.storage, &msg.threshold)?; |
| 26 | + |
| 27 | + Ok(Response::new() |
| 28 | + .add_attribute("action", "instantiate") |
| 29 | + .add_attribute("owner", msg.owner)) |
| 30 | +} |
| 31 | + |
| 32 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 33 | +pub fn execute( |
| 34 | + deps: DepsMut, |
| 35 | + _env: Env, |
| 36 | + info: MessageInfo, |
| 37 | + msg: ExecuteMsg, |
| 38 | +) -> Result<Response, ContractError> { |
| 39 | + match msg { |
| 40 | + ExecuteMsg::Deposit {} => deposit(deps, info), |
| 41 | + ExecuteMsg::Withdraw { amount } => withdraw(deps, info, amount), |
| 42 | + ExecuteMsg::OwnerAction { msg } => owner_action(deps, info, msg), |
| 43 | + ExecuteMsg::UpdateConfig { new_threshold } => update_config(deps, info, new_threshold), |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +/// Deposit entry point for user |
| 48 | +pub fn deposit(deps: DepsMut, info: MessageInfo) -> Result<Response, ContractError> { |
| 49 | + // validate denom |
| 50 | + let amount = must_pay(&info, DENOM).unwrap(); |
| 51 | + |
| 52 | + // increase total stake |
| 53 | + let mut user_balance = BALANCES |
| 54 | + .load(deps.storage, &info.sender) |
| 55 | + .unwrap_or_default(); |
| 56 | + user_balance += amount; |
| 57 | + |
| 58 | + BALANCES.save(deps.storage, &info.sender, &user_balance)?; |
| 59 | + |
| 60 | + let current_threshold = THRESHOLD.load(deps.storage)?; |
| 61 | + |
| 62 | + if user_balance > current_threshold { |
| 63 | + THRESHOLD.save(deps.storage, &user_balance)?; |
| 64 | + TOP_DEPOSITOR.save(deps.storage, &info.sender)?; |
| 65 | + } |
| 66 | + |
| 67 | + Ok(Response::new() |
| 68 | + .add_attribute("action", "deposit") |
| 69 | + .add_attribute("user", info.sender) |
| 70 | + .add_attribute("amount", amount)) |
| 71 | +} |
| 72 | + |
| 73 | +/// Withdrawal entry point for user |
| 74 | +pub fn withdraw( |
| 75 | + deps: DepsMut, |
| 76 | + info: MessageInfo, |
| 77 | + amount: Uint128, |
| 78 | +) -> Result<Response, ContractError> { |
| 79 | + // decrease total stake |
| 80 | + let mut user_balance = BALANCES.load(deps.storage, &info.sender)?; |
| 81 | + |
| 82 | + // Cosmwasm's Uint128 checks math operations |
| 83 | + user_balance -= amount; |
| 84 | + |
| 85 | + BALANCES.save(deps.storage, &info.sender, &user_balance)?; |
| 86 | + |
| 87 | + let msg = BankMsg::Send { |
| 88 | + to_address: info.sender.to_string(), |
| 89 | + amount: vec![coin(amount.u128(), DENOM)], |
| 90 | + }; |
| 91 | + |
| 92 | + Ok(Response::new() |
| 93 | + .add_attribute("action", "withdraw") |
| 94 | + .add_attribute("user", info.sender) |
| 95 | + .add_attribute("amount", amount) |
| 96 | + .add_message(msg)) |
| 97 | +} |
| 98 | + |
| 99 | +/// Entry point for owner to update threshold |
| 100 | +pub fn update_config( |
| 101 | + deps: DepsMut, |
| 102 | + info: MessageInfo, |
| 103 | + new_threshold: Uint128, |
| 104 | +) -> Result<Response, ContractError> { |
| 105 | + let owner = OWNER.load(deps.storage)?; |
| 106 | + |
| 107 | + if owner != info.sender { |
| 108 | + return Err(ContractError::Unauthorized {}); |
| 109 | + } |
| 110 | + |
| 111 | + THRESHOLD.save(deps.storage, &new_threshold)?; |
| 112 | + |
| 113 | + Ok(Response::new() |
| 114 | + .add_attribute("action", "Update config") |
| 115 | + .add_attribute("threshold", new_threshold)) |
| 116 | +} |
| 117 | + |
| 118 | +/// Entry point for owner to execute arbitrary Cosmos messages |
| 119 | +pub fn owner_action( |
| 120 | + deps: DepsMut, |
| 121 | + info: MessageInfo, |
| 122 | + msg: CosmosMsg, |
| 123 | +) -> Result<Response, ContractError> { |
| 124 | + let owner = OWNER.load(deps.storage)?; |
| 125 | + |
| 126 | + if owner != info.sender { |
| 127 | + return Err(ContractError::Unauthorized {}); |
| 128 | + } |
| 129 | + |
| 130 | + Ok(Response::new() |
| 131 | + .add_attribute("action", "owner_action") |
| 132 | + .add_message(msg)) |
| 133 | +} |
| 134 | + |
| 135 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 136 | +pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> { |
| 137 | + match msg { |
| 138 | + QueryMsg::Config {} => to_binary(&query_config(deps)?), |
| 139 | + QueryMsg::UserBalance { address } => to_binary(&query_balance(deps, address)?), |
| 140 | + QueryMsg::Top {} => to_binary(&query_top_depositor(deps)?), |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +/// Returns balance for specified address |
| 145 | +pub fn query_balance(deps: Deps, address: String) -> StdResult<Uint128> { |
| 146 | + let address = deps.api.addr_validate(&address)?; |
| 147 | + BALANCES.load(deps.storage, &address) |
| 148 | +} |
| 149 | + |
| 150 | +/// Returns contract configuration |
| 151 | +pub fn query_config(deps: Deps) -> StdResult<ConfigQueryResponse> { |
| 152 | + let owner = OWNER.load(deps.storage)?; |
| 153 | + let threshold = THRESHOLD.load(deps.storage)?; |
| 154 | + |
| 155 | + Ok(ConfigQueryResponse { owner, threshold }) |
| 156 | +} |
| 157 | + |
| 158 | +/// Returns the top depositor |
| 159 | +pub fn query_top_depositor(deps: Deps) -> StdResult<Addr> { |
| 160 | + TOP_DEPOSITOR.load(deps.storage) |
| 161 | +} |
0 commit comments