multiversx_sc/contract_base/
contract_base_trait.rs

1use super::{
2    BlockchainWrapper, CallValueWrapper, CryptoWrapper, ErrorHelper, ManagedSerializer,
3    SendRawWrapper, SendWrapper, StorageRawWrapper,
4};
5use crate::{
6    api::VMApi,
7    types::{Tx, TxBaseWithEnv, TxScEnv},
8};
9
10/// Interface to be used by the actual smart contract code.
11///
12/// Note: contracts and the api are not mutable.
13/// They simply pass on/retrieve data to/from the protocol.
14/// When mocking the blockchain state, we use the Rc/RefCell pattern
15/// to isolate mock state mutability from the contract interface.
16pub trait ContractBase: Sized {
17    type Api: VMApi;
18
19    /// Gateway into the call value retrieval functionality.
20    /// The payment annotations should normally be the ones to handle this,
21    /// but the developer is also given direct access to the API.
22    fn call_value(&self) -> CallValueWrapper<Self::Api> {
23        CallValueWrapper::new()
24    }
25
26    /// Gateway to the functionality related to sending transactions from the current contract.
27    #[inline]
28    fn send(&self) -> SendWrapper<Self::Api> {
29        SendWrapper::new()
30    }
31
32    /// Starts the declaration of a new transaction.
33    #[inline]
34    fn tx(&self) -> TxBaseWithEnv<TxScEnv<Self::Api>> {
35        Tx::new_tx_from_sc()
36    }
37
38    /// Low-level functionality related to sending transactions from the current contract.
39    ///
40    /// For almost all cases contracts should instead use `self.send()` and `ContractCall`.
41    #[inline]
42    fn send_raw(&self) -> SendRawWrapper<Self::Api> {
43        SendRawWrapper::new()
44    }
45
46    /// Gateway blockchain info related to the current transaction and to accounts.
47    #[inline]
48    fn blockchain(&self) -> BlockchainWrapper<Self::Api> {
49        BlockchainWrapper::<Self::Api>::new()
50    }
51
52    /// Stateless crypto functions provided by the Arwen VM.
53    #[inline]
54    fn crypto(&self) -> CryptoWrapper<Self::Api> {
55        CryptoWrapper::new()
56    }
57
58    /// Component that provides contract developers access
59    /// to highly optimized manual serialization and deserialization.
60    #[inline]
61    fn serializer(&self) -> ManagedSerializer<Self::Api> {
62        ManagedSerializer::new()
63    }
64
65    #[inline]
66    fn error(&self) -> ErrorHelper<Self::Api> {
67        ErrorHelper::new()
68    }
69
70    #[inline]
71    fn storage_raw(&self) -> StorageRawWrapper<Self::Api> {
72        StorageRawWrapper::new()
73    }
74}