-
Notifications
You must be signed in to change notification settings - Fork 44
feat(sdk): improve token state transition functionalities in rs-sdk #2657
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
95d5c59
feat: add token state transition functionalities
QuantumExplorer 605ac58
changed name of functions
QuantumExplorer 2b60932
elided
QuantumExplorer a1c28df
fixes
QuantumExplorer fda9945
added documentation
QuantumExplorer 23ecbc5
changed builders so they could be more easily passed through ffi
QuantumExplorer 5a4c46a
Merge branch 'v2.0-dev' into featadd-token-transitions
pauldelucia f28666c
added options
QuantumExplorer d3a708a
Merge branch 'featadd-token-transitions' of github.com:dashpay/platfo…
QuantumExplorer 18aaef6
renamed
QuantumExplorer File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| use crate::platform::transition::broadcast::BroadcastStateTransition; | ||
| use crate::platform::transition::fungible_tokens::burn::TokenBurnTransitionBuilder; | ||
| use crate::{Error, Sdk}; | ||
| use dpp::balances::credits::TokenAmount; | ||
| use dpp::data_contract::group::GroupSumPower; | ||
| use dpp::document::Document; | ||
| use dpp::group::group_action_status::GroupActionStatus; | ||
| use dpp::identity::signer::Signer; | ||
| use dpp::identity::IdentityPublicKey; | ||
| use dpp::platform_value::Identifier; | ||
| use dpp::state_transition::proof_result::StateTransitionProofResult; | ||
|
|
||
| pub enum BurnResult { | ||
| TokenBalance(Identifier, TokenAmount), | ||
| HistoricalDocument(Document), | ||
| GroupActionWithDocument(GroupSumPower, Option<Document>), | ||
| GroupActionWithBalance(GroupSumPower, GroupActionStatus, Option<TokenAmount>), | ||
| } | ||
|
|
||
| impl Sdk { | ||
| pub async fn token_burn<S: Signer>( | ||
| &self, | ||
| burn_tokens_transition_builder: TokenBurnTransitionBuilder<'_>, | ||
| signing_key: &IdentityPublicKey, | ||
| signer: &S, | ||
| ) -> Result<BurnResult, Error> { | ||
| let platform_version = self.version(); | ||
|
|
||
| let state_transition = burn_tokens_transition_builder | ||
| .sign(self, signing_key, signer, platform_version, None) | ||
| .await?; | ||
|
|
||
| let proof_result = state_transition | ||
| .broadcast_and_wait::<StateTransitionProofResult>(self, None) | ||
| .await?; | ||
|
|
||
| match proof_result { | ||
| StateTransitionProofResult::VerifiedTokenBalance(owner_id, remaining_balance) => { | ||
| Ok(BurnResult::TokenBalance(owner_id, remaining_balance)) | ||
| } | ||
| StateTransitionProofResult::VerifiedTokenActionWithDocument(doc) => { | ||
| // This means the token keeps burning history | ||
| Ok(BurnResult::HistoricalDocument(doc)) | ||
| } | ||
| StateTransitionProofResult::VerifiedTokenGroupActionWithDocument(power, doc) => { | ||
| // This means it's a group action with history | ||
| Ok(BurnResult::GroupActionWithDocument(power, doc)) | ||
| } | ||
| StateTransitionProofResult::VerifiedTokenGroupActionWithTokenBalance(power, status, balance) => { | ||
| // Group action without history | ||
| Ok(BurnResult::GroupActionWithBalance(power, status, balance)) | ||
| } | ||
| _ => Err(Error::DriveProofError( | ||
| drive::error::proof::ProofError::UnexpectedResultProof( | ||
| "Expected VerifiedTokenBalance, VerifiedTokenActionWithDocument, VerifiedTokenGroupActionWithDocument, or VerifiedTokenGroupActionWithTokenBalance for burn transition".to_string(), | ||
| ), | ||
| vec![], | ||
| Default::default(), | ||
| )), | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| use crate::platform::transition::broadcast::BroadcastStateTransition; | ||
| use crate::platform::transition::fungible_tokens::claim::TokenClaimTransitionBuilder; | ||
| use crate::{Error, Sdk}; | ||
| use dpp::data_contract::group::GroupSumPower; | ||
| use dpp::document::Document; | ||
| use dpp::identity::signer::Signer; | ||
| use dpp::identity::IdentityPublicKey; | ||
| use dpp::state_transition::proof_result::StateTransitionProofResult; | ||
|
|
||
| pub enum ClaimResult { | ||
| Document(Document), | ||
| GroupActionWithDocument(GroupSumPower, Document), | ||
| } | ||
|
|
||
| impl Sdk { | ||
| pub async fn token_claim<S: Signer>( | ||
| &self, | ||
| claim_tokens_transition_builder: TokenClaimTransitionBuilder<'_>, | ||
| signing_key: &IdentityPublicKey, | ||
| signer: &S, | ||
| ) -> Result<ClaimResult, Error> { | ||
| let platform_version = self.version(); | ||
|
|
||
| let state_transition = claim_tokens_transition_builder | ||
| .sign(self, signing_key, signer, platform_version, None) | ||
| .await?; | ||
|
|
||
| let proof_result = state_transition | ||
| .broadcast_and_wait::<StateTransitionProofResult>(self, None) | ||
| .await?; | ||
|
|
||
| match proof_result { | ||
| StateTransitionProofResult::VerifiedTokenActionWithDocument(document) => { | ||
| Ok(ClaimResult::Document(document)) | ||
| } | ||
| StateTransitionProofResult::VerifiedTokenGroupActionWithDocument(power, Some(document)) => { | ||
| Ok(ClaimResult::GroupActionWithDocument(power, document)) | ||
| } | ||
| StateTransitionProofResult::VerifiedTokenGroupActionWithDocument(_, None) => { | ||
| Err(Error::DriveProofError( | ||
| drive::error::proof::ProofError::IncorrectProof( | ||
| "Expected document in group action result".to_string(), | ||
| ), | ||
| vec![], | ||
| Default::default(), | ||
| )) | ||
| } | ||
| _ => Err(Error::DriveProofError( | ||
| drive::error::proof::ProofError::UnexpectedResultProof( | ||
| "Expected VerifiedTokenActionWithDocument or VerifiedTokenGroupActionWithDocument for claim transition".to_string(), | ||
| ), | ||
| vec![], | ||
| Default::default(), | ||
| )), | ||
| } | ||
| } | ||
| } |
58 changes: 58 additions & 0 deletions
58
packages/rs-sdk/src/platform/tokens/transitions/config_update.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| use crate::platform::transition::broadcast::BroadcastStateTransition; | ||
| use crate::platform::transition::fungible_tokens::config_update::TokenConfigUpdateTransitionBuilder; | ||
| use crate::{Error, Sdk}; | ||
| use dpp::data_contract::group::GroupSumPower; | ||
| use dpp::document::Document; | ||
| use dpp::identity::signer::Signer; | ||
| use dpp::identity::IdentityPublicKey; | ||
| use dpp::state_transition::proof_result::StateTransitionProofResult; | ||
|
|
||
| pub enum ConfigUpdateResult { | ||
| Document(Document), | ||
| GroupActionWithDocument(GroupSumPower, Document), | ||
| } | ||
|
|
||
| impl Sdk { | ||
| pub async fn token_update_contract_token_configuration<S: Signer>( | ||
| &self, | ||
| config_update_transition_builder: TokenConfigUpdateTransitionBuilder<'_>, | ||
| signing_key: &IdentityPublicKey, | ||
| signer: &S, | ||
| ) -> Result<ConfigUpdateResult, Error> { | ||
| let platform_version = self.version(); | ||
|
|
||
| let state_transition = config_update_transition_builder | ||
| .sign(self, signing_key, signer, platform_version, None) | ||
| .await?; | ||
|
|
||
| let proof_result = state_transition | ||
| .broadcast_and_wait::<StateTransitionProofResult>(self, None) | ||
| .await?; | ||
|
|
||
| match proof_result { | ||
| StateTransitionProofResult::VerifiedTokenActionWithDocument(document) => { | ||
| Ok(ConfigUpdateResult::Document(document)) | ||
| } | ||
| StateTransitionProofResult::VerifiedTokenGroupActionWithDocument(power, Some(document)) => { | ||
| Ok(ConfigUpdateResult::GroupActionWithDocument(power, document)) | ||
| } | ||
| StateTransitionProofResult::VerifiedTokenGroupActionWithDocument(_, None) => { | ||
| Err(Error::DriveProofError( | ||
| drive::error::proof::ProofError::IncorrectProof( | ||
| "Expected document in group action result".to_string(), | ||
| ), | ||
| vec![], | ||
| Default::default(), | ||
| )) | ||
| } | ||
| _ => Err(Error::DriveProofError( | ||
| drive::error::proof::ProofError::UnexpectedResultProof( | ||
| "Expected VerifiedTokenActionWithDocument or VerifiedTokenGroupActionWithDocument for config update transition" | ||
| .to_string(), | ||
| ), | ||
| vec![], | ||
| Default::default(), | ||
| )), | ||
| } | ||
| } | ||
| } |
50 changes: 50 additions & 0 deletions
50
packages/rs-sdk/src/platform/tokens/transitions/destroy_frozen_funds.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| use crate::platform::transition::broadcast::BroadcastStateTransition; | ||
| use crate::platform::transition::fungible_tokens::destroy::TokenDestroyFrozenFundsTransitionBuilder; | ||
| use crate::{Error, Sdk}; | ||
| use dpp::data_contract::group::GroupSumPower; | ||
| use dpp::document::Document; | ||
| use dpp::identity::signer::Signer; | ||
| use dpp::identity::IdentityPublicKey; | ||
| use dpp::state_transition::proof_result::StateTransitionProofResult; | ||
|
|
||
| pub enum DestroyFrozenFundsResult { | ||
| HistoricalDocument(Document), | ||
| GroupActionWithDocument(GroupSumPower, Option<Document>), | ||
| } | ||
|
|
||
| impl Sdk { | ||
| pub async fn token_destroy_frozen_funds<S: Signer>( | ||
| &self, | ||
| destroy_frozen_funds_transition_builder: TokenDestroyFrozenFundsTransitionBuilder<'_>, | ||
| signing_key: &IdentityPublicKey, | ||
| signer: &S, | ||
| ) -> Result<DestroyFrozenFundsResult, Error> { | ||
| let platform_version = self.version(); | ||
|
|
||
| let state_transition = destroy_frozen_funds_transition_builder | ||
| .sign(self, signing_key, signer, platform_version, None) | ||
| .await?; | ||
|
|
||
| let proof_result = state_transition | ||
| .broadcast_and_wait::<StateTransitionProofResult>(self, None) | ||
| .await?; | ||
|
|
||
| match proof_result { | ||
| StateTransitionProofResult::VerifiedTokenActionWithDocument(doc) => { | ||
| // DestroyFrozenFunds always keeps history | ||
| Ok(DestroyFrozenFundsResult::HistoricalDocument(doc)) | ||
| } | ||
| StateTransitionProofResult::VerifiedTokenGroupActionWithDocument(power, doc) => { | ||
| // Group action with history | ||
| Ok(DestroyFrozenFundsResult::GroupActionWithDocument(power, doc)) | ||
| } | ||
| _ => Err(Error::DriveProofError( | ||
| drive::error::proof::ProofError::UnexpectedResultProof( | ||
| "Expected VerifiedTokenActionWithDocument or VerifiedTokenGroupActionWithDocument for destroy frozen funds transition".to_string(), | ||
| ), | ||
| vec![], | ||
| Default::default(), | ||
| )), | ||
| } | ||
| } | ||
| } | ||
55 changes: 55 additions & 0 deletions
55
packages/rs-sdk/src/platform/tokens/transitions/direct_purchase.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| use crate::platform::transition::broadcast::BroadcastStateTransition; | ||
| use crate::platform::transition::fungible_tokens::purchase::TokenDirectPurchaseTransitionBuilder; | ||
| use crate::{Error, Sdk}; | ||
| use dpp::balances::credits::TokenAmount; | ||
| use dpp::identity::signer::Signer; | ||
| use dpp::identity::IdentityPublicKey; | ||
| use dpp::platform_value::Identifier; | ||
| use dpp::state_transition::proof_result::StateTransitionProofResult; | ||
|
|
||
| impl Sdk { | ||
| pub async fn token_purchase<S: Signer>( | ||
| &self, | ||
| purchase_tokens_transition_builder: TokenDirectPurchaseTransitionBuilder<'_>, | ||
| signing_key: &IdentityPublicKey, | ||
| signer: &S, | ||
| ) -> Result<DirectPurchaseResult, Error> { | ||
| let platform_version = self.version(); | ||
|
|
||
| let state_transition = purchase_tokens_transition_builder | ||
| .sign(self, signing_key, signer, platform_version, None) | ||
| .await?; | ||
|
|
||
| let proof_result = state_transition | ||
| .broadcast_and_wait::<StateTransitionProofResult>(self, None) | ||
| .await?; | ||
|
|
||
| match proof_result { | ||
| StateTransitionProofResult::VerifiedTokenBalance(owner_id, balance) => { | ||
| Ok(DirectPurchaseResult::TokenBalance(owner_id, balance)) | ||
| } | ||
| StateTransitionProofResult::VerifiedTokenActionWithDocument(doc) => { | ||
| Ok(DirectPurchaseResult::HistoricalDocument(doc)) | ||
| } | ||
| StateTransitionProofResult::VerifiedTokenGroupActionWithDocument(power, doc) => { | ||
| Ok(DirectPurchaseResult::GroupActionWithDocument(power, doc)) | ||
| } | ||
QuantumExplorer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| _ => Err(Error::DriveProofError( | ||
| drive::error::proof::ProofError::UnexpectedResultProof( | ||
| "Expected VerifiedTokenBalance, VerifiedTokenActionWithDocument, or VerifiedTokenGroupActionWithDocument for direct purchase transition".to_string(), | ||
| ), | ||
| vec![], | ||
| Default::default(), | ||
| )), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub enum DirectPurchaseResult { | ||
| TokenBalance(Identifier, TokenAmount), | ||
| HistoricalDocument(dpp::document::Document), | ||
| GroupActionWithDocument( | ||
| dpp::data_contract::group::GroupSumPower, | ||
| Option<dpp::document::Document>, | ||
| ), | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
packages/rs-sdk/src/platform/tokens/transitions/emergency_action.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| use crate::platform::transition::broadcast::BroadcastStateTransition; | ||
| use crate::platform::transition::fungible_tokens::emergency_action::TokenEmergencyActionTransitionBuilder; | ||
| use crate::{Error, Sdk}; | ||
| use dpp::data_contract::group::GroupSumPower; | ||
| use dpp::document::Document; | ||
| use dpp::identity::signer::Signer; | ||
| use dpp::identity::IdentityPublicKey; | ||
| use dpp::state_transition::proof_result::StateTransitionProofResult; | ||
|
|
||
| impl Sdk { | ||
| pub async fn token_emergency_action<S: Signer>( | ||
| &self, | ||
| emergency_action_transition_builder: TokenEmergencyActionTransitionBuilder<'_>, | ||
| signing_key: &IdentityPublicKey, | ||
| signer: &S, | ||
| ) -> Result<EmergencyActionResult, Error> { | ||
| let platform_version = self.version(); | ||
|
|
||
| let state_transition = emergency_action_transition_builder | ||
| .sign(self, signing_key, signer, platform_version, None) | ||
| .await?; | ||
|
|
||
| let proof_result = state_transition | ||
| .broadcast_and_wait::<StateTransitionProofResult>(self, None) | ||
| .await?; | ||
|
|
||
| match proof_result { | ||
| StateTransitionProofResult::VerifiedTokenGroupActionWithDocument( | ||
| group_power, | ||
| document, | ||
| ) => Ok(EmergencyActionResult::GroupActionWithDocument( | ||
| group_power, | ||
| document, | ||
| )), | ||
| _ => Err(Error::DriveProofError( | ||
| drive::error::proof::ProofError::UnexpectedResultProof( | ||
| "Expected VerifiedTokenGroupActionWithDocument for emergency action transition" | ||
| .to_string(), | ||
| ), | ||
| vec![], | ||
| Default::default(), | ||
| )), | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub enum EmergencyActionResult { | ||
| GroupActionWithDocument(GroupSumPower, Option<Document>), | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.