-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcontract.rs
360 lines (323 loc) · 13.6 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
use astroport::asset::{addr_opt_validate, validate_native_denom};
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
attr, coins, ensure, ensure_eq, to_json_binary, wasm_execute, BankMsg, Binary, CosmosMsg, Deps,
DepsMut, Empty, Env, MessageInfo, Order, Response, StdError, StdResult, Uint128,
};
use cw2::set_contract_version;
use cw20::{BalanceResponse, Logo, LogoInfo, MarketingInfoResponse, TokenInfoResponse};
use cw20_base::contract::{execute_update_marketing, query_marketing_info};
use cw20_base::state::{MinterData, TokenInfo, LOGO, MARKETING_INFO, TOKEN_INFO};
use cw_storage_plus::Bound;
use cw_utils::must_pay;
use astroport_governance::emissions_controller;
use astroport_governance::emissions_controller::consts::MAX_PAGE_LIMIT;
use astroport_governance::voting_escrow::{
Config, ExecuteMsg, InstantiateMsg, LockInfoResponse, QueryMsg,
};
use crate::error::ContractError;
use crate::state::{get_total_vp, Lock, CONFIG, LOCKED, PRIVILEGED};
/// Contract name that is used for migration.
pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
/// Contract version that is used for migration.
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
/// Creates a new contract with the specified parameters in [`InstantiateMsg`].
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn instantiate(
deps: DepsMut,
env: Env,
_info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, ContractError> {
set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
validate_native_denom(&msg.deposit_denom)?;
let config = Config {
deposit_denom: msg.deposit_denom,
emissions_controller: deps.api.addr_validate(&msg.emissions_controller)?,
};
CONFIG.save(deps.storage, &config)?;
let logo = match &msg.marketing.logo {
Logo::Url(url) => {
LOGO.save(deps.storage, &msg.marketing.logo)?;
Some(LogoInfo::Url(url.clone()))
}
_ => {
return Err(StdError::generic_err("Logo url must be set").into());
}
};
let data = MarketingInfoResponse {
project: msg.marketing.project,
description: msg.marketing.description,
marketing: addr_opt_validate(deps.api, &msg.marketing.marketing)?,
logo,
};
MARKETING_INFO.save(deps.storage, &data)?;
// Store token info
let data = TokenInfo {
name: "Vote Escrowed xASTRO".to_string(),
symbol: "vxASTRO".to_string(),
decimals: 6,
total_supply: Uint128::zero(),
mint: Some(MinterData {
minter: env.contract.address,
cap: None,
}),
};
TOKEN_INFO.save(deps.storage, &data)?;
PRIVILEGED.save(deps.storage, &vec![])?;
Ok(Response::default())
}
/// Exposes all the execute functions available in the contract.
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response, ContractError> {
match msg {
ExecuteMsg::Lock { receiver } => {
let config = CONFIG.load(deps.storage)?;
let deposit = must_pay(&info, &config.deposit_denom)?;
let receiver = addr_opt_validate(deps.api, &receiver)?.unwrap_or(info.sender);
let block_ts = env.block.time.seconds();
let mut position = Lock::load(deps.storage, block_ts, &receiver)?;
position.lock(deps.storage, deposit)?;
// Update user votes in emissions controller
let update_votes_msg = wasm_execute(
&config.emissions_controller,
&emissions_controller::msg::ExecuteMsg::<Empty>::UpdateUserVotes {
user: receiver.to_string(),
is_unlock: false,
},
vec![],
)?;
Ok(Response::default()
.add_message(update_votes_msg)
.add_attributes([
attr("action", "lock"),
attr("receiver", receiver),
attr("deposit_amount", deposit),
attr("new_lock_amount", position.amount),
]))
}
ExecuteMsg::Unlock {} => {
let mut position = Lock::load(deps.storage, env.block.time.seconds(), &info.sender)?;
let unlock_time = position.unlock(deps.storage)?;
// Update user votes in emissions controller
let config = CONFIG.load(deps.storage)?;
let update_votes_msg = wasm_execute(
config.emissions_controller,
&emissions_controller::msg::ExecuteMsg::<Empty>::UpdateUserVotes {
user: info.sender.to_string(),
is_unlock: true,
},
vec![],
)?;
Ok(Response::default()
.add_message(update_votes_msg)
.add_attributes([
attr("action", "unlock"),
attr("receiver", info.sender),
attr("unlocked_amount", position.amount),
attr("unlock_time", unlock_time.to_string()),
]))
}
ExecuteMsg::InstantUnlock { amount } => {
let privileged = PRIVILEGED.load(deps.storage)?;
ensure!(
privileged.contains(&info.sender),
ContractError::Unauthorized {}
);
let mut position = Lock::load(deps.storage, env.block.time.seconds(), &info.sender)?;
position.instant_unlock(deps.storage, amount)?;
// Update user votes in emissions controller
let config = CONFIG.load(deps.storage)?;
let update_votes_msg: CosmosMsg = wasm_execute(
config.emissions_controller,
&emissions_controller::msg::ExecuteMsg::<Empty>::UpdateUserVotes {
user: info.sender.to_string(),
// In this context, we don't need confirmation from emissions controller
// as xASTRO is instantly withdrawn
is_unlock: false,
},
vec![],
)?
.into();
let send_msg: CosmosMsg = BankMsg::Send {
to_address: info.sender.to_string(),
amount: coins(amount.u128(), config.deposit_denom),
}
.into();
Ok(Response::default()
.add_messages([update_votes_msg, send_msg])
.add_attributes([
attr("action", "instant_unlock"),
attr("receiver", info.sender),
attr("unlocked_amount", amount),
]))
}
ExecuteMsg::Relock {} => {
let mut position = Lock::load(deps.storage, env.block.time.seconds(), &info.sender)?;
position.relock(deps.storage)?;
// Update user votes in emissions controller
let config = CONFIG.load(deps.storage)?;
let update_votes_msg = wasm_execute(
config.emissions_controller,
&emissions_controller::msg::ExecuteMsg::<Empty>::UpdateUserVotes {
user: info.sender.to_string(),
is_unlock: false,
},
vec![],
)?;
Ok(Response::default()
.add_message(update_votes_msg)
.add_attributes([attr("action", "relock"), attr("receiver", info.sender)]))
}
ExecuteMsg::ForceRelock { user } => {
let config = CONFIG.load(deps.storage)?;
ensure!(
info.sender == config.emissions_controller,
ContractError::Unauthorized {}
);
let user = deps.api.addr_validate(&user)?;
let mut position = Lock::load(deps.storage, env.block.time.seconds(), &user)?;
position.relock(deps.storage)?;
Ok(Response::default()
.add_attributes([attr("action", "force_relock"), attr("receiver", user)]))
}
ExecuteMsg::ConfirmUnlock { user } => {
let config = CONFIG.load(deps.storage)?;
ensure!(
info.sender == config.emissions_controller,
ContractError::Unauthorized {}
);
let user = deps.api.addr_validate(&user)?;
let mut position = Lock::load(deps.storage, env.block.time.seconds(), &user)?;
position.confirm_unlock(deps.storage)?;
Ok(Response::default()
.add_attributes([attr("action", "confirm_unlock"), attr("receiver", user)]))
}
ExecuteMsg::Withdraw {} => {
let mut position = Lock::load(deps.storage, env.block.time.seconds(), &info.sender)?;
let config = CONFIG.load(deps.storage)?;
let amount = position.withdraw(deps.storage)?;
let send_msg = BankMsg::Send {
to_address: info.sender.to_string(),
amount: coins(amount.u128(), config.deposit_denom),
};
Ok(Response::new().add_message(send_msg).add_attributes([
attr("action", "withdraw"),
attr("receiver", info.sender),
attr("withdrawn_amount", amount),
]))
}
ExecuteMsg::SetPrivilegedList { list } => {
let config = CONFIG.load(deps.storage)?;
// Query result deserialization into hub::Config
// ensures we can call this endpoint only on the Hub
let emissions_owner = deps
.querier
.query_wasm_smart::<emissions_controller::hub::Config>(
&config.emissions_controller,
&emissions_controller::hub::QueryMsg::Config {},
)?
.owner;
ensure_eq!(info.sender, emissions_owner, ContractError::Unauthorized {});
let privileged = list
.iter()
.map(|addr| deps.api.addr_validate(addr))
.collect::<StdResult<Vec<_>>>()?;
PRIVILEGED.save(deps.storage, &privileged)?;
Ok(Response::default().add_attribute("action", "set_privileged_list"))
}
ExecuteMsg::UpdateMarketing {
project,
description,
marketing,
} => execute_update_marketing(deps, env, info, project, description, marketing)
.map_err(Into::into),
}
}
/// Expose available contract queries.
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::TotalVotingPower { timestamp } => to_json_binary(&get_total_vp(
deps.storage,
env.block.time.seconds(),
timestamp,
)?),
QueryMsg::UserVotingPower { user, timestamp } => {
to_json_binary(&query_user_voting_power(deps, env, user, timestamp)?)
}
QueryMsg::LockInfo { user, timestamp } => {
let user = deps.api.addr_validate(&user)?;
let lock_info_resp: LockInfoResponse =
Lock::load_at_ts(deps.storage, env.block.time.seconds(), &user, timestamp)?.into();
to_json_binary(&lock_info_resp)
}
QueryMsg::Config {} => to_json_binary(&CONFIG.load(deps.storage)?),
QueryMsg::Balance { address } => {
let user_vp = query_user_voting_power(deps, env, address, None)?;
to_json_binary(&BalanceResponse { balance: user_vp })
}
QueryMsg::TokenInfo {} => to_json_binary(&query_token_info(deps, env)?),
QueryMsg::MarketingInfo {} => to_json_binary(&query_marketing_info(deps)?),
QueryMsg::PrivilegedList {} => to_json_binary(&PRIVILEGED.load(deps.storage)?),
QueryMsg::UsersLockInfo {
limit,
start_after,
timestamp,
} => {
let limit = limit.unwrap_or(MAX_PAGE_LIMIT) as usize;
let start_after = addr_opt_validate(deps.api, &start_after)?;
let user_infos = LOCKED
.keys(
deps.storage,
start_after.as_ref().map(Bound::exclusive),
None,
Order::Ascending,
)
.take(limit)
.map(|user| {
user.and_then(|user| {
let lock_info_resp: LockInfoResponse = Lock::load_at_ts(
deps.storage,
env.block.time.seconds(),
&user,
timestamp,
)?
.into();
Ok((user, lock_info_resp))
})
})
.collect::<StdResult<Vec<_>>>()?;
Ok(to_json_binary(&user_infos)?)
}
}
}
/// Fetch the vxASTRO token information, such as the token name, symbol, decimals and total supply (total voting power).
pub fn query_token_info(deps: Deps, env: Env) -> StdResult<TokenInfoResponse> {
let token_info = TOKEN_INFO.load(deps.storage)?;
let res = TokenInfoResponse {
name: token_info.name,
symbol: token_info.symbol,
decimals: token_info.decimals,
total_supply: get_total_vp(deps.storage, env.block.time.seconds(), None)?,
};
Ok(res)
}
pub fn query_user_voting_power(
deps: Deps,
env: Env,
address: String,
timestamp: Option<u64>,
) -> StdResult<Uint128> {
let address = deps.api.addr_validate(&address)?;
let voting_power =
Lock::load_at_ts(deps.storage, env.block.time.seconds(), &address, timestamp)?
.get_voting_power();
Ok(voting_power)
}