Skip to content

Commit

Permalink
Remove some try_from_dto_with_params
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Nov 15, 2023
1 parent 36d6985 commit 560ec42
Show file tree
Hide file tree
Showing 17 changed files with 116 additions and 182 deletions.
2 changes: 1 addition & 1 deletion bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
Response::Bech32Address(client.hex_public_key_to_bech32_address(&hex, bech32_hrp).await?)
}
ClientMethod::ComputeMinimumOutputAmount { output } => {
let output = Output::try_from_dto_with_params(output, client.get_token_supply().await?)?;
let output = Output::try_from(output)?;
let storage_score_params = client.get_storage_score_parameters().await?;

Response::OutputAmount(output.minimum_amount(storage_score_params))
Expand Down
4 changes: 2 additions & 2 deletions bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
output,
storage_score_parameters: storage_params,
} => {
let out = Output::try_from_dto(output)?;
let out = Output::try_from(output)?;
Response::OutputAmount(out.minimum_amount(storage_params))
}
UtilsMethod::VerifyMnemonic { mnemonic } => {
Expand Down Expand Up @@ -105,7 +105,7 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
UtilsMethod::OutputIdToUtxoInput { output_id } => Response::Input(UtxoInput::from(output_id)),
UtilsMethod::ComputeSlotCommitmentId { slot_commitment } => Response::SlotCommitmentId(slot_commitment.id()),
UtilsMethod::OutputHexBytes { output } => {
let output = Output::try_from_dto(output)?;
let output = Output::try_from(output)?;
Response::HexBytes(prefix_hex::encode(output.pack_to_vec()))
}
};
Expand Down
6 changes: 2 additions & 4 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,12 +297,11 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
Response::PreparedTransaction(PreparedTransactionDataDto::from(&data))
}
WalletMethod::PrepareTransaction { outputs, options } => {
let token_supply = wallet.client().get_token_supply().await?;
let data = wallet
.prepare_transaction(
outputs
.into_iter()
.map(|o| Ok(Output::try_from_dto_with_params(o, token_supply)?))
.map(|o| Ok(Output::try_from(o)?))
.collect::<Result<Vec<Output>>>()?,
options,
)
Expand Down Expand Up @@ -342,12 +341,11 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM
Response::SentTransaction(TransactionWithMetadataDto::from(&transaction))
}
WalletMethod::SendOutputs { outputs, options } => {
let token_supply = wallet.client().get_token_supply().await?;
let transaction = wallet
.send_outputs(
outputs
.into_iter()
.map(|o| Ok(Output::try_from_dto_with_params(o, token_supply)?))
.map(|o| Ok(Output::try_from(o)?))
.collect::<iota_sdk::wallet::Result<Vec<Output>>>()?,
options,
)
Expand Down
18 changes: 8 additions & 10 deletions sdk/src/client/api/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,13 @@ impl TryFromDto for PreparedTransactionData {
inputs_data: dto
.inputs_data
.into_iter()
.map(|i| InputSigningData::try_from_dto_with_params(i, &params))
.map(|i| InputSigningData::try_from(i))
.collect::<crate::client::Result<Vec<InputSigningData>>>()
.map_err(|_| Error::InvalidField("input_data"))?,
remainder: match dto.remainder {
Some(remainder) => Some(
RemainderData::try_from_dto_with_params(remainder, &params)
.map_err(|_| Error::InvalidField("remainder"))?,
),
Some(remainder) => {
Some(RemainderData::try_from(remainder).map_err(|_| Error::InvalidField("remainder"))?)
}
None => None,
},
})
Expand Down Expand Up @@ -121,7 +120,7 @@ impl TryFromDto for SignedTransactionData {
inputs_data: dto
.inputs_data
.into_iter()
.map(|i| InputSigningData::try_from_dto_with_params(i, &params))
.map(|i| InputSigningData::try_from(i))
.collect::<crate::client::Result<Vec<InputSigningData>>>()
.map_err(|_| Error::InvalidField("inputs_data"))?,
})
Expand Down Expand Up @@ -151,13 +150,12 @@ pub struct RemainderDataDto {
pub address: Address,
}

