From 6884d912b7444453f889ae7614c1ba6c22aa7702 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 15 Nov 2023 10:12:35 +0100 Subject: [PATCH 1/5] Remove SDRUC supply check --- bindings/core/src/method_handler/client.rs | 4 -- .../client/output/build_basic_output.rs | 16 +---- .../how_tos/outputs/unlock_conditions.rs | 7 +- sdk/src/types/block/output/account.rs | 12 ++-- sdk/src/types/block/output/anchor.rs | 12 ++-- sdk/src/types/block/output/basic.rs | 23 ++----- sdk/src/types/block/output/delegation.rs | 12 ++-- sdk/src/types/block/output/foundry.rs | 12 ++-- sdk/src/types/block/output/mod.rs | 22 +++--- sdk/src/types/block/output/nft.rs | 23 ++----- .../block/output/unlock_condition/mod.rs | 21 ++---- .../storage_deposit_return.rs | 69 +++++-------------- .../operations/transaction/high_level/send.rs | 3 +- .../high_level/send_native_tokens.rs | 3 +- .../operations/transaction/prepare_output.rs | 10 +-- sdk/tests/client/mod.rs | 7 +- 16 files changed, 76 insertions(+), 180 deletions(-) diff --git a/bindings/core/src/method_handler/client.rs b/bindings/core/src/method_handler/client.rs index e1be21057c..a5982664fe 100644 --- a/bindings/core/src/method_handler/client.rs +++ b/bindings/core/src/method_handler/client.rs @@ -79,7 +79,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM unlock_conditions, features, immutable_features, - client.get_token_supply().await?, )?); Response::Output(OutputDto::from(&output)) @@ -101,7 +100,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM native_tokens, unlock_conditions, features, - client.get_token_supply().await?, )?); Response::Output(OutputDto::from(&output)) @@ -127,7 +125,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM unlock_conditions, features, immutable_features, - client.get_token_supply().await?, )?); Response::Output(OutputDto::from(&output)) @@ -153,7 +150,6 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM unlock_conditions, features, immutable_features, - client.get_token_supply().await?, )?); Response::Output(OutputDto::from(&output)) diff --git a/sdk/examples/client/output/build_basic_output.rs b/sdk/examples/client/output/build_basic_output.rs index 5f92114e56..c7ef6391ac 100644 --- a/sdk/examples/client/output/build_basic_output.rs +++ b/sdk/examples/client/output/build_basic_output.rs @@ -9,7 +9,7 @@ //! ``` use iota_sdk::{ - client::{Client, Result}, + client::Result, types::block::{ address::Address, output::{ @@ -30,14 +30,6 @@ async fn main() -> Result<()> { // This example uses secrets in environment variables for simplicity which should not be done in production. dotenvy::dotenv().ok(); - // Create a node client. - let client = Client::builder() - .with_node(&std::env::var("NODE_URL").unwrap())? - .finish() - .await?; - - let token_supply = client.get_token_supply().await?; - let address = std::env::args() .nth(1) .unwrap_or("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy".to_string()); @@ -57,11 +49,7 @@ async fn main() -> Result<()> { // with storage deposit return basic_output_builder .clone() - .add_unlock_condition(StorageDepositReturnUnlockCondition::new( - address.clone(), - 1_000_000, - token_supply, - )?) + .add_unlock_condition(StorageDepositReturnUnlockCondition::new(address.clone(), 1_000_000)) .finish_output()?, // with expiration basic_output_builder diff --git a/sdk/examples/how_tos/outputs/unlock_conditions.rs b/sdk/examples/how_tos/outputs/unlock_conditions.rs index e926e9fde8..94cb8ce779 100644 --- a/sdk/examples/how_tos/outputs/unlock_conditions.rs +++ b/sdk/examples/how_tos/outputs/unlock_conditions.rs @@ -33,7 +33,6 @@ async fn main() -> Result<()> { // Create a client instance. let client = Client::builder().with_node(&node_url)?.finish().await?; - let token_supply = client.get_token_supply().await?; let storage_score_params = client.get_storage_score_parameters().await?; let address = Address::try_from_bech32("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy")?; @@ -51,11 +50,7 @@ async fn main() -> Result<()> { // with storage deposit return unlock condition basic_output_builder .clone() - .add_unlock_condition(StorageDepositReturnUnlockCondition::new( - address.clone(), - 1000000, - token_supply, - )?) + .add_unlock_condition(StorageDepositReturnUnlockCondition::new(address.clone(), 1000000)) .finish_output()?, // with timeout unlock condition basic_output_builder diff --git a/sdk/src/types/block/output/account.rs b/sdk/src/types/block/output/account.rs index ddb2ec9501..aa8630581c 100644 --- a/sdk/src/types/block/output/account.rs +++ b/sdk/src/types/block/output/account.rs @@ -676,7 +676,7 @@ pub(crate) mod dto { type Dto = AccountOutputDto; type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result { + fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result { let mut builder = AccountOutputBuilder::new_with_amount(dto.amount, dto.account_id) .with_mana(dto.mana) .with_foundry_counter(dto.foundry_counter) @@ -685,7 +685,7 @@ pub(crate) mod dto { .with_immutable_features(dto.immutable_features); for u in dto.unlock_conditions { - builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, ¶ms)?); + builder = builder.add_unlock_condition(UnlockCondition::from(u)); } builder.finish() @@ -703,9 +703,7 @@ pub(crate) mod dto { unlock_conditions: Vec, features: Option>, immutable_features: Option>, - params: impl Into> + Send, ) -> Result { - let params = params.into(); let mut builder = match amount { OutputBuilderAmount::Amount(amount) => AccountOutputBuilder::new_with_amount(amount, *account_id), OutputBuilderAmount::MinimumAmount(params) => { @@ -724,8 +722,8 @@ pub(crate) mod dto { let unlock_conditions = unlock_conditions .into_iter() - .map(|u| UnlockCondition::try_from_dto_with_params(u, ¶ms)) - .collect::, Error>>()?; + .map(UnlockCondition::from) + .collect::>(); builder = builder.with_unlock_conditions(unlock_conditions); if let Some(features) = features { @@ -780,7 +778,6 @@ mod tests { output.unlock_conditions().iter().map(Into::into).collect(), Some(output.features().to_vec()), Some(output.immutable_features().to_vec()), - &protocol_parameters, ) .unwrap(); assert_eq!(output, output_split); @@ -799,7 +796,6 @@ mod tests { builder.unlock_conditions.iter().map(Into::into).collect(), Some(builder.features.iter().cloned().collect()), Some(builder.immutable_features.iter().cloned().collect()), - &protocol_parameters, ) .unwrap(); assert_eq!(builder.finish().unwrap(), output_split); diff --git a/sdk/src/types/block/output/anchor.rs b/sdk/src/types/block/output/anchor.rs index 60ae4bada5..cac739c1d8 100644 --- a/sdk/src/types/block/output/anchor.rs +++ b/sdk/src/types/block/output/anchor.rs @@ -758,7 +758,7 @@ pub(crate) mod dto { type Dto = AnchorOutputDto; type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result { + fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result { let mut builder = AnchorOutputBuilder::new_with_amount(dto.amount, dto.anchor_id) .with_mana(dto.mana) .with_state_index(dto.state_index) @@ -768,7 +768,7 @@ pub(crate) mod dto { .with_state_metadata(dto.state_metadata); for u in dto.unlock_conditions { - builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, ¶ms)?); + builder = builder.add_unlock_condition(UnlockCondition::from(u)); } builder.finish() @@ -787,9 +787,7 @@ pub(crate) mod dto { unlock_conditions: Vec, features: Option>, immutable_features: Option>, - params: impl Into> + Send, ) -> Result { - let params = params.into(); let mut builder = match amount { OutputBuilderAmount::Amount(amount) => AnchorOutputBuilder::new_with_amount(amount, *anchor_id), OutputBuilderAmount::MinimumAmount(params) => { @@ -809,8 +807,8 @@ pub(crate) mod dto { let unlock_conditions = unlock_conditions .into_iter() - .map(|u| UnlockCondition::try_from_dto_with_params(u, ¶ms)) - .collect::, Error>>()?; + .map(UnlockCondition::from) + .collect::>(); builder = builder.with_unlock_conditions(unlock_conditions); if let Some(features) = features { @@ -865,7 +863,6 @@ mod tests { output.unlock_conditions().iter().map(Into::into).collect(), Some(output.features().to_vec()), Some(output.immutable_features().to_vec()), - &protocol_parameters, ) .unwrap(); assert_eq!(output, output_split); @@ -885,7 +882,6 @@ mod tests { builder.unlock_conditions.iter().map(Into::into).collect(), Some(builder.features.iter().cloned().collect()), Some(builder.immutable_features.iter().cloned().collect()), - &protocol_parameters, ) .unwrap(); assert_eq!(builder.finish().unwrap(), output_split); diff --git a/sdk/src/types/block/output/basic.rs b/sdk/src/types/block/output/basic.rs index 4407b942d7..23872aa848 100644 --- a/sdk/src/types/block/output/basic.rs +++ b/sdk/src/types/block/output/basic.rs @@ -156,7 +156,6 @@ impl BasicOutputBuilder { mut self, return_address: impl Into
, params: StorageScoreParameters, - token_supply: u64, ) -> Result { Ok(match self.amount { OutputBuilderAmount::Amount(amount) => { @@ -171,11 +170,8 @@ impl BasicOutputBuilder { .finish()? .amount(); // Add a temporary storage deposit unlock condition so the new storage requirement can be calculated - self = self.add_unlock_condition(StorageDepositReturnUnlockCondition::new( - return_address.clone(), - 1, - token_supply, - )?); + self = + self.add_unlock_condition(StorageDepositReturnUnlockCondition::new(return_address.clone(), 1)); // Get the min amount of the output with the added storage deposit return unlock condition let min_amount_with_sdruc = self.clone().finish()?.minimum_amount(params); // If the return storage cost and amount are less than the required min @@ -192,8 +188,7 @@ impl BasicOutputBuilder { .replace_unlock_condition(StorageDepositReturnUnlockCondition::new( return_address, sdruc_amount, - token_supply, - )?) + )) } else { self } @@ -468,14 +463,14 @@ pub(crate) mod dto { type Dto = BasicOutputDto; type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result { + fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result { let mut builder = BasicOutputBuilder::new_with_amount(dto.amount) .with_native_tokens(dto.native_tokens) .with_mana(dto.mana) .with_features(dto.features); for u in dto.unlock_conditions { - builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, ¶ms)?); + builder = builder.add_unlock_condition(UnlockCondition::from(u)); } builder.finish() @@ -489,9 +484,7 @@ pub(crate) mod dto { native_tokens: Option>, unlock_conditions: Vec, features: Option>, - params: impl Into> + Send, ) -> Result { - let params = params.into(); let mut builder = match amount { OutputBuilderAmount::Amount(amount) => BasicOutputBuilder::new_with_amount(amount), OutputBuilderAmount::MinimumAmount(params) => BasicOutputBuilder::new_with_minimum_amount(params), @@ -504,8 +497,8 @@ pub(crate) mod dto { let unlock_conditions = unlock_conditions .into_iter() - .map(|u| UnlockCondition::try_from_dto_with_params(u, ¶ms)) - .collect::, Error>>()?; + .map(UnlockCondition::from) + .collect::>(); builder = builder.with_unlock_conditions(unlock_conditions); if let Some(features) = features { @@ -552,7 +545,6 @@ mod tests { Some(output.native_tokens().to_vec()), output.unlock_conditions().iter().map(Into::into).collect(), Some(output.features().to_vec()), - protocol_parameters.token_supply(), ) .unwrap(); assert_eq!(output, output_split); @@ -567,7 +559,6 @@ mod tests { Some(builder.native_tokens.iter().copied().collect()), builder.unlock_conditions.iter().map(Into::into).collect(), Some(builder.features.iter().cloned().collect()), - protocol_parameters.token_supply(), ) .unwrap(); assert_eq!(builder.finish().unwrap(), output_split); diff --git a/sdk/src/types/block/output/delegation.rs b/sdk/src/types/block/output/delegation.rs index b4683aa063..cb512d2b06 100644 --- a/sdk/src/types/block/output/delegation.rs +++ b/sdk/src/types/block/output/delegation.rs @@ -439,7 +439,7 @@ pub(crate) mod dto { output::{unlock_condition::dto::UnlockConditionDto, OutputBuilderAmount}, Error, }, - TryFromDto, ValidationParams, + TryFromDto, }, utils::serde::string, }; @@ -481,7 +481,7 @@ pub(crate) mod dto { fn try_from_dto_with_params_inner( dto: Self::Dto, - params: crate::types::ValidationParams<'_>, + _params: crate::types::ValidationParams<'_>, ) -> Result { let mut builder = DelegationOutputBuilder::new_with_amount( dto.amount, @@ -493,7 +493,7 @@ pub(crate) mod dto { .with_end_epoch(dto.end_epoch); for u in dto.unlock_conditions { - builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, ¶ms)?); + builder = builder.add_unlock_condition(UnlockCondition::from(u)); } builder.finish() @@ -510,9 +510,7 @@ pub(crate) mod dto { start_epoch: impl Into, end_epoch: impl Into, unlock_conditions: Vec, - params: impl Into> + Send, ) -> Result { - let params = params.into(); let mut builder = match amount { OutputBuilderAmount::Amount(amount) => DelegationOutputBuilder::new_with_amount( amount, @@ -532,8 +530,8 @@ pub(crate) mod dto { let unlock_conditions = unlock_conditions .into_iter() - .map(|u| UnlockCondition::try_from_dto_with_params(u, ¶ms)) - .collect::, Error>>()?; + .map(UnlockCondition::from) + .collect::>(); builder = builder.with_unlock_conditions(unlock_conditions); builder.finish() diff --git a/sdk/src/types/block/output/foundry.rs b/sdk/src/types/block/output/foundry.rs index cc4f9b59d9..b69eb9d480 100644 --- a/sdk/src/types/block/output/foundry.rs +++ b/sdk/src/types/block/output/foundry.rs @@ -718,7 +718,7 @@ pub(crate) mod dto { type Dto = FoundryOutputDto; type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result { + fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result { let mut builder = FoundryOutputBuilder::new_with_amount(dto.amount, dto.serial_number, dto.token_scheme); for t in dto.native_tokens { @@ -734,7 +734,7 @@ pub(crate) mod dto { } for u in dto.unlock_conditions { - builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, ¶ms)?); + builder = builder.add_unlock_condition(UnlockCondition::from(u)); } builder.finish() @@ -751,10 +751,7 @@ pub(crate) mod dto { unlock_conditions: Vec, features: Option>, immutable_features: Option>, - params: impl Into> + Send, ) -> Result { - let params = params.into(); - let mut builder = match amount { OutputBuilderAmount::Amount(amount) => { FoundryOutputBuilder::new_with_amount(amount, serial_number, token_scheme) @@ -770,8 +767,8 @@ pub(crate) mod dto { let unlock_conditions = unlock_conditions .into_iter() - .map(|u| UnlockCondition::try_from_dto_with_params(u, ¶ms)) - .collect::, Error>>()?; + .map(UnlockCondition::from) + .collect::>(); builder = builder.with_unlock_conditions(unlock_conditions); if let Some(features) = features { @@ -831,7 +828,6 @@ mod tests { builder.unlock_conditions.iter().map(Into::into).collect(), Some(builder.features.iter().cloned().collect()), Some(builder.immutable_features.iter().cloned().collect()), - protocol_parameters.clone(), ) .unwrap(); assert_eq!(builder.finish().unwrap(), output_split); diff --git a/sdk/src/types/block/output/mod.rs b/sdk/src/types/block/output/mod.rs index 53095f9ee5..e948c51a8b 100644 --- a/sdk/src/types/block/output/mod.rs +++ b/sdk/src/types/block/output/mod.rs @@ -448,17 +448,17 @@ impl StorageScore for Output { impl MinimumOutputAmount for Output {} -pub(crate) fn verify_output_amount_supply(amount: u64, token_supply: u64) -> Result<(), Error> { - if amount > token_supply { - Err(Error::InvalidOutputAmount(amount)) - } else { - Ok(()) - } -} - -pub(crate) fn verify_output_amount(amount: u64, token_supply: u64) -> Result<(), Error> { - verify_output_amount_supply(amount, token_supply) -} +// pub(crate) fn verify_output_amount_supply(amount: u64, token_supply: u64) -> Result<(), Error> { +// if amount > token_supply { +// Err(Error::InvalidOutputAmount(amount)) +// } else { +// Ok(()) +// } +// } + +// pub(crate) fn verify_output_amount(amount: u64, token_supply: u64) -> Result<(), Error> { +// verify_output_amount_supply(amount, token_supply) +// } /// A trait that is shared by all output types, which is used to calculate the minimum amount the output /// must contain to satisfy its storage cost. diff --git a/sdk/src/types/block/output/nft.rs b/sdk/src/types/block/output/nft.rs index 11b758d456..4b1b3fe19c 100644 --- a/sdk/src/types/block/output/nft.rs +++ b/sdk/src/types/block/output/nft.rs @@ -225,7 +225,6 @@ impl NftOutputBuilder { mut self, return_address: impl Into
, params: StorageScoreParameters, - token_supply: u64, ) -> Result { Ok(match self.amount { OutputBuilderAmount::Amount(amount) => { @@ -240,11 +239,8 @@ impl NftOutputBuilder { .finish()? .amount(); // Add a temporary storage deposit unlock condition so the new storage requirement can be calculated - self = self.add_unlock_condition(StorageDepositReturnUnlockCondition::new( - return_address.clone(), - 1, - token_supply, - )?); + self = + self.add_unlock_condition(StorageDepositReturnUnlockCondition::new(return_address.clone(), 1)); // Get the min amount of the output with the added storage deposit return unlock condition let min_amount_with_sdruc = self.clone().finish()?.minimum_amount(params); // If the return storage cost and amount are less than the required min @@ -261,8 +257,7 @@ impl NftOutputBuilder { .replace_unlock_condition(StorageDepositReturnUnlockCondition::new( return_address, sdruc_amount, - token_supply, - )?) + )) } else { self } @@ -642,7 +637,7 @@ pub(crate) mod dto { type Dto = NftOutputDto; type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result { + fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result { let mut builder = NftOutputBuilder::new_with_amount(dto.amount, dto.nft_id) .with_mana(dto.mana) .with_native_tokens(dto.native_tokens) @@ -650,7 +645,7 @@ pub(crate) mod dto { .with_immutable_features(dto.immutable_features); for u in dto.unlock_conditions { - builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, ¶ms)?); + builder = builder.add_unlock_condition(UnlockCondition::from(u)); } builder.finish() @@ -667,9 +662,7 @@ pub(crate) mod dto { unlock_conditions: Vec, features: Option>, immutable_features: Option>, - params: impl Into> + Send, ) -> Result { - let params = params.into(); let mut builder = match amount { OutputBuilderAmount::Amount(amount) => NftOutputBuilder::new_with_amount(amount, *nft_id), OutputBuilderAmount::MinimumAmount(params) => { @@ -684,8 +677,8 @@ pub(crate) mod dto { let unlock_conditions = unlock_conditions .into_iter() - .map(|u| UnlockCondition::try_from_dto_with_params(u, ¶ms)) - .collect::, Error>>()?; + .map(UnlockCondition::from) + .collect::>(); builder = builder.with_unlock_conditions(unlock_conditions); if let Some(features) = features { @@ -740,7 +733,6 @@ mod tests { output.unlock_conditions().iter().map(Into::into).collect(), Some(output.features().to_vec()), Some(output.immutable_features().to_vec()), - &protocol_parameters, ) .unwrap(); assert_eq!(output, output_split); @@ -754,7 +746,6 @@ mod tests { builder.unlock_conditions.iter().map(Into::into).collect(), Some(builder.features.iter().cloned().collect()), Some(builder.immutable_features.iter().cloned().collect()), - &protocol_parameters, ) .unwrap(); assert_eq!(builder.finish().unwrap(), output_split); diff --git a/sdk/src/types/block/output/unlock_condition/mod.rs b/sdk/src/types/block/output/unlock_condition/mod.rs index 6a6c9688f2..6c5d9becfc 100644 --- a/sdk/src/types/block/output/unlock_condition/mod.rs +++ b/sdk/src/types/block/output/unlock_condition/mod.rs @@ -436,9 +436,7 @@ mod test { pub mod dto { use serde::{Deserialize, Serialize}; - pub use self::storage_deposit_return::dto::StorageDepositReturnUnlockConditionDto; use super::*; - use crate::types::{block::Error, TryFromDto, ValidationParams}; #[derive(Clone, Debug, Eq, PartialEq, From, Serialize, Deserialize)] #[serde(untagged)] @@ -446,7 +444,7 @@ pub mod dto { /// An address unlock condition. Address(AddressUnlockCondition), /// A storage deposit return unlock condition. - StorageDepositReturn(StorageDepositReturnUnlockConditionDto), + StorageDepositReturn(StorageDepositReturnUnlockCondition), /// A timelock unlock condition. Timelock(TimelockUnlockCondition), /// An expiration unlock condition. @@ -463,7 +461,7 @@ pub mod dto { fn from(value: &UnlockCondition) -> Self { match value { UnlockCondition::Address(v) => Self::Address(v.clone()), - UnlockCondition::StorageDepositReturn(v) => Self::StorageDepositReturn(v.into()), + UnlockCondition::StorageDepositReturn(v) => Self::StorageDepositReturn(v.clone()), UnlockCondition::Timelock(v) => Self::Timelock(*v), UnlockCondition::Expiration(v) => Self::Expiration(v.clone()), UnlockCondition::StateControllerAddress(v) => Self::StateControllerAddress(v.clone()), @@ -473,22 +471,17 @@ pub mod dto { } } - impl TryFromDto for UnlockCondition { - type Dto = UnlockConditionDto; - type Error = Error; - - fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result { - Ok(match dto { + impl From for UnlockCondition { + fn from(dto: UnlockConditionDto) -> Self { + match dto { UnlockConditionDto::Address(v) => Self::Address(v), - UnlockConditionDto::StorageDepositReturn(v) => Self::StorageDepositReturn( - StorageDepositReturnUnlockCondition::try_from_dto_with_params_inner(v, params)?, - ), + UnlockConditionDto::StorageDepositReturn(v) => Self::StorageDepositReturn(v), UnlockConditionDto::Timelock(v) => Self::Timelock(v), UnlockConditionDto::Expiration(v) => Self::Expiration(v), UnlockConditionDto::StateControllerAddress(v) => Self::StateControllerAddress(v), UnlockConditionDto::GovernorAddress(v) => Self::GovernorAddress(v), UnlockConditionDto::ImmutableAccountAddress(v) => Self::ImmutableAccountAddress(v), - }) + } } } diff --git a/sdk/src/types/block/output/unlock_condition/storage_deposit_return.rs b/sdk/src/types/block/output/unlock_condition/storage_deposit_return.rs index fb937a0e05..6824e728ec 100644 --- a/sdk/src/types/block/output/unlock_condition/storage_deposit_return.rs +++ b/sdk/src/types/block/output/unlock_condition/storage_deposit_return.rs @@ -3,12 +3,8 @@ use crate::types::block::{ address::Address, - output::{ - storage_score::{StorageScore, StorageScoreParameters}, - verify_output_amount, - }, + output::storage_score::{StorageScore, StorageScoreParameters}, protocol::ProtocolParameters, - Error, }; /// Defines the amount of IOTAs used as storage deposit that have to be returned to the return [`Address`]. @@ -18,7 +14,6 @@ pub struct StorageDepositReturnUnlockCondition { // The [`Address`] to return the amount to. return_address: Address, // Amount of IOTA coins the consuming transaction should deposit to `return_address`. - #[packable(verify_with = verify_amount_packable)] amount: u64, } @@ -29,13 +24,11 @@ impl StorageDepositReturnUnlockCondition { /// Creates a new [`StorageDepositReturnUnlockCondition`]. #[inline(always)] - pub fn new(return_address: impl Into
, amount: u64, token_supply: u64) -> Result { - verify_amount::(amount, token_supply)?; - - Ok(Self { + pub fn new(return_address: impl Into
, amount: u64) -> Self { + Self { return_address: return_address.into(), amount, - }) + } } /// Returns the return address. @@ -57,32 +50,14 @@ impl StorageScore for StorageDepositReturnUnlockCondition { } } -fn verify_amount(amount: u64, token_supply: u64) -> Result<(), Error> { - if VERIFY { - verify_output_amount(amount, token_supply).map_err(|_| Error::InvalidStorageDepositAmount(amount))?; - } - - Ok(()) -} - -fn verify_amount_packable( - amount: &u64, - protocol_parameters: &ProtocolParameters, -) -> Result<(), Error> { - verify_amount::(*amount, protocol_parameters.token_supply()) -} - #[cfg(feature = "serde")] -pub(crate) mod dto { +mod dto { use alloc::format; use serde::{Deserialize, Serialize}; use super::*; - use crate::{ - types::{block::Error, TryFromDto, ValidationParams}, - utils::serde::string, - }; + use crate::utils::serde::string; #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(rename_all = "camelCase")] @@ -119,28 +94,18 @@ pub(crate) mod dto { } } - impl TryFromDto for StorageDepositReturnUnlockCondition { - type Dto = StorageDepositReturnUnlockConditionDto; - type Error = Error; - - fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result { - Ok(if let Some(token_supply) = params.token_supply() { - Self::new(dto.return_address, dto.amount, token_supply)? - } else { - Self { - return_address: dto.return_address, - amount: dto.amount, - } - }) + impl From for StorageDepositReturnUnlockCondition { + fn from(dto: StorageDepositReturnUnlockConditionDto) -> Self { + Self { + return_address: dto.return_address, + amount: dto.amount, + } } } - impl Serialize for StorageDepositReturnUnlockCondition { - fn serialize(&self, s: S) -> Result - where - S: serde::Serializer, - { - StorageDepositReturnUnlockConditionDto::from(self).serialize(s) - } - } + crate::impl_serde_typed_dto!( + StorageDepositReturnUnlockCondition, + StorageDepositReturnUnlockConditionDto, + "storage deposit return unlock condition" + ); } diff --git a/sdk/src/wallet/operations/transaction/high_level/send.rs b/sdk/src/wallet/operations/transaction/high_level/send.rs index 6c43bcd0e7..accd247f79 100644 --- a/sdk/src/wallet/operations/transaction/high_level/send.rs +++ b/sdk/src/wallet/operations/transaction/high_level/send.rs @@ -137,7 +137,6 @@ where log::debug!("[TRANSACTION] prepare_send"); let options = options.into(); let storage_score_params = self.client().get_storage_score_parameters().await?; - let token_supply = self.client().get_token_supply().await?; let wallet_address = self.address().await; @@ -186,7 +185,7 @@ where return_address.clone(), expiration_slot_index, )?) - .with_sufficient_storage_deposit(return_address, storage_score_params, token_supply)? + .with_sufficient_storage_deposit(return_address, storage_score_params)? .finish_output()?; if !options.as_ref().map(|o| o.allow_micro_amount).unwrap_or_default() { diff --git a/sdk/src/wallet/operations/transaction/high_level/send_native_tokens.rs b/sdk/src/wallet/operations/transaction/high_level/send_native_tokens.rs index 67fac19420..a3dbc318b4 100644 --- a/sdk/src/wallet/operations/transaction/high_level/send_native_tokens.rs +++ b/sdk/src/wallet/operations/transaction/high_level/send_native_tokens.rs @@ -128,7 +128,6 @@ where { log::debug!("[TRANSACTION] prepare_send_native_tokens"); let storage_score_params = self.client().get_storage_score_parameters().await?; - let token_supply = self.client().get_token_supply().await?; let wallet_address = self.address().await; let default_return_address = wallet_address.to_bech32(self.client().get_bech32_hrp().await?); @@ -179,7 +178,7 @@ where return_address.clone(), expiration_slot_index, )?) - .with_sufficient_storage_deposit(return_address, storage_score_params, token_supply)? + .with_sufficient_storage_deposit(return_address, storage_score_params)? .finish_output()?, ) } diff --git a/sdk/src/wallet/operations/transaction/prepare_output.rs b/sdk/src/wallet/operations/transaction/prepare_output.rs index ae9e822e7d..afc93d3c2f 100644 --- a/sdk/src/wallet/operations/transaction/prepare_output.rs +++ b/sdk/src/wallet/operations/transaction/prepare_output.rs @@ -45,7 +45,6 @@ where ) -> crate::wallet::Result { log::debug!("[OUTPUT] prepare_output {params:?}"); let transaction_options = transaction_options.into(); - let token_supply = self.client().get_token_supply().await?; self.client().bech32_hrp_matches(params.recipient_address.hrp()).await?; @@ -141,8 +140,7 @@ where remainder_address.clone(), // Return minimum amount min_amount_basic_output, - token_supply, - )?); + )); // Update output amount, so recipient still gets the provided amount let new_amount = params.amount + min_amount_basic_output; @@ -163,8 +161,7 @@ where remainder_address.clone(), // Return minimum amount min_amount_basic_output + additional_required_amount, - token_supply, - )?); + )); } else { // new_amount is enough second_output_builder = second_output_builder.with_amount(new_amount); @@ -219,8 +216,7 @@ where remainder_address, // Return minimum amount new_sdr_amount, - token_supply, - )?); + )); } } else { // Would leave dust behind, so return what's required for a remainder diff --git a/sdk/tests/client/mod.rs b/sdk/tests/client/mod.rs index 4d8db30740..d01b8a394c 100644 --- a/sdk/tests/client/mod.rs +++ b/sdk/tests/client/mod.rs @@ -39,7 +39,6 @@ use iota_sdk::{ }, }; -const TOKEN_SUPPLY: u64 = 1_813_620_509_061_365; const ACCOUNT_ID_0: &str = "0x0000000000000000000000000000000000000000000000000000000000000000"; const ACCOUNT_ID_1: &str = "0x1111111111111111111111111111111111111111111111111111111111111111"; const ACCOUNT_ID_2: &str = "0x2222222222222222222222222222222222222222222222222222222222222222"; @@ -119,8 +118,7 @@ fn build_basic_output( } if let Some((address, amount)) = sdruc { - builder = builder - .add_unlock_condition(StorageDepositReturnUnlockCondition::new(address, amount, TOKEN_SUPPLY).unwrap()); + builder = builder.add_unlock_condition(StorageDepositReturnUnlockCondition::new(address, amount)); } if let Some(timelock) = timelock { @@ -165,8 +163,7 @@ fn build_nft_output( } if let Some((address, amount)) = sdruc { - builder = builder - .add_unlock_condition(StorageDepositReturnUnlockCondition::new(address, amount, TOKEN_SUPPLY).unwrap()); + builder = builder.add_unlock_condition(StorageDepositReturnUnlockCondition::new(address, amount)); } if let Some((address, timestamp)) = expiration { From 36d698501493db757dd5c76eb437824ba3bac626 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 15 Nov 2023 10:15:38 +0100 Subject: [PATCH 2/5] Remove commented code --- sdk/src/types/block/output/mod.rs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/sdk/src/types/block/output/mod.rs b/sdk/src/types/block/output/mod.rs index e948c51a8b..cdcbe8eef5 100644 --- a/sdk/src/types/block/output/mod.rs +++ b/sdk/src/types/block/output/mod.rs @@ -448,18 +448,6 @@ impl StorageScore for Output { impl MinimumOutputAmount for Output {} -// pub(crate) fn verify_output_amount_supply(amount: u64, token_supply: u64) -> Result<(), Error> { -// if amount > token_supply { -// Err(Error::InvalidOutputAmount(amount)) -// } else { -// Ok(()) -// } -// } - -// pub(crate) fn verify_output_amount(amount: u64, token_supply: u64) -> Result<(), Error> { -// verify_output_amount_supply(amount, token_supply) -// } - /// A trait that is shared by all output types, which is used to calculate the minimum amount the output /// must contain to satisfy its storage cost. pub trait MinimumOutputAmount: StorageScore { From 560ec422116067fb74317f1313bdff544c0fde73 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 15 Nov 2023 14:53:41 +0100 Subject: [PATCH 3/5] Remove some try_from_dto_with_params --- bindings/core/src/method_handler/client.rs | 2 +- bindings/core/src/method_handler/utils.rs | 4 +- bindings/core/src/method_handler/wallet.rs | 6 +-- sdk/src/client/api/types.rs | 18 ++++----- sdk/src/client/node_api/core/routes.rs | 7 ++-- sdk/src/client/secret/types.rs | 12 ++---- sdk/src/types/block/output/account.rs | 33 ++++++--------- sdk/src/types/block/output/anchor.rs | 35 +++++++--------- sdk/src/types/block/output/basic.rs | 31 ++++++-------- sdk/src/types/block/output/delegation.rs | 17 +++----- sdk/src/types/block/output/foundry.rs | 40 ++++++++----------- sdk/src/types/block/output/mod.rs | 20 ++++------ sdk/src/types/block/output/nft.rs | 31 ++++++-------- .../payload/signed_transaction/transaction.rs | 2 +- sdk/src/wallet/core/mod.rs | 6 +-- sdk/src/wallet/types/mod.rs | 10 ++--- sdk/tests/client/input_signing_data.rs | 24 ++++------- 17 files changed, 116 insertions(+), 182 deletions(-) diff --git a/bindings/core/src/method_handler/client.rs b/bindings/core/src/method_handler/client.rs index a5982664fe..22bb79bb2f 100644 --- a/bindings/core/src/method_handler/client.rs +++ b/bindings/core/src/method_handler/client.rs @@ -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)) diff --git a/bindings/core/src/method_handler/utils.rs b/bindings/core/src/method_handler/utils.rs index ae1d3f8131..89a25e8424 100644 --- a/bindings/core/src/method_handler/utils.rs +++ b/bindings/core/src/method_handler/utils.rs @@ -76,7 +76,7 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result { - let out = Output::try_from_dto(output)?; + let out = Output::try_from(output)?; Response::OutputAmount(out.minimum_amount(storage_params)) } UtilsMethod::VerifyMnemonic { mnemonic } => { @@ -105,7 +105,7 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result 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())) } }; diff --git a/bindings/core/src/method_handler/wallet.rs b/bindings/core/src/method_handler/wallet.rs index ee8102677d..25502f563b 100644 --- a/bindings/core/src/method_handler/wallet.rs +++ b/bindings/core/src/method_handler/wallet.rs @@ -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::>>()?, options, ) @@ -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::>>()?, options, ) diff --git a/sdk/src/client/api/types.rs b/sdk/src/client/api/types.rs index 3e73780289..c81dc1b691 100644 --- a/sdk/src/client/api/types.rs +++ b/sdk/src/client/api/types.rs @@ -68,14 +68,13 @@ impl TryFromDto for PreparedTransactionData { inputs_data: dto .inputs_data .into_iter() - .map(|i| InputSigningData::try_from_dto_with_params(i, ¶ms)) + .map(|i| InputSigningData::try_from(i)) .collect::>>() .map_err(|_| Error::InvalidField("input_data"))?, remainder: match dto.remainder { - Some(remainder) => Some( - RemainderData::try_from_dto_with_params(remainder, ¶ms) - .map_err(|_| Error::InvalidField("remainder"))?, - ), + Some(remainder) => { + Some(RemainderData::try_from(remainder).map_err(|_| Error::InvalidField("remainder"))?) + } None => None, }, }) @@ -121,7 +120,7 @@ impl TryFromDto for SignedTransactionData { inputs_data: dto .inputs_data .into_iter() - .map(|i| InputSigningData::try_from_dto_with_params(i, ¶ms)) + .map(|i| InputSigningData::try_from(i)) .collect::>>() .map_err(|_| Error::InvalidField("inputs_data"))?, }) @@ -151,13 +150,12 @@ pub struct RemainderDataDto { pub address: Address, } -impl TryFromDto for RemainderData { - type Dto = RemainderDataDto; +impl TryFrom for RemainderData { type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result { + fn try_from(dto: RemainderDataDto) -> Result { 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, }) diff --git a/sdk/src/client/node_api/core/routes.rs b/sdk/src/client/node_api/core/routes.rs index 05379506ab..191ec80130 100644 --- a/sdk/src/client/node_api/core/routes.rs +++ b/sdk/src/client/node_api/core/routes.rs @@ -226,10 +226,9 @@ impl ClientInner { pub async fn get_output(&self, output_id: &OutputId) -> Result { let path = &format!("api/core/v3/outputs/{output_id}"); - let output = self.get_request::(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::(path, None, false, true).await?, + )?) } /// Finds an output by its ID and returns it as raw bytes. diff --git a/sdk/src/client/secret/types.rs b/sdk/src/client/secret/types.rs index 62cc0e5bac..352869b3ab 100644 --- a/sdk/src/client/secret/types.rs +++ b/sdk/src/client/secret/types.rs @@ -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, }; @@ -167,13 +164,12 @@ pub struct InputSigningDataDto { pub chain: Option, } -impl TryFromDto for InputSigningData { - type Dto = InputSigningDataDto; +impl TryFrom for InputSigningData { type Error = crate::client::Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result { + fn try_from(dto: InputSigningDataDto) -> Result { 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, }) diff --git a/sdk/src/types/block/output/account.rs b/sdk/src/types/block/output/account.rs index aa8630581c..e6b8e8faa0 100644 --- a/sdk/src/types/block/output/account.rs +++ b/sdk/src/types/block/output/account.rs @@ -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, }; @@ -672,11 +669,10 @@ pub(crate) mod dto { } } - impl TryFromDto for AccountOutput { - type Dto = AccountOutputDto; + impl TryFrom for AccountOutput { type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result { + fn try_from(dto: AccountOutputDto) -> Result { let mut builder = AccountOutputBuilder::new_with_amount(dto.amount, dto.account_id) .with_mana(dto.mana) .with_foundry_counter(dto.foundry_counter) @@ -744,19 +740,16 @@ 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] @@ -764,9 +757,9 @@ mod tests { 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( diff --git a/sdk/src/types/block/output/anchor.rs b/sdk/src/types/block/output/anchor.rs index cac739c1d8..edcf29e7d4 100644 --- a/sdk/src/types/block/output/anchor.rs +++ b/sdk/src/types/block/output/anchor.rs @@ -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}, }; @@ -754,11 +751,10 @@ pub(crate) mod dto { } } - impl TryFromDto for AnchorOutput { - type Dto = AnchorOutputDto; + impl TryFrom for AnchorOutput { type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result { + fn try_from(dto: AnchorOutputDto) -> Result { let mut builder = AnchorOutputBuilder::new_with_amount(dto.amount, dto.anchor_id) .with_mana(dto.mana) .with_state_index(dto.state_index) @@ -827,20 +823,17 @@ 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] @@ -848,9 +841,9 @@ mod tests { 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( diff --git a/sdk/src/types/block/output/basic.rs b/sdk/src/types/block/output/basic.rs index 23872aa848..ecc803410f 100644 --- a/sdk/src/types/block/output/basic.rs +++ b/sdk/src/types/block/output/basic.rs @@ -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, }; @@ -459,11 +456,10 @@ pub(crate) mod dto { } } - impl TryFromDto for BasicOutput { - type Dto = BasicOutputDto; + impl TryFrom for BasicOutput { type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result { + fn try_from(dto: BasicOutputDto) -> Result { let mut builder = BasicOutputBuilder::new_with_amount(dto.amount) .with_native_tokens(dto.native_tokens) .with_mana(dto.mana) @@ -515,18 +511,15 @@ 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] @@ -534,9 +527,9 @@ mod tests { 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( diff --git a/sdk/src/types/block/output/delegation.rs b/sdk/src/types/block/output/delegation.rs index cb512d2b06..b1b3049cb3 100644 --- a/sdk/src/types/block/output/delegation.rs +++ b/sdk/src/types/block/output/delegation.rs @@ -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, }; @@ -475,14 +472,10 @@ pub(crate) mod dto { } } - impl TryFromDto for DelegationOutput { - type Dto = DelegationOutputDto; + impl TryFrom for DelegationOutput { type Error = Error; - fn try_from_dto_with_params_inner( - dto: Self::Dto, - _params: crate::types::ValidationParams<'_>, - ) -> Result { + fn try_from(dto: DelegationOutputDto) -> Result { let mut builder = DelegationOutputBuilder::new_with_amount( dto.amount, dto.delegated_amount, diff --git a/sdk/src/types/block/output/foundry.rs b/sdk/src/types/block/output/foundry.rs index b69eb9d480..8cafce7ace 100644 --- a/sdk/src/types/block/output/foundry.rs +++ b/sdk/src/types/block/output/foundry.rs @@ -674,10 +674,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, }; @@ -714,12 +711,12 @@ pub(crate) mod dto { } } - impl TryFromDto for FoundryOutput { - type Dto = FoundryOutputDto; + impl TryFrom for FoundryOutput { type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result { - let mut builder = FoundryOutputBuilder::new_with_amount(dto.amount, dto.serial_number, dto.token_scheme); + fn try_from(dto: FoundryOutputDto) -> Result { + let mut builder: FoundryOutputBuilder = + FoundryOutputBuilder::new_with_amount(dto.amount, dto.serial_number, dto.token_scheme); for t in dto.native_tokens { builder = builder.add_native_token(t); @@ -789,22 +786,19 @@ mod tests { use pretty_assertions::assert_eq; use super::*; - use crate::types::{ - block::{ + use crate::types::block::{ + output::{ + dto::OutputDto, unlock_condition::ImmutableAccountAddressUnlockCondition, FoundryId, SimpleTokenScheme, + TokenId, + }, + protocol::protocol_parameters, + rand::{ + address::rand_account_address, output::{ - dto::OutputDto, 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, - }, + feature::{rand_allowed_features, rand_metadata_feature}, + rand_foundry_output, rand_token_scheme, }, }, - TryFromDto, }; #[test] @@ -812,9 +806,9 @@ mod tests { let protocol_parameters = protocol_parameters(); let output = rand_foundry_output(protocol_parameters.token_supply()); let dto = OutputDto::Foundry((&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_foundry()); - 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_foundry()); let foundry_id = FoundryId::build(&rand_account_address(), 0, SimpleTokenScheme::KIND); diff --git a/sdk/src/types/block/output/mod.rs b/sdk/src/types/block/output/mod.rs index cdcbe8eef5..dd4de9391a 100644 --- a/sdk/src/types/block/output/mod.rs +++ b/sdk/src/types/block/output/mod.rs @@ -469,7 +469,6 @@ pub mod dto { account::dto::AccountOutputDto, anchor::dto::AnchorOutputDto, basic::dto::BasicOutputDto, delegation::dto::DelegationOutputDto, foundry::dto::FoundryOutputDto, nft::dto::NftOutputDto, }; - use crate::types::{block::Error, TryFromDto, ValidationParams}; /// Describes all the different output types. #[derive(Clone, Debug, Eq, PartialEq, From)] @@ -495,20 +494,17 @@ pub mod dto { } } - impl TryFromDto for Output { - type Dto = OutputDto; + impl TryFrom for Output { type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, params: ValidationParams<'_>) -> Result { + fn try_from(dto: OutputDto) -> Result { Ok(match dto { - OutputDto::Basic(o) => Self::Basic(BasicOutput::try_from_dto_with_params_inner(o, params)?), - OutputDto::Account(o) => Self::Account(AccountOutput::try_from_dto_with_params_inner(o, params)?), - OutputDto::Anchor(o) => Self::Anchor(AnchorOutput::try_from_dto_with_params_inner(o, params)?), - OutputDto::Foundry(o) => Self::Foundry(FoundryOutput::try_from_dto_with_params_inner(o, params)?), - OutputDto::Nft(o) => Self::Nft(NftOutput::try_from_dto_with_params_inner(o, params)?), - OutputDto::Delegation(o) => { - Self::Delegation(DelegationOutput::try_from_dto_with_params_inner(o, params)?) - } + OutputDto::Basic(o) => Self::Basic(BasicOutput::try_from(o)?), + OutputDto::Account(o) => Self::Account(AccountOutput::try_from(o)?), + OutputDto::Anchor(o) => Self::Anchor(AnchorOutput::try_from(o)?), + OutputDto::Foundry(o) => Self::Foundry(FoundryOutput::try_from(o)?), + OutputDto::Nft(o) => Self::Nft(NftOutput::try_from(o)?), + OutputDto::Delegation(o) => Self::Delegation(DelegationOutput::try_from(o)?), }) } } diff --git a/sdk/src/types/block/output/nft.rs b/sdk/src/types/block/output/nft.rs index 4b1b3fe19c..4f6bd4f0af 100644 --- a/sdk/src/types/block/output/nft.rs +++ b/sdk/src/types/block/output/nft.rs @@ -592,10 +592,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, }; @@ -633,11 +630,10 @@ pub(crate) mod dto { } } - impl TryFromDto for NftOutput { - type Dto = NftOutputDto; + impl TryFrom for NftOutput { type Error = Error; - fn try_from_dto_with_params_inner(dto: Self::Dto, _params: ValidationParams<'_>) -> Result { + fn try_from(dto: NftOutputDto) -> Result { let mut builder = NftOutputBuilder::new_with_amount(dto.amount, dto.nft_id) .with_mana(dto.mana) .with_native_tokens(dto.native_tokens) @@ -699,18 +695,15 @@ 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_nft_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_nft_output, unlock_condition::rand_address_unlock_condition, }, }, - TryFromDto, }; #[test] @@ -718,9 +711,9 @@ mod tests { let protocol_parameters = protocol_parameters(); let output = rand_nft_output(protocol_parameters.token_supply()); let dto = OutputDto::Nft((&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_nft()); - 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_nft()); let foundry_id = FoundryId::build(&rand_account_address(), 0, SimpleTokenScheme::KIND); diff --git a/sdk/src/types/block/payload/signed_transaction/transaction.rs b/sdk/src/types/block/payload/signed_transaction/transaction.rs index f211230ddf..7dc7437b4c 100644 --- a/sdk/src/types/block/payload/signed_transaction/transaction.rs +++ b/sdk/src/types/block/payload/signed_transaction/transaction.rs @@ -593,7 +593,7 @@ pub(crate) mod dto { let outputs = dto .outputs .into_iter() - .map(|o| Output::try_from_dto_with_params(o, ¶ms)) + .map(|o| Output::try_from(o)) .collect::, Error>>()?; let mut builder = Self::builder(network_id) diff --git a/sdk/src/wallet/core/mod.rs b/sdk/src/wallet/core/mod.rs index a39f4516b0..f9ee311fab 100644 --- a/sdk/src/wallet/core/mod.rs +++ b/sdk/src/wallet/core/mod.rs @@ -593,13 +593,13 @@ impl TryFromDto for WalletData { outputs: dto .outputs .into_iter() - .map(|(id, o)| Ok((id, OutputData::try_from_dto_with_params(o, ¶ms)?))) + .map(|(id, o)| Ok((id, OutputData::try_from(o)?))) .collect::>()?, locked_outputs: dto.locked_outputs, unspent_outputs: dto .unspent_outputs .into_iter() - .map(|(id, o)| Ok((id, OutputData::try_from_dto_with_params(o, ¶ms)?))) + .map(|(id, o)| Ok((id, OutputData::try_from(o)?))) .collect::>()?, transactions: dto .transactions @@ -616,7 +616,7 @@ impl TryFromDto for WalletData { native_token_foundries: dto .native_token_foundries .into_iter() - .map(|(id, o)| Ok((id, FoundryOutput::try_from_dto_with_params(o, ¶ms)?))) + .map(|(id, o)| Ok((id, FoundryOutput::try_from(o)?))) .collect::>()?, }) } diff --git a/sdk/src/wallet/types/mod.rs b/sdk/src/wallet/types/mod.rs index 4ecef57081..cc70a0a417 100644 --- a/sdk/src/wallet/types/mod.rs +++ b/sdk/src/wallet/types/mod.rs @@ -122,18 +122,14 @@ impl From<&OutputData> for OutputDataDto { } } -impl TryFromDto for OutputData { - type Dto = OutputDataDto; +impl TryFrom for OutputData { type Error = BlockError; - fn try_from_dto_with_params_inner( - dto: Self::Dto, - params: crate::types::ValidationParams<'_>, - ) -> Result { + fn try_from(dto: OutputDataDto) -> Result { Ok(Self { output_id: dto.output_id, metadata: dto.metadata, - output: Output::try_from_dto_with_params(dto.output, params)?, + output: Output::try_from(dto.output)?, is_spent: dto.is_spent, address: dto.address, network_id: dto diff --git a/sdk/tests/client/input_signing_data.rs b/sdk/tests/client/input_signing_data.rs index 3af8a84422..491cf180f5 100644 --- a/sdk/tests/client/input_signing_data.rs +++ b/sdk/tests/client/input_signing_data.rs @@ -9,24 +9,18 @@ use iota_sdk::{ constants::SHIMMER_COIN_TYPE, secret::types::{InputSigningData, InputSigningDataDto}, }, - types::{ - block::{ - address::Address, - output::{unlock_condition::AddressUnlockCondition, BasicOutput, OutputId, OutputMetadata}, - payload::signed_transaction::TransactionId, - protocol::protocol_parameters, - slot::SlotCommitmentId, - BlockId, - }, - TryFromDto, + types::block::{ + address::Address, + output::{unlock_condition::AddressUnlockCondition, BasicOutput, OutputId, OutputMetadata}, + payload::signed_transaction::TransactionId, + slot::SlotCommitmentId, + BlockId, }, }; use pretty_assertions::assert_eq; #[test] fn input_signing_data_conversion() { - let protocol_parameters = protocol_parameters(); - let bip44_chain = Bip44::new(SHIMMER_COIN_TYPE); let output = BasicOutput::build_with_amount(1_000_000) @@ -68,8 +62,7 @@ fn input_signing_data_conversion() { let input_signing_data_dto = InputSigningDataDto::from(&input_signing_data); assert_eq!(input_signing_data_dto.chain.as_ref(), Some(&bip44_chain)); - let restored_input_signing_data = - InputSigningData::try_from_dto_with_params(input_signing_data_dto.clone(), &protocol_parameters).unwrap(); + let restored_input_signing_data = InputSigningData::try_from(input_signing_data_dto.clone()).unwrap(); assert_eq!(input_signing_data, restored_input_signing_data); let input_signing_data_dto_json = serde_json::json!({ @@ -113,8 +106,7 @@ fn input_signing_data_conversion() { serde_json::from_value::(input_signing_data_dto_json).unwrap(); assert_eq!(restored_input_signing_data_dto.chain.as_ref(), Some(&bip44_chain)); - let restored_input_signing_data = - InputSigningData::try_from_dto_with_params(restored_input_signing_data_dto, &protocol_parameters).unwrap(); + let restored_input_signing_data = InputSigningData::try_from(restored_input_signing_data_dto).unwrap(); assert!(restored_input_signing_data.output.is_basic()); assert_eq!(restored_input_signing_data.chain, Some(bip44_chain)); } From 884771ce1122581dd448fc92f049e764bb73a517 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 15 Nov 2023 15:06:39 +0100 Subject: [PATCH 4/5] Nit --- sdk/src/types/block/payload/signed_transaction/transaction.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/src/types/block/payload/signed_transaction/transaction.rs b/sdk/src/types/block/payload/signed_transaction/transaction.rs index 7dc7437b4c..b9f085d3da 100644 --- a/sdk/src/types/block/payload/signed_transaction/transaction.rs +++ b/sdk/src/types/block/payload/signed_transaction/transaction.rs @@ -593,7 +593,7 @@ pub(crate) mod dto { let outputs = dto .outputs .into_iter() - .map(|o| Output::try_from(o)) + .map(Output::try_from) .collect::, Error>>()?; let mut builder = Self::builder(network_id) From 150599b2a86791c81f7012f9d79d29f3d8d69d88 Mon Sep 17 00:00:00 2001 From: Thibault Martinez Date: Wed, 15 Nov 2023 15:37:51 +0100 Subject: [PATCH 5/5] sad clippy noises --- bindings/core/src/method_handler/wallet.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/bindings/core/src/method_handler/wallet.rs b/bindings/core/src/method_handler/wallet.rs index 25502f563b..6825dc72e0 100644 --- a/bindings/core/src/method_handler/wallet.rs +++ b/bindings/core/src/method_handler/wallet.rs @@ -17,10 +17,10 @@ use iota_sdk::{ wallet::{types::TransactionWithMetadataDto, OutputDataDto, PreparedCreateNativeTokenTransactionDto, Wallet}, }; -use crate::{method::WalletMethod, response::Response, Result}; +use crate::{method::WalletMethod, response::Response}; /// Call a wallet method. -pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletMethod) -> Result { +pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletMethod) -> crate::Result { let response = match method { WalletMethod::Accounts => { let accounts = wallet.accounts().await; @@ -301,8 +301,8 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM .prepare_transaction( outputs .into_iter() - .map(|o| Ok(Output::try_from(o)?)) - .collect::>>()?, + .map(Output::try_from) + .collect::, _>>()?, options, ) .await?; @@ -345,8 +345,8 @@ pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletM .send_outputs( outputs .into_iter() - .map(|o| Ok(Output::try_from(o)?)) - .collect::>>()?, + .map(Output::try_from) + .collect::, _>>()?, options, ) .await?;