Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 85 additions & 16 deletions packages/rs-sdk/src/platform/tokens/transitions/config_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,25 @@ 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<Document>),
}

impl Sdk {
/// Updates the configuration of a token contract.
///
/// 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
///
Expand All @@ -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<S: Signer<IdentityPublicKey>>(
&self,
config_update_transition_builder: TokenConfigUpdateTransitionBuilder,
Expand All @@ -66,22 +69,22 @@ impl Sdk {
.broadcast_and_wait::<StateTransitionProofResult>(self, put_settings)
.await?;

ConfigUpdateResult::try_from(proof_result)
}
}

impl TryFrom<StateTransitionProofResult> for ConfigUpdateResult {
type Error = Error;

fn try_from(proof_result: StateTransitionProofResult) -> Result<Self, Self::Error> {
match proof_result {
StateTransitionProofResult::VerifiedTokenActionWithDocument(document) => {
Ok(ConfigUpdateResult::Document(document))
}
Comment thread
Claudius-Maginificent marked this conversation as resolved.
Resolved
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"
Expand All @@ -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(_),
_,
_
))
));
}
}
35 changes: 33 additions & 2 deletions packages/wasm-sdk/src/state_transitions/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down Expand Up @@ -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")),
}
Comment thread
Claudius-Maginificent marked this conversation as resolved.
Resolved
}
}
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand Down
Loading