Skip to content

Commit

Permalink
Some more examples fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thibault-martinez committed Oct 3, 2023
1 parent 72bfa5e commit de48e04
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 270 deletions.
5 changes: 2 additions & 3 deletions sdk/examples/client/output/build_account_output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use iota_sdk::{
address::Address,
output::{
feature::{IssuerFeature, MetadataFeature, SenderFeature},
unlock_condition::{GovernorAddressUnlockCondition, StateControllerAddressUnlockCondition},
unlock_condition::AddressUnlockCondition,
AccountId, AccountOutputBuilder,
},
},
Expand Down Expand Up @@ -48,8 +48,7 @@ async fn main() -> Result<()> {
.add_feature(MetadataFeature::new(metadata)?)
.add_immutable_feature(IssuerFeature::new(address))
.add_immutable_feature(MetadataFeature::new(metadata)?)
.add_unlock_condition(StateControllerAddressUnlockCondition::new(address))
.add_unlock_condition(GovernorAddressUnlockCondition::new(address))
.add_unlock_condition(AddressUnlockCondition::new(address))
.finish_output(token_supply)?;

println!("{account_output:#?}");
Expand Down
12 changes: 2 additions & 10 deletions sdk/examples/how_tos/outputs/unlock_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ use iota_sdk::{
output::{
dto::OutputDto,
unlock_condition::{
AddressUnlockCondition, ExpirationUnlockCondition, GovernorAddressUnlockCondition,
ImmutableAccountAddressUnlockCondition, StateControllerAddressUnlockCondition,
AddressUnlockCondition, ExpirationUnlockCondition, ImmutableAccountAddressUnlockCondition,
StorageDepositReturnUnlockCondition, TimelockUnlockCondition,
},
AccountId, AccountOutputBuilder, BasicOutputBuilder, FoundryOutputBuilder, SimpleTokenScheme, TokenScheme,
BasicOutputBuilder, FoundryOutputBuilder, SimpleTokenScheme, TokenScheme,
},
},
};
Expand All @@ -44,8 +43,6 @@ async fn main() -> Result<()> {

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

Expand All @@ -70,11 +67,6 @@ async fn main() -> Result<()> {
basic_output_builder
.add_unlock_condition(ExpirationUnlockCondition::new(address, 1)?)
.finish_output(token_supply)?,
// with governor and state controller unlock condition
account_output_builder
.add_unlock_condition(GovernorAddressUnlockCondition::new(address))
.add_unlock_condition(StateControllerAddressUnlockCondition::new(address))
.finish_output(token_supply)?,
// with immutable account unlock condition
foundry_output_builder
.add_unlock_condition(ImmutableAccountAddressUnlockCondition::new(
Expand Down
1 change: 1 addition & 0 deletions sdk/src/client/api/block_builder/input_selection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ impl InputSelection {
self.available_inputs.retain(|input| {
// Keep account outputs because at this point we do not know if a state or governor address will be
// required.
// TODO remove ?
if input.output.is_account() {
return true;
}
Expand Down
1 change: 1 addition & 0 deletions sdk/src/client/node_api/indexer/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ impl ClientInner {
self.get_output_ids(route, query_parameters, true, false).await
}

// TODO
/// Get account outputs filtered by the given parameters.
/// GET with query parameter returns all outputIDs that fit these filter criteria.
/// Query parameters: "stateController", "governor", "issuer", "sender", "createdBefore", "createdAfter"
Expand Down
4 changes: 4 additions & 0 deletions sdk/src/types/block/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub enum Error {
// TODO remove?
SelfControlledAccountOutput(AccountId),
SelfControlledAnchorOutput(AnchorId),
SelfDepositAccount(AccountId),
SelfDepositNft(NftId),
SignaturePublicKeyMismatch {
expected: String,
Expand Down Expand Up @@ -354,6 +355,9 @@ impl fmt::Display for Error {
Self::SelfDepositNft(nft_id) => {
write!(f, "self deposit nft output, NFT ID {nft_id}")
}
Self::SelfDepositAccount(account_id) => {
write!(f, "self deposit account output, account ID {account_id}")
}
Self::SignaturePublicKeyMismatch { expected, actual } => {
write!(f, "signature public key mismatch: expected {expected} but got {actual}",)
}
Expand Down
39 changes: 13 additions & 26 deletions sdk/src/types/block/output/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ impl From<&OutputId> for AccountId {
impl AccountId {
///
pub fn or_from_output_id(self, output_id: &OutputId) -> Self {
if self.is_null() { Self::from(output_id) } else { self }
if self.is_null() {
Self::from(output_id)
} else {
self
}
}
}

Expand Down Expand Up @@ -628,24 +632,14 @@ fn verify_index_counter(account_id: &AccountId, foundry_counter: u32) -> Result<
}

fn verify_unlock_conditions(unlock_conditions: &UnlockConditions, account_id: &AccountId) -> Result<(), Error> {
if let Some(unlock_condition) = unlock_conditions.state_controller_address() {
if let Address::Account(account_address) = unlock_condition.address() {
if account_address.account_id() == account_id {
return Err(Error::SelfControlledAccountOutput(*account_id));
}
}
} else {
return Err(Error::MissingStateControllerUnlockCondition);
}

if let Some(unlock_condition) = unlock_conditions.governor_address() {
if let Some(unlock_condition) = unlock_conditions.address() {
if let Address::Account(account_address) = unlock_condition.address() {
if account_address.account_id() == account_id {
return Err(Error::SelfControlledAccountOutput(*account_id));
return Err(Error::SelfDepositAccount(*account_id));
}
}
} else {
return Err(Error::MissingGovernorUnlockCondition);
return Err(Error::MissingAddressUnlockCondition);
}

verify_allowed_unlock_conditions(unlock_conditions, AccountOutput::ALLOWED_UNLOCK_CONDITIONS)
Expand Down Expand Up @@ -782,12 +776,8 @@ mod tests {
rand::{
address::rand_account_address,
output::{
feature::rand_allowed_features,
rand_account_id, rand_account_output,
unlock_condition::{
rand_governor_address_unlock_condition_different_from,
rand_state_controller_address_unlock_condition_different_from,
},
feature::rand_allowed_features, rand_account_id, rand_account_output,
unlock_condition::rand_address_unlock_condition_different_from_account_id,
},
},
},
Expand Down Expand Up @@ -820,8 +810,7 @@ mod tests {

let account_id = rand_account_id();
let foundry_id = FoundryId::build(&rand_account_address(), 0, SimpleTokenScheme::KIND);
let gov_address = rand_governor_address_unlock_condition_different_from(&account_id);
let state_address = rand_state_controller_address_unlock_condition_different_from(&account_id);
let address = rand_address_unlock_condition_different_from_account_id(&account_id);

let test_split_dto = |builder: AccountOutputBuilder| {
let output_split = AccountOutput::try_from_dtos(
Expand All @@ -841,17 +830,15 @@ mod tests {

let builder = AccountOutput::build_with_amount(100, account_id)
.add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap())
.add_unlock_condition(gov_address)
.add_unlock_condition(state_address)
.add_unlock_condition(address)
.with_features(rand_allowed_features(AccountOutput::ALLOWED_FEATURES))
.with_immutable_features(rand_allowed_features(AccountOutput::ALLOWED_IMMUTABLE_FEATURES));
test_split_dto(builder);

let builder =
AccountOutput::build_with_minimum_storage_deposit(protocol_parameters.rent_structure(), account_id)
.add_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap())
.add_unlock_condition(gov_address)
.add_unlock_condition(state_address)
.add_unlock_condition(address)
.with_features(rand_allowed_features(AccountOutput::ALLOWED_FEATURES))
.with_immutable_features(rand_allowed_features(AccountOutput::ALLOWED_IMMUTABLE_FEATURES));
test_split_dto(builder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use derive_more::From;

use crate::types::block::address::Address;

// TODO
/// Defines the Governor Address that owns this output, that is, it can unlock it with the proper Unlock in a
/// transaction that governance transitions the account output.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, From, packable::Packable)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use derive_more::From;

use crate::types::block::address::Address;

// TODO
/// Defines the State Controller Address that owns this output, that is, it can unlock it with the proper Unlock in a
/// transaction that state transitions the account output.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, From, packable::Packable)]
Expand Down
8 changes: 2 additions & 6 deletions sdk/src/types/block/rand/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ use crate::types::block::{
feature::rand_allowed_features,
unlock_condition::{
rand_address_unlock_condition, rand_address_unlock_condition_different_from,
rand_governor_address_unlock_condition_different_from,
rand_state_controller_address_unlock_condition_different_from,
rand_address_unlock_condition_different_from_account_id,
},
},
transaction::rand_transaction_id,
Expand Down Expand Up @@ -59,10 +58,7 @@ pub fn rand_account_output(token_supply: u64) -> AccountOutput {

AccountOutput::build_with_amount(rand_number_range(Output::AMOUNT_MIN..token_supply), account_id)
.with_features(rand_allowed_features(AccountOutput::ALLOWED_FEATURES))
.add_unlock_condition(rand_state_controller_address_unlock_condition_different_from(
&account_id,
))
.add_unlock_condition(rand_governor_address_unlock_condition_different_from(&account_id))
.add_unlock_condition(rand_address_unlock_condition_different_from_account_id(&account_id))
.finish_with_params(token_supply)
.unwrap()
}
Expand Down
13 changes: 13 additions & 0 deletions sdk/src/types/block/rand/output/unlock_condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ pub fn rand_governor_address_unlock_condition_different_from(account_id: &Accoun
address.into()
}

/// Generates a random [`AddressUnlockCondition`] that is different from `account_id`.
pub fn rand_address_unlock_condition_different_from_account_id(account_id: &AccountId) -> AddressUnlockCondition {
let mut address = rand_address();

if let Address::Account(mut account_address) = &mut address {
while account_address.account_id() == account_id {
account_address = rand_account_address();
}
}

address.into()
}

/// Generates a random [`AddressUnlockCondition`] that is different from `nft_id`.
pub fn rand_address_unlock_condition_different_from(nft_id: &NftId) -> AddressUnlockCondition {
let mut address = rand_address();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ use crate::{
types::block::{
address::Bech32Address,
output::{
feature::MetadataFeature,
unlock_condition::{GovernorAddressUnlockCondition, StateControllerAddressUnlockCondition},
AccountId, AccountOutputBuilder, Output,
feature::MetadataFeature, unlock_condition::AddressUnlockCondition, AccountId, AccountOutputBuilder, Output,
},
},
utils::serde::option_prefix_hex_bytes,
Expand Down Expand Up @@ -74,7 +72,7 @@ where
let rent_structure = self.client().get_rent_structure().await?;
let token_supply = self.client().get_token_supply().await?;

let controller_address = match params.as_ref().and_then(|options| options.address.as_ref()) {
let address = match params.as_ref().and_then(|options| options.address.as_ref()) {
Some(bech32_address) => {
self.client().bech32_hrp_matches(bech32_address.hrp()).await?;
*bech32_address.inner()
Expand All @@ -92,8 +90,7 @@ where
let mut account_output_builder =
AccountOutputBuilder::new_with_minimum_storage_deposit(rent_structure, AccountId::null())
.with_foundry_counter(0)
.add_unlock_condition(StateControllerAddressUnlockCondition::new(controller_address))
.add_unlock_condition(GovernorAddressUnlockCondition::new(controller_address));
.add_unlock_condition(AddressUnlockCondition::new(address));
if let Some(CreateAccountParams {
immutable_metadata,
metadata,
Expand Down
Loading

0 comments on commit de48e04

Please sign in to comment.