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
use oasis_types::{Address, Balance};
pub trait Service {
fn coalesce() -> Self;
fn sunder(c: Self);
fn address(&self) -> Address {
crate::backend::address()
}
}
pub trait Event: crate::abi::Serialize {
fn emit(&self);
}
const TOPIC_LEN: usize = 32;
#[doc(hidden)]
pub fn encode_event_topic<T: crate::abi::Serialize>(topic: &T) -> [u8; TOPIC_LEN] {
let repr = crate::abi_encode!(topic).unwrap();
if repr.len() <= TOPIC_LEN {
let mut topic = [0u8; TOPIC_LEN];
topic[..repr.len()].copy_from_slice(&repr);
topic
} else {
tiny_keccak::keccak256(&repr)
}
}
#[derive(Default, Copy, Clone, Debug)]
pub struct Context {
#[doc(hidden)]
pub sender: Option<Address>,
#[doc(hidden)]
pub value: Option<Balance>,
#[doc(hidden)]
pub gas: Option<u64>,
}
impl Context {
pub fn with_gas(mut self, gas: u64) -> Self {
self.gas = Some(gas);
self
}
pub fn sender(&self) -> Address {
self.sender.unwrap_or_else(crate::backend::sender)
}
pub fn aad(&self) -> Vec<u8> {
crate::backend::aad()
}
pub fn value(&self) -> Balance {
self.value.unwrap_or_else(crate::backend::value)
}
}
impl Context {
#[cfg(any(test, not(target_os = "wasi")))]
pub fn with_sender(mut self, sender: Address) -> Self {
self.sender = Some(sender);
self
}
pub fn with_value<B: Into<Balance>>(mut self, value: B) -> Self {
self.value = Some(value.into());
self
}
}