Skip to content

Commit

Permalink
Remove SDRUC supply check (#1619)
Browse files Browse the repository at this point in the history
* Remove SDRUC supply check

* Remove commented code

* Remove some try_from_dto_with_params

* Nit

* sad clippy noises
  • Loading branch information
thibault-martinez authored Nov 16, 2023
1 parent cb1568a commit bf705a0
Show file tree
Hide file tree
Showing 25 changed files with 178 additions and 360 deletions.
6 changes: 1 addition & 5 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand All @@ -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))
Expand Down Expand Up @@ -296,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
14 changes: 6 additions & 8 deletions bindings/core/src/method_handler/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Response> {
pub(crate) async fn call_wallet_method_internal(wallet: &Wallet, method: WalletMethod) -> crate::Result<Response> {
let response = match method {
WalletMethod::Accounts => {
let accounts = wallet.accounts().await;
Expand Down Expand Up @@ -297,13 +297,12 @@ 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)?))
.collect::<Result<Vec<Output>>>()?,
.map(Output::try_from)
.collect::<Result<Vec<Output>, _>>()?,
options,
)
.await?;
Expand Down Expand Up @@ -342,13 +341,12 @@ 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)?))
.collect::<iota_sdk::wallet::Result<Vec<Output>>>()?,
.map(Output::try_from)
.collect::<Result<Vec<Output>, _>>()?,
options,
)
.await?;
Expand Down
16 changes: 2 additions & 14 deletions sdk/examples/client/output/build_basic_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! ```
use iota_sdk::{
client::{Client, Result},
client::Result,
types::block::{
address::Address,
output::{
Expand All @@ -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());
Expand All @@ -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
Expand Down
7 changes: 1 addition & 6 deletions sdk/examples/how_tos/outputs/unlock_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand All @@ -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
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
43 changes: 16 additions & 27 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 All @@ -685,7 +681,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, &params)?);
builder = builder.add_unlock_condition(UnlockCondition::from(u));
}

builder.finish()
Expand All @@ -703,9 +699,7 @@ pub(crate) mod dto {
unlock_conditions: Vec<UnlockConditionDto>,
features: Option<Vec<Feature>>,
immutable_features: Option<Vec<Feature>>,
params: impl Into<ValidationParams<'a>> + Send,
) -> Result<Self, Error> {
let params = params.into();
let mut builder = match amount {
OutputBuilderAmount::Amount(amount) => AccountOutputBuilder::new_with_amount(amount, *account_id),
OutputBuilderAmount::MinimumAmount(params) => {
Expand All @@ -724,8 +718,8 @@ pub(crate) mod dto {

let unlock_conditions = unlock_conditions
.into_iter()
.map(|u| UnlockCondition::try_from_dto_with_params(u, &params))
.collect::<Result<Vec<UnlockCondition>, Error>>()?;
.map(UnlockCondition::from)
.collect::<Vec<UnlockCondition>>();
builder = builder.with_unlock_conditions(unlock_conditions);

if let Some(features) = features {
Expand All @@ -746,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 All @@ -780,7 +771,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);
Expand All @@ -799,7 +789,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);
Expand Down
Loading

0 comments on commit bf705a0

Please sign in to comment.