Skip to content

Commit

Permalink
refactor(sdk)!: change state transitions api
Browse files Browse the repository at this point in the history
  • Loading branch information
lklimek committed Nov 20, 2024
1 parent 21e229a commit ebf61e0
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 435 deletions.
1 change: 1 addition & 0 deletions packages/rs-sdk/src/platform/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub mod transfer_document;
mod txid;
pub mod update_price_of_document;
pub mod vote;
pub mod waitable;
pub mod withdraw_from_identity;

pub use txid::TxId;
58 changes: 7 additions & 51 deletions packages/rs-sdk/src/platform/transition/purchase_document.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,19 @@
use std::sync::Arc;

use crate::{Error, Sdk};

use super::broadcast::BroadcastStateTransition;
use super::waitable::Waitable;
use crate::platform::transition::put_settings::PutSettings;
use dpp::data_contract::document_type::accessors::DocumentTypeV0Getters;
use dpp::data_contract::document_type::DocumentType;
use dpp::data_contract::DataContract;
use dpp::document::{Document, DocumentV0Getters};
use dpp::document::Document;
use dpp::fee::Credits;
use dpp::identity::signer::Signer;
use dpp::identity::IdentityPublicKey;
use dpp::prelude::Identifier;
use dpp::state_transition::documents_batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0;
use dpp::state_transition::documents_batch_transition::DocumentsBatchTransition;
use dpp::state_transition::proof_result::StateTransitionProofResult;
use dpp::state_transition::StateTransition;

use super::broadcast::BroadcastStateTransition;

#[async_trait::async_trait]
/// A trait for purchasing a document on Platform
pub trait PurchaseDocument<S: Signer> {
Expand All @@ -34,14 +30,6 @@ pub trait PurchaseDocument<S: Signer> {
settings: Option<PutSettings>,
) -> Result<StateTransition, Error>;

/// Waits for the response of a state transition after it has been broadcast
async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
data_contract: Arc<DataContract>,
) -> Result<Document, Error>;

/// Tries to purchase a document on platform and waits for the response
async fn purchase_document_and_wait_for_response(
&self,
Expand All @@ -50,8 +38,8 @@ pub trait PurchaseDocument<S: Signer> {
document_type: DocumentType,
purchaser_id: Identifier,
identity_public_key: IdentityPublicKey,
data_contract: Arc<DataContract>,
signer: &S,
settings: Option<PutSettings>,
) -> Result<Document, Error>;
}

Expand Down Expand Up @@ -98,39 +86,15 @@ impl<S: Signer> PurchaseDocument<S> for Document {
Ok(transition)
}

async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
_data_contract: Arc<DataContract>,
) -> Result<Document, Error> {
let result = state_transition.wait_for_response(sdk, None).await?;

match result {
StateTransitionProofResult::VerifiedDocuments(mut documents) => {
let document = documents
.remove(self.id_ref())
.ok_or(Error::InvalidProvedResponse(
"did not prove the sent document".to_string(),
))?
.ok_or(Error::InvalidProvedResponse(
"expected there to actually be a document".to_string(),
))?;
Ok(document)
}
_ => Err(Error::DapiClientError("proved a non document".to_string())),
}
}

async fn purchase_document_and_wait_for_response(
&self,
price: Credits,
sdk: &Sdk,
document_type: DocumentType,
purchaser_id: Identifier,
identity_public_key: IdentityPublicKey,
data_contract: Arc<DataContract>,
signer: &S,
settings: Option<PutSettings>,
) -> Result<Document, Error> {
let state_transition = self
.purchase_document(
Expand All @@ -140,18 +104,10 @@ impl<S: Signer> PurchaseDocument<S> for Document {
purchaser_id,
identity_public_key,
signer,
None,
settings,
)
.await?;

let document = <Self as PurchaseDocument<S>>::wait_for_response(
self,
sdk,
state_transition,
data_contract,
)
.await?;

Ok(document)
<Self as Waitable>::wait_for_response(sdk, state_transition, settings).await
}
}
35 changes: 6 additions & 29 deletions packages/rs-sdk/src/platform/transition/put_contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use dpp::identity::signer::Signer;
use dpp::identity::{IdentityPublicKey, PartialIdentity};
use dpp::state_transition::data_contract_create_transition::methods::DataContractCreateTransitionMethodsV0;
use dpp::state_transition::data_contract_create_transition::DataContractCreateTransition;
use dpp::state_transition::proof_result::StateTransitionProofResult;
use dpp::state_transition::StateTransition;

use super::broadcast::BroadcastStateTransition;
use super::waitable::Waitable;