impl TryFromDto for RemainderData {
type Dto = RemainderDataDto;
impl TryFrom<RemainderDataDto> for RemainderData {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from(dto: RemainderDataDto) -> Result<Self, Self::Error> {
Ok(Self {
output: Output::try_from_dto_with_params_inner(dto.output, params)?,
output: Output::try_from(dto.output)?,
chain: dto.chain,
address: dto.address,
})
Expand Down
7 changes: 3 additions & 4 deletions sdk/src/client/node_api/core/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,10 +226,9 @@ impl ClientInner {
pub async fn get_output(&self, output_id: &OutputId) -> Result<Output> {
let path = &format!("api/core/v3/outputs/{output_id}");

let output = self.get_request::<OutputDto>(path, None, false, true).await?;
let token_supply = self.get_token_supply().await?;

Ok(Output::try_from_dto_with_params(output, token_supply)?)
Ok(Output::try_from(
self.get_request::<OutputDto>(path, None, false, true).await?,
)?)
}

/// Finds an output by its ID and returns it as raw bytes.
Expand Down
12 changes: 4 additions & 8 deletions sdk/src/client/secret/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ use crypto::keys::bip44::Bip44;
use serde::{Deserialize, Serialize};

use crate::{
types::{
block::output::{dto::OutputDto, Output, OutputId, OutputMetadata},
TryFromDto, ValidationParams,
},
types::block::output::{dto::OutputDto, Output, OutputId, OutputMetadata},
utils::serde::bip44::option_bip44,
};

Expand Down Expand Up @@ -167,13 +164,12 @@ pub struct InputSigningDataDto {
pub chain: Option<Bip44>,
}

impl TryFromDto for InputSigningData {
type Dto = InputSigningDataDto;
impl TryFrom<InputSigningDataDto> for InputSigningData {
type Error = crate::client::Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from(dto: InputSigningDataDto) -> Result<Self, Self::Error> {
Ok(Self {
output: Output::try_from_dto_with_params_inner(dto.output, params)?,
output: Output::try_from(dto.output)?,
output_metadata: dto.output_metadata,
chain: dto.chain,
})
Expand Down
33 changes: 13 additions & 20 deletions sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,10 +628,7 @@ pub(crate) mod dto {

use super::*;
use crate::{
types::{
block::{output::unlock_condition::dto::UnlockConditionDto, Error},
TryFromDto, ValidationParams,
},
types::block::{output::unlock_condition::dto::UnlockConditionDto, Error},
utils::serde::string,
};

Expand Down Expand Up @@ -672,11 +669,10 @@ pub(crate) mod dto {
}
}

impl TryFromDto for AccountOutput {
type Dto = AccountOutputDto;
impl TryFrom<AccountOutputDto> for AccountOutput {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from(dto: AccountOutputDto) -> Result<Self, Self::Error> {
let mut builder = AccountOutputBuilder::new_with_amount(dto.amount, dto.account_id)
.with_mana(dto.mana)
.with_foundry_counter(dto.foundry_counter)
Expand Down Expand Up @@ -744,29 +740,26 @@ mod tests {
use pretty_assertions::assert_eq;

use super::*;
use crate::types::{
block::{
output::{dto::OutputDto, FoundryId, SimpleTokenScheme, TokenId},
protocol::protocol_parameters,
rand::{
address::rand_account_address,
output::{
feature::rand_allowed_features, rand_account_id, rand_account_output,
unlock_condition::rand_address_unlock_condition_different_from_account_id,
},
use crate::types::block::{
output::{dto::OutputDto, FoundryId, SimpleTokenScheme, TokenId},
protocol::protocol_parameters,
rand::{
address::rand_account_address,
output::{
feature::rand_allowed_features, rand_account_id, rand_account_output,
unlock_condition::rand_address_unlock_condition_different_from_account_id,
},
},
TryFromDto,
};

#[test]
fn to_from_dto() {
let protocol_parameters = protocol_parameters();
let output = rand_account_output(protocol_parameters.token_supply());
let dto = OutputDto::Account((&output).into());
let output_unver = Output::try_from_dto(dto.clone()).unwrap();
let output_unver = Output::try_from(dto.clone()).unwrap();
assert_eq!(&output, output_unver.as_account());
let output_ver = Output::try_from_dto_with_params(dto, &protocol_parameters).unwrap();
let output_ver = Output::try_from(dto).unwrap();
assert_eq!(&output, output_ver.as_account());

let output_split = AccountOutput::try_from_dtos(
Expand Down
35 changes: 14 additions & 21 deletions sdk/src/types/block/output/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -707,10 +707,7 @@ pub(crate) mod dto {

use super::*;
use crate::{
types::{
block::{output::unlock_condition::dto::UnlockConditionDto, Error},
TryFromDto, ValidationParams,
},
types::block::{output::unlock_condition::dto::UnlockConditionDto, Error},
utils::serde::{prefix_hex_bytes, string},
};

Expand Down Expand Up @@ -754,11 +751,10 @@ pub(crate) mod dto {
}
}

impl TryFromDto for AnchorOutput {
type Dto = AnchorOutputDto;
impl TryFrom<AnchorOutputDto> for AnchorOutput {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from(dto: AnchorOutputDto) -> Result<Self, Self::Error> {
let mut builder = AnchorOutputBuilder::new_with_amount(dto.amount, dto.anchor_id)
.with_mana(dto.mana)
.with_state_index(dto.state_index)
Expand Down Expand Up @@ -827,30 +823,27 @@ pub(crate) mod dto {
#[cfg(test)]
mod tests {
use super::*;
use crate::types::{
block::{
output::dto::OutputDto,
protocol::protocol_parameters,
rand::output::{
feature::rand_allowed_features,
rand_anchor_id, rand_anchor_output,
unlock_condition::{
rand_governor_address_unlock_condition_different_from,
rand_state_controller_address_unlock_condition_different_from,
},
use crate::types::block::{
output::dto::OutputDto,
protocol::protocol_parameters,
rand::output::{
feature::rand_allowed_features,
rand_anchor_id, rand_anchor_output,
unlock_condition::{
rand_governor_address_unlock_condition_different_from,
rand_state_controller_address_unlock_condition_different_from,
},
},
TryFromDto,
};

#[test]
fn to_from_dto() {
let protocol_parameters = protocol_parameters();
let output = rand_anchor_output(protocol_parameters.token_supply());
let dto = OutputDto::Anchor((&output).into());
let output_unver = Output::try_from_dto(dto.clone()).unwrap();
let output_unver = Output::try_from(dto.clone()).unwrap();
assert_eq!(&output, output_unver.as_anchor());
let output_ver = Output::try_from_dto_with_params(dto, &protocol_parameters).unwrap();
let output_ver = Output::try_from(dto).unwrap();
assert_eq!(&output, output_ver.as_anchor());

let output_split = AnchorOutput::try_from_dtos(
Expand Down
31 changes: 12 additions & 19 deletions sdk/src/types/block/output/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,7 @@ pub(crate) mod dto {

use super::*;
use crate::{
types::{
block::{output::unlock_condition::dto::UnlockConditionDto, Error},
TryFromDto, ValidationParams,
},
types::block::{output::unlock_condition::dto::UnlockConditionDto, Error},
utils::serde::string,
};

Expand Down Expand Up @@ -459,11 +456,10 @@ pub(crate) mod dto {
}
}

impl TryFromDto for BasicOutput {
type Dto = BasicOutputDto;
impl TryFrom<BasicOutputDto> for BasicOutput {
type Error = Error;

fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result<Self, Self::Error> {
fn try_from(dto: BasicOutputDto) -> Result<Self, Self::Error> {
let mut builder = BasicOutputBuilder::new_with_amount(dto.amount)
.with_native_tokens(dto.native_tokens)
.with_mana(dto.mana)
Expand Down Expand Up @@ -515,28 +511,25 @@ mod tests {
use pretty_assertions::assert_eq;

use super::*;
use crate::types::{
block::{
output::{dto::OutputDto, FoundryId, SimpleTokenScheme, TokenId},
protocol::protocol_parameters,
rand::{
address::rand_account_address,
output::{
feature::rand_allowed_features, rand_basic_output, unlock_condition::rand_address_unlock_condition,
},
use crate::types::block::{
output::{dto::OutputDto, FoundryId, SimpleTokenScheme, TokenId},
protocol::protocol_parameters,
rand::{
address::rand_account_address,
output::{
feature::rand_allowed_features, rand_basic_output, unlock_condition::rand_address_unlock_condition,
},
},
TryFromDto,
};

#[test]
fn to_from_dto() {
let protocol_parameters = protocol_parameters();
let output = rand_basic_output(protocol_parameters.token_supply());
let dto = OutputDto::Basic((&output).into());
let output_unver = Output::try_from_dto(dto.clone()).unwrap();
let output_unver = Output::try_from(dto.clone()).unwrap();
assert_eq!(&output, output_unver.as_basic());
let output_ver = Output::try_from_dto_with_params(dto, &protocol_parameters).unwrap();
let output_ver = Output::try_from(dto).unwrap();
assert_eq!(&output, output_ver.as_basic());

let output_split = BasicOutput::try_from_dtos(
Expand Down
17 changes: 5 additions & 12 deletions sdk/src/types/block/output/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,12 +434,9 @@ pub(crate) mod dto {

use super::*;
use crate::{
types::{
block::{
output::{unlock_condition::dto::UnlockConditionDto, OutputBuilderAmount},
Error,
},
TryFromDto,
types::block::{
output::{unlock_condition::dto::UnlockConditionDto, OutputBuilderAmount},
Error,
},
utils::serde::string,
};
Expand Down Expand Up @@ -475,14 +472,10 @@ pub(crate) mod dto {
}
}

impl TryFromDto for DelegationOutput {
type Dto = DelegationOutputDto;
impl TryFrom<DelegationOutputDto> for DelegationOutput {
type Error = Error;

fn try_from_dto_with_params_inner(
dto: Self::Dto,
_params: crate::types::ValidationParams<'_>,
) -> Result<Self, Self::Error> {
fn try_from(dto: DelegationOutputDto) -> Result<Self, Self::Error> {
let mut builder = DelegationOutputBuilder::new_with_amount(
dto.amount,
dto.delegated_amount,
Expand Down
Loading

0 comments on commit 560ec42

Please sign in to comment.