1use std::future::Future;
2use std::pin::Pin;
3
4use ic_cdk::api::call::CallResult;
5use ic_cdk::export::candid::utils::{ArgumentDecoder, ArgumentEncoder};
6use ic_cdk::export::candid::{decode_args, encode_args};
7use ic_cdk::export::{candid, Principal};
8
9pub type CallResponse<T> = Pin<Box<dyn Future<Output = CallResult<T>>>>;
10
11#[derive(Debug)]
13pub struct StableMemoryError();
14
15pub trait Context {
16 fn trap(&self, message: &str) -> !;
18
19 fn print<S: AsRef<str>>(&self, s: S);
21
22 fn id(&self) -> Principal;
24
25 fn time(&self) -> u64;
27
28 fn balance(&self) -> u64;
30
31 fn caller(&self) -> Principal;
33
34 fn msg_cycles_available(&self) -> u64;
36
37 fn msg_cycles_accept(&self, amount: u64) -> u64;
39
40 fn msg_cycles_refunded(&self) -> u64;
43
44 fn stable_store<T>(&self, data: T) -> Result<(), candid::Error>
46 where
47 T: ArgumentEncoder;
48
49 fn stable_restore<T>(&self) -> Result<T, String>
52 where
53 T: for<'de> ArgumentDecoder<'de>;
54
55 fn call_raw<S: Into<String>>(
57 &'static self,
58 id: Principal,
59 method: S,
60 args_raw: Vec<u8>,
61 cycles: u64,
62 ) -> CallResponse<Vec<u8>>;
63
64 #[inline(always)]
66 fn call<T: ArgumentEncoder, R: for<'a> ArgumentDecoder<'a>, S: Into<String>>(
67 &'static self,
68 id: Principal,
69 method: S,
70 args: T,
71 ) -> CallResponse<R> {
72 self.call_with_payment(id, method, args, 0)
73 }
74
75 #[inline(always)]
76 fn call_with_payment<T: ArgumentEncoder, R: for<'a> ArgumentDecoder<'a>, S: Into<String>>(
77 &'static self,
78 id: Principal,
79 method: S,
80 args: T,
81 cycles: u64,
82 ) -> CallResponse<R> {
83 let args_raw = encode_args(args).expect("Failed to encode arguments.");
84 let method = method.into();
85 Box::pin(async move {
86 let bytes = self.call_raw(id, method, args_raw, cycles).await?;
87 decode_args(&bytes).map_err(|err| panic!("{:?}", err))
88 })
89 }
90
91 fn set_certified_data(&self, data: &[u8]);
93
94 fn data_certificate(&self) -> Option<Vec<u8>>;
96
97 fn spawn<F: 'static + std::future::Future<Output = ()>>(&mut self, future: F);
99
100 fn stable_size(&self) -> u32;
103
104 fn stable_grow(&self, new_pages: u32) -> Result<u32, StableMemoryError>;
109
110 fn stable_write(&self, offset: u32, buf: &[u8]);
112
113 fn stable_read(&self, offset: u32, buf: &mut [u8]);
115
116 fn with<T: 'static + Default, U, F: FnOnce(&T) -> U>(&self, callback: F) -> U;
119
120 fn maybe_with<T: 'static, U, F: FnOnce(&T) -> U>(&self, callback: F) -> Option<U>;
123
124 fn with_mut<T: 'static + Default, U, F: FnOnce(&mut T) -> U>(&self, callback: F) -> U;
127
128 fn maybe_with_mut<T: 'static, U, F: FnOnce(&mut T) -> U>(&self, callback: F) -> Option<U>;
131
132 fn take<T: 'static>(&self) -> Option<T>;
134
135 fn swap<T: 'static>(&self, value: T) -> Option<T>;
138
139 #[deprecated]
141 fn store<T: 'static>(&self, data: T);
142
143 #[deprecated]
145 fn get_maybe<T: 'static>(&self) -> Option<&T>;
146
147 #[deprecated]
149 fn get<T: 'static + Default>(&self) -> &T;
150
151 #[deprecated]
153 fn get_mut<T: 'static + Default>(&self) -> &mut T;
154
155 #[deprecated]
157 fn delete<T: 'static + Default>(&self) -> bool;
158}