-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Implement set_attribute
& set_class_attribute
nonfungibles trait functions for Uniques
pallet
#9206
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -18,7 +18,7 @@ | |||||
//! Various pieces of common functionality. | ||||||
|
||||||
use super::*; | ||||||
use frame_support::{ensure, traits::Get}; | ||||||
use frame_support::{ensure, traits::Get, BoundedVec}; | ||||||
use sp_runtime::{DispatchResult, DispatchError}; | ||||||
|
||||||
impl<T: Config<I>, I: 'static> Pallet<T, I> { | ||||||
|
@@ -112,4 +112,47 @@ impl<T: Config<I>, I: 'static> Pallet<T, I> { | |||||
Self::deposit_event(Event::Burned(class, instance, owner)); | ||||||
Ok(()) | ||||||
} | ||||||
|
||||||
pub(super) fn do_set_attribute( | ||||||
class: T::ClassId, | ||||||
maybe_instance: Option<T::InstanceId>, | ||||||
maybe_check_owner: &Option<T::AccountId>, | ||||||
key: BoundedVec<u8, T::KeyLimit>, | ||||||
value: BoundedVec<u8, T::ValueLimit>, | ||||||
with_details: impl FnOnce(&ClassDetailsFor<T, I>) -> DispatchResult, | ||||||
) -> DispatchResult { | ||||||
let mut class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::Unknown)?; | ||||||
with_details(&class_details)?; | ||||||
|
||||||
let maybe_is_frozen = match maybe_instance { | ||||||
None => ClassMetadataOf::<T, I>::get(class).map(|v| v.is_frozen), | ||||||
Some(instance) => InstanceMetadataOf::<T, I>::get(class, instance).map(|v| v.is_frozen), | ||||||
}; | ||||||
ensure!(!maybe_is_frozen.unwrap_or(false), Error::<T, I>::Frozen); | ||||||
|
||||||
let attribute = Attribute::<T, I>::get((class, maybe_instance, &key)); | ||||||
if attribute.is_none() { | ||||||
class_details.attributes.saturating_inc(); | ||||||
} | ||||||
let old_deposit = attribute.map_or(Zero::zero(), |m| m.1); | ||||||
class_details.total_deposit.saturating_reduce(old_deposit); | ||||||
let mut deposit = Zero::zero(); | ||||||
if !class_details.free_holding && maybe_check_owner.is_some() { | ||||||
deposit = T::DepositPerByte::get() | ||||||
.saturating_mul(((key.len() + value.len()) as u32).into()) | ||||||
.saturating_add(T::AttributeDepositBase::get()); | ||||||
} | ||||||
class_details.total_deposit.saturating_accrue(deposit); | ||||||
if deposit > old_deposit { | ||||||
T::Currency::reserve(&class_details.owner, deposit - old_deposit)?; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the suggestion, done here 6fabf25 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is NOT needed, and actually probably some additional overhead. Note the check just 1 line above: This subtraction can never panic please undo |
||||||
} else if deposit < old_deposit { | ||||||
T::Currency::unreserve(&class_details.owner, old_deposit - deposit); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the suggestion, done here 6fabf25 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. undo |
||||||
} | ||||||
|
||||||
Attribute::<T, I>::insert((&class, maybe_instance, &key), (&value, deposit)); | ||||||
Class::<T, I>::insert(class, &class_details); | ||||||
|
||||||
Self::deposit_event(Event::AttributeSet(class, maybe_instance, key, value)); | ||||||
Ok(()) | ||||||
} | ||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the suggestion, done here 6fabf25
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this one i think is okay, but would want you to look closer before actually making this change
and then a test