dpp/data_contract/
data_contract_facade.rs

1use crate::data_contract::{DataContract, DataContractFactory};
2
3use crate::data_contract::created_data_contract::CreatedDataContract;
4use crate::prelude::{Identifier, IdentityNonce};
5#[cfg(feature = "state-transitions")]
6use crate::state_transition::data_contract_create_transition::DataContractCreateTransition;
7#[cfg(feature = "state-transitions")]
8use crate::state_transition::data_contract_update_transition::DataContractUpdateTransition;
9
10use crate::ProtocolError;
11use platform_value::Value;
12
13/// # Data Contract Facade
14///
15/// This module acts as a simplified, high-level interface to a more complex
16/// body of code. It forwards requests to appropriate subsystems.
17///
18/// ## Versioning
19///
20/// In Dash Platform, facades are not versioned because the interface they
21/// provide remains stable, even when changes occur in the underlying system.
22/// Since these modifications do not affect the facade's interface, versioning
23/// is not necessary. The primary function of the facade is to provide a stable
24/// API to the rest of the system, effectively isolating consumers of the API
25/// from changes in the underlying implementation.
26pub struct DataContractFacade {
27    factory: DataContractFactory,
28}
29
30impl DataContractFacade {
31    pub fn new(protocol_version: u32) -> Result<Self, ProtocolError> {
32        Ok(Self {
33            factory: DataContractFactory::new(protocol_version)?,
34        })
35    }
36
37    /// Create Data Contract
38    pub fn create(
39        &self,
40        owner_id: Identifier,
41        identity_nonce: IdentityNonce,
42        documents: Value,
43        config: Option<Value>,
44        definitions: Option<Value>,
45    ) -> Result<CreatedDataContract, ProtocolError> {
46        self.factory.create_with_value_config(
47            owner_id,
48            identity_nonce,
49            documents,
50            config,
51            definitions,
52        )
53    }
54
55    /// Create Data Contract from plain object
56    #[cfg(all(feature = "identity-serialization", feature = "client"))]
57    pub fn create_from_object(
58        &self,
59        raw_data_contract: Value,
60        #[cfg(feature = "validation")] skip_validation: bool,
61    ) -> Result<DataContract, ProtocolError> {
62        self.factory.create_from_object(
63            raw_data_contract,
64            #[cfg(feature = "validation")]
65            skip_validation,
66        )
67    }
68
69    /// Create Data Contract from buffer
70    #[cfg(all(feature = "identity-serialization", feature = "client"))]
71    pub fn create_from_buffer(
72        &self,
73        buffer: Vec<u8>,
74        #[cfg(feature = "validation")] skip_validation: bool,
75    ) -> Result<DataContract, ProtocolError> {
76        self.factory.create_from_buffer(
77            buffer,
78            #[cfg(feature = "validation")]
79            skip_validation,
80        )
81    }
82
83    #[cfg(feature = "state-transitions")]
84    /// Create Data Contract Create State Transition
85    pub fn create_data_contract_create_transition(
86        &self,
87        created_data_contract: CreatedDataContract,
88    ) -> Result<DataContractCreateTransition, ProtocolError> {
89        self.factory
90            .create_data_contract_create_transition(created_data_contract)
91    }
92
93    #[cfg(feature = "state-transitions")]
94    /// Create Data Contract Update State Transition
95    pub fn create_data_contract_update_transition(
96        &self,
97        data_contract: DataContract,
98        identity_contract_nonce: IdentityNonce,
99    ) -> Result<DataContractUpdateTransition, ProtocolError> {
100        self.factory
101            .create_data_contract_update_transition(data_contract, identity_contract_nonce)
102    }
103}