difftreelog
warning removed
in: master
3 files changed
pallets/nft/src/lib.rsdiffbeforeafterboth--- a/pallets/nft/src/lib.rs
+++ b/pallets/nft/src/lib.rs
@@ -1,5 +1,6 @@
#![cfg_attr(not(feature = "std"), no_std)]
+use codec::{Decode, Encode};
/// A FRAME pallet template with necessary imports
/// Feel free to remove or edit this file as needed.
@@ -8,13 +9,8 @@
/// For more guidance on Substrate FRAME, see the example pallet
/// https://github.com/paritytech/substrate/blob/master/frame/example/src/lib.rs
-
-use frame_support::{
- dispatch::DispatchResult, decl_module, decl_storage, decl_event,
- ensure,
-};
-use frame_system::{self as system, ensure_signed };
-use codec::{Encode, Decode};
+use frame_support::{decl_event, decl_module, decl_storage, dispatch::DispatchResult, ensure};
+use frame_system::{self as system, ensure_signed};
use sp_runtime::sp_std::prelude::Vec;
#[cfg(test)]
@@ -26,391 +22,367 @@
#[derive(Encode, Decode, Default, Clone, PartialEq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct CollectionType<AccountId> {
- pub owner: AccountId,
- pub next_item_id: u64,
- pub custom_data_size: u32,
+ pub owner: AccountId,
+ pub next_item_id: u64,
+ pub custom_data_size: u32,
}
#[derive(Encode, Decode, Default, Clone, PartialEq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct CollectionAdminsType<AccountId> {
- pub admin: AccountId,
- pub collection_id: u64,
+ pub admin: AccountId,
+ pub collection_id: u64,
}
#[derive(Encode, Decode, Default, Clone, PartialEq)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct NftItemType<AccountId> {
- pub collection: u64,
- pub owner: AccountId,
- pub data: Vec<u8>,
+ pub collection: u64,
+ pub owner: AccountId,
+ pub data: Vec<u8>,
}
/// The pallet's configuration trait.
pub trait Trait: system::Trait {
- // Add other types and constants required to configure this pallet.
+ // Add other types and constants required to configure this pallet.
- /// The overarching event type.
- type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
+ /// The overarching event type.
+ type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
}
// This pallet's storage items.
decl_storage! {
- // It is important to update your storage name so that your pallet's
- // storage items are isolated from other pallets.
- trait Store for Module<T: Trait> as Nft {
+ // It is important to update your storage name so that your pallet's
+ // storage items are isolated from other pallets.
+ trait Store for Module<T: Trait> as Nft {
- /// Next available collection ID
- pub NextCollectionID get(fn next_collection_id): u64;
+ /// Next available collection ID
+ pub NextCollectionID get(fn next_collection_id): u64;
- /// Collection map
- pub Collection get(collection): map hasher(identity) u64 => CollectionType<T::AccountId>;
+ /// Collection map
+ pub Collection get(collection): map hasher(identity) u64 => CollectionType<T::AccountId>;
- /// Admins map (collection)
- pub AdminList get(admin_list_collection): map hasher(identity) u64 => Vec<T::AccountId>;
+ /// Admins map (collection)
+ pub AdminList get(admin_list_collection): map hasher(identity) u64 => Vec<T::AccountId>;
- /// Balance owner per collection map
- pub Balance get(balance_count): map hasher(blake2_128_concat) (u64, T::AccountId) => u64;
+ /// Balance owner per collection map
+ pub Balance get(balance_count): map hasher(blake2_128_concat) (u64, T::AccountId) => u64;
- pub ApprovedList get(approved): map hasher(blake2_128_concat) (u64, u64) => Vec<T::AccountId>;
- pub ItemList get(item_id): map hasher(blake2_128_concat) (u64, u64) => NftItemType<T::AccountId>;
- pub ItemListIndex get(item_index): map hasher(blake2_128_concat) u64 => u64;
- }
+ pub ApprovedList get(approved): map hasher(blake2_128_concat) (u64, u64) => Vec<T::AccountId>;
+ pub ItemList get(item_id): map hasher(blake2_128_concat) (u64, u64) => NftItemType<T::AccountId>;
+ pub ItemListIndex get(item_index): map hasher(blake2_128_concat) u64 => u64;
+ }
}
// The pallet's events
decl_event!(
- pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {
- Created(u32, AccountId),
- }
+ pub enum Event<T>
+ where
+ AccountId = <T as system::Trait>::AccountId,
+ {
+ Created(u32, AccountId),
+ }
);
// The pallet's dispatchable functions.
decl_module! {
- /// The module declaration.
- pub struct Module<T: Trait> for enum Call where origin: T::Origin {
+ /// The module declaration.
+ pub struct Module<T: Trait> for enum Call where origin: T::Origin {
- // Initializing events
- // this is needed only if you are using events in your pallet
- fn deposit_event() = default;
+ // Initializing events
+ // this is needed only if you are using events in your pallet
+ fn deposit_event() = default;
- // Initializing events
- // this is needed only if you are using events in your module
- // fn deposit_event<T>() = default;
+ // Initializing events
+ // this is needed only if you are using events in your module
+ // fn deposit_event<T>() = default;
- // Create collection of NFT with given parameters
- //
- // @param customDataSz size of custom data in each collection item
- // returns collection ID
+ // Create collection of NFT with given parameters
+ //
+ // @param customDataSz size of custom data in each collection item
+ // returns collection ID
- // Create collection of NFT with given parameters
- //
- // @param customDataSz size of custom data in each collection item
- // returns collection ID
- #[weight = frame_support::weights::SimpleDispatchInfo::default()]
- pub fn create_collection(origin, custom_data_sz: u32) -> DispatchResult {
- // Anyone can create a collection
- let who = ensure_signed(origin)?;
+ // Create collection of NFT with given parameters
+ //
+ // @param customDataSz size of custom data in each collection item
+ // returns collection ID
+ #[weight = frame_support::weights::SimpleDispatchInfo::default()]
+ pub fn create_collection(origin, custom_data_sz: u32) -> DispatchResult {
+ // Anyone can create a collection
+ let who = ensure_signed(origin)?;
- // Generate next collection ID
- let next_id = NextCollectionID::get()
- .checked_add(1)
- .expect("collection id error");
+ // Generate next collection ID
+ let next_id = NextCollectionID::get()
+ .checked_add(1)
+ .expect("collection id error");
- NextCollectionID::put(next_id);
+ NextCollectionID::put(next_id);
- // Create new collection
- let new_collection = CollectionType {
- owner: who,
- next_item_id: next_id,
- custom_data_size: custom_data_sz,
- };
-
- // Add new collection to map
- <Collection<T>>::insert(next_id, new_collection);
+ // Create new collection
+ let new_collection = CollectionType {
+ owner: who,
+ next_item_id: next_id,
+ custom_data_size: custom_data_sz,
+ };
- Ok(())
- }
+ // Add new collection to map
+ <Collection<T>>::insert(next_id, new_collection);
- #[weight = frame_support::weights::SimpleDispatchInfo::default()]
- pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {
+ Ok(())
+ }
- let sender = ensure_signed(origin)?;
- ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
+ #[weight = frame_support::weights::SimpleDispatchInfo::default()]
+ pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {
- let owner = <Collection<T>>::get(collection_id).owner;
- ensure!(sender == owner, "You do not own this collection");
- <Collection<T>>::remove(collection_id);
+ let sender = ensure_signed(origin)?;
+ ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
- Ok(())
- }
+ let owner = <Collection<T>>::get(collection_id).owner;
+ ensure!(sender == owner, "You do not own this collection");
+ <Collection<T>>::remove(collection_id);
- #[weight = frame_support::weights::SimpleDispatchInfo::default()]
- pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {
+ Ok(())
+ }
- let sender = ensure_signed(origin)?;
- ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
+ #[weight = frame_support::weights::SimpleDispatchInfo::default()]
+ pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {
- let mut target_collection = <Collection<T>>::get(collection_id);
- ensure!(sender == target_collection.owner, "You do not own this collection");
+ let sender = ensure_signed(origin)?;
+ ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
- target_collection.owner = new_owner;
- <Collection<T>>::insert(collection_id, target_collection);
+ let mut target_collection = <Collection<T>>::get(collection_id);
+ ensure!(sender == target_collection.owner, "You do not own this collection");
- Ok(())
- }
+ target_collection.owner = new_owner;
+ <Collection<T>>::insert(collection_id, target_collection);
- #[weight = frame_support::weights::SimpleDispatchInfo::default()]
- pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {
+ Ok(())
+ }
- let sender = ensure_signed(origin)?;
- ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
+ #[weight = frame_support::weights::SimpleDispatchInfo::default()]
+ pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {
- let target_collection = <Collection<T>>::get(collection_id);
- let is_owner = sender == target_collection.owner;
+ let sender = ensure_signed(origin)?;
+ ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
- let no_perm_mes = "You do not have permissions to modify this collection";
- let exists = <AdminList<T>>::contains_key(collection_id);
+ let target_collection = <Collection<T>>::get(collection_id);
+ let is_owner = sender == target_collection.owner;
- if !is_owner
- {
- ensure!(exists, no_perm_mes);
- ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
- }
-
- let mut admin_arr: Vec<T::AccountId> = Vec::new();
- if exists
- {
- admin_arr = <AdminList<T>>::get(collection_id);
- ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role");
- }
+ let no_perm_mes = "You do not have permissions to modify this collection";
+ let exists = <AdminList<T>>::contains_key(collection_id);
- admin_arr.push(new_admin_id);
- <AdminList<T>>::insert(collection_id, admin_arr);
+ if !is_owner
+ {
+ ensure!(exists, no_perm_mes);
+ ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
+ }
- Ok(())
- }
+ let mut admin_arr: Vec<T::AccountId> = Vec::new();
+ if exists
+ {
+ admin_arr = <AdminList<T>>::get(collection_id);
+ ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role");
+ }
- #[weight = frame_support::weights::SimpleDispatchInfo::default()]
- pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {
+ admin_arr.push(new_admin_id);
+ <AdminList<T>>::insert(collection_id, admin_arr);
- let sender = ensure_signed(origin)?;
- ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
+ Ok(())
+ }
- let target_collection = <Collection<T>>::get(collection_id);
- let is_owner = sender == target_collection.owner;
+ #[weight = frame_support::weights::SimpleDispatchInfo::default()]
+ pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {
- let no_perm_mes = "You do not have permissions to modify this collection";
- let exists = <AdminList<T>>::contains_key(collection_id);
+ let sender = ensure_signed(origin)?;
+ ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
- if !is_owner
- {
- ensure!(exists, no_perm_mes);
- ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
- }
+ let target_collection = <Collection<T>>::get(collection_id);
+ let is_owner = sender == target_collection.owner;
- if exists
- {
- let mut admin_arr = <AdminList<T>>::get(collection_id);
- admin_arr.retain(|i| *i != account_id);
- <AdminList<T>>::insert(collection_id, admin_arr);
- }
+ let no_perm_mes = "You do not have permissions to modify this collection";
+ let exists = <AdminList<T>>::contains_key(collection_id);
- Ok(())
- }
+ if !is_owner
+ {
+ ensure!(exists, no_perm_mes);
+ ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
+ }
- #[weight = frame_support::weights::SimpleDispatchInfo::default()]
- pub fn create_item(origin, collection_id: u64, properties: Vec<u8>) -> DispatchResult {
+ if exists
+ {
+ let mut admin_arr = <AdminList<T>>::get(collection_id);
+ admin_arr.retain(|i| *i != account_id);
+ <AdminList<T>>::insert(collection_id, admin_arr);
+ }
- let sender = ensure_signed(origin)?;
- ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
+ Ok(())
+ }
- let target_collection = <Collection<T>>::get(collection_id);
- ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");
- let is_owner = sender == target_collection.owner;
-
- let no_perm_mes = "You do not have permissions to modify this collection";
- let exists = <AdminList<T>>::contains_key(collection_id);
-
- if !is_owner
- {
- ensure!(exists, no_perm_mes);
- ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
- }
-
- let new_balance = <Balance<T>>::get((collection_id, sender.clone())) + 1;
- <Balance<T>>::insert((collection_id, sender.clone()), new_balance);
-
- // Create new item
- let new_item = NftItemType {
- collection: collection_id,
- owner: sender,
- data: properties,
- };
+ #[weight = frame_support::weights::SimpleDispatchInfo::default()]
+ pub fn create_item(origin, collection_id: u64, properties: Vec<u8>) -> DispatchResult {
- let current_index = <ItemListIndex>::get(collection_id) + 1;
- <ItemListIndex>::insert(collection_id, current_index);
- <ItemList<T>>::insert((collection_id, current_index), new_item);
+ let sender = ensure_signed(origin)?;
+ ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
- Ok(())
- }
+ let target_collection = <Collection<T>>::get(collection_id);
+ ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");
+ let is_owner = sender == target_collection.owner;
- #[weight = frame_support::weights::SimpleDispatchInfo::default()]
- pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {
+ let no_perm_mes = "You do not have permissions to modify this collection";
+ let exists = <AdminList<T>>::contains_key(collection_id);
- let sender = ensure_signed(origin)?;
- ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
+ if !is_owner
+ {
+ ensure!(exists, no_perm_mes);
+ ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
+ }
- let target_collection = <Collection<T>>::get(collection_id);
- let is_owner = sender == target_collection.owner;
+ let new_balance = <Balance<T>>::get((collection_id, sender.clone())) + 1;
+ <Balance<T>>::insert((collection_id, sender.clone()), new_balance);
- ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");
- let item = <ItemList<T>>::get((collection_id, item_id));
+ // Create new item
+ let new_item = NftItemType {
+ collection: collection_id,
+ owner: sender,
+ data: properties,
+ };
- if !is_owner
- {
- // check if item owner
- if item.owner != sender
- {
- let no_perm_mes = "You do not have permissions to modify this collection";
+ let current_index = <ItemListIndex>::get(collection_id) + 1;
+ <ItemListIndex>::insert(collection_id, current_index);
+ <ItemList<T>>::insert((collection_id, current_index), new_item);
- ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);
- ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
- }
- }
- <ItemList<T>>::remove((collection_id, item_id));
+ Ok(())
+ }
- // update balance
- let new_balance = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;
- <Balance<T>>::insert((collection_id, item.owner.clone()), new_balance);
+ #[weight = frame_support::weights::SimpleDispatchInfo::default()]
+ pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {
- Ok(())
- }
+ let sender = ensure_signed(origin)?;
+ ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
- #[weight = frame_support::weights::SimpleDispatchInfo::default()]
- pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {
+ let target_collection = <Collection<T>>::get(collection_id);
+ let is_owner = sender == target_collection.owner;
- let sender = ensure_signed(origin)?;
- ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
+ ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");
+ let item = <ItemList<T>>::get((collection_id, item_id));
- let target_collection = <Collection<T>>::get(collection_id);
- let is_owner = sender == target_collection.owner;
+ if !is_owner
+ {
+ // check if item owner
+ if item.owner != sender
+ {
+ let no_perm_mes = "You do not have permissions to modify this collection";
- ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");
- let mut item = <ItemList<T>>::get((collection_id, item_id));
+ ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);
+ ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
+ }
+ }
+ <ItemList<T>>::remove((collection_id, item_id));
- if !is_owner
- {
- // check if item owner
- if item.owner != sender
- {
- let no_perm_mes = "You do not have permissions to modify this collection";
+ // update balance
+ let new_balance = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;
+ <Balance<T>>::insert((collection_id, item.owner.clone()), new_balance);
- ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);
- ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
- }
- }
- <ItemList<T>>::remove((collection_id, item_id));
+ Ok(())
+ }
- // update balance
- let balance_old_owner = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;
- <Balance<T>>::insert((collection_id, item.owner.clone()), balance_old_owner);
+ #[weight = frame_support::weights::SimpleDispatchInfo::default()]
+ pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {
- let balance_new_owner = <Balance<T>>::get((collection_id, new_owner.clone())) + 1;
- <Balance<T>>::insert((collection_id, new_owner.clone()), balance_new_owner);
+ let sender = ensure_signed(origin)?;
+ ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
- // change owner
- item.owner = new_owner;
- <ItemList<T>>::insert((collection_id, item_id), item);
+ let target_collection = <Collection<T>>::get(collection_id);
+ let is_owner = sender == target_collection.owner;
- // reset approved list
- let itm: Vec<T::AccountId> = Vec::new();
- <ApprovedList<T>>::insert((collection_id, item_id), itm);
+ ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");
+ let mut item = <ItemList<T>>::get((collection_id, item_id));
- Ok(())
- }
+ if !is_owner
+ {
+ // check if item owner
+ if item.owner != sender
+ {
+ let no_perm_mes = "You do not have permissions to modify this collection";
- #[weight = frame_support::weights::SimpleDispatchInfo::default()]
- pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult {
+ ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);
+ ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
+ }
+ }
+ <ItemList<T>>::remove((collection_id, item_id));
- let sender = ensure_signed(origin)?;
- ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
+ // update balance
+ let balance_old_owner = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;
+ <Balance<T>>::insert((collection_id, item.owner.clone()), balance_old_owner);
- let target_collection = <Collection<T>>::get(collection_id);
- let is_owner = sender == target_collection.owner;
+ let balance_new_owner = <Balance<T>>::get((collection_id, new_owner.clone())) + 1;
+ <Balance<T>>::insert((collection_id, new_owner.clone()), balance_new_owner);
- ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");
- let item = <ItemList<T>>::get((collection_id, item_id));
+ // change owner
+ item.owner = new_owner;
+ <ItemList<T>>::insert((collection_id, item_id), item);
- if !is_owner
- {
- // check if item owner
- if item.owner != sender
- {
- let no_perm_mes = "You do not have permissions to modify this collection";
+ // reset approved list
+ let itm: Vec<T::AccountId> = Vec::new();
+ <ApprovedList<T>>::insert((collection_id, item_id), itm);
- ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);
- ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
- }
- }
+ Ok(())
+ }
- let list_exists = <ApprovedList<T>>::contains_key((collection_id, item_id));
- if list_exists {
-
- let mut list = <ApprovedList<T>>::get((collection_id, item_id));
- let item_contains = list.contains(&approved.clone());
+ #[weight = frame_support::weights::SimpleDispatchInfo::default()]
+ pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult {
- if !item_contains {
- list.push(approved.clone());
- }
- } else {
-
- let mut itm = Vec::new();
- itm.push(approved.clone());
- <ApprovedList<T>>::insert((collection_id, item_id), itm);
- }
+ let sender = ensure_signed(origin)?;
+ ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
- Ok(())
- }
+ let target_collection = <Collection<T>>::get(collection_id);
+ let is_owner = sender == target_collection.owner;
- #[weight = frame_support::weights::SimpleDispatchInfo::default()]
- pub fn transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {
+ ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");
+ let item = <ItemList<T>>::get((collection_id, item_id));
- // let sender = ensure_signed(origin)?;
- // ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");
+ if !is_owner
+ {
+ // check if item owner
+ if item.owner != sender
+ {
+ let no_perm_mes = "You do not have permissions to modify this collection";
- // let target_collection = <Collection<T>>::get(collection_id);
- // let is_owner = sender == target_collection.owner;
+ ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);
+ ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
+ }
+ }
- // ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");
- // let mut item = <ItemList<T>>::get((collection_id, item_id));
+ let list_exists = <ApprovedList<T>>::contains_key((collection_id, item_id));
+ if list_exists {
- // if !is_owner
- // {
- // let no_perm_mes = "You do not have permissions to modify this collection";
+ let mut list = <ApprovedList<T>>::get((collection_id, item_id));
+ let item_contains = list.contains(&approved.clone());
- // // check if item owner
- // if item.owner != sender
- // {
- // ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);
- // ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);
- // }
+ if !item_contains {
+ list.push(approved.clone());
+ }
+ } else {
- // ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);
- // let list_itm = <ApprovedList<T>>::get((collection_id, item_id));
- // ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);
+ let mut itm = Vec::new();
+ itm.push(approved.clone());
+ <ApprovedList<T>>::insert((collection_id, item_id), itm);
+ }
- // }
+ Ok(())
+ }
+ #[weight = frame_support::weights::SimpleDispatchInfo::default()]
+ pub fn transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {
- let no_perm_mes = "You do not have permissions to modify this collection";
- ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);
- let list_itm = <ApprovedList<T>>::get((collection_id, item_id));
- ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);
+ let no_perm_mes = "You do not have permissions to modify this collection";
+ ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);
+ let list_itm = <ApprovedList<T>>::get((collection_id, item_id));
+ ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);
- Self::transfer(origin, collection_id, item_id, new_owner);
+ Self::transfer(origin, collection_id, item_id, new_owner)?;
- Ok(())
- }
- }
-}
\ No newline at end of file
+ Ok(())
+ }
+ }
+}
pallets/nft/src/mock.rsdiffbeforeafterboth1// Creating mock runtime here1// Creating mock runtime here223use crate::{Module, Trait};3use crate::{Module, Trait};4use sp_core::H256;5use frame_support::{impl_outer_origin, parameter_types, weights::Weight};4use frame_support::{impl_outer_origin, parameter_types, weights::Weight};5use frame_system as system;6use sp_core::H256;6use sp_runtime::{7use sp_runtime::{8 testing::Header,7 traits::{BlakeTwo256, IdentityLookup}, testing::Header, Perbill,9 traits::{BlakeTwo256, IdentityLookup},10 Perbill,8};11};9use frame_system as system;101211impl_outer_origin! {13impl_outer_origin! {12 pub enum Origin for Test {}14 pub enum Origin for Test {}pallets/nft/src/tests.rsdiffbeforeafterboth--- a/pallets/nft/src/tests.rs
+++ b/pallets/nft/src/tests.rs
@@ -1,6 +1,6 @@
// Tests to be written here
-use crate::{ mock::*};
-use frame_support::{assert_ok, assert_noop};
+use crate::mock::*;
+use frame_support::{assert_noop, assert_ok};
#[test]
fn create_collection_test() {
@@ -14,69 +14,83 @@
#[test]
fn change_collection_owner() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
- assert_ok!(TemplateModule::change_collection_owner(origin1.clone(), 1, 2));
+ assert_ok!(TemplateModule::change_collection_owner(
+ origin1.clone(),
+ 1,
+ 2
+ ));
assert_eq!(TemplateModule::collection(1).owner, 2);
- });
+ });
}
#[test]
fn destroy_collection() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
assert_ok!(TemplateModule::destroy_collection(origin1.clone(), 1));
- });
+ });
}
#[test]
fn create_item() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), 1, 2));
- assert_ok!(TemplateModule::create_item(origin2.clone(), 1, [1,1,1].to_vec()));
+ assert_ok!(TemplateModule::create_item(
+ origin2.clone(),
+ 1,
+ [1, 1, 1].to_vec()
+ ));
// check balance (collection with id = 1, user id = 2)
assert_eq!(TemplateModule::balance_count((1, 2)), 1);
- });
+ });
}
#[test]
fn burn_item() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), 1, 2));
- assert_ok!(TemplateModule::create_item(origin2.clone(), 1, [1,1,1].to_vec()));
+ assert_ok!(TemplateModule::create_item(
+ origin2.clone(),
+ 1,
+ [1, 1, 1].to_vec()
+ ));
// check balance (collection with id = 1, user id = 2)
assert_eq!(TemplateModule::balance_count((1, 2)), 1);
// burn item
assert_ok!(TemplateModule::burn_item(origin1.clone(), 1, 1));
- assert_noop!(TemplateModule::burn_item(origin1.clone(), 1, 1), "Item does not exists");
+ assert_noop!(
+ TemplateModule::burn_item(origin1.clone(), 1, 1),
+ "Item does not exists"
+ );
assert_eq!(TemplateModule::balance_count((1, 1)), 0);
- });
+ });
}
-
#[test]
fn add_collection_admin() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
@@ -96,12 +110,12 @@
assert_eq!(TemplateModule::admin_list_collection(1).contains(&2), true);
assert_eq!(TemplateModule::admin_list_collection(1).contains(&3), true);
- });
+ });
}
#[test]
fn remove_collection_admin() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
@@ -123,14 +137,18 @@
assert_eq!(TemplateModule::admin_list_collection(1).contains(&3), true);
// remove admin
- assert_ok!(TemplateModule::remove_collection_admin(origin2.clone(), 1, 3));
+ assert_ok!(TemplateModule::remove_collection_admin(
+ origin2.clone(),
+ 1,
+ 3
+ ));
assert_eq!(TemplateModule::admin_list_collection(1).contains(&3), false);
- });
+ });
}
#[test]
fn balance_of() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
@@ -148,17 +166,21 @@
assert_eq!(TemplateModule::balance_count((1, 1)), 0);
// create item
- assert_ok!(TemplateModule::create_item(origin1.clone(), 1, [1,1,1].to_vec()));
-
+ assert_ok!(TemplateModule::create_item(
+ origin1.clone(),
+ 1,
+ [1, 1, 1].to_vec()
+ ));
+
// check balance (collection with id = 1, user id = 2)
assert_eq!(TemplateModule::balance_count((1, 1)), 1);
- assert_eq!(TemplateModule::item_id((1,1)).owner, 1);
- });
+ assert_eq!(TemplateModule::item_id((1, 1)).owner, 1);
+ });
}
#[test]
fn transfer() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
@@ -173,21 +195,25 @@
assert_eq!(TemplateModule::collection(3).owner, 3);
// create item
- assert_ok!(TemplateModule::create_item(origin1.clone(), 1, [1,1,1].to_vec()));
+ assert_ok!(TemplateModule::create_item(
+ origin1.clone(),
+ 1,
+ [1, 1, 1].to_vec()
+ ));
// transfer
assert_ok!(TemplateModule::transfer(origin1.clone(), 1, 1, 2));
- assert_eq!(TemplateModule::item_id((1,1)).owner, 2);
+ assert_eq!(TemplateModule::item_id((1, 1)).owner, 2);
// balance_of check
assert_eq!(TemplateModule::balance_count((1, 1)), 0);
assert_eq!(TemplateModule::balance_count((1, 2)), 1);
- });
+ });
}
#[test]
fn approve() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
@@ -202,18 +228,21 @@
assert_eq!(TemplateModule::collection(3).owner, 3);
// create item
- assert_ok!(TemplateModule::create_item(origin1.clone(), 1, [1,1,1].to_vec()));
+ assert_ok!(TemplateModule::create_item(
+ origin1.clone(),
+ 1,
+ [1, 1, 1].to_vec()
+ ));
// approve
assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1));
- assert_eq!(TemplateModule::approved((1,1)).contains(&2), true);
-
- });
+ assert_eq!(TemplateModule::approved((1, 1)).contains(&2), true);
+ });
}
#[test]
fn get_approved() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
@@ -228,18 +257,21 @@
assert_eq!(TemplateModule::collection(3).owner, 3);
// create item
- assert_ok!(TemplateModule::create_item(origin1.clone(), 1, [1,1,1].to_vec()));
+ assert_ok!(TemplateModule::create_item(
+ origin1.clone(),
+ 1,
+ [1, 1, 1].to_vec()
+ ));
// approve
assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1));
- assert_eq!(TemplateModule::approved((1,1)).contains(&2), true);
-
- });
+ assert_eq!(TemplateModule::approved((1, 1)).contains(&2), true);
+ });
}
#[test]
fn transfer_from() {
- new_test_ext().execute_with(|| {
+ new_test_ext().execute_with(|| {
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
@@ -254,11 +286,14 @@
assert_eq!(TemplateModule::collection(3).owner, 3);
// create item
- assert_ok!(TemplateModule::create_item(origin1.clone(), 1, [1,1,1].to_vec()));
+ assert_ok!(TemplateModule::create_item(
+ origin1.clone(),
+ 1,
+ [1, 1, 1].to_vec()
+ ));
// approve
assert_ok!(TemplateModule::approve(origin1.clone(), 2, 1, 1));
assert_ok!(TemplateModule::transfer_from(origin1.clone(), 1, 1, 2));
-
- });
-}
\ No newline at end of file
+ });
+}