Skip to content

Commit

Permalink
Remove output token supply check (#1599)
Browse files Browse the repository at this point in the history
* Remove output token supply check

* Remove unneeded lifetimes
  • Loading branch information
thibault-martinez authored Nov 8, 2023
1 parent 0b7f6ae commit 544311a
Show file tree
Hide file tree
Showing 58 changed files with 207 additions and 345 deletions.
3 changes: 1 addition & 2 deletions cli/src/wallet_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,6 @@ pub async fn send_native_token_command(
let transaction = if gift_storage_deposit.unwrap_or(false) {
// Send native tokens together with the required storage deposit
let rent_structure = wallet.client().get_rent_structure().await?;
let token_supply = wallet.client().get_token_supply().await?;

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

Expand All @@ -741,7 +740,7 @@ pub async fn send_native_token_command(
TokenId::from_str(&token_id)?,
U256::from_dec_str(&amount).map_err(|e| Error::Miscellaneous(e.to_string()))?,
)?])
.finish_output(token_supply)?];
.finish_output()?];

wallet.send_outputs(outputs, None).await?
} else {
Expand Down
3 changes: 1 addition & 2 deletions sdk/examples/client/output/build_account_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ async fn main() -> Result<()> {
.finish()
.await?;

let token_supply = client.get_token_supply().await?;
let rent_structure = client.get_rent_structure().await?;

let address = std::env::args()
Expand All @@ -49,7 +48,7 @@ async fn main() -> Result<()> {
.add_immutable_feature(IssuerFeature::new(address.clone()))
.add_immutable_feature(MetadataFeature::new(metadata)?)
.add_unlock_condition(AddressUnlockCondition::new(address))
.finish_output(token_supply)?;
.finish_output()?;

println!("{account_output:#?}");

Expand Down
14 changes: 7 additions & 7 deletions sdk/examples/client/output/build_basic_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@ async fn main() -> Result<()> {

let outputs = [
// most simple output
basic_output_builder.clone().finish_output(token_supply)?,
basic_output_builder.clone().finish_output()?,
// with metadata feature block
basic_output_builder
.clone()
.add_feature(MetadataFeature::new(METADATA)?)
.finish_output(token_supply)?,
.finish_output()?,
// with storage deposit return
basic_output_builder
.clone()
Expand All @@ -62,26 +62,26 @@ async fn main() -> Result<()> {
1_000_000,
token_supply,
)?)
.finish_output(token_supply)?,
.finish_output()?,
// with expiration
basic_output_builder
.clone()
.add_unlock_condition(ExpirationUnlockCondition::new(address.clone(), 1)?)
.finish_output(token_supply)?,
.finish_output()?,
// with timelock
basic_output_builder
.clone()
.add_unlock_condition(TimelockUnlockCondition::new(1)?)
.finish_output(token_supply)?,
.finish_output()?,
// with tag feature
basic_output_builder
.clone()
.add_feature(TagFeature::new(METADATA)?)
.finish_output(token_supply)?,
.finish_output()?,
// with sender feature
basic_output_builder
.add_feature(SenderFeature::new(address))
.finish_output(token_supply)?,
.finish_output()?,
];

println!("{outputs:#?}");
Expand Down
3 changes: 1 addition & 2 deletions sdk/examples/client/output/build_nft_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ async fn main() -> Result<()> {
.finish()
.await?;

let token_supply = client.get_token_supply().await?;
let rent_structure = client.get_rent_structure().await?;

let address = std::env::args()
Expand Down Expand Up @@ -63,7 +62,7 @@ async fn main() -> Result<()> {
.add_feature(TagFeature::new(TAG)?)
.add_immutable_feature(IssuerFeature::new(address))
.add_immutable_feature(MetadataFeature::new(tip_27_immutable_metadata)?)
.finish_output(token_supply)?;
.finish_output()?;

println!("{nft_output:#?}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! cargo run --release --all-features --example check_balance
//! ```

use iota_sdk::{types::block::address::ToBech32Ext, wallet::Result, Wallet};
use iota_sdk::{wallet::Result, Wallet};

#[tokio::main]
async fn main() -> Result<()> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn main() -> Result<()> {
"rms1qpszqzadsym6wpppd6z037dvlejmjuke7s24hm95s9fg9vpua7vluaw60xu",
)?))
.add_unlock_condition(TimelockUnlockCondition::new(slot_index)?)
.finish_output(wallet.client().get_token_supply().await?)?;
.finish_output()?;

let transaction = wallet.send_outputs(vec![basic_output], None).await?;
println!("Transaction sent: {}", transaction.transaction_id);
Expand Down
3 changes: 1 addition & 2 deletions sdk/examples/how_tos/nfts/mint_nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,13 @@ async fn main() -> Result<()> {
println!("Minted NFT 1");

// Build an NFT manually by using the `NftOutputBuilder`
let token_supply = wallet.client().get_token_supply().await?;
let outputs = [
// address of the owner of the NFT
NftOutputBuilder::new_with_amount(NFT2_AMOUNT, NftId::null())
.add_unlock_condition(AddressUnlockCondition::new(sender_address.clone()))
.add_feature(SenderFeature::new(sender_address.clone()))
.add_immutable_feature(IssuerFeature::new(sender_address))
.finish_output(token_supply)?,
.finish_output()?,
];

let transaction = wallet.send_outputs(outputs, None).await?;
Expand Down
11 changes: 5 additions & 6 deletions sdk/examples/how_tos/outputs/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,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 rent_structure = client.get_rent_structure().await?;

let address = Address::try_from_bech32("rms1qpllaj0pyveqfkwxmnngz2c488hfdtmfrj3wfkgxtk4gtyrax0jaxzt70zy")?;
Expand All @@ -44,26 +43,26 @@ async fn main() -> Result<()> {
nft_output_builder
.clone()
.add_feature(SenderFeature::new(address.clone()))
.finish_output(token_supply)?,
.finish_output()?,
// with issuer feature
nft_output_builder
.clone()
.add_immutable_feature(IssuerFeature::new(address))
.finish_output(token_supply)?,
.finish_output()?,
// with metadata feature block
nft_output_builder
.clone()
.add_feature(MetadataFeature::new("Hello, World!")?)
.finish_output(token_supply)?,
.finish_output()?,
// with immutable metadata feature block
nft_output_builder
.clone()
.add_immutable_feature(MetadataFeature::new("Hello, World!")?)
.finish_output(token_supply)?,
.finish_output()?,
// with tag feature
nft_output_builder
.add_feature(TagFeature::new("Hello, World!")?)
.finish_output(token_supply)?,
.finish_output()?,
];

// Convert ouput array to json array
Expand Down
10 changes: 5 additions & 5 deletions sdk/examples/how_tos/outputs/unlock_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async fn main() -> Result<()> {

let outputs = [
//// most simple output
basic_output_builder.clone().finish_output(token_supply)?,
basic_output_builder.clone().finish_output()?,
// with storage deposit return unlock condition
basic_output_builder
.clone()
Expand All @@ -57,22 +57,22 @@ async fn main() -> Result<()> {
1000000,
token_supply,
)?)
.finish_output(token_supply)?,
.finish_output()?,
// with timeout unlock condition
basic_output_builder
.clone()
.add_unlock_condition(TimelockUnlockCondition::new(1)?)
.finish_output(token_supply)?,
.finish_output()?,
// with expiration unlock condition
basic_output_builder
.add_unlock_condition(ExpirationUnlockCondition::new(address.clone(), 1)?)
.finish_output(token_supply)?,
.finish_output()?,
// with immutable account unlock condition
foundry_output_builder
.add_unlock_condition(ImmutableAccountAddressUnlockCondition::new(
*account_address.as_account(),
))
.finish_output(token_supply)?,
.finish_output()?,
];

// Convert ouput array to json array
Expand Down
7 changes: 2 additions & 5 deletions sdk/examples/wallet/17_check_unlock_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@
//! ```

use iota_sdk::{
types::block::{
address::Bech32Address,
output::{unlock_condition::AddressUnlockCondition, BasicOutputBuilder, UnlockCondition},
},
types::block::output::{unlock_condition::AddressUnlockCondition, BasicOutputBuilder, UnlockCondition},
wallet::Result,
Wallet,
};
Expand All @@ -38,7 +35,7 @@ async fn main() -> Result<()> {

let output = BasicOutputBuilder::new_with_amount(AMOUNT)
.add_unlock_condition(AddressUnlockCondition::new(wallet_address.clone()))
.finish_output(wallet.client().get_token_supply().await?)?;
.finish_output()?;

let controlled_by_account = if let [UnlockCondition::Address(address_unlock_condition)] = output
.unlock_conditions()
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/wallet/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ async fn main() -> Result<()> {
// send transaction
let outputs = [BasicOutputBuilder::new_with_amount(SEND_AMOUNT)
.add_unlock_condition(AddressUnlockCondition::new(Address::try_from_bech32(RECV_ADDRESS)?))
.finish_output(wallet.client().get_token_supply().await?)?];
.finish_output()?];

let transaction = wallet.send_outputs(outputs, None).await?;
println!("Transaction sent: {}", transaction.transaction_id);
Expand Down
2 changes: 1 addition & 1 deletion sdk/examples/wallet/offline_signing/0_generate_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use iota_sdk::{
client::{
constants::{SHIMMER_BECH32_HRP, SHIMMER_COIN_TYPE},
constants::SHIMMER_COIN_TYPE,
secret::{stronghold::StrongholdSecretManager, SecretManager},
},
crypto::keys::{bip39::Mnemonic, bip44::Bip44},
Expand Down
16 changes: 4 additions & 12 deletions sdk/src/client/api/block_builder/input_selection/remainder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,7 @@ impl InputSelection {
remainder_builder = remainder_builder.with_native_tokens(native_tokens);
}

Ok((
remainder_builder
.finish_output(self.protocol_parameters.token_supply())?
.amount(),
native_tokens_remainder,
))
Ok((remainder_builder.finish_output()?.amount(), native_tokens_remainder))
}

pub(crate) fn remainder_and_storage_deposit_return_outputs(
Expand All @@ -97,7 +92,7 @@ impl InputSelection {
let diff = amount - output_sdr_amount;
let srd_output = BasicOutputBuilder::new_with_amount(diff)
.with_unlock_conditions([AddressUnlockCondition::new(address.clone())])
.finish_output(self.protocol_parameters.token_supply())?;
.finish_output()?;

// TODO verify_storage_deposit ?

Expand Down Expand Up @@ -141,14 +136,11 @@ impl InputSelection {
remainder_builder = remainder_builder.with_native_tokens(native_tokens);
}

let remainder = remainder_builder.finish_output(self.protocol_parameters.token_supply())?;
let remainder = remainder_builder.finish_output()?;

log::debug!("Created remainder output of {diff} for {remainder_address:?}");

remainder.verify_storage_deposit(
self.protocol_parameters.rent_structure(),
self.protocol_parameters.token_supply(),
)?;
remainder.verify_storage_deposit(self.protocol_parameters.rent_structure())?;

Ok((
Some(RemainderData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,13 +249,13 @@ impl InputSelection {
let new_output = match output {
Output::Account(output) => AccountOutputBuilder::from(&*output)
.with_amount(new_amount)
.finish_output(self.protocol_parameters.token_supply())?,
.finish_output()?,
Output::Foundry(output) => FoundryOutputBuilder::from(&*output)
.with_amount(new_amount)
.finish_output(self.protocol_parameters.token_supply())?,
.finish_output()?,
Output::Nft(output) => NftOutputBuilder::from(&*output)
.with_amount(new_amount)
.finish_output(self.protocol_parameters.token_supply())?,
.finish_output()?,
_ => panic!("only account, nft and foundry can be automatically created"),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl InputSelection {
.with_foundry_counter(u32::max(highest_foundry_serial_number, input.foundry_counter()))
.with_features(features);

let output = builder.finish_output(self.protocol_parameters.token_supply())?;
let output = builder.finish_output()?;

self.automatically_transitioned.insert(ChainId::from(account_id));

Expand Down Expand Up @@ -100,7 +100,7 @@ impl InputSelection {
let output = NftOutputBuilder::from(input)
.with_nft_id(nft_id)
.with_features(features)
.finish_output(self.protocol_parameters.token_supply())?;
.finish_output()?;

self.automatically_transitioned.insert(ChainId::from(nft_id));

Expand Down Expand Up @@ -138,7 +138,7 @@ impl InputSelection {
return Ok(None);
}

let output = FoundryOutputBuilder::from(input).finish_output(self.protocol_parameters.token_supply())?;
let output = FoundryOutputBuilder::from(input).finish_output()?;

self.automatically_transitioned.insert(ChainId::from(foundry_id));

Expand Down
29 changes: 7 additions & 22 deletions sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ use crate::types::{
unlock_condition::{
verify_allowed_unlock_conditions, UnlockCondition, UnlockConditionFlags, UnlockConditions,
},
verify_output_amount_min, verify_output_amount_packable, verify_output_amount_supply, ChainId, NativeToken,
NativeTokens, Output, OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError,
StateTransitionVerifier,
verify_output_amount_min, verify_output_amount_packable, ChainId, NativeToken, NativeTokens, Output,
OutputBuilderAmount, OutputId, Rent, RentStructure, StateTransitionError, StateTransitionVerifier,
},
payload::signed_transaction::TransactionCapabilityFlag,
protocol::ProtocolParameters,
Expand Down Expand Up @@ -271,23 +270,9 @@ impl AccountOutputBuilder {
Ok(output)
}

///
pub fn finish_with_params<'a>(
self,
params: impl Into<ValidationParams<'a>> + Send,
) -> Result<AccountOutput, Error> {
let output = self.finish()?;

if let Some(token_supply) = params.into().token_supply() {
verify_output_amount_supply(output.amount, token_supply)?;
}

Ok(output)
}

/// Finishes the [`AccountOutputBuilder`] into an [`Output`].
pub fn finish_output<'a>(self, params: impl Into<ValidationParams<'a>> + Send) -> Result<Output, Error> {
Ok(Output::Account(self.finish_with_params(params)?))
pub fn finish_output(self) -> Result<Output, Error> {
Ok(Output::Account(self.finish()?))
}
}

Expand Down Expand Up @@ -702,7 +687,7 @@ pub(crate) mod dto {
builder = builder.add_unlock_condition(UnlockCondition::try_from_dto_with_params(u, &params)?);
}

builder.finish_with_params(params)
builder.finish()
}
}

Expand Down Expand Up @@ -750,7 +735,7 @@ pub(crate) mod dto {
builder = builder.with_immutable_features(immutable_features);
}

builder.finish_with_params(params)
builder.finish()
}
}
}
Expand Down Expand Up @@ -816,7 +801,7 @@ mod tests {
&protocol_parameters,
)
.unwrap();
assert_eq!(builder.finish_with_params(&protocol_parameters).unwrap(), output_split);
assert_eq!(builder.finish().unwrap(), output_split);
};

let builder = AccountOutput::build_with_amount(100, account_id)
Expand Down
Loading

0 comments on commit 544311a

Please sign in to comment.