diff --git a/packages/rs-sdk/src/platform/transition/fungible_tokens/config_update.rs b/packages/rs-sdk/src/platform/transition/fungible_tokens/config_update.rs new file mode 100644 index 00000000000..a455511ce32 --- /dev/null +++ b/packages/rs-sdk/src/platform/transition/fungible_tokens/config_update.rs @@ -0,0 +1,159 @@ +use crate::platform::transition::put_settings::PutSettings; +use crate::platform::Identifier; +use crate::{Error, Sdk}; +use dpp::data_contract::accessors::v0::DataContractV0Getters; +use dpp::data_contract::associated_token::token_configuration_item::TokenConfigurationChangeItem; +use dpp::data_contract::{DataContract, TokenContractPosition}; +use dpp::group::GroupStateTransitionInfoStatus; +use dpp::identity::signer::Signer; +use dpp::identity::IdentityPublicKey; +use dpp::prelude::UserFeeIncrease; +use dpp::state_transition::batch_transition::methods::v1::DocumentsBatchTransitionMethodsV1; +use dpp::state_transition::batch_transition::BatchTransition; +use dpp::state_transition::StateTransition; +use dpp::tokens::calculate_token_id; +use dpp::version::PlatformVersion; + +/// A builder to configure and broadcast token config_update transitions +pub struct TokenConfigUpdateTransitionBuilder<'a> { + data_contract: &'a DataContract, + token_position: TokenContractPosition, + owner_id: Identifier, + update_token_configuration_item: TokenConfigurationChangeItem, + public_note: Option, + using_group_info: Option, + settings: Option, + user_fee_increase: Option, +} + +impl<'a> TokenConfigUpdateTransitionBuilder<'a> { + /// Start building a config_update tokens transition for the provided DataContract. + /// + /// # Arguments + /// + /// * `data_contract` - A reference to the data contract + /// * `token_position` - The position of the token in the contract + /// * `owner_id` - The identifier of the state transition owner + /// * `update_token_configuration_item` - The token configuration change item + /// * `using_group_info` - Group transition info status + /// + /// # Returns + /// + /// * `Self` - The new builder instance + pub fn new( + data_contract: &'a DataContract, + token_position: TokenContractPosition, + owner_id: Identifier, + update_token_configuration_item: TokenConfigurationChangeItem, + using_group_info: Option, + ) -> Self { + // TODO: Validate token position + + Self { + data_contract, + token_position, + owner_id, + update_token_configuration_item, + public_note: None, + using_group_info, + settings: None, + user_fee_increase: None, + } + } + + /// Adds a public note to the token config_update transition + /// + /// # Arguments + /// + /// * `note` - The public note to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_public_note(mut self, note: String) -> Self { + self.public_note = Some(note); + self + } + + /// Adds a user fee increase to the token config_update transition + /// + /// # Arguments + /// + /// * `user_fee_increase` - The user fee increase to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_user_fee_increase(mut self, user_fee_increase: UserFeeIncrease) -> Self { + self.user_fee_increase = Some(user_fee_increase); + self + } + + /// Adds settings to the token config_update transition + /// + /// # Arguments + /// + /// * `settings` - The settings to add + /// + /// # Returns + /// + /// * `Self` - The updated builder + pub fn with_settings(mut self, settings: PutSettings) -> Self { + self.settings = Some(settings); + self + } + + /// Signs the token config_update transition + /// + /// # Arguments + /// + /// * `sdk` - The SDK instance + /// * `identity_public_key` - The public key of the identity + /// * `signer` - The signer instance + /// * `platform_version` - The platform version + /// + /// # Returns + /// + /// * `Result` - The signed state transition or an error + pub async fn sign( + &self, + sdk: &Sdk, + identity_public_key: &IdentityPublicKey, + signer: &impl Signer, + platform_version: &PlatformVersion, + ) -> Result { + let token_id = Identifier::from(calculate_token_id( + self.data_contract.id().as_bytes(), + self.token_position, + )); + + let identity_contract_nonce = sdk + .get_identity_contract_nonce( + self.owner_id, + self.data_contract.id(), + true, + self.settings, + ) + .await?; + + let state_transition = BatchTransition::new_token_config_update_transition( + token_id, + self.owner_id, + self.data_contract.id(), + self.token_position, + self.update_token_configuration_item.clone(), + self.public_note.clone(), + self.using_group_info, + identity_public_key, + identity_contract_nonce, + self.user_fee_increase.unwrap_or_default(), + signer, + platform_version, + None, + None, + None, + )?; + + Ok(state_transition) + } +} diff --git a/packages/rs-sdk/src/platform/transition/fungible_tokens/mod.rs b/packages/rs-sdk/src/platform/transition/fungible_tokens/mod.rs index babb7fdfcdd..71c24371ce2 100644 --- a/packages/rs-sdk/src/platform/transition/fungible_tokens/mod.rs +++ b/packages/rs-sdk/src/platform/transition/fungible_tokens/mod.rs @@ -2,6 +2,7 @@ pub mod burn; pub mod claim; +pub mod config_update; pub mod destroy; pub mod emergency_action; pub mod freeze;