diff --git a/packages/rs-sdk/src/platform/tokens/transitions/config_update.rs b/packages/rs-sdk/src/platform/tokens/transitions/config_update.rs index 48c1409de79..f534b239e79 100644 --- a/packages/rs-sdk/src/platform/tokens/transitions/config_update.rs +++ b/packages/rs-sdk/src/platform/tokens/transitions/config_update.rs @@ -15,12 +15,16 @@ use dpp::state_transition::proof_result::StateTransitionProofResult; /// Result types returned from token configuration update operations. /// /// This enum represents the different possible outcomes when updating token -/// configuration, always returning a document with the updated settings. +/// configuration, depending on whether it's a group action. pub enum ConfigUpdateResult { /// Standard configuration update result containing the updated configuration document. Document(Document), - /// Group-based configuration update action with document and group power information. - GroupActionWithDocument(GroupSumPower, Document), + /// Group-based configuration update action with group power information. + /// + /// The document is `None` while the group action is still active (a proposal, + /// or a co-signature that does not close the action); it is present once the + /// action closes and the update is applied. + GroupActionWithDocument(GroupSumPower, Option), } impl Sdk { @@ -28,8 +32,8 @@ impl Sdk { /// /// This method broadcasts a configuration update transition to modify token /// settings such as minting rules, transfer restrictions, or other contract - /// parameters. The result always includes a document with the updated - /// configuration details. + /// parameters. The result includes a document with the updated + /// configuration details, except for a group action that is still active. /// /// # Arguments /// @@ -47,7 +51,6 @@ impl Sdk { /// - The transition signing fails /// - Broadcasting the transition fails /// - The proof verification returns an unexpected result type - /// - A group action result is missing the expected document pub async fn token_update_contract_token_configuration>( &self, config_update_transition_builder: TokenConfigUpdateTransitionBuilder, @@ -66,22 +69,22 @@ impl Sdk { .broadcast_and_wait::(self, put_settings) .await?; + ConfigUpdateResult::try_from(proof_result) + } +} + +impl TryFrom for ConfigUpdateResult { + type Error = Error; + + fn try_from(proof_result: StateTransitionProofResult) -> Result { match proof_result { StateTransitionProofResult::VerifiedTokenActionWithDocument(document) => { Ok(ConfigUpdateResult::Document(document)) } - StateTransitionProofResult::VerifiedTokenGroupActionWithDocument(power, Some(document)) => { + StateTransitionProofResult::VerifiedTokenGroupActionWithDocument(power, document) => { + // The document is absent while the group action is still active 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" @@ -93,3 +96,69 @@ impl Sdk { } } } + +#[cfg(test)] +mod tests { + use super::*; + use dpp::document::DocumentV0; + + #[test] + fn should_forward_standard_config_update_document() { + let document = Document::V0(DocumentV0 { + id: [9; 32].into(), + ..Default::default() + }); + let result = ConfigUpdateResult::try_from( + StateTransitionProofResult::VerifiedTokenActionWithDocument(document.clone()), + ) + .expect("convert verified standard config update"); + assert!(matches!( + result, + ConfigUpdateResult::Document(forwarded) if forwarded == document + )); + } + + #[test] + fn should_forward_active_group_config_update_without_document() { + // Drive proves a still-active group action (a proposal, or a co-signature + // that does not reach the threshold) with no historical document. + let result = ConfigUpdateResult::try_from( + StateTransitionProofResult::VerifiedTokenGroupActionWithDocument(5, None), + ) + .expect("an active group action is a verified result, not a proof error"); + assert!(matches!( + result, + ConfigUpdateResult::GroupActionWithDocument(5, None) + )); + } + + #[test] + fn should_forward_closed_group_config_update_with_document() { + let result = ConfigUpdateResult::try_from( + StateTransitionProofResult::VerifiedTokenGroupActionWithDocument( + 7, + Some(Document::V0(DocumentV0::default())), + ), + ) + .expect("convert verified closed group action"); + assert!(matches!( + result, + ConfigUpdateResult::GroupActionWithDocument(7, Some(_)) + )); + } + + #[test] + fn should_reject_a_proof_result_of_another_transition() { + let result = ConfigUpdateResult::try_from( + StateTransitionProofResult::VerifiedTokenBalance(Default::default(), 1), + ); + assert!(matches!( + result, + Err(Error::DriveProofError( + drive::error::proof::ProofError::UnexpectedResultProof(_), + _, + _ + )) + )); + } +} diff --git a/packages/wasm-sdk/src/state_transitions/token.rs b/packages/wasm-sdk/src/state_transitions/token.rs index 4818d5c8e56..6315b43295b 100644 --- a/packages/wasm-sdk/src/state_transitions/token.rs +++ b/packages/wasm-sdk/src/state_transitions/token.rs @@ -2607,7 +2607,7 @@ fn deserialize_token_config_update_options( /// /// The result type depends on token configuration: /// - Standard tokens: returns a document -/// - Group-managed tokens: returns group power and document +/// - Group-managed tokens: returns group power, and the document once the group action closes /// /// Check which optional fields are present to determine the result type. #[wasm_bindgen(js_name = "TokenConfigUpdateResult")] @@ -2636,7 +2636,7 @@ impl TokenConfigUpdateResultWasm { ConfigUpdateResult::GroupActionWithDocument(power, doc) => { TokenConfigUpdateResultWasm { group_power: Some(power), - document: Some(document_to_wasm(doc, contract_id, "configUpdate")), + document: doc.map(|d| document_to_wasm(d, contract_id, "configUpdate")), } } } @@ -2721,6 +2721,7 @@ impl WasmSdk { #[cfg(test)] mod tests { use super::*; + use dash_sdk::dpp::document::DocumentV0; use dash_sdk::dpp::group::group_action_status::GroupActionStatus; #[test] @@ -2750,6 +2751,36 @@ mod tests { } } + #[test] + fn should_omit_document_for_active_group_config_update() { + let result = TokenConfigUpdateResultWasm::from_result( + ConfigUpdateResult::GroupActionWithDocument(5, None), + Identifier::from([3; 32]), + ); + assert_eq!(result.group_power, Some(5)); + assert!(result.document.is_none()); + } + + #[test] + fn should_forward_document_for_closed_group_config_update() { + let contract_id = Identifier::from([3; 32]); + let document = Document::V0(DocumentV0 { + id: Identifier::from([9; 32]), + ..Default::default() + }); + let result = TokenConfigUpdateResultWasm::from_result( + ConfigUpdateResult::GroupActionWithDocument(7, Some(document.clone())), + contract_id, + ); + assert_eq!(result.group_power, Some(7)); + let forwarded = result + .document + .expect("closed group action carries the document"); + assert_eq!(forwarded.inner(), &document); + assert_eq!(Identifier::from(forwarded.data_contract_id()), contract_id); + assert_eq!(forwarded.document_type_name(), "configUpdate"); + } + /// An empty tier map is passed through as an empty schedule: rs-dpp's structure /// validation refuses it, not the SDK. #[test]