diff --git a/bindings/core/src/method_handler/client.rs b/bindings/core/src/method_handler/client.rs index b5f2ae738f..ca809129f5 100644 --- a/bindings/core/src/method_handler/client.rs +++ b/bindings/core/src/method_handler/client.rs @@ -9,7 +9,7 @@ use iota_sdk::{ api::core::OutputWithMetadataResponse, block::{ output::{ - AccountOutput, BasicOutput, FoundryOutput, MinimumOutputAmount, NftOutput, Output, OutputBuilderAmount, + AccountOutputBuilder, BasicOutputBuilder, FoundryOutputBuilder, MinimumOutputAmount, NftOutputBuilder, }, payload::Payload, Block, BlockDto, UnsignedBlockDto, @@ -64,21 +64,23 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM features, immutable_features, } => { - let output = Output::from(AccountOutput::try_from_dtos( - if let Some(amount) = amount { - OutputBuilderAmount::Amount(amount) - } else { - OutputBuilderAmount::MinimumAmount(client.get_storage_score_parameters().await?) - }, - mana, - &account_id, - foundry_counter, - unlock_conditions, - features, - immutable_features, - )?); + let mut output_builder = if let Some(amount) = amount { + AccountOutputBuilder::new_with_amount(amount, account_id) + } else { + AccountOutputBuilder::new_with_minimum_amount(client.get_storage_score_parameters().await?, account_id) + } + .with_mana(mana) + .with_foundry_counter(foundry_counter) + .with_unlock_conditions(unlock_conditions); + + if let Some(features) = features { + output_builder = output_builder.with_features(features); + } + if let Some(immutable_features) = immutable_features { + output_builder = output_builder.with_immutable_features(immutable_features) + } - Response::Output(output) + Response::Output(output_builder.finish_output()?) } ClientMethod::BuildBasicOutput { amount, @@ -86,18 +88,19 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM unlock_conditions, features, } => { - let output = Output::from(BasicOutput::try_from_dtos( - if let Some(amount) = amount { - OutputBuilderAmount::Amount(amount) - } else { - OutputBuilderAmount::MinimumAmount(client.get_storage_score_parameters().await?) - }, - mana, - unlock_conditions, - features, - )?); + let mut output_builder = if let Some(amount) = amount { + BasicOutputBuilder::new_with_amount(amount) + } else { + BasicOutputBuilder::new_with_minimum_amount(client.get_storage_score_parameters().await?) + } + .with_mana(mana) + .with_unlock_conditions(unlock_conditions); - Response::Output(output) + if let Some(features) = features { + output_builder = output_builder.with_features(features); + } + + Response::Output(output_builder.finish_output()?) } ClientMethod::BuildFoundryOutput { amount, @@ -107,20 +110,25 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM features, immutable_features, } => { - let output = Output::from(FoundryOutput::try_from_dtos( - if let Some(amount) = amount { - OutputBuilderAmount::Amount(amount) - } else { - OutputBuilderAmount::MinimumAmount(client.get_storage_score_parameters().await?) - }, - serial_number, - token_scheme, - unlock_conditions, - features, - immutable_features, - )?); + let mut output_builder = if let Some(amount) = amount { + FoundryOutputBuilder::new_with_amount(amount, serial_number, token_scheme) + } else { + FoundryOutputBuilder::new_with_minimum_amount( + client.get_storage_score_parameters().await?, + serial_number, + token_scheme, + ) + } + .with_unlock_conditions(unlock_conditions); - Response::Output(output) + if let Some(features) = features { + output_builder = output_builder.with_features(features); + } + if let Some(immutable_features) = immutable_features { + output_builder = output_builder.with_immutable_features(immutable_features) + } + + Response::Output(output_builder.finish_output()?) } ClientMethod::BuildNftOutput { amount, @@ -130,20 +138,22 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM features, immutable_features, } => { - let output = Output::from(NftOutput::try_from_dtos( - if let Some(amount) = amount { - OutputBuilderAmount::Amount(amount) - } else { - OutputBuilderAmount::MinimumAmount(client.get_storage_score_parameters().await?) - }, - mana, - &nft_id, - unlock_conditions, - features, - immutable_features, - )?); + let mut output_builder = if let Some(amount) = amount { + NftOutputBuilder::new_with_amount(amount, nft_id) + } else { + NftOutputBuilder::new_with_minimum_amount(client.get_storage_score_parameters().await?, nft_id) + } + .with_mana(mana) + .with_unlock_conditions(unlock_conditions); + + if let Some(features) = features { + output_builder = output_builder.with_features(features); + } + if let Some(immutable_features) = immutable_features { + output_builder = output_builder.with_immutable_features(immutable_features) + } - Response::Output(output) + Response::Output(output_builder.finish_output()?) } ClientMethod::BuildBasicBlock { issuer_id, payload } => { let payload = if let Some(payload) = payload { diff --git a/sdk/src/types/block/address/multi.rs b/sdk/src/types/block/address/multi.rs index 377a11cce2..593f300598 100644 --- a/sdk/src/types/block/address/multi.rs +++ b/sdk/src/types/block/address/multi.rs @@ -128,7 +128,7 @@ impl MultiAddress { pub fn hash(&self) -> [u8; 32] { let mut digest = Blake2b256::new(); - digest.update([MultiAddress::KIND]); + digest.update([Self::KIND]); digest.update(self.pack_to_vec()); digest.finalize().into() diff --git a/sdk/src/types/block/output/account.rs b/sdk/src/types/block/output/account.rs index 942693651c..04f601d233 100644 --- a/sdk/src/types/block/output/account.rs +++ b/sdk/src/types/block/output/account.rs @@ -602,47 +602,6 @@ mod dto { } } - impl AccountOutput { - #[allow(clippy::too_many_arguments)] - pub fn try_from_dtos( - amount: OutputBuilderAmount, - mana: u64, - account_id: &AccountId, - foundry_counter: Option, - unlock_conditions: Vec, - features: Option>, - immutable_features: Option>, - ) -> Result { - let mut builder = match amount { - OutputBuilderAmount::Amount(amount) => AccountOutputBuilder::new_with_amount(amount, *account_id), - OutputBuilderAmount::MinimumAmount(params) => { - AccountOutputBuilder::new_with_minimum_amount(params, *account_id) - } - } - .with_mana(mana); - - if let Some(foundry_counter) = foundry_counter { - builder = builder.with_foundry_counter(foundry_counter); - } - - let unlock_conditions = unlock_conditions - .into_iter() - .map(UnlockCondition::from) - .collect::>(); - builder = builder.with_unlock_conditions(unlock_conditions); - - if let Some(features) = features { - builder = builder.with_features(features); - } - - if let Some(immutable_features) = immutable_features { - builder = builder.with_immutable_features(immutable_features); - } - - builder.finish() - } - } - crate::impl_serde_typed_dto!(AccountOutput, AccountOutputDto, "account output"); } @@ -667,47 +626,5 @@ mod tests { let dto = AccountOutputDto::from(&account_output); let output = Output::Account(AccountOutput::try_from(dto).unwrap()); assert_eq!(&account_output, output.as_account()); - - let output_split = AccountOutput::try_from_dtos( - OutputBuilderAmount::Amount(account_output.amount()), - account_output.mana(), - account_output.account_id(), - account_output.foundry_counter().into(), - account_output.unlock_conditions().to_vec(), - Some(account_output.features().to_vec()), - Some(account_output.immutable_features().to_vec()), - ) - .unwrap(); - assert_eq!(account_output, output_split); - - let account_id = rand_account_id(); - let address = rand_address_unlock_condition_different_from_account_id(&account_id); - - let test_split_dto = |builder: AccountOutputBuilder| { - let output_split = AccountOutput::try_from_dtos( - builder.amount, - builder.mana, - &builder.account_id, - builder.foundry_counter, - builder.unlock_conditions.iter().cloned().collect(), - Some(builder.features.iter().cloned().collect()), - Some(builder.immutable_features.iter().cloned().collect()), - ) - .unwrap(); - assert_eq!(builder.finish().unwrap(), output_split); - }; - - let builder = AccountOutput::build_with_amount(100, account_id) - .add_unlock_condition(address.clone()) - .with_features(rand_allowed_features(AccountOutput::ALLOWED_FEATURES)) - .with_immutable_features(rand_allowed_features(AccountOutput::ALLOWED_IMMUTABLE_FEATURES)); - test_split_dto(builder); - - let builder = - AccountOutput::build_with_minimum_amount(protocol_parameters.storage_score_parameters(), account_id) - .add_unlock_condition(address) - .with_features(rand_allowed_features(AccountOutput::ALLOWED_FEATURES)) - .with_immutable_features(rand_allowed_features(AccountOutput::ALLOWED_IMMUTABLE_FEATURES)); - test_split_dto(builder); } } diff --git a/sdk/src/types/block/output/anchor.rs b/sdk/src/types/block/output/anchor.rs index a2d2bcef20..6f5c926934 100644 --- a/sdk/src/types/block/output/anchor.rs +++ b/sdk/src/types/block/output/anchor.rs @@ -668,44 +668,6 @@ mod dto { } } - impl AnchorOutput { - #[allow(clippy::too_many_arguments)] - pub fn try_from_dtos( - amount: OutputBuilderAmount, - mana: u64, - anchor_id: &AnchorId, - state_index: u32, - unlock_conditions: Vec, - features: Option>, - immutable_features: Option>, - ) -> Result { - let mut builder = match amount { - OutputBuilderAmount::Amount(amount) => AnchorOutputBuilder::new_with_amount(amount, *anchor_id), - OutputBuilderAmount::MinimumAmount(params) => { - AnchorOutputBuilder::new_with_minimum_amount(params, *anchor_id) - } - } - .with_mana(mana) - .with_state_index(state_index); - - let unlock_conditions = unlock_conditions - .into_iter() - .map(UnlockCondition::from) - .collect::>(); - builder = builder.with_unlock_conditions(unlock_conditions); - - if let Some(features) = features { - builder = builder.with_features(features); - } - - if let Some(immutable_features) = immutable_features { - builder = builder.with_immutable_features(immutable_features); - } - - builder.finish() - } - } - crate::impl_serde_typed_dto!(AnchorOutput, AnchorOutputDto, "anchor output"); } @@ -732,50 +694,5 @@ mod tests { let dto = AnchorOutputDto::from(&anchor_output); let output = Output::Anchor(AnchorOutput::try_from(dto).unwrap()); assert_eq!(&anchor_output, output.as_anchor()); - - let output_split = AnchorOutput::try_from_dtos( - OutputBuilderAmount::Amount(output.amount()), - anchor_output.mana(), - anchor_output.anchor_id(), - anchor_output.state_index(), - anchor_output.unlock_conditions().to_vec(), - Some(anchor_output.features().to_vec()), - Some(anchor_output.immutable_features().to_vec()), - ) - .unwrap(); - assert_eq!(anchor_output, output_split); - - let anchor_id = rand_anchor_id(); - let gov_address = rand_governor_address_unlock_condition_different_from(&anchor_id); - let state_address = rand_state_controller_address_unlock_condition_different_from(&anchor_id); - - let test_split_dto = |builder: AnchorOutputBuilder| { - let output_split = AnchorOutput::try_from_dtos( - builder.amount, - builder.mana, - &builder.anchor_id, - builder.state_index, - builder.unlock_conditions.iter().cloned().collect(), - Some(builder.features.iter().cloned().collect()), - Some(builder.immutable_features.iter().cloned().collect()), - ) - .unwrap(); - assert_eq!(builder.finish().unwrap(), output_split); - }; - - let builder = AnchorOutput::build_with_amount(100, anchor_id) - .add_unlock_condition(gov_address.clone()) - .add_unlock_condition(state_address.clone()) - .with_features(rand_allowed_features(AnchorOutput::ALLOWED_FEATURES)) - .with_immutable_features(rand_allowed_features(AnchorOutput::ALLOWED_IMMUTABLE_FEATURES)); - test_split_dto(builder); - - let builder = - AnchorOutput::build_with_minimum_amount(protocol_parameters.storage_score_parameters(), anchor_id) - .add_unlock_condition(gov_address) - .add_unlock_condition(state_address) - .with_features(rand_allowed_features(AnchorOutput::ALLOWED_FEATURES)) - .with_immutable_features(rand_allowed_features(AnchorOutput::ALLOWED_IMMUTABLE_FEATURES)); - test_split_dto(builder); } } diff --git a/sdk/src/types/block/output/basic.rs b/sdk/src/types/block/output/basic.rs index 6b07bd0f21..9b31c18782 100644 --- a/sdk/src/types/block/output/basic.rs +++ b/sdk/src/types/block/output/basic.rs @@ -446,33 +446,6 @@ mod dto { } } - impl BasicOutput { - pub fn try_from_dtos( - amount: OutputBuilderAmount, - mana: u64, - unlock_conditions: Vec, - features: Option>, - ) -> Result { - let mut builder = match amount { - OutputBuilderAmount::Amount(amount) => BasicOutputBuilder::new_with_amount(amount), - OutputBuilderAmount::MinimumAmount(params) => BasicOutputBuilder::new_with_minimum_amount(params), - } - .with_mana(mana); - - let unlock_conditions = unlock_conditions - .into_iter() - .map(UnlockCondition::from) - .collect::>(); - builder = builder.with_unlock_conditions(unlock_conditions); - - if let Some(features) = features { - builder = builder.with_features(features); - } - - builder.finish() - } - } - crate::impl_serde_typed_dto!(BasicOutput, BasicOutputDto, "basic output"); } @@ -499,41 +472,6 @@ mod tests { let dto = BasicOutputDto::from(&basic_output); let output = Output::Basic(BasicOutput::try_from(dto).unwrap()); assert_eq!(&basic_output, output.as_basic()); - - let output_split = BasicOutput::try_from_dtos( - OutputBuilderAmount::Amount(basic_output.amount()), - basic_output.mana(), - basic_output.unlock_conditions().to_vec(), - Some(basic_output.features().to_vec()), - ) - .unwrap(); - assert_eq!(basic_output, output_split); - - let foundry_id = FoundryId::build(&rand_account_address(), 0, SimpleTokenScheme::KIND); - let address = rand_address_unlock_condition(); - - let test_split_dto = |builder: BasicOutputBuilder| { - let output_split = BasicOutput::try_from_dtos( - builder.amount, - builder.mana, - builder.unlock_conditions.iter().cloned().collect(), - Some(builder.features.iter().cloned().collect()), - ) - .unwrap(); - assert_eq!(builder.finish().unwrap(), output_split); - }; - - let builder = BasicOutput::build_with_amount(100) - .with_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) - .add_unlock_condition(address.clone()) - .with_features(rand_allowed_features(BasicOutput::ALLOWED_FEATURES)); - test_split_dto(builder); - - let builder = BasicOutput::build_with_minimum_amount(protocol_parameters.storage_score_parameters()) - .with_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) - .add_unlock_condition(address) - .with_features(rand_allowed_features(BasicOutput::ALLOWED_FEATURES)); - test_split_dto(builder); } // TODO: re-enable when rent is figured out diff --git a/sdk/src/types/block/output/delegation.rs b/sdk/src/types/block/output/delegation.rs index 00c909c919..e3d8b9ffd9 100644 --- a/sdk/src/types/block/output/delegation.rs +++ b/sdk/src/types/block/output/delegation.rs @@ -400,10 +400,7 @@ mod dto { use super::*; use crate::{ - types::block::{ - output::{unlock_condition::UnlockCondition, OutputBuilderAmount}, - Error, - }, + types::block::{output::unlock_condition::UnlockCondition, Error}, utils::serde::string, }; @@ -459,43 +456,5 @@ mod dto { } } - impl DelegationOutput { - #[allow(clippy::too_many_arguments)] - pub fn try_from_dtos( - amount: OutputBuilderAmount, - delegated_amount: u64, - delegation_id: &DelegationId, - validator_address: &AccountAddress, - start_epoch: impl Into, - end_epoch: impl Into, - unlock_conditions: Vec, - ) -> Result { - let mut builder = match amount { - OutputBuilderAmount::Amount(amount) => DelegationOutputBuilder::new_with_amount( - amount, - delegated_amount, - *delegation_id, - *validator_address, - ), - OutputBuilderAmount::MinimumAmount(params) => DelegationOutputBuilder::new_with_minimum_amount( - params, - delegated_amount, - *delegation_id, - *validator_address, - ), - } - .with_start_epoch(start_epoch) - .with_end_epoch(end_epoch); - - let unlock_conditions = unlock_conditions - .into_iter() - .map(UnlockCondition::from) - .collect::>(); - builder = builder.with_unlock_conditions(unlock_conditions); - - builder.finish() - } - } - crate::impl_serde_typed_dto!(DelegationOutput, DelegationOutputDto, "delegation output"); } diff --git a/sdk/src/types/block/output/foundry.rs b/sdk/src/types/block/output/foundry.rs index ccc61cdbcc..66fa6789fd 100644 --- a/sdk/src/types/block/output/foundry.rs +++ b/sdk/src/types/block/output/foundry.rs @@ -638,43 +638,6 @@ mod dto { } } - impl FoundryOutput { - #[allow(clippy::too_many_arguments)] - pub fn try_from_dtos( - amount: OutputBuilderAmount, - serial_number: u32, - token_scheme: TokenScheme, - unlock_conditions: Vec, - features: Option>, - immutable_features: Option>, - ) -> Result { - let mut builder = match amount { - OutputBuilderAmount::Amount(amount) => { - FoundryOutputBuilder::new_with_amount(amount, serial_number, token_scheme) - } - OutputBuilderAmount::MinimumAmount(params) => { - FoundryOutputBuilder::new_with_minimum_amount(params, serial_number, token_scheme) - } - }; - - let unlock_conditions = unlock_conditions - .into_iter() - .map(UnlockCondition::from) - .collect::>(); - builder = builder.with_unlock_conditions(unlock_conditions); - - if let Some(features) = features { - builder = builder.with_features(features); - } - - if let Some(immutable_features) = immutable_features { - builder = builder.with_immutable_features(immutable_features); - } - - builder.finish() - } - } - crate::impl_serde_typed_dto!(FoundryOutput, FoundryOutputDto, "foundry output"); } @@ -684,18 +647,7 @@ mod tests { use super::*; use crate::types::block::{ - output::{ - foundry::dto::FoundryOutputDto, unlock_condition::ImmutableAccountAddressUnlockCondition, FoundryId, - SimpleTokenScheme, TokenId, - }, - protocol::protocol_parameters, - rand::{ - address::rand_account_address, - output::{ - feature::{rand_allowed_features, rand_metadata_feature}, - rand_foundry_output, rand_token_scheme, - }, - }, + output::foundry::dto::FoundryOutputDto, protocol::protocol_parameters, rand::output::rand_foundry_output, }; #[test] @@ -705,38 +657,5 @@ mod tests { let dto = FoundryOutputDto::from(&foundry_output); let output = Output::Foundry(FoundryOutput::try_from(dto).unwrap()); assert_eq!(&foundry_output, output.as_foundry()); - - let foundry_id = FoundryId::build(&rand_account_address(), 0, SimpleTokenScheme::KIND); - - let test_split_dto = |builder: FoundryOutputBuilder| { - let output_split = FoundryOutput::try_from_dtos( - builder.amount, - builder.serial_number, - builder.token_scheme.clone(), - builder.unlock_conditions.iter().cloned().collect(), - Some(builder.features.iter().cloned().collect()), - Some(builder.immutable_features.iter().cloned().collect()), - ) - .unwrap(); - assert_eq!(builder.finish().unwrap(), output_split); - }; - - let builder = FoundryOutput::build_with_amount(100, 123, rand_token_scheme()) - .with_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) - .add_unlock_condition(ImmutableAccountAddressUnlockCondition::new(rand_account_address())) - .add_immutable_feature(rand_metadata_feature()) - .with_features(rand_allowed_features(FoundryOutput::ALLOWED_FEATURES)); - test_split_dto(builder); - - let builder = FoundryOutput::build_with_minimum_amount( - protocol_parameters.storage_score_parameters(), - 123, - rand_token_scheme(), - ) - .with_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap()) - .add_unlock_condition(ImmutableAccountAddressUnlockCondition::new(rand_account_address())) - .add_immutable_feature(rand_metadata_feature()) - .with_features(rand_allowed_features(FoundryOutput::ALLOWED_FEATURES)); - test_split_dto(builder); } } diff --git a/sdk/src/types/block/output/nft.rs b/sdk/src/types/block/output/nft.rs index 0e5930b880..9636399d5c 100644 --- a/sdk/src/types/block/output/nft.rs +++ b/sdk/src/types/block/output/nft.rs @@ -564,42 +564,6 @@ mod dto { } } - impl NftOutput { - #[allow(clippy::too_many_arguments)] - pub fn try_from_dtos( - amount: OutputBuilderAmount, - mana: u64, - nft_id: &NftId, - unlock_conditions: Vec, - features: Option>, - immutable_features: Option>, - ) -> Result { - let mut builder = match amount { - OutputBuilderAmount::Amount(amount) => NftOutputBuilder::new_with_amount(amount, *nft_id), - OutputBuilderAmount::MinimumAmount(params) => { - NftOutputBuilder::new_with_minimum_amount(params, *nft_id) - } - } - .with_mana(mana); - - let unlock_conditions = unlock_conditions - .into_iter() - .map(UnlockCondition::from) - .collect::>(); - builder = builder.with_unlock_conditions(unlock_conditions); - - if let Some(features) = features { - builder = builder.with_features(features); - } - - if let Some(immutable_features) = immutable_features { - builder = builder.with_immutable_features(immutable_features); - } - - builder.finish() - } - } - crate::impl_serde_typed_dto!(NftOutput, NftOutputDto, "nft output"); } @@ -609,11 +573,7 @@ mod tests { use super::*; use crate::types::block::{ - output::nft::dto::NftOutputDto, - protocol::protocol_parameters, - rand::output::{ - feature::rand_allowed_features, rand_nft_output, unlock_condition::rand_address_unlock_condition, - }, + output::nft::dto::NftOutputDto, protocol::protocol_parameters, rand::output::rand_nft_output, }; #[test] @@ -623,42 +583,5 @@ mod tests { let dto = NftOutputDto::from(&nft_output); let output = Output::Nft(NftOutput::try_from(dto).unwrap()); assert_eq!(&nft_output, output.as_nft()); - - let output_split = NftOutput::try_from_dtos( - OutputBuilderAmount::Amount(nft_output.amount()), - nft_output.mana(), - nft_output.nft_id(), - nft_output.unlock_conditions().to_vec(), - Some(nft_output.features().to_vec()), - Some(nft_output.immutable_features().to_vec()), - ) - .unwrap(); - assert_eq!(nft_output, output_split); - - let test_split_dto = |builder: NftOutputBuilder| { - let output_split = NftOutput::try_from_dtos( - builder.amount, - builder.mana, - &builder.nft_id, - builder.unlock_conditions.iter().cloned().collect(), - Some(builder.features.iter().cloned().collect()), - Some(builder.immutable_features.iter().cloned().collect()), - ) - .unwrap(); - assert_eq!(builder.finish().unwrap(), output_split); - }; - - let builder = NftOutput::build_with_amount(100, NftId::null()) - .add_unlock_condition(rand_address_unlock_condition()) - .with_features(rand_allowed_features(NftOutput::ALLOWED_FEATURES)) - .with_immutable_features(rand_allowed_features(NftOutput::ALLOWED_IMMUTABLE_FEATURES)); - test_split_dto(builder); - - let builder = - NftOutput::build_with_minimum_amount(protocol_parameters.storage_score_parameters(), NftId::null()) - .add_unlock_condition(rand_address_unlock_condition()) - .with_features(rand_allowed_features(NftOutput::ALLOWED_FEATURES)) - .with_immutable_features(rand_allowed_features(NftOutput::ALLOWED_IMMUTABLE_FEATURES)); - test_split_dto(builder); } }