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
use std::future::Future;
use std::pin::Pin;
use ic_cdk::api::call::CallResult;
use ic_cdk::export::candid::utils::{ArgumentDecoder, ArgumentEncoder};
use ic_cdk::export::candid::{decode_args, encode_args};
use ic_cdk::export::{candid, Principal};
pub type CallResponse<T> = Pin<Box<dyn Future<Output = CallResult<T>>>>;
pub trait Context {
fn trap(&self, message: &str) -> !;
fn print<S: std::convert::AsRef<str>>(&self, s: S);
fn id(&self) -> Principal;
fn time(&self) -> u64;
fn balance(&self) -> u64;
fn caller(&self) -> Principal;
fn msg_cycles_available(&self) -> u64;
fn msg_cycles_accept(&self, amount: u64) -> u64;
fn msg_cycles_refunded(&self) -> u64;
fn store<T: 'static + Default>(&self, data: T);
#[inline]
fn get<T: 'static + Default>(&self) -> &T {
self.get_mut()
}
fn get_mut<T: 'static + Default>(&self) -> &mut T;
fn delete<T: 'static + Default>(&self) -> bool;
fn stable_store<T>(&self, data: T) -> Result<(), candid::Error>
where
T: ArgumentEncoder;
fn stable_restore<T>(&self) -> Result<T, String>
where
T: for<'de> ArgumentDecoder<'de>;
fn call_raw<S: Into<String>>(
&'static self,
id: Principal,
method: S,
args_raw: Vec<u8>,
cycles: u64,
) -> CallResponse<Vec<u8>>;
#[inline(always)]
fn call<T: ArgumentEncoder, R: for<'a> ArgumentDecoder<'a>, S: Into<String>>(
&'static self,
id: Principal,
method: S,
args: T,
) -> CallResponse<R> {
self.call_with_payment(id, method, args, 0)
}
#[inline(always)]
fn call_with_payment<T: ArgumentEncoder, R: for<'a> ArgumentDecoder<'a>, S: Into<String>>(
&'static self,
id: Principal,
method: S,
args: T,
cycles: u64,
) -> CallResponse<R> {
let args_raw = encode_args(args).expect("Failed to encode arguments.");
let method = method.into();
Box::pin(async move {
let bytes = self.call_raw(id, method, args_raw, cycles).await?;
decode_args(&bytes).map_err(|err| panic!("{:?}", err))
})
}
fn set_certified_data(&self, data: &[u8]);
fn data_certificate(&self) -> Option<Vec<u8>>;
fn spawn<F: 'static + std::future::Future<Output = ()> + std::marker::Send>(&self, future: F);
}