Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove try_from_dtos #1817

Merged
merged 4 commits into from
Jan 9, 2024
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
114 changes: 62 additions & 52 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -64,40 +64,43 @@ 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,
mana,
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,
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion sdk/src/types/block/address/multi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
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);
}
}
Loading