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
#![feature(
bind_by_move_pattern_guards,
linkage,
non_exhaustive,
proc_macro_hygiene,
trait_alias
)]
#![cfg_attr(target_os = "wasi", feature(wasi_ext))]
#[macro_use]
pub extern crate serde;
extern crate oasis_macros;
pub mod backend;
pub mod exe;
pub mod reexports {
pub use serde;
pub use serde_cbor;
pub use tiny_keccak;
}
pub use oasis_macros::{default, Event, Service};
pub use oasis_types::{Address, Balance};
pub use crate::exe::*;
#[macro_export]
macro_rules! service {
($svc:path) => {};
}
#[macro_export]
macro_rules! invoke {
($address:expr, $method:expr, $ctx:expr, $( $arg:expr ),* $(,)?) => {{
use crate::serde::ser::{Serializer as _, SerializeStruct as _};
let mut serializer = $crate::reexports::serde_cbor::Serializer::new(Vec::new());
serializer.serialize_struct("Message", 2).and_then(|mut message| {
message.serialize_field("method", $method)?;
message.serialize_field("payload", &( $( &$arg ),* ))?;
message.end()
})
.map_err(|_| $crate::backend::Error::InvalidInput)
.and_then(|_| $address.call($ctx, &serializer.into_inner()))
}}
}
pub trait AddressExt {
fn call(&self, ctx: &Context, payload: &[u8]) -> Result<Vec<u8>, crate::backend::Error>;
fn transfer<B: Into<Balance>>(&self, value: B) -> Result<(), crate::backend::Error>;
fn balance(&self) -> Balance;
fn code(&self) -> Vec<u8>;
}
impl AddressExt for Address {
fn call(&self, ctx: &Context, payload: &[u8]) -> Result<Vec<u8>, crate::backend::Error> {
crate::backend::transact(self, ctx.value(), payload)
}
fn transfer<B: Into<Balance>>(&self, value: B) -> Result<(), crate::backend::Error> {
crate::backend::transact(self, value.into(), &[]).map(|_| ())
}
fn balance(&self) -> Balance {
crate::backend::balance(self).unwrap()
}
fn code(&self) -> Vec<u8> {
crate::backend::code(self).unwrap()
}
}
#[cfg(test)]
mod tests {
use super::*;
extern crate oasis_test;
#[test]
fn test_invoke() {
invoke!(
Address::default(),
"method",
&Context::default(),
"an arg",
&["some", "more", "args"],
42,
)
.unwrap();
}
}