Skip to content

Commit

Permalink
Merge branch '2.0' into refactor-MetadataFeature
Browse files Browse the repository at this point in the history
  • Loading branch information
Thoralf-M authored Jan 15, 2024
2 parents 141c05a + 6db23a1 commit c4d3b6b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
use derive_more::From;

use crate::types::block::{
address::AccountAddress,
address::{AccountAddress, Address},
error::Error,
output::{StorageScore, StorageScoreParameters},
};

/// Defines the permanent [`AccountAddress`] that owns this output.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, From, packable::Packable)]
pub struct ImmutableAccountAddressUnlockCondition(AccountAddress);
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, From, packable::Packable)]
pub struct ImmutableAccountAddressUnlockCondition(#[packable(verify_with = verify_address)] Address);

impl ImmutableAccountAddressUnlockCondition {
/// The [`UnlockCondition`](crate::types::block::output::UnlockCondition) kind of an
Expand All @@ -20,12 +21,12 @@ impl ImmutableAccountAddressUnlockCondition {
/// Creates a new [`ImmutableAccountAddressUnlockCondition`].
#[inline(always)]
pub fn new(address: impl Into<AccountAddress>) -> Self {
Self(address.into())
Self(Address::from(address.into()))
}

/// Returns the account address of an [`ImmutableAccountAddressUnlockCondition`].
pub fn address(&self) -> &AccountAddress {
&self.0
self.0.as_account()
}
}

Expand All @@ -35,6 +36,15 @@ impl StorageScore for ImmutableAccountAddressUnlockCondition {
}
}

#[inline]
fn verify_address<const VERIFY: bool>(address: &Address) -> Result<(), Error> {
if VERIFY && !address.is_account() {
Err(Error::InvalidAddressKind(address.kind()))
} else {
Ok(())
}
}

#[cfg(feature = "serde")]
mod dto {
use serde::{Deserialize, Serialize};
Expand All @@ -45,14 +55,14 @@ mod dto {
struct ImmutableAccountAddressUnlockConditionDto {
#[serde(rename = "type")]
kind: u8,
address: AccountAddress,
address: Address,
}

impl From<&ImmutableAccountAddressUnlockCondition> for ImmutableAccountAddressUnlockConditionDto {
fn from(value: &ImmutableAccountAddressUnlockCondition) -> Self {
Self {
kind: ImmutableAccountAddressUnlockCondition::KIND,
address: value.0,
address: value.0.clone(),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions sdk/tests/types/output/foundry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn builder() {
let mut builder = FoundryOutput::build_with_amount(amount, 234, rand_token_scheme())
.with_serial_number(85)
.with_native_token(NativeToken::new(TokenId::from(foundry_id), 1000).unwrap())
.with_unlock_conditions([account_1])
.with_unlock_conditions([account_1.clone()])
.add_feature(metadata_1.clone())
.replace_feature(metadata_2.clone())
.with_immutable_features([metadata_2.clone()])
Expand All @@ -45,7 +45,7 @@ fn builder() {
.clear_unlock_conditions()
.clear_features()
.clear_immutable_features()
.replace_unlock_condition(account_2);
.replace_unlock_condition(account_2.clone());
let output = builder.clone().finish().unwrap();
assert_eq!(output.unlock_conditions().immutable_account_address(), Some(&account_2));
assert!(output.features().is_empty());
Expand Down

0 comments on commit c4d3b6b

Please sign in to comment.