#[async_trait::async_trait]
/// A trait for putting a contract to platform
pub trait PutContract<S: Signer> {
pub trait PutContract<S: Signer>: Waitable {
/// Puts a document on platform
/// setting settings to `None` sets default connection behavior
async fn put_to_platform(
Expand All @@ -28,19 +28,13 @@ pub trait PutContract<S: Signer> {
settings: Option<PutSettings>,
) -> Result<StateTransition, Error>;

/// Waits for the response of a state transition after it has been broadcast
async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
) -> Result<DataContract, Error>;

/// Puts a contract on platform and waits for the confirmation proof
async fn put_to_platform_and_wait_for_response(
&self,
sdk: &Sdk,
identity_public_key: IdentityPublicKey,
signer: &S,
settings: Option<PutSettings>,
) -> Result<DataContract, Error>;
}

Expand Down Expand Up @@ -82,34 +76,17 @@ impl<S: Signer> PutContract<S> for DataContract {
Ok(transition)
}

async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
) -> Result<DataContract, Error> {
let result = state_transition.wait_for_response(sdk, None).await?;

//todo verify

match result {
StateTransitionProofResult::VerifiedDataContract(data_contract) => Ok(data_contract),
_ => Err(Error::DapiClientError("proved a non document".to_string())),
}
}

async fn put_to_platform_and_wait_for_response(
&self,
sdk: &Sdk,
identity_public_key: IdentityPublicKey,
signer: &S,
settings: Option<PutSettings>,
) -> Result<DataContract, Error> {
let state_transition = self
.put_to_platform(sdk, identity_public_key, signer, None)
.put_to_platform(sdk, identity_public_key, signer, settings)
.await?;

let data_contract =
<Self as PutContract<S>>::wait_for_response(self, sdk, state_transition).await?;

Ok(data_contract)
Self::wait_for_response(sdk, state_transition, settings).await
}
}
58 changes: 5 additions & 53 deletions packages/rs-sdk/src/platform/transition/put_document.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
use super::broadcast::BroadcastStateTransition;
use super::waitable::Waitable;
use crate::platform::transition::put_settings::PutSettings;
use crate::{Error, Sdk};
use dpp::data_contract::document_type::accessors::DocumentTypeV0Getters;
use dpp::data_contract::document_type::DocumentType;
use dpp::data_contract::DataContract;
use dpp::document::{Document, DocumentV0Getters};
use dpp::identity::signer::Signer;
use dpp::identity::IdentityPublicKey;
use dpp::state_transition::documents_batch_transition::methods::v0::DocumentsBatchTransitionMethodsV0;
use dpp::state_transition::documents_batch_transition::DocumentsBatchTransition;
use dpp::state_transition::proof_result::StateTransitionProofResult;
use dpp::state_transition::StateTransition;
use std::sync::Arc;

#[async_trait::async_trait]
/// A trait for putting a document to platform
Expand All @@ -28,23 +26,15 @@ pub trait PutDocument<S: Signer> {
settings: Option<PutSettings>,
) -> Result<StateTransition, Error>;

/// Waits for the response of a state transition after it has been broadcast
async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
data_contract: Arc<DataContract>,
) -> Result<Document, Error>;

/// Puts an identity on platform and waits for the confirmation proof
async fn put_to_platform_and_wait_for_response(
&self,
sdk: &Sdk,
document_type: DocumentType,
document_state_transition_entropy: [u8; 32],
identity_public_key: IdentityPublicKey,
data_contract: Arc<DataContract>,
signer: &S,
settings: Option<PutSettings>,
) -> Result<Document, Error>;
}

Expand Down Expand Up @@ -89,38 +79,14 @@ impl<S: Signer> PutDocument<S> for Document {
Ok(transition)
}

async fn wait_for_response(
&self,
sdk: &Sdk,
state_transition: StateTransition,
_data_contract: Arc<DataContract>,
) -> Result<Document, Error> {
let result = state_transition.wait_for_response(sdk, None).await?;
//todo verify
match result {
StateTransitionProofResult::VerifiedDocuments(mut documents) => {
let document = documents
.remove(self.id_ref())
.ok_or(Error::InvalidProvedResponse(
"did not prove the sent document".to_string(),
))?
.ok_or(Error::InvalidProvedResponse(
"expected there to actually be a document".to_string(),
))?;
Ok(document)
}
_ => Err(Error::DapiClientError("proved a non document".to_string())),
}
}

async fn put_to_platform_and_wait_for_response(
&self,
sdk: &Sdk,
document_type: DocumentType,
document_state_transition_entropy: [u8; 32],
identity_public_key: IdentityPublicKey,
_data_contract: Arc<DataContract>,
signer: &S,
settings: Option<PutSettings>,
) -> Result<Document, Error> {
let state_transition = self
.put_to_platform(
Expand All @@ -129,24 +95,10 @@ impl<S: Signer> PutDocument<S> for Document {
document_state_transition_entropy,
identity_public_key,
signer,
None,
settings,
)
.await?;

let result = state_transition.broadcast_and_wait(sdk, None).await?;
match result {
StateTransitionProofResult::VerifiedDocuments(mut documents) => {
let document = documents
.remove(self.id_ref())
.ok_or(Error::InvalidProvedResponse(
"did not prove the sent document".to_string(),
))?
.ok_or(Error::InvalidProvedResponse(
"expected there to actually be a document".to_string(),
))?;
Ok(document)
}
_ => Err(Error::DapiClientError("proved a non document".to_string())),
}
<Self as Waitable>::wait_for_response(sdk, state_transition, settings).await
}
}
Loading

0 comments on commit ebf61e0

Please sign in to comment.