git.delta.rocks / unique-network / refs/commits / 8efe571e586c

difftreelog

source

pallets/nft/src/lib.rs10.0 KiBsourcehistory
1#![cfg_attr(not(feature = "std"), no_std)]23/// A FRAME pallet template with necessary imports45/// Feel free to remove or edit this file as needed.6/// If you change the name of this file, make sure to update its references in runtime/src/lib.rs7/// If you remove this file, you can remove those references89/// For more guidance on Substrate FRAME, see the example pallet10/// https://github.com/paritytech/substrate/blob/master/frame/example/src/lib.rs1112use frame_support::{13	dispatch::DispatchResult, decl_module, decl_storage, decl_event,14	ensure,15};16use frame_system::{self as system, ensure_signed };17use codec::{Encode, Decode};18use sp_runtime::sp_std::prelude::Vec;1920#[cfg(test)]21mod mock;2223#[cfg(test)]24mod tests;2526#[derive(Encode, Decode, Default, Clone, PartialEq)]27#[cfg_attr(feature = "std", derive(Debug))]28pub struct CollectionType<AccountId> {29	pub owner: AccountId,30	pub next_item_id: u64,31	pub custom_data_size: u32,32}3334#[derive(Encode, Decode, Default, Clone, PartialEq)]35#[cfg_attr(feature = "std", derive(Debug))]36pub struct CollectionAdminsType<AccountId> {37	pub admin: AccountId,38	pub collection_id: u64,39}4041#[derive(Encode, Decode, Default, Clone, PartialEq)]42#[cfg_attr(feature = "std", derive(Debug))]43pub struct NftItemType<AccountId> {44	pub collection: u64,45	pub owner: AccountId,46	pub data: Vec<u8>,47}4849/// The pallet's configuration trait.50pub trait Trait: system::Trait {51	// Add other types and constants required to configure this pallet.5253	/// The overarching event type.54	type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;55}5657// This pallet's storage items.58decl_storage! {59	// It is important to update your storage name so that your pallet's60	// storage items are isolated from other pallets.61	trait Store for Module<T: Trait> as TemplateModule {6263		/// Next available collection ID64		pub NextCollectionID get(next_collection_id): u64 = 1;6566		/// Collection map67		pub Collection get(collection): map hasher(blake2_128_concat) u64 => CollectionType<T::AccountId>;6869		/// Admins map (collection)70		pub AdminList get(admin_list_collection): map hasher(blake2_128_concat) u64 => Vec<T::AccountId>;7172		/// Balance owner per collection map73		pub Balance get(balance_count): map hasher(blake2_128_concat) (u64, T::AccountId) => u64;7475		/// Item double map (collection)76		pub ItemList get(item_id): map hasher(blake2_128_concat) (u64, u64) => NftItemType<T::AccountId>;77		pub ItemListIndex get(item_index): map hasher(blake2_128_concat) u64 => u64;78	}79}8081// The pallet's events82decl_event!(83	pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {84		Created(u32, AccountId),85	}86);8788// The pallet's dispatchable functions.89decl_module! {90	/// The module declaration.91	pub struct Module<T: Trait> for enum Call where origin: T::Origin {9293		// Initializing events94		// this is needed only if you are using events in your pallet95		fn deposit_event() = default;9697		// Initializing events98		// this is needed only if you are using events in your module99		// fn deposit_event<T>() = default;100101		// Create collection of NFT with given parameters102		//103		// @param customDataSz size of custom data in each collection item104		// returns collection ID105106		// Create collection of NFT with given parameters107		//108		// @param customDataSz size of custom data in each collection item109		// returns collection ID110		#[weight = frame_support::weights::SimpleDispatchInfo::default()]111		pub fn create_collection(origin, custom_data_sz: u32) -> DispatchResult {112			// Anyone can create a collection113			let who = ensure_signed(origin)?;114115			// Generate next collection ID116			let next_id = Self::next_collection_id();117			<NextCollectionID>::put(next_id+1);118119			// Create new collection120			let new_collection = CollectionType {121				owner: who,122				next_item_id: next_id,123				custom_data_size: custom_data_sz,124			};125			126			// Add new collection to map127			<Collection<T>>::insert(next_id, new_collection);128129			Ok(())130		}131132		#[weight = frame_support::weights::SimpleDispatchInfo::default()]133		pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {134135			let sender = ensure_signed(origin)?;136			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");137138			let owner = <Collection<T>>::get(collection_id).owner;139			ensure!(sender == owner, "You do not own this collection");140			<Collection<T>>::remove(collection_id);141142			Ok(())143		}144145		#[weight = frame_support::weights::SimpleDispatchInfo::default()]146		pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {147148			let sender = ensure_signed(origin)?;149			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");150151			let mut target_collection = <Collection<T>>::get(collection_id);152			ensure!(sender == target_collection.owner, "You do not own this collection");153154			target_collection.owner = new_owner;155			<Collection<T>>::insert(collection_id, target_collection);156157			Ok(())158		}159160		#[weight = frame_support::weights::SimpleDispatchInfo::default()]161		pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {162163			let sender = ensure_signed(origin)?;164			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");165166			let target_collection = <Collection<T>>::get(collection_id);167			let is_owner = sender == target_collection.owner;168169			let no_perm_mes = "You do not have permissions to modify this collection";170			let exists = <AdminList<T>>::contains_key(collection_id);171172			if !is_owner 173			{174				 ensure!(exists, no_perm_mes);175				 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);176			}177			178			let mut admin_arr: Vec<T::AccountId> = Vec::new();179			if exists180			{181				admin_arr = <AdminList<T>>::get(collection_id);182				ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role");183			}184185			admin_arr.push(new_admin_id);186			<AdminList<T>>::insert(collection_id, admin_arr);187188			Ok(())189		}190191		#[weight = frame_support::weights::SimpleDispatchInfo::default()]192		pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {193194			let sender = ensure_signed(origin)?;195			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");196197			let target_collection = <Collection<T>>::get(collection_id);198			let is_owner = sender == target_collection.owner;199200			let no_perm_mes = "You do not have permissions to modify this collection";201			let exists = <AdminList<T>>::contains_key(collection_id);202203			if !is_owner 204			{205				ensure!(exists, no_perm_mes);206				ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);207			}208209			if exists210			{211				let mut admin_arr = <AdminList<T>>::get(collection_id);212				admin_arr.retain(|i| *i != account_id);213				<AdminList<T>>::insert(collection_id, admin_arr);214			}215216			Ok(())217		}218219		#[weight = frame_support::weights::SimpleDispatchInfo::default()]220		pub fn create_item(origin, collection_id: u64, properties: Vec<u8>) -> DispatchResult {221222			let sender = ensure_signed(origin)?;223			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");224225			let target_collection = <Collection<T>>::get(collection_id);226			ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");227			let is_owner = sender == target_collection.owner;228229			let no_perm_mes = "You do not have permissions to modify this collection";230			let exists = <AdminList<T>>::contains_key(collection_id);231232			if !is_owner 233			{234				ensure!(exists, no_perm_mes);235				ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);236			}237238			let new_balance = <Balance<T>>::get((collection_id, sender.clone())) + 1;239			<Balance<T>>::insert((collection_id, sender.clone()), new_balance);240241			// Create new item242			let new_item = NftItemType {243				collection: collection_id,244				owner: sender,245				data: properties,246			};247248			let current_index = <ItemListIndex>::get(collection_id);249			<ItemListIndex>::insert(collection_id, current_index+1);250			<ItemList<T>>::insert((collection_id, current_index), new_item);251252			Ok(())253		}254255		#[weight = frame_support::weights::SimpleDispatchInfo::default()]256		pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {257258			let sender = ensure_signed(origin)?;259			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");260261			let target_collection = <Collection<T>>::get(collection_id);262			let is_owner = sender == target_collection.owner;263264			ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");265			let item = <ItemList<T>>::get((collection_id, item_id));266267			if !is_owner 268			{269				// check if item owner270				if item.owner != sender 271				{272					let no_perm_mes = "You do not have permissions to modify this collection";273274					ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);275					ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);276				}277			}278			<ItemList<T>>::remove((collection_id, item_id));279280			Ok(())281		}282283		#[weight = frame_support::weights::SimpleDispatchInfo::default()]284		pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {285286			let sender = ensure_signed(origin)?;287			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");288289			let target_collection = <Collection<T>>::get(collection_id);290			let is_owner = sender == target_collection.owner;291292			ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");293			let mut item = <ItemList<T>>::get((collection_id, item_id));294295			if !is_owner 296			{297				// check if item owner298				if item.owner != sender 299				{300					let no_perm_mes = "You do not have permissions to modify this collection";301302					ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);303					ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);304				}305			}306			<ItemList<T>>::remove((collection_id, item_id));307308			// change owner309			item.owner = new_owner;310			<ItemList<T>>::insert((collection_id, item_id), item);311312			Ok(())313		}314	}315}