1#![cfg_attr(not(feature = "std"), no_std)]23456789101112use 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}484950pub trait Trait: system::Trait {51 5253 54 type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;55}565758decl_storage! {59 60 61 trait Store for Module<T: Trait> as Nft {6263 64 pub NextCollectionID get(fn next_collection_id): u64;6566 67 pub Collection get(collection): map hasher(identity) u64 => CollectionType<T::AccountId>;6869 70 pub AdminList get(admin_list_collection): map hasher(identity) u64 => Vec<T::AccountId>;7172 73 pub Balance get(balance_count): map hasher(blake2_128_concat) (u64, T::AccountId) => u64;7475 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}808182decl_event!(83 pub enum Event<T> where AccountId = <T as system::Trait>::AccountId {84 Created(u32, AccountId),85 }86);878889decl_module! {90 91 pub struct Module<T: Trait> for enum Call where origin: T::Origin {9293 94 95 fn deposit_event() = default;9697 98 99 100101 102 103 104 105106 107 108 109 110 #[weight = frame_support::weights::SimpleDispatchInfo::default()]111 pub fn create_collection(origin, custom_data_sz: u32) -> DispatchResult {112 113 let who = ensure_signed(origin)?;114115 116 let next_id = NextCollectionID::get()117 .checked_add(1)118 .expect("collection id error");;119120 NextCollectionID::put(next_id);121122 123 let new_collection = CollectionType {124 owner: who,125 next_item_id: next_id,126 custom_data_size: custom_data_sz,127 };128 129 130 <Collection<T>>::insert(next_id, new_collection);131132 Ok(())133 }134135 #[weight = frame_support::weights::SimpleDispatchInfo::default()]136 pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {137138 let sender = ensure_signed(origin)?;139 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");140141 let owner = <Collection<T>>::get(collection_id).owner;142 ensure!(sender == owner, "You do not own this collection");143 <Collection<T>>::remove(collection_id);144145 Ok(())146 }147148 #[weight = frame_support::weights::SimpleDispatchInfo::default()]149 pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {150151 let sender = ensure_signed(origin)?;152 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");153154 let mut target_collection = <Collection<T>>::get(collection_id);155 ensure!(sender == target_collection.owner, "You do not own this collection");156157 target_collection.owner = new_owner;158 <Collection<T>>::insert(collection_id, target_collection);159160 Ok(())161 }162163 #[weight = frame_support::weights::SimpleDispatchInfo::default()]164 pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {165166 let sender = ensure_signed(origin)?;167 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");168169 let target_collection = <Collection<T>>::get(collection_id);170 let is_owner = sender == target_collection.owner;171172 let no_perm_mes = "You do not have permissions to modify this collection";173 let exists = <AdminList<T>>::contains_key(collection_id);174175 if !is_owner 176 {177 ensure!(exists, no_perm_mes);178 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);179 }180 181 let mut admin_arr: Vec<T::AccountId> = Vec::new();182 if exists183 {184 admin_arr = <AdminList<T>>::get(collection_id);185 ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role");186 }187188 admin_arr.push(new_admin_id);189 <AdminList<T>>::insert(collection_id, admin_arr);190191 Ok(())192 }193194 #[weight = frame_support::weights::SimpleDispatchInfo::default()]195 pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {196197 let sender = ensure_signed(origin)?;198 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");199200 let target_collection = <Collection<T>>::get(collection_id);201 let is_owner = sender == target_collection.owner;202203 let no_perm_mes = "You do not have permissions to modify this collection";204 let exists = <AdminList<T>>::contains_key(collection_id);205206 if !is_owner 207 {208 ensure!(exists, no_perm_mes);209 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);210 }211212 if exists213 {214 let mut admin_arr = <AdminList<T>>::get(collection_id);215 admin_arr.retain(|i| *i != account_id);216 <AdminList<T>>::insert(collection_id, admin_arr);217 }218219 Ok(())220 }221222 #[weight = frame_support::weights::SimpleDispatchInfo::default()]223 pub fn create_item(origin, collection_id: u64, properties: Vec<u8>) -> DispatchResult {224225 let sender = ensure_signed(origin)?;226 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");227228 let target_collection = <Collection<T>>::get(collection_id);229 ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");230 let is_owner = sender == target_collection.owner;231232 let no_perm_mes = "You do not have permissions to modify this collection";233 let exists = <AdminList<T>>::contains_key(collection_id);234235 if !is_owner 236 {237 ensure!(exists, no_perm_mes);238 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);239 }240241 let new_balance = <Balance<T>>::get((collection_id, sender.clone())) + 1;242 <Balance<T>>::insert((collection_id, sender.clone()), new_balance);243244 245 let new_item = NftItemType {246 collection: collection_id,247 owner: sender,248 data: properties,249 };250251 let current_index = <ItemListIndex>::get(collection_id) + 1;252 <ItemListIndex>::insert(collection_id, current_index);253 <ItemList<T>>::insert((collection_id, current_index), new_item);254255 Ok(())256 }257258 #[weight = frame_support::weights::SimpleDispatchInfo::default()]259 pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {260261 let sender = ensure_signed(origin)?;262 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");263264 let target_collection = <Collection<T>>::get(collection_id);265 let is_owner = sender == target_collection.owner;266267 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");268 let item = <ItemList<T>>::get((collection_id, item_id));269270 if !is_owner 271 {272 273 if item.owner != sender 274 {275 let no_perm_mes = "You do not have permissions to modify this collection";276277 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);278 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);279 }280 }281 <ItemList<T>>::remove((collection_id, item_id));282283 Ok(())284 }285286 #[weight = frame_support::weights::SimpleDispatchInfo::default()]287 pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {288289 let sender = ensure_signed(origin)?;290 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");291292 let target_collection = <Collection<T>>::get(collection_id);293 let is_owner = sender == target_collection.owner;294295 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");296 let mut item = <ItemList<T>>::get((collection_id, item_id));297298 if !is_owner 299 {300 301 if item.owner != sender 302 {303 let no_perm_mes = "You do not have permissions to modify this collection";304305 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);306 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);307 }308 }309 <ItemList<T>>::remove((collection_id, item_id));310311 312 item.owner = new_owner;313 <ItemList<T>>::insert((collection_id, item_id), item);314315 Ok(())316 }317 }318}