Skip to content

Commit

Permalink
Remove try_from_dtos()
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M committed Jan 9, 2024
1 parent 35fd64d commit 4ceb1c4
Show file tree
Hide file tree
Showing 6 changed files with 2 additions and 426 deletions.
83 changes: 0 additions & 83 deletions sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32>,
unlock_conditions: Vec<UnlockCondition>,
features: Option<Vec<Feature>>,
immutable_features: Option<Vec<Feature>>,
) -> Result<Self, Error> {
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::<Vec<UnlockCondition>>();
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");
}

Expand All @@ -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);
}
}
83 changes: 0 additions & 83 deletions sdk/src/types/block/output/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnlockCondition>,
features: Option<Vec<Feature>>,
immutable_features: Option<Vec<Feature>>,
) -> Result<Self, Error> {
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::<Vec<UnlockCondition>>();
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");
}

Expand All @@ -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);
}
}
62 changes: 0 additions & 62 deletions sdk/src/types/block/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,33 +446,6 @@ mod dto {
}
}

impl BasicOutput {
pub fn try_from_dtos(
amount: OutputBuilderAmount,
mana: u64,
unlock_conditions: Vec<UnlockCondition>,
features: Option<Vec<Feature>>,
) -> Result<Self, Error> {
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::<Vec<UnlockCondition>>();
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");
}

Expand All @@ -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
Expand Down
38 changes: 0 additions & 38 deletions sdk/src/types/block/output/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<EpochIndex>,
end_epoch: impl Into<EpochIndex>,
unlock_conditions: Vec<UnlockCondition>,
) -> Result<Self, Error> {
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::<Vec<UnlockCondition>>();
builder = builder.with_unlock_conditions(unlock_conditions);

builder.finish()
}
}

crate::impl_serde_typed_dto!(DelegationOutput, DelegationOutputDto, "delegation output");
}
Loading

0 comments on commit 4ceb1c4

Please sign in to comment.