/
contract.rs
122 lines (104 loc) · 3.58 KB
/
contract.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use crate::state::{read_config, store_config, Config};
use cosmwasm_std::{
to_binary, Binary, CosmosMsg, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult,
Uint128, WasmMsg,
};
use anchor_token::community::{ConfigResponse, ExecuteMsg, InstantiateMsg, MigrateMsg, QueryMsg};
use cw20::Cw20ExecuteMsg;
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> StdResult<Response> {
store_config(
deps.storage,
&Config {
gov_contract: deps.api.addr_canonicalize(&msg.gov_contract)?,
anchor_token: deps.api.addr_canonicalize(&msg.anchor_token)?,
spend_limit: msg.spend_limit,
},
)?;
Ok(Response::default())
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
_env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response> {
match msg {
ExecuteMsg::UpdateConfig { spend_limit } => update_config(deps, info, spend_limit),
ExecuteMsg::Spend { recipient, amount } => spend(deps, info, recipient, amount),
}
}
pub fn update_config(
deps: DepsMut,
info: MessageInfo,
spend_limit: Option<Uint128>,
) -> StdResult<Response> {
let mut config: Config = read_config(deps.storage)?;
if config.gov_contract != deps.api.addr_canonicalize(info.sender.as_str())? {
return Err(StdError::generic_err("unauthorized"));
}
if let Some(spend_limit) = spend_limit {
config.spend_limit = spend_limit;
}
store_config(deps.storage, &config)?;
Ok(Response::new().add_attributes(vec![("action", "update_config")]))
}
/// Spend
/// Owner can execute spend operation to send
/// `amount` of ANC token to `recipient` for community purpose
pub fn spend(
deps: DepsMut,
info: MessageInfo,
recipient: String,
amount: Uint128,
) -> StdResult<Response> {
let config: Config = read_config(deps.storage)?;
if config.gov_contract != deps.api.addr_canonicalize(info.sender.as_str())? {
return Err(StdError::generic_err("unauthorized"));
}
if config.spend_limit < amount {
return Err(StdError::generic_err("Cannot spend more than spend_limit"));
}
let anchor_token = deps.api.addr_humanize(&config.anchor_token)?.to_string();
Ok(Response::new()
.add_messages(vec![CosmosMsg::Wasm(WasmMsg::Execute {
contract_addr: anchor_token,
funds: vec![],
msg: to_binary(&Cw20ExecuteMsg::Transfer {
recipient: recipient.clone(),
amount,
})?,
})])
.add_attributes(vec![
("action", "spend"),
("recipient", recipient.as_str()),
("amount", &amount.to_string()),
]))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Config {} => to_binary(&query_config(deps)?),
}
}
pub fn query_config(deps: Deps) -> StdResult<ConfigResponse> {
let state = read_config(deps.storage)?;
let resp = ConfigResponse {
gov_contract: deps.api.addr_humanize(&state.gov_contract)?.to_string(),
anchor_token: deps.api.addr_humanize(&state.anchor_token)?.to_string(),
spend_limit: state.spend_limit,
};
Ok(resp)
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(_deps: DepsMut, _env: Env, _msg: MigrateMsg) -> StdResult<Response> {
Ok(Response::default())
}