Skip to content

Commit

Permalink
Merge pull request #10 from uni-arts-chain/dev
Browse files Browse the repository at this point in the history
add update_item_properties
  • Loading branch information
tuminfei authored May 20, 2022
2 parents 1505351 + e10b5d2 commit 6767f97
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pallets/nft-multi/src/default_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ impl crate::WeightInfo for () {
.saturating_add(DbWeight::get().reads(5 as Weight))
.saturating_add(DbWeight::get().writes(3 as Weight))
}
fn update_item_properties() -> Weight {
(66_234_000 as Weight)
.saturating_add(DbWeight::get().reads(5 as Weight))
}
fn burn_item() -> Weight {
(66_234_000 as Weight)
.saturating_add(DbWeight::get().reads(5 as Weight))
Expand Down
62 changes: 62 additions & 0 deletions pallets/nft-multi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub trait WeightInfo {
fn create_item() -> Weight;
fn burn_item() -> Weight;
fn transfer() -> Weight;
fn update_item_properties() -> Weight;
fn admin_transfer() -> Weight;
fn transfer_and_lock() -> Weight;
fn batch_create_nft_item() -> Weight;
Expand Down Expand Up @@ -410,6 +411,7 @@ decl_event!(
Created(u64, u8, AccountId),
ItemCreated(u64, u64),
ItemDestroyed( u64, u64),
ItemUpdated(u64, u64),
ItemTransfer(u64, u64, u64, AccountId, AccountId),
AdminTransfer(u64, u64, u64, AccountId, AccountId),
ItemLock(u64, u64, u64, AccountId, AccountId),
Expand Down Expand Up @@ -437,6 +439,7 @@ decl_error! {
PermissionError,
CollectionModeInvalid,
AmountValueError,
NotSupportedForType,
}
}

Expand Down Expand Up @@ -882,6 +885,65 @@ decl_module! {
Ok(())
}

#[weight = T::WeightInfo::update_item_properties()]
pub fn update_item_properties(origin, collection_id: u64, item_id: u64, properties: Vec<u8>) -> DispatchResult {

let sender = ensure_signed(origin)?;
Self::collection_exists(collection_id)?;
let item_owner = Self::is_item_owner(sender.clone(), collection_id, item_id);
if !item_owner
{
if !Self::is_owner_or_admin_permissions(collection_id, sender.clone()) {
Self::check_white_list(collection_id, sender.clone())?;
}
}
let target_collection = <Collection<T>>::get(collection_id);

match target_collection.mode
{
CollectionMode::NFT(_) => {
// check size
ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");
ensure!(
<NftItemList<T>>::contains_key(collection_id, item_id),
"Item not exists"
);


<NftItemList<T>>::mutate(collection_id, item_id, |item| {
item.data = properties.clone();
});

},
CollectionMode::Fungible(_) => {
ensure!(1 == 0, Error::<T>::NotSupportedForType);
},
CollectionMode::ReFungible(_, _) => {
// check size
ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");
ensure!(
<ReFungibleItemList<T>>::contains_key(collection_id, item_id),
"Item not exists"
);


<ReFungibleItemList<T>>::mutate(collection_id, item_id, |item| {
item.data = properties.clone();
});
},
_ => {
ensure!(1 == 0, Error::<T>::NotSupportedForType);
},

};


// call event
Self::deposit_event(RawEvent::ItemUpdated(collection_id, item_id));

Ok(())
}

#[weight = T::WeightInfo::burn_item()]
pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {

Expand Down
5 changes: 5 additions & 0 deletions pallets/nft/src/default_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ impl crate::WeightInfo for () {
.saturating_add(DbWeight::get().reads(5 as Weight))
.saturating_add(DbWeight::get().writes(3 as Weight))
}
fn update_item_properties() -> Weight {
(66_234_000 as Weight)
.saturating_add(DbWeight::get().reads(5 as Weight))
.saturating_add(DbWeight::get().writes(3 as Weight))
}
fn burn_item() -> Weight {
(66_234_000 as Weight)
.saturating_add(DbWeight::get().reads(5 as Weight))
Expand Down
63 changes: 63 additions & 0 deletions pallets/nft/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub trait WeightInfo {
fn remove_collection_sponsor() -> Weight;
fn create_item() -> Weight;
fn burn_item() -> Weight;
fn update_item_properties() -> Weight;
fn transfer() -> Weight;
fn transfer_and_lock() -> Weight;
fn batch_create_nft_item() -> Weight;
Expand Down Expand Up @@ -382,6 +383,7 @@ decl_event!(
Created(u64, u8, AccountId),
ItemCreated(u64, u64),
ItemDestroyed(u64, u64),
ItemUpdated(u64, u64),
ItemTransfer(u64, u64, u64, AccountId, AccountId),
ItemLock(u64, u64, u64, AccountId, AccountId),
ItemUnlock(u64, u64, u64, AccountId, AccountId),
Expand All @@ -408,6 +410,7 @@ decl_error! {
PermissionError,
CollectionModeInvalid,
AmountValueError,
NotSupportedForType,
}
}

Expand Down Expand Up @@ -848,6 +851,65 @@ decl_module! {
Ok(())
}

#[weight = T::WeightInfo::update_item_properties()]
pub fn update_item_properties(origin, collection_id: u64, item_id: u64, properties: Vec<u8>) -> DispatchResult {

let sender = ensure_signed(origin)?;
Self::collection_exists(collection_id)?;
let item_owner = Self::is_item_owner(sender.clone(), collection_id, item_id);
if !item_owner
{
if !Self::is_owner_or_admin_permissions(collection_id, sender.clone()) {
Self::check_white_list(collection_id, sender.clone())?;
}
}
let target_collection = <Collection<T>>::get(collection_id);

match target_collection.mode
{
CollectionMode::NFT(_) => {
// check size
ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");
ensure!(
<NftItemList<T>>::contains_key(collection_id, item_id),
"Item not exists"
);


<NftItemList<T>>::mutate(collection_id, item_id, |item| {
item.data = properties.clone();
});

},
CollectionMode::Fungible(_) => {
ensure!(1 == 0, Error::<T>::NotSupportedForType);
},
CollectionMode::ReFungible(_, _) => {
// check size
ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");
ensure!(
<ReFungibleItemList<T>>::contains_key(collection_id, item_id),
"Item not exists"
);


<ReFungibleItemList<T>>::mutate(collection_id, item_id, |item| {
item.data = properties.clone();
});
},
_ => {
ensure!(1 == 0, Error::<T>::NotSupportedForType);
},

};


// call event
Self::deposit_event(RawEvent::ItemUpdated(collection_id, item_id));

Ok(())
}

#[weight = T::WeightInfo::burn_item()]
pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {

Expand Down Expand Up @@ -1766,6 +1828,7 @@ impl<T: Config> Module<T> {
Ok(())
}


fn burn_nft_item(collection_id: u64, item_id: u64) -> DispatchResult {
ensure!(
<NftItemList<T>>::contains_key(collection_id, item_id),
Expand Down

0 comments on commit 6767f97

Please sign in to comment.