Skip to content

Commit

Permalink
almost done
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex6323 committed Oct 6, 2023
1 parent c79c024 commit 824b5e0
Show file tree
Hide file tree
Showing 47 changed files with 441 additions and 298 deletions.
15 changes: 7 additions & 8 deletions bindings/core/src/method_handler/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use iota_sdk::{
dto::OutputDto, AccountOutput, BasicOutput, FoundryOutput, NftOutput, Output, OutputBuilderAmount,
},
payload::Payload,
rent::StorageScore,
rent::{RentStructure, StorageScore},
BlockWrapper, BlockWrapperDto,
},
TryFromDto,
Expand Down Expand Up @@ -72,7 +72,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
if let Some(amount) = amount {
OutputBuilderAmount::Amount(amount)
} else {
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?)
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?.into())
},
mana,
native_tokens,
Expand All @@ -99,7 +99,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
if let Some(amount) = amount {
OutputBuilderAmount::Amount(amount)
} else {
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?)
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?.into())
},
mana,
native_tokens,
Expand All @@ -123,7 +123,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
if let Some(amount) = amount {
OutputBuilderAmount::Amount(amount)
} else {
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?)
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?.into())
},
native_tokens,
serial_number,
Expand All @@ -149,7 +149,7 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
if let Some(amount) = amount {
OutputBuilderAmount::Amount(amount)
} else {
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?)
OutputBuilderAmount::MinimumStorageDeposit(client.get_rent_parameters().await?.into())
},
mana,
native_tokens,
Expand Down Expand Up @@ -296,9 +296,8 @@ pub(crate) async fn call_client_method_internal(client: &Client, method: ClientM
}
ClientMethod::MinimumRequiredStorageDeposit { output } => {
let output = Output::try_from_dto_with_params(output, client.get_token_supply().await?)?;
let rent_params = client.get_rent_parameters().await?;

let minimum_storage_deposit = output.rent_cost(rent_params);
let rent_struct = client.get_rent_parameters().await?.into();
let minimum_storage_deposit = output.rent_cost(rent_struct);

Response::MinimumRequiredStorageDeposit(minimum_storage_deposit.to_string())
}
Expand Down
2 changes: 1 addition & 1 deletion bindings/core/src/method_handler/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub(crate) fn call_utils_method_internal(method: UtilsMethod) -> Result<Response
}
UtilsMethod::ComputeStorageDeposit { output, rent } => {
let out = Output::try_from_dto(output)?;
Response::MinimumRequiredStorageDeposit(out.rent_cost(rent).to_string())
Response::MinimumRequiredStorageDeposit(out.rent_cost(rent.into()).to_string())
}
UtilsMethod::VerifyMnemonic { mnemonic } => {
let mnemonic = Mnemonic::from(mnemonic);
Expand Down
4 changes: 2 additions & 2 deletions cli/src/command/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,12 @@ pub async fn send_native_token_command(
let address = address.convert()?;
let transaction = if gift_storage_deposit.unwrap_or(false) {
// Send native tokens together with the required storage deposit
let rent_params = account.client().get_rent_parameters().await?;
let rent_struct = account.client().get_rent_parameters().await?.into();
let token_supply = account.client().get_token_supply().await?;

account.client().bech32_hrp_matches(address.hrp()).await?;

let outputs = [BasicOutputBuilder::new_with_minimum_storage_deposit(rent_params)
let outputs = [BasicOutputBuilder::new_with_minimum_storage_deposit(rent_struct)
.add_unlock_condition(AddressUnlockCondition::new(address))
.with_native_tokens([NativeToken::new(
TokenId::from_str(&token_id)?,
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/client/output/build_account_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ async fn main() -> Result<()> {
.await?;

let token_supply = client.get_token_supply().await?;
let rent_params = client.get_rent_parameters().await?;
let rent_struct = client.get_rent_parameters().await?.into();

let address = std::env::args()
.nth(1)
.unwrap_or("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy".to_string());
let address = Address::try_from_bech32(address)?;

// Account id needs to be null the first time
let account_output = AccountOutputBuilder::new_with_minimum_storage_deposit(rent_params, AccountId::null())
let account_output = AccountOutputBuilder::new_with_minimum_storage_deposit(rent_struct, AccountId::null())
.with_state_metadata(metadata)
.add_feature(SenderFeature::new(address))
.add_feature(MetadataFeature::new(metadata)?)
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/client/output/build_nft_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ async fn main() -> Result<()> {
.await?;

let token_supply = client.get_token_supply().await?;
let rent_params = client.get_rent_parameters().await?;
let rent_struct = client.get_rent_parameters().await?.into();

let address = std::env::args()
.nth(1)
Expand All @@ -56,7 +56,7 @@ async fn main() -> Result<()> {
.to_string();

// NftId needs to be null the first time
let nft_output = NftOutputBuilder::new_with_minimum_storage_deposit(rent_params, NftId::null())
let nft_output = NftOutputBuilder::new_with_minimum_storage_deposit(rent_struct, NftId::null())
.add_unlock_condition(AddressUnlockCondition::new(address))
.add_feature(SenderFeature::new(address))
.add_feature(MetadataFeature::new(MUTABLE_METADATA)?)
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/how_tos/account/state_transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ async fn main() -> Result<()> {
);

let token_supply = account.client().get_token_supply().await?;
let rent_params = account.client().get_rent_parameters().await?;
let rent_struct = account.client().get_rent_parameters().await?.into();

let account_output = account_output_data.output.as_account();
let updated_account_output = AccountOutputBuilder::from(account_output)
// Minimum required storage deposit will change if the new metadata has a different size, so we will update
// the amount
.with_minimum_storage_deposit(rent_params)
.with_minimum_storage_deposit(rent_struct)
.with_state_metadata(NEW_STATE_METADATA.as_bytes().to_vec())
.with_state_index(account_output.state_index() + 1)
.finish_output(token_supply)?;
Expand Down
4 changes: 2 additions & 2 deletions sdk/examples/how_tos/outputs/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ async fn main() -> Result<()> {
let client = Client::builder().with_node(&node_url)?.finish().await?;

let token_supply = client.get_token_supply().await?;
let rent_params = client.get_rent_parameters().await?;
let rent_struct = client.get_rent_parameters().await?.into();

let address = Address::try_from_bech32("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy")?;

let nft_output_builder = NftOutputBuilder::new_with_minimum_storage_deposit(rent_params, NftId::null())
let nft_output_builder = NftOutputBuilder::new_with_minimum_storage_deposit(rent_struct, NftId::null())
.add_unlock_condition(AddressUnlockCondition::new(address));

let outputs = [
Expand Down
8 changes: 4 additions & 4 deletions sdk/examples/how_tos/outputs/unlock_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ async fn main() -> Result<()> {
let client = Client::builder().with_node(&node_url)?.finish().await?;

let token_supply = client.get_token_supply().await?;
let rent_params = client.get_rent_parameters().await?;
let rent_struct = client.get_rent_parameters().await?.into();

let address = Address::try_from_bech32("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy")?;
let account_address = Address::try_from_bech32("rms1pr59qm43mjtvhcajfmupqf23x29llam88yecn6pyul80rx099krmv2fnnux")?;

let token_scheme = TokenScheme::Simple(SimpleTokenScheme::new(50, 0, 100)?);

let basic_output_builder = BasicOutputBuilder::new_with_minimum_storage_deposit(rent_params)
let basic_output_builder = BasicOutputBuilder::new_with_minimum_storage_deposit(rent_struct)
.add_unlock_condition(AddressUnlockCondition::new(address));
let account_output_builder = AccountOutputBuilder::new_with_minimum_storage_deposit(rent_params, AccountId::null());
let foundry_output_builder = FoundryOutputBuilder::new_with_minimum_storage_deposit(rent_params, 1, token_scheme);
let account_output_builder = AccountOutputBuilder::new_with_minimum_storage_deposit(rent_struct, AccountId::null());
let foundry_output_builder = FoundryOutputBuilder::new_with_minimum_storage_deposit(rent_struct, 1, token_scheme);

let outputs = [
//// most simple output
Expand Down
5 changes: 3 additions & 2 deletions sdk/src/client/api/block_builder/input_selection/remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
types::block::{
address::{Address, Ed25519Address},
output::{unlock_condition::AddressUnlockCondition, BasicOutputBuilder, NativeTokensBuilder, Output},
rent::RentStructure,
},
};

Expand Down Expand Up @@ -65,7 +66,7 @@ impl InputSelection {
let native_tokens_remainder = native_tokens_diff.is_some();

let mut remainder_builder =
BasicOutputBuilder::new_with_minimum_storage_deposit(self.protocol_parameters.rent_parameters())
BasicOutputBuilder::new_with_minimum_storage_deposit(self.protocol_parameters.rent_parameters().into())
.add_unlock_condition(AddressUnlockCondition::new(Address::from(Ed25519Address::from(
[0; 32],
))));
Expand Down Expand Up @@ -144,7 +145,7 @@ impl InputSelection {
log::debug!("Created remainder output of {diff} for {remainder_address:?}");

remainder.verify_storage_deposit(
self.protocol_parameters.rent_parameters(),
self.protocol_parameters.rent_parameters().into(),
self.protocol_parameters.token_supply(),
)?;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
unlock_condition::StorageDepositReturnUnlockCondition, AccountOutputBuilder, AccountTransition,
FoundryOutputBuilder, NftOutputBuilder, Output, OutputId,
},
rent::StorageScore,
rent::{RentStructure, StorageScore},
slot::SlotIndex,
},
};
Expand Down Expand Up @@ -241,7 +241,7 @@ impl InputSelection {
for output in outputs {
let diff = amount_selection.missing_amount();
let amount = output.amount();
let rent = output.rent_cost(self.protocol_parameters.rent_parameters());
let rent = output.rent_cost(self.protocol_parameters.rent_parameters().into());

let new_amount = if amount >= diff + rent { amount - diff } else { rent };

Expand Down
12 changes: 11 additions & 1 deletion sdk/src/types/block/address/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use core::str::FromStr;

use derive_more::{AsRef, Deref, From};

use crate::types::block::{output::AccountId, Error};
use crate::types::block::{
output::AccountId,
rent::{RentStructure, StorageScore},
Error,
};

/// An account address.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, packable::Packable)]
Expand Down Expand Up @@ -57,6 +61,12 @@ impl core::fmt::Debug for AccountAddress {
}
}

impl StorageScore for AccountAddress {
fn score(&self, _rent_struct: RentStructure) -> u64 {
0
}
}

#[cfg(feature = "serde")]
mod dto {
use serde::{Deserialize, Serialize};
Expand Down
16 changes: 15 additions & 1 deletion sdk/src/types/block/address/ed25519.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use core::str::FromStr;
use crypto::signatures::ed25519::PublicKey;
use derive_more::{AsRef, Deref, From};

use crate::types::block::Error;
use crate::types::block::{
rent::{RentStructure, StorageScore},
Error,
};

/// An Ed25519 address.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, packable::Packable)]
Expand All @@ -24,6 +27,11 @@ impl Ed25519Address {
pub fn new(address: [u8; Self::LENGTH]) -> Self {
Self::from(address)
}

/// Creates a dummy [`Ed25519Address`] used to calculate a storage score for implicit account addresses.
pub(crate) fn dummy() -> Self {
Self([0; Self::LENGTH])
}
}

impl FromStr for Ed25519Address {
Expand All @@ -46,6 +54,12 @@ impl core::fmt::Debug for Ed25519Address {
}
}

impl StorageScore for Ed25519Address {
fn score(&self, _rent_struct: RentStructure) -> u64 {
0
}
}

#[cfg(feature = "serde")]
pub(crate) mod dto {
use serde::{Deserialize, Serialize};
Expand Down
18 changes: 6 additions & 12 deletions sdk/src/types/block/address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,13 @@ impl From<&Self> for Address {
}
}

/// A trait to facilitate the rent cost computation for addresses, which is central to dust protection.
pub trait AddressStorageCost {
/// Computes the storage score of an address given a [`RentStructure`].
fn storage_cost(&self, rent_structure: RentStructure) -> u64;
}

impl AddressStorageCost for Address {
fn storage_cost(&self, rent_structure: RentStructure) -> u64 {
impl StorageScore for Address {
fn score(&self, rent_struct: RentStructure) -> u64 {
match self {
Self::Account(_) | Self::Ed25519(_) | Self::Nft(_) => 0,
// TODO: implicit account address and restricted address
// Address::ImplicitAccountCreation(_) =>
// rent_stucture.storage_score_offset_implicit_account_creation_address
Self::Account(account) => account.score(rent_struct),
Self::Ed25519(ed25519) => ed25519.score(rent_struct),
Self::Nft(nft) => nft.score(rent_struct),
// TODO: other address types once merged
}
}
}
12 changes: 11 additions & 1 deletion sdk/src/types/block/address/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use core::str::FromStr;

use derive_more::{AsRef, Deref, From};

use crate::types::block::{output::NftId, Error};
use crate::types::block::{
output::NftId,
rent::{RentStructure, StorageScore},
Error,
};

/// An NFT address.
#[derive(Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, From, AsRef, Deref, packable::Packable)]
Expand Down Expand Up @@ -57,6 +61,12 @@ impl core::fmt::Debug for NftAddress {
}
}

impl StorageScore for NftAddress {
fn score(&self, _rent_struct: RentStructure) -> u64 {
0
}
}

#[cfg(feature = "serde")]
pub(crate) mod dto {
use serde::{Deserialize, Serialize};
Expand Down
Loading

0 comments on commit 824b5e0

Please sign in to comment.