-
Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathgithub-21-Lilly-Protocol-lily-contracts.rs
More file actions
82 lines (68 loc) · 1.98 KB
/
Copy pathgithub-21-Lilly-Protocol-lily-contracts.rs
File metadata and controls
82 lines (68 loc) · 1.98 KB
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
// contracts/lily/src/contract.rs
use cosmwasm_std::{
entry_point, to_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdResult,
};
use crate::state::{CONFIG, STATE, Config, State};
const INITIALIZE_MSG: &str = "initialize";
#[entry_point]
pub fn instantiate(
deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: Empty,
) -> StdResult<Response> {
// Initialize state with is_initialized = false
STATE.save(deps.storage, &State::Uninitialized)?;
Ok(Response::new())
}
#[entry_point]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response> {
match msg {
ExecuteMsg::Initialize { .. } => execute_initialize(deps, env, info),
_ => Err(StdError::generic_err("Unsupported execute message")),
}
}
pub fn execute_initialize(
deps: DepsMut,
_env: Env,
info: MessageInfo,
) -> StdResult<Response> {
// Only owner can initialize
let mut config = CONFIG.load(deps.storage)?;
if info.sender != config.owner {
return Err(StdError::generic_err("Unauthorized"));
}
// Set initialized state
STATE.save(deps.storage, &State::Initialized)?;
Ok(Response::new().add_attribute("action", "initialize"))
}
#[entry_point]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::IsInitialized {} => to_binary(&query_is_initialized(deps)?),
_ => Err(StdError::generic_err("Unsupported query message")),
}
}
pub fn query_is_initialized(deps: Deps) -> StdResult<bool> {
match STATE.load(deps.storage)? {
State::Initialized => Ok(true),
State::Uninitialized => Ok(false),
}
}
// contracts/lily/src/state.rs
use cosmwasm_std::{Addr, Storage};
use cw_storage_plus::Item;
pub enum State {
Uninitialized,
Initialized,
}
pub const STATE: Item<State> = Item::new("state");
pub const CONFIG: Item<Config> = Item::new("config");
pub struct Config {
pub owner: Addr,
}