|
| 1 | +#[cfg(not(feature = "library"))] |
| 2 | +use cosmwasm_std::{ |
| 3 | + entry_point, from_binary, to_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, |
| 4 | + QueryRequest, Response, StdResult, Uint128, WasmQuery, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::error::ContractError; |
| 8 | +use crate::msg::{Cw20HookMsg, ExecuteMsg, InstantiateMsg, QueryMsg}; |
| 9 | +use crate::state::{Config, Proposal, CONFIG, PROPOSAL}; |
| 10 | +use cw20::{BalanceResponse, Cw20QueryMsg, Cw20ReceiveMsg, TokenInfoResponse}; |
| 11 | + |
| 12 | +pub const DENOM: &str = "uawesome"; |
| 13 | + |
| 14 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 15 | +pub fn instantiate( |
| 16 | + deps: DepsMut, |
| 17 | + _env: Env, |
| 18 | + _info: MessageInfo, |
| 19 | + msg: InstantiateMsg, |
| 20 | +) -> Result<Response, ContractError> { |
| 21 | + let config = Config { |
| 22 | + voting_window: msg.window, |
| 23 | + voting_token: deps.api.addr_validate(&msg.token)?, |
| 24 | + owner: deps.api.addr_validate(&msg.owner)?, |
| 25 | + }; |
| 26 | + CONFIG.save(deps.storage, &config)?; |
| 27 | + |
| 28 | + Ok(Response::new() |
| 29 | + .add_attribute("action", "instantiate") |
| 30 | + .add_attribute("voting_window", msg.window.to_string()) |
| 31 | + .add_attribute("voting_token", msg.token) |
| 32 | + .add_attribute("owner", msg.owner)) |
| 33 | +} |
| 34 | + |
| 35 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 36 | +pub fn execute( |
| 37 | + deps: DepsMut, |
| 38 | + env: Env, |
| 39 | + info: MessageInfo, |
| 40 | + msg: ExecuteMsg, |
| 41 | +) -> Result<Response, ContractError> { |
| 42 | + match msg { |
| 43 | + ExecuteMsg::Propose {} => propose(deps, env, info), |
| 44 | + ExecuteMsg::ResolveProposal {} => resolve_proposal(deps, env, info), |
| 45 | + ExecuteMsg::OwnerAction { action } => owner_action(deps, info, action), |
| 46 | + ExecuteMsg::Receive(msg) => receive_cw20(deps, env, info, msg), |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// Entry point when receiving CW20 tokens |
| 51 | +pub fn receive_cw20( |
| 52 | + deps: DepsMut, |
| 53 | + env: Env, |
| 54 | + info: MessageInfo, |
| 55 | + cw20_msg: Cw20ReceiveMsg, |
| 56 | +) -> Result<Response, ContractError> { |
| 57 | + let config = CONFIG.load(deps.storage)?; |
| 58 | + let current_proposal = PROPOSAL.load(deps.storage)?; |
| 59 | + |
| 60 | + match from_binary(&cw20_msg.msg) { |
| 61 | + Ok(Cw20HookMsg::CastVote {}) => { |
| 62 | + if config.voting_token != info.sender { |
| 63 | + return Err(ContractError::Unauthorized {}); |
| 64 | + } |
| 65 | + |
| 66 | + if current_proposal |
| 67 | + .timestamp |
| 68 | + .plus_seconds(config.voting_window) |
| 69 | + < env.block.time |
| 70 | + { |
| 71 | + return Err(ContractError::VotingWindowClosed {}); |
| 72 | + } |
| 73 | + |
| 74 | + Ok(Response::default() |
| 75 | + .add_attribute("action", "Vote casting") |
| 76 | + .add_attribute("voter", cw20_msg.sender) |
| 77 | + .add_attribute("power", cw20_msg.amount)) |
| 78 | + } |
| 79 | + _ => Ok(Response::default()), |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +/// Propose a new proposal |
| 84 | +pub fn propose(deps: DepsMut, env: Env, info: MessageInfo) -> Result<Response, ContractError> { |
| 85 | + let current_proposal = PROPOSAL.load(deps.storage); |
| 86 | + |
| 87 | + if current_proposal.is_ok() { |
| 88 | + return Err(ContractError::ProposalAlreadyExists {}); |
| 89 | + } |
| 90 | + |
| 91 | + PROPOSAL.save( |
| 92 | + deps.storage, |
| 93 | + &Proposal { |
| 94 | + proposer: info.sender.clone(), |
| 95 | + timestamp: env.block.time, |
| 96 | + }, |
| 97 | + )?; |
| 98 | + |
| 99 | + Ok(Response::new() |
| 100 | + .add_attribute("action", "New proposal") |
| 101 | + .add_attribute("proposer", info.sender)) |
| 102 | +} |
| 103 | + |
| 104 | +/// Resolve an existing proposal |
| 105 | +pub fn resolve_proposal( |
| 106 | + deps: DepsMut, |
| 107 | + env: Env, |
| 108 | + _info: MessageInfo, |
| 109 | +) -> Result<Response, ContractError> { |
| 110 | + let config = CONFIG.load(deps.storage)?; |
| 111 | + let current_proposal = PROPOSAL.load(deps.storage)?; |
| 112 | + |
| 113 | + if current_proposal |
| 114 | + .timestamp |
| 115 | + .plus_seconds(config.voting_window) |
| 116 | + < env.block.time |
| 117 | + { |
| 118 | + return Err(ContractError::ProposalNotReady {}); |
| 119 | + } |
| 120 | + |
| 121 | + let vtoken_info: TokenInfoResponse = |
| 122 | + deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { |
| 123 | + contract_addr: config.voting_token.to_string(), |
| 124 | + msg: to_binary(&Cw20QueryMsg::TokenInfo {})?, |
| 125 | + }))?; |
| 126 | + |
| 127 | + let balance: BalanceResponse = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { |
| 128 | + contract_addr: config.voting_token.to_string(), |
| 129 | + msg: to_binary(&Cw20QueryMsg::Balance { |
| 130 | + address: env.contract.address.to_string(), |
| 131 | + })?, |
| 132 | + }))?; |
| 133 | + |
| 134 | + let mut response = Response::new().add_attribute("action", "resolve_proposal"); |
| 135 | + |
| 136 | + if balance.balance >= (vtoken_info.total_supply / Uint128::from(3u32)) { |
| 137 | + CONFIG.update(deps.storage, |mut config| -> StdResult<_> { |
| 138 | + config.owner = current_proposal.proposer; |
| 139 | + Ok(config) |
| 140 | + })?; |
| 141 | + response = response.add_attribute("result", "Passed"); |
| 142 | + } else { |
| 143 | + PROPOSAL.remove(deps.storage); |
| 144 | + response = response.add_attribute("result", "Failed"); |
| 145 | + } |
| 146 | + |
| 147 | + Ok(response) |
| 148 | +} |
| 149 | + |
| 150 | +/// Entry point for owner to execute arbitrary Cosmos messages |
| 151 | +pub fn owner_action( |
| 152 | + deps: DepsMut, |
| 153 | + info: MessageInfo, |
| 154 | + msg: CosmosMsg, |
| 155 | +) -> Result<Response, ContractError> { |
| 156 | + let config = CONFIG.load(deps.storage)?; |
| 157 | + if config.owner != info.sender { |
| 158 | + return Err(ContractError::Unauthorized {}); |
| 159 | + } |
| 160 | + |
| 161 | + Ok(Response::new() |
| 162 | + .add_attribute("action", "owner_action") |
| 163 | + .add_message(msg)) |
| 164 | +} |
| 165 | + |
| 166 | +#[cfg_attr(not(feature = "library"), entry_point)] |
| 167 | +pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> { |
| 168 | + match msg { |
| 169 | + QueryMsg::Config {} => to_binary(&query_config(deps)?), |
| 170 | + QueryMsg::Proposal {} => to_binary(&query_proposal(deps)?), |
| 171 | + QueryMsg::Balance {} => to_binary(&query_balance(deps, env)?), |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +/// Returns contract configuration |
| 176 | +pub fn query_config(deps: Deps) -> StdResult<Config> { |
| 177 | + CONFIG.load(deps.storage) |
| 178 | +} |
| 179 | + |
| 180 | +/// Returns proposal information |
| 181 | +pub fn query_proposal(deps: Deps) -> StdResult<Proposal> { |
| 182 | + PROPOSAL.load(deps.storage) |
| 183 | +} |
| 184 | + |
| 185 | +/// Returns balance of voting token in this contract |
| 186 | +pub fn query_balance(deps: Deps, env: Env) -> StdResult<Uint128> { |
| 187 | + let config = CONFIG.load(deps.storage)?; |
| 188 | + let balance: BalanceResponse = deps.querier.query(&QueryRequest::Wasm(WasmQuery::Smart { |
| 189 | + contract_addr: config.voting_token.to_string(), |
| 190 | + msg: to_binary(&Cw20QueryMsg::Balance { |
| 191 | + address: env.contract.address.to_string(), |
| 192 | + })?, |
| 193 | + }))?; |
| 194 | + |
| 195 | + Ok(balance.balance) |
| 196 | +} |
0 commit comments