odra_core/
contract_context.rs

1use crate::call_def::CallDef;
2use crate::casper_types::bytesrepr::Bytes;
3use crate::casper_types::U512;
4use crate::consts::RANDOM_BYTES_COUNT;
5use crate::prelude::*;
6use crate::validator::ValidatorInfo;
7use casper_types::{CLValue, PublicKey};
8
9/// Trait representing the context of a smart contract.
10#[cfg_attr(test, allow(unreachable_code))]
11#[cfg_attr(test, mockall::automock)]
12pub trait ContractContext {
13    /// Retrieves from the storage the value associated with the given key.
14    ///
15    /// # Arguments
16    ///
17    /// * `key` - The key to retrieve the value for.
18    ///
19    /// # Returns
20    ///
21    /// An `Option` containing the value associated with the key, or `None` if the key is not found.
22    fn get_value(&self, key: &[u8]) -> Option<Bytes>;
23
24    /// Writes to the storage the value associated with the given key.
25    ///
26    /// # Arguments
27    ///
28    /// * `key` - The key to set the value for.
29    /// * `value` - The value to be set.
30    fn set_value(&self, key: &[u8], value: Bytes);
31
32    /// Retrieves the value behind a named key.
33    ///
34    /// # Arguments
35    ///  
36    /// * `name` - The name of the key.
37    fn get_named_value(&self, name: &str) -> Option<Bytes>;
38
39    /// Sets the value behind a named key.
40    ///
41    /// # Arguments
42    ///
43    /// * `name` - The name of the key.
44    /// * `value` - The value to set.
45    fn set_named_value(&self, name: &str, value: CLValue);
46
47    /// Retrieves the key value behind a named dictionary.
48    ///
49    /// # Arguments
50    ///
51    /// * `dictionary_name` - The name of the dictionary.
52    /// * `key` - The key to retrieve the value for.
53    fn get_dictionary_value(&self, dictionary_name: &str, key: &[u8]) -> Option<Bytes>;
54
55    /// Sets the key value behind a named dictionary.
56    ///
57    /// # Arguments
58    ///
59    /// * `dictionary_name` - The name of the dictionary.
60    /// * `key` - The key to set the value for.
61    /// * `value` - The value to set.
62    fn set_dictionary_value(&self, dictionary_name: &str, key: &[u8], value: CLValue);
63
64    /// Removes the named key from the storage.
65    ///
66    /// # Arguments
67    /// * `dictionary_name` - The name of the dictionary.
68    fn remove_dictionary(&self, dictionary_name: &str);
69
70    /// Initializes the empty dictionary with the given name.
71    ///
72    /// # Arguments
73    /// * `dictionary_name` - The name of the dictionary to initialize.
74    fn init_dictionary(&self, dictionary_name: &str);
75
76    /// Retrieves the address of the caller.
77    fn caller(&self) -> Address;
78
79    /// Retrieves the address of the current contract.
80    fn self_address(&self) -> Address;
81
82    /// Calls another contract at the specified address with the given call definition.
83    ///
84    /// # Arguments
85    ///
86    /// * `address` - The address of the contract to call.
87    /// * `call_def` - The call definition specifying the method and arguments to call.
88    ///
89    /// # Returns
90    ///
91    /// The result of the contract call as a byte array.
92    fn call_contract(&self, address: Address, call_def: CallDef) -> Bytes;
93
94    /// Retrieves the current block time in milliseconds.
95    ///
96    /// # Returns
97    ///
98    /// The current block time as a `u64` value.
99    fn get_block_time(&self) -> u64;
100
101    /// Retrieves the value attached to the call.
102    ///
103    /// # Returns
104    ///
105    /// The attached value as a `U512` value.
106    fn attached_value(&self) -> U512;
107
108    /// Retrieves the balance of the current contract.
109    ///
110    /// # Returns
111    /// The balance of the current contract in U512
112    fn self_balance(&self) -> U512;
113
114    /// Emits an event with the specified event data.
115    ///
116    /// # Arguments
117    ///
118    /// * `event` - The event data to emit.
119    fn emit_event(&self, event: &Bytes);
120
121    /// Emits an event with the specified event data using native mechanism.
122    ///
123    /// # Arguments
124    ///
125    /// * `event` - The event data to emit.
126    fn emit_native_event(&self, event: &Bytes);
127
128    /// Transfers tokens to the specified address.
129    ///
130    /// # Arguments
131    ///
132    /// * `to` - The address to transfer the tokens to.
133    /// * `amount` - The amount of tokens to transfer.
134    fn transfer_tokens(&self, to: &Address, amount: &U512);
135
136    /// Reverts the contract execution with the specified error.
137    ///
138    /// # Arguments
139    ///
140    /// * `error` - The error to revert with.
141    ///
142    /// # Panics
143    ///
144    /// This function will panic and abort the contract execution.
145    fn revert(&self, error: OdraError) -> !;
146
147    /// Retrieves the value of the named argument as a byte array.
148    ///
149    /// # Arguments
150    ///
151    /// * `name` - The name of the argument.
152    ///
153    /// # Returns
154    ///
155    /// The value of the named argument as a byte array.
156    fn get_named_arg_bytes(&self, name: &str) -> OdraResult<Bytes>;
157
158    /// Similar to `get_named_arg_bytes`, but returns `None` if the named argument is not present.
159    fn get_opt_named_arg_bytes(&self, name: &str) -> Option<Bytes>;
160
161    /// Handles the value attached to the call. Sets the value in the contract context.
162    fn handle_attached_value(&self);
163
164    /// Clears the value attached to the call.
165    fn clear_attached_value(&self);
166
167    /// Computes the hash of the given byte array.
168    ///
169    /// # Arguments
170    ///
171    /// * `bytes` - The byte array to compute the hash for.
172    ///
173    /// # Returns
174    ///
175    /// The computed hash as a fixed-size byte array of length 32.
176    fn hash(&self, bytes: &[u8]) -> [u8; 32];
177
178    /// Delegate the amount of tokens to the validator
179    ///
180    /// # Arguments
181    ///
182    /// * `validator` - The validator to delegate the tokens to.
183    /// * `amount` - The amount of tokens to delegate.
184    fn delegate(&self, validator: PublicKey, amount: U512);
185
186    /// Undelegate the amount of tokens from the validator
187    ///
188    /// # Arguments
189    ///
190    /// * `validator` - The validator to undelegate the tokens from.
191    /// * `amount` - The amount of tokens to undelegate.
192    fn undelegate(&self, validator: PublicKey, amount: U512);
193
194    /// Gets the amount of tokens delegated to the validator
195    ///
196    /// # Arguments
197    ///
198    /// * `validator` - The validator to get the delegated amount from.
199    ///
200    /// # Returns
201    ///
202    /// The amount of tokens delegated to the validator as a `U512` value.
203    fn delegated_amount(&self, validator: PublicKey) -> U512;
204
205    /// Returns information about the validator
206    ///
207    /// # Arguments
208    /// - validator - The validator to query
209    ///
210    /// # Returns
211    /// Option<ValidatorBid>
212    fn get_validator_info(&self, validator: PublicKey) -> Option<ValidatorInfo>;
213
214    /// Returns a vector of pseudorandom bytes of the specified size.
215    /// There is no guarantee that the returned bytes are in any way cryptographically secure.
216    fn pseudorandom_bytes(&self) -> [u8; RANDOM_BYTES_COUNT];
217}