git.delta.rocks / unique-network / refs/commits / 623e2e26812b

difftreelog

source

pallets/template/src/lib.rs10.1 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	weights::{DispatchClass, ClassifyDispatch, WeighData, Weight},15	ensure,16};17use frame_system::{self as system, ensure_signed };18use codec::{Encode, Decode};19use sp_runtime::sp_std::prelude::Vec;2021#[cfg(test)]22mod mock;2324#[cfg(test)]25mod tests;2627#[derive(Encode, Decode, Default, Clone, PartialEq)]28#[cfg_attr(feature = "std", derive(Debug))]29pub struct CollectionType<AccountId> {30	pub owner: AccountId,31	pub next_item_id: u64,32	pub custom_data_size: u32,33}3435#[derive(Encode, Decode, Default, Clone, PartialEq)]36#[cfg_attr(feature = "std", derive(Debug))]37pub struct CollectionAdminsType<AccountId> {38	pub admin: AccountId,39	pub collection_id: u64,40}4142#[derive(Encode, Decode, Default, Clone, PartialEq)]43#[cfg_attr(feature = "std", derive(Debug))]44pub struct NftItemType<AccountId> {45	pub collection: u64,46	pub owner: AccountId,47	pub data: Vec<u8>,48}4950/// The pallet's configuration trait.51pub trait Trait: system::Trait {52	// Add other types and constants required to configure this pallet.5354	/// The overarching event type.55	type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;56}5758// This pallet's storage items.59decl_storage! {60	// It is important to update your storage name so that your pallet's61	// storage items are isolated from other pallets.62	trait Store for Module<T: Trait> as TemplateModule {6364		/// Next available collection ID65		pub NextCollectionID get(next_collection_id): u64;6667		/// Collection map68		pub Collection get(collection): map hasher(blake2_128_concat) u64 => CollectionType<T::AccountId>;6970		/// Admins map (collection)71		pub AdminList get(admin_list_collection): map hasher(blake2_128_concat) u64 => Vec<T::AccountId>;7273		/// Balance owner per collection map74		pub Balance get(balance_count): map hasher(blake2_128_concat) (u64, T::AccountId) => u64;7576		/// Item double map (collection)77		pub ItemList get(item_id): map hasher(blake2_128_concat) (u64, u64) => NftItemType<T::AccountId>;78		pub ItemListIndex get(item_index): map hasher(blake2_128_concat) u64 => u64;79	}80}8182// The pallet's events83decl_event!(84	pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {85		Created(u32, AccountId),86	}87);8889// The pallet's dispatchable functions.90decl_module! {91	/// The module declaration.92	pub struct Module<T: Trait> for enum Call where origin: T::Origin {9394		// Initializing events95		// this is needed only if you are using events in your pallet96		fn deposit_event() = default;9798		// Initializing events99		// this is needed only if you are using events in your module100		// fn deposit_event<T>() = default;101102		// Create collection of NFT with given parameters103		//104		// @param customDataSz size of custom data in each collection item105		// returns collection ID106107		// Create collection of NFT with given parameters108		//109		// @param customDataSz size of custom data in each collection item110		// returns collection ID111		#[weight = frame_support::weights::SimpleDispatchInfo::default()]112		pub fn create_collection(origin, custom_data_sz: u32) -> DispatchResult {113			// Anyone can create a collection114			let who = ensure_signed(origin)?;115116			// Generate next collection ID117			let next_id = Self::next_collection_id();118			<NextCollectionID>::put(next_id+1);119120			// Create new collection121			let new_collection = CollectionType {122				owner: who,123				next_item_id: next_id,124				custom_data_size: custom_data_sz,125			};126			127			// Add new collection to map128			<Collection<T>>::insert(next_id, new_collection);129130			Ok(())131		}132133		#[weight = frame_support::weights::SimpleDispatchInfo::default()]134		pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {135136			let sender = ensure_signed(origin)?;137			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");138139			let owner = <Collection<T>>::get(collection_id).owner;140			ensure!(sender == owner, "You do not own this collection");141			<Collection<T>>::remove(collection_id);142143			Ok(())144		}145146		#[weight = frame_support::weights::SimpleDispatchInfo::default()]147		pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {148149			let sender = ensure_signed(origin)?;150			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");151152			let mut target_collection = <Collection<T>>::get(collection_id);153			ensure!(sender == target_collection.owner, "You do not own this collection");154155			target_collection.owner = new_owner;156			<Collection<T>>::insert(collection_id, target_collection);157158			Ok(())159		}160161		#[weight = frame_support::weights::SimpleDispatchInfo::default()]162		pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {163164			let sender = ensure_signed(origin)?;165			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");166167			let target_collection = <Collection<T>>::get(collection_id);168			let is_owner = sender == target_collection.owner;169170			let no_perm_mes = "You do not have permissions to modify this collection";171			let exists = <AdminList<T>>::contains_key(collection_id);172173			if !is_owner 174			{175				 ensure!(exists, no_perm_mes);176				 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);177			}178			179			let mut admin_arr: Vec<T::AccountId> = Vec::new();180			if exists181			{182				admin_arr = <AdminList<T>>::get(collection_id);183				ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role");184			}185186			admin_arr.push(new_admin_id);187			<AdminList<T>>::insert(collection_id, admin_arr);188189			Ok(())190		}191192		#[weight = frame_support::weights::SimpleDispatchInfo::default()]193		pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {194195			let sender = ensure_signed(origin)?;196			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");197198			let target_collection = <Collection<T>>::get(collection_id);199			let is_owner = sender == target_collection.owner;200201			let no_perm_mes = "You do not have permissions to modify this collection";202			let exists = <AdminList<T>>::contains_key(collection_id);203204			if !is_owner 205			{206				ensure!(exists, no_perm_mes);207				ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);208			}209210			if exists211			{212				let mut admin_arr = <AdminList<T>>::get(collection_id);213				admin_arr.retain(|i| *i != account_id);214				<AdminList<T>>::insert(collection_id, admin_arr);215			}216217			Ok(())218		}219220		#[weight = frame_support::weights::SimpleDispatchInfo::default()]221		pub fn create_item(origin, collection_id: u64, properties: Vec<u8>) -> DispatchResult {222223			let sender = ensure_signed(origin)?;224			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");225226			let target_collection = <Collection<T>>::get(collection_id);227			ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");228			let is_owner = sender == target_collection.owner;229230			let no_perm_mes = "You do not have permissions to modify this collection";231			let exists = <AdminList<T>>::contains_key(collection_id);232233			if !is_owner 234			{235				ensure!(exists, no_perm_mes);236				ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);237			}238239			let new_balance = <Balance<T>>::get((collection_id, sender.clone())) + 1;240			<Balance<T>>::insert((collection_id, sender.clone()), new_balance);241242			// Create new item243			let new_item = NftItemType {244				collection: collection_id,245				owner: sender,246				data: properties,247			};248249			let current_index = <ItemListIndex>::get(collection_id);250			<ItemListIndex>::insert(collection_id, current_index+1);251			<ItemList<T>>::insert((collection_id, current_index), new_item);252253			Ok(())254		}255256		#[weight = frame_support::weights::SimpleDispatchInfo::default()]257		pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {258259			let sender = ensure_signed(origin)?;260			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");261262			let target_collection = <Collection<T>>::get(collection_id);263			let is_owner = sender == target_collection.owner;264265			ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");266			let item = <ItemList<T>>::get((collection_id, item_id));267268			if !is_owner 269			{270				// check if item owner271				if item.owner != sender 272				{273					let no_perm_mes = "You do not have permissions to modify this collection";274275					ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);276					ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);277				}278			}279			<ItemList<T>>::remove((collection_id, item_id));280281			Ok(())282		}283284		#[weight = frame_support::weights::SimpleDispatchInfo::default()]285		pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {286287			let sender = ensure_signed(origin)?;288			ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");289290			let target_collection = <Collection<T>>::get(collection_id);291			let is_owner = sender == target_collection.owner;292293			ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");294			let mut item = <ItemList<T>>::get((collection_id, item_id));295296			if !is_owner 297			{298				// check if item owner299				if item.owner != sender 300				{301					let no_perm_mes = "You do not have permissions to modify this collection";302303					ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);304					ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);305				}306			}307			<ItemList<T>>::remove((collection_id, item_id));308309			// change owner310			item.owner = new_owner;311			<ItemList<T>>::insert((collection_id, item_id), item);312313			Ok(())314		}315	}316}