numbat_wasm/contract_base/contract_base_trait.rs
1use super::{
2 BlockchainWrapper, CallValueWrapper, CryptoWrapper, ErrorHelper, ManagedSerializer,
3 ManagedTypeHelper, SendWrapper,
4};
5use crate::api::VMApi;
6
7/// Interface to be used by the actual smart contract code.
8///
9/// Note: contracts and the api are not mutable.
10/// They simply pass on/retrieve data to/from the protocol.
11/// When mocking the blockchain state, we use the Rc/RefCell pattern
12/// to isolate mock state mutability from the contract interface.
13pub trait ContractBase: Sized {
14 type Api: VMApi;
15
16 /// Grants direct access to the underlying VM API.
17 /// Avoid using it directly.
18 fn raw_vm_api(&self) -> Self::Api;
19
20 /// Gateway into the call value retrieval functionality.
21 /// The payment annotations should normally be the ones to handle this,
22 /// but the developer is also given direct access to the API.
23 fn call_value(&self) -> CallValueWrapper<Self::Api> {
24 CallValueWrapper::new(self.raw_vm_api())
25 }
26
27 /// Gateway to the functionality related to sending transactions from the current contract.
28 #[inline]
29 fn send(&self) -> SendWrapper<Self::Api> {
30 SendWrapper::new(self.raw_vm_api())
31 }
32
33 /// Managed types API. Required to create new instances of managed types.
34 #[inline]
35 fn type_manager(&self) -> Self::Api {
36 self.raw_vm_api()
37 }
38
39 /// Helps create new instances of managed types
40 #[inline]
41 fn types(&self) -> ManagedTypeHelper<Self::Api> {
42 ManagedTypeHelper::new(self.raw_vm_api())
43 }
44
45 /// Gateway blockchain info related to the current transaction and to accounts.
46 #[inline]
47 fn blockchain(&self) -> BlockchainWrapper<Self::Api> {
48 BlockchainWrapper::new(self.raw_vm_api())
49 }
50
51 /// Stateless crypto functions provided by the Andes VM.
52 #[inline]
53 fn crypto(&self) -> CryptoWrapper<Self::Api> {
54 CryptoWrapper::new(self.raw_vm_api())
55 }
56
57 /// Component that provides contract developers access
58 /// to highly optimized manual serialization and deserialization.
59 #[inline]
60 fn serializer(&self) -> ManagedSerializer<Self::Api> {
61 ManagedSerializer::new(self.raw_vm_api())
62 }
63
64 #[inline]
65 fn error(&self) -> ErrorHelper<Self::Api> {
66 ErrorHelper::new_instance(self.raw_vm_api())
67 }
68}