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
use candid::Principal;
use std::convert::TryFrom;
pub mod call;
pub mod management_canister;
pub mod stable;
pub fn print<S: std::convert::AsRef<str>>(s: S) {
let s = s.as_ref();
unsafe {
ic0::debug_print(s.as_ptr() as i32, s.len() as i32);
}
}
pub fn trap(message: &str) -> ! {
unsafe {
ic0::trap(message.as_ptr() as i32, message.len() as i32);
}
unreachable!()
}
pub fn time() -> u64 {
unsafe { ic0::time() as u64 }
}
pub fn caller() -> Principal {
let len: u32 = unsafe { ic0::msg_caller_size() as u32 };
let mut bytes = vec![0u8; len as usize];
unsafe {
ic0::msg_caller_copy(bytes.as_mut_ptr() as i32, 0, len as i32);
}
Principal::try_from(&bytes).unwrap()
}
pub fn id() -> Principal {
let len: u32 = unsafe { ic0::canister_self_size() as u32 };
let mut bytes = vec![0u8; len as usize];
unsafe {
ic0::canister_self_copy(bytes.as_mut_ptr() as i32, 0, len as i32);
}
Principal::try_from(&bytes).unwrap()
}
pub fn canister_balance() -> u64 {
unsafe { ic0::canister_cycle_balance() as u64 }
}
pub fn canister_balance128() -> u128 {
let mut recv = 0u128;
unsafe { ic0::canister_cycle_balance128(&mut recv as *mut u128 as i32) }
recv
}
pub fn set_certified_data(data: &[u8]) {
unsafe { ic0::certified_data_set(data.as_ptr() as i32, data.len() as i32) }
}
pub fn data_certificate() -> Option<Vec<u8>> {
if unsafe { ic0::data_certificate_present() } == 0 {
return None;
}
let n = unsafe { ic0::data_certificate_size() };
let mut buf = vec![0u8; n as usize];
unsafe {
ic0::data_certificate_copy(buf.as_mut_ptr() as i32, 0i32, n);
}
Some(buf)
}
#[inline]
pub fn instruction_counter() -> u64 {
performance_counter(0)
}
#[inline]
pub fn performance_counter(counter_type: u32) -> u64 {
unsafe { ic0::performance_counter(counter_type as i32) as u64 }
}