Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Allow to clear attributes via traits (#13055)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsidorenko authored Jan 4, 2023
1 parent 2967726 commit d1fe09c
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
43 changes: 43 additions & 0 deletions frame/nfts/src/impl_nonfungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,49 @@ impl<T: Config<I>, I: 'static> Mutate<<T as SystemConfig>::AccountId, ItemConfig
})
})
}

fn clear_attribute(
collection: &Self::CollectionId,
item: &Self::ItemId,
key: &[u8],
) -> DispatchResult {
Self::do_clear_attribute(
None,
*collection,
Some(*item),
AttributeNamespace::Pallet,
Self::construct_attribute_key(key.to_vec())?,
)
}

fn clear_typed_attribute<K: Encode>(
collection: &Self::CollectionId,
item: &Self::ItemId,
key: &K,
) -> DispatchResult {
key.using_encoded(|k| {
<Self as Mutate<T::AccountId, ItemConfig>>::clear_attribute(collection, item, k)
})
}

fn clear_collection_attribute(collection: &Self::CollectionId, key: &[u8]) -> DispatchResult {
Self::do_clear_attribute(
None,
*collection,
None,
AttributeNamespace::Pallet,
Self::construct_attribute_key(key.to_vec())?,
)
}

fn clear_typed_collection_attribute<K: Encode>(
collection: &Self::CollectionId,
key: &K,
) -> DispatchResult {
key.using_encoded(|k| {
<Self as Mutate<T::AccountId, ItemConfig>>::clear_collection_attribute(collection, k)
})
}
}

impl<T: Config<I>, I: 'static> Transfer<T::AccountId> for Pallet<T, I> {
Expand Down
16 changes: 16 additions & 0 deletions frame/nfts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,22 @@ fn validate_deposit_required_setting() {
assert_eq!(Balances::reserved_balance(1), 0);
assert_eq!(Balances::reserved_balance(2), 3);
assert_eq!(Balances::reserved_balance(3), 3);

assert_ok!(
<Nfts as Mutate<<Test as SystemConfig>::AccountId, ItemConfig>>::clear_attribute(
&0,
&0,
&[3],
)
);
assert_eq!(
attributes(0),
vec![
(Some(0), AttributeNamespace::CollectionOwner, bvec![0], bvec![0]),
(Some(0), AttributeNamespace::ItemOwner, bvec![1], bvec![0]),
(Some(0), AttributeNamespace::Account(3), bvec![2], bvec![0]),
]
);
});
}

Expand Down
24 changes: 24 additions & 0 deletions frame/support/src/traits/tokens/nonfungible_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ pub trait Mutate<AccountId, ItemConfig>: Inspect<AccountId> {
) -> DispatchResult {
key.using_encoded(|k| value.using_encoded(|v| Self::set_attribute(item, k, v)))
}

/// Clear attribute of `item`'s `key`.
///
/// By default, this is not a supported operation.
fn clear_attribute(_item: &Self::ItemId, _key: &[u8]) -> DispatchResult {
Err(TokenError::Unsupported.into())
}

/// Attempt to clear the strongly-typed attribute of `item`'s `key`.
///
/// By default this just attempts to use `clear_attribute`.
fn clear_typed_attribute<K: Encode>(item: &Self::ItemId, key: &K) -> DispatchResult {
key.using_encoded(|k| Self::clear_attribute(item, k))
}
}

/// Trait for transferring a non-fungible item.
Expand Down Expand Up @@ -234,6 +248,16 @@ impl<
value,
)
}
fn clear_attribute(item: &Self::ItemId, key: &[u8]) -> DispatchResult {
<F as nonfungibles::Mutate<AccountId, ItemConfig>>::clear_attribute(&A::get(), item, key)
}
fn clear_typed_attribute<K: Encode>(item: &Self::ItemId, key: &K) -> DispatchResult {
<F as nonfungibles::Mutate<AccountId, ItemConfig>>::clear_typed_attribute(
&A::get(),
item,
key,
)
}
}

impl<
Expand Down
39 changes: 39 additions & 0 deletions frame/support/src/traits/tokens/nonfungibles_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,45 @@ pub trait Mutate<AccountId, ItemConfig>: Inspect<AccountId> {
value.using_encoded(|v| Self::set_collection_attribute(collection, k, v))
})
}

/// Clear attribute of `item` of `collection`'s `key`.
///
/// By default, this is not a supported operation.
fn clear_attribute(
_collection: &Self::CollectionId,
_item: &Self::ItemId,
_key: &[u8],
) -> DispatchResult {
Err(TokenError::Unsupported.into())
}

/// Attempt to clear the strongly-typed attribute of `item` of `collection`'s `key`.
///
/// By default this just attempts to use `clear_attribute`.
fn clear_typed_attribute<K: Encode>(
collection: &Self::CollectionId,
item: &Self::ItemId,
key: &K,
) -> DispatchResult {
key.using_encoded(|k| Self::clear_attribute(collection, item, k))
}

/// Clear attribute of `collection`'s `key`.
///
/// By default, this is not a supported operation.
fn clear_collection_attribute(_collection: &Self::CollectionId, _key: &[u8]) -> DispatchResult {
Err(TokenError::Unsupported.into())
}

/// Attempt to clear the strongly-typed attribute of `collection`'s `key`.
///
/// By default this just attempts to use `clear_attribute`.
fn clear_typed_collection_attribute<K: Encode>(
collection: &Self::CollectionId,
key: &K,
) -> DispatchResult {
key.using_encoded(|k| Self::clear_collection_attribute(collection, k))
}
}

/// Trait for transferring non-fungible sets of items.
Expand Down

0 comments on commit d1fe09c

Please sign in to comment.