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
use fadroma::prelude::*;

/// Initial configuration of the contract.
/// In this example, you can ask the instantiation to fail.
#[message] pub struct InitMsg { fail: bool }

/// Transactions that this contract supports.
#[message] pub enum HandleMsg {
    /// Return the input message as the .data property of the response
    Echo,
    /// Return an error
    Fail
}

/// Queries that this contract supports.
#[message] pub enum QueryMsg {
    /// Return the input message
    Echo,
    /// Return an error
    Fail
}

pub fn init<S: Storage, A: Api, Q: Querier>(
    _deps: &mut Extern<S, A, Q>, _env: Env, msg: InitMsg,
) -> StdResult<InitResponse> {
    if !msg.fail {
        InitResponse::default().log("Echo", &to_binary(&msg)?.to_base64())
    } else {
        Err(StdError::generic_err("caller requested the init to fail"))
    }
}

pub fn handle<S: Storage, A: Api, Q: Querier>(
    _deps: &mut Extern<S, A, Q>, _env: Env, msg: HandleMsg,
) -> StdResult<HandleResponse> {
    match msg {
        HandleMsg::Echo => HandleResponse::default().data(&msg),
        HandleMsg::Fail => Err(StdError::generic_err("this transaction always fails"))
    }
}

pub fn query<S: Storage, A: Api, Q: Querier>(
    _deps: &Extern<S, A, Q>, msg: QueryMsg,
) -> StdResult<Binary> {
    match msg {
        QueryMsg::Echo => to_binary(&msg),
        QueryMsg::Fail => Err(StdError::generic_err("this query always fails"))
    }
}

fadroma::entrypoint!(fadroma, init, handle, query);