/
lib.rs
52 lines (45 loc) · 1.88 KB
/
lib.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
#![no_std]
use gstd::prelude::*;
use io::Contract;
// contract state
static mut STATE: Option<Contract> = None;
// what this line says is "here is a C function written in Rust"
#[no_mangle]
extern "C" fn init() {
unsafe { STATE = Some(Contract::default()) }
}
#[no_mangle]
extern "C" fn handle() {
// use io::{Action, Event};
type Handle = <Contract as gmeta::Metadata>::Handle;
type Action = <Handle as gmeta::Types>::Input;
type Event = <Handle as gmeta::Types>::Output;
let state: &mut Contract = unsafe { STATE.as_mut().expect("failed to get state as mut") };
let action: Action = gstd::msg::load().expect("failed to load action");
let event: Event = match action {
Action::AddUrl { code, url } => {
state.add_url(code.clone(), url.clone());
Event::Added { code, url }
}
};
gstd::msg::reply(event, 0).expect("failed to reply");
}
#[no_mangle]
extern "C" fn state() {
// use io::{Query, Reply};
type State = <Contract as gmeta::Metadata>::State;
type Query = <State as gmeta::Types>::Input;
type Reply = <State as gmeta::Types>::Output;
let state: &Contract = unsafe { STATE.as_ref().expect("failed to get contract state") };
let query: Query = gstd::msg::load().expect("failed to load query");
let reply: Reply = match query {
Query::All => Reply::All(state.clone()),
Query::Code(code) => Reply::Url(state.get_url(code)),
Query::Whoami => Reply::Whoami(gstd::msg::source()), // all zero addr
Query::BlockNumber => Reply::BlockNumber(gstd::exec::block_height()),
Query::BlockTimestamp => Reply::BlockTimestamp(gstd::exec::block_timestamp()),
Query::ProgramId => Reply::ProgramId(gstd::exec::program_id()),
Query::MessageId => Reply::MessageId(gstd::msg::id()), // all zero id
};
gstd::msg::reply(reply, 0).expect("Failed to share state");
}