-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathcontract.rs
More file actions
86 lines (77 loc) · 2.83 KB
/
contract.rs
File metadata and controls
86 lines (77 loc) · 2.83 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
83
84
85
86
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
to_json_binary, Binary, Deps, DepsMut, Env, MessageInfo, Response, StdError, StdResult,
};
use cw2::set_contract_version;
use cw_storage_plus::Item;
use injective_cosmwasm::{InjectiveMsgWrapper, InjectiveQueryWrapper};
use crate::error::ContractError;
use crate::mock_pyth_attestation::execute_trigger_pyth_update;
use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg, SudoMsg};
// version info for migration info
const CONTRACT_NAME: &str = "crates.io:injective:dummy";
const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
pub const COUNTER: Item<u32> = Item::new("counter");
pub const ACTIVE: Item<bool> = Item::new("registered");
#[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)?;
COUNTER.save(deps.storage, &0u32)?;
ACTIVE.save(deps.storage, &false)?;
Ok(Response::new()
.add_attribute("method", "instantiate")
.add_attribute("owner", info.sender))
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn execute(
deps: DepsMut<InjectiveQueryWrapper>,
env: Env,
_info: MessageInfo,
msg: ExecuteMsg,
) -> Result<Response<InjectiveMsgWrapper>, ContractError> {
match msg {
ExecuteMsg::Ping { .. } => {
let mut response = Response::new();
response.data = Some(to_json_binary("pong")?);
Ok(response)
}
ExecuteMsg::Error { .. } => Err(ContractError::Std(StdError::msg("oh no!"))),
ExecuteMsg::TriggerPythUpdate { price } => execute_trigger_pyth_update(deps, env, price),
}
}
#[entry_point]
pub fn sudo(deps: DepsMut, _env: Env, msg: SudoMsg) -> Result<Response, ContractError> {
match msg {
SudoMsg::BeginBlocker {} => {
let runs = COUNTER.load(deps.storage)? + 1;
COUNTER.save(deps.storage, &runs)?;
ACTIVE.save(deps.storage, &true)?;
Ok(Response::new())
}
SudoMsg::Deregister {} | SudoMsg::Deactivate {} => {
ACTIVE.save(deps.storage, &false)?;
Ok(Response::new())
}
}
}
#[cfg_attr(not(feature = "library"), entry_point)]
pub fn query(deps: Deps, _env: Env, msg: QueryMsg) -> StdResult<Binary> {
match msg {
QueryMsg::Ping { .. } => to_json_binary("pong"),
QueryMsg::Error { .. } => Err(StdError::msg("oh no!")),
QueryMsg::Runs {} => {
let runs_count = COUNTER.load(deps.storage)?;
to_json_binary(&format!("{runs_count}"))
}
QueryMsg::Active {} => {
let is_active = ACTIVE.load(deps.storage)?;
to_json_binary(&format!("{is_active}"))
}
}
}