From 4ceb1c45d1a34f1e08718d5009a0f51c711ea6ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thoralf=20M=C3=BCller?= Date: Tue, 9 Jan 2024 14:39:19 +0100 Subject: [PATCH] Remove try_from_dtos() --- sdk/src/types/block/output/account.rs | 83 ------------------------ sdk/src/types/block/output/anchor.rs | 83 ------------------------ sdk/src/types/block/output/basic.rs | 62 ------------------ sdk/src/types/block/output/delegation.rs | 38 ----------- sdk/src/types/block/output/foundry.rs | 83 +----------------------- sdk/src/types/block/output/nft.rs | 79 +--------------------- 6 files changed, 2 insertions(+), 426 deletions(-) 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..2b289e91e2 100644 --- a/sdk/src/types/block/output/delegation.rs +++ b/sdk/src/types/block/output/delegation.rs @@ -459,43 +459,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); } }