1#![cfg_attr(not(feature = "std"), no_std)]23use codec::{Decode, Encode};456789101112use frame_support::{decl_event, decl_module, decl_storage, dispatch::DispatchResult, ensure};13use frame_system::{self as system, ensure_signed};14use sp_runtime::sp_std::prelude::Vec;1516#[cfg(test)]17mod mock;1819#[cfg(test)]20mod tests;2122#[derive(Encode, Decode, Default, Clone, PartialEq)]23#[cfg_attr(feature = "std", derive(Debug))]24pub struct CollectionType<AccountId> {25 pub owner: AccountId,26 pub next_item_id: u64,27 pub custom_data_size: u32,28}2930#[derive(Encode, Decode, Default, Clone, PartialEq)]31#[cfg_attr(feature = "std", derive(Debug))]32pub struct CollectionAdminsType<AccountId> {33 pub admin: AccountId,34 pub collection_id: u64,35}3637#[derive(Encode, Decode, Default, Clone, PartialEq)]38#[cfg_attr(feature = "std", derive(Debug))]39pub struct NftItemType<AccountId> {40 pub collection: u64,41 pub owner: AccountId,42 pub data: Vec<u8>,43}444546pub trait Trait: system::Trait {47 4849 50 type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;51}525354decl_storage! {55 56 57 trait Store for Module<T: Trait> as Nft {5859 60 pub NextCollectionID get(fn next_collection_id): u64;6162 pub Collection get(collection): map hasher(identity) u64 => CollectionType<T::AccountId>;63 6465 pub AdminList get(admin_list_collection): map hasher(identity) u64 => Vec<T::AccountId>;6667 68 pub Balance get(balance_count): map hasher(blake2_128_concat) (u64, T::AccountId) => u64;69 pub ApprovedList get(approved): map hasher(blake2_128_concat) (u64, u64) => Vec<T::AccountId>;7071 pub ItemList get(item_id): map hasher(blake2_128_concat) (u64, u64) => NftItemType<T::AccountId>;72 7374 pub ItemListIndex get(item_index): map hasher(blake2_128_concat) u64 => u64;75 76 }77}787980decl_event!(81 pub enum Event<T>82 where83 AccountId = <T as system::Trait>::AccountId,84 {85 Created(u32, AccountId),86 }87);888990decl_module! {91 92 pub struct Module<T: Trait> for enum Call where origin: T::Origin {9394 95 96 fn deposit_event() = default;9798 99 100 101102 103 104 105 106107 108 109 110 111 #[weight = frame_support::weights::SimpleDispatchInfo::default()]112 pub fn create_collection(origin, custom_data_sz: u32) -> DispatchResult {113 114 let who = ensure_signed(origin)?;115116 117 let next_id = NextCollectionID::get();118119 NextCollectionID::put(next_id);120121 122 let new_collection = CollectionType {123 owner: who,124 next_item_id: next_id,125 custom_data_size: custom_data_sz,126 };127128 129 <Collection<T>>::insert(next_id, new_collection);130131 Ok(())132 }133134 #[weight = frame_support::weights::SimpleDispatchInfo::default()]135 pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {136137 let sender = ensure_signed(origin)?;138 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");139140 let owner = <Collection<T>>::get(collection_id).owner;141 ensure!(sender == owner, "You do not own this collection");142 <Collection<T>>::remove(collection_id);143144 Ok(())145 }146147 #[weight = frame_support::weights::SimpleDispatchInfo::default()]148 pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {149150 let sender = ensure_signed(origin)?;151 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");152153 let mut target_collection = <Collection<T>>::get(collection_id);154 ensure!(sender == target_collection.owner, "You do not own this collection");155156 target_collection.owner = new_owner;157 <Collection<T>>::insert(collection_id, target_collection);158159 Ok(())160 }161162 #[weight = frame_support::weights::SimpleDispatchInfo::default()]163 pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {164165 let sender = ensure_signed(origin)?;166 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");167168 let target_collection = <Collection<T>>::get(collection_id);169 let is_owner = sender == target_collection.owner;170171 let no_perm_mes = "You do not have permissions to modify this collection";172 let exists = <AdminList<T>>::contains_key(collection_id);173174 if !is_owner175 {176 ensure!(exists, no_perm_mes);177 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);178 }179180 let mut admin_arr: Vec<T::AccountId> = Vec::new();181 if exists182 {183 admin_arr = <AdminList<T>>::get(collection_id);184 ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role");185 }186187 admin_arr.push(new_admin_id);188 <AdminList<T>>::insert(collection_id, admin_arr);189190 Ok(())191 }192193 #[weight = frame_support::weights::SimpleDispatchInfo::default()]194 pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {195196 let sender = ensure_signed(origin)?;197 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");198199 let target_collection = <Collection<T>>::get(collection_id);200 let is_owner = sender == target_collection.owner;201202 let no_perm_mes = "You do not have permissions to modify this collection";203 let exists = <AdminList<T>>::contains_key(collection_id);204205 if !is_owner206 {207 ensure!(exists, no_perm_mes);208 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);209 }210211 if exists212 {213 let mut admin_arr = <AdminList<T>>::get(collection_id);214 admin_arr.retain(|i| *i != account_id);215 <AdminList<T>>::insert(collection_id, admin_arr);216 }217218 Ok(())219 }220221 #[weight = frame_support::weights::SimpleDispatchInfo::default()]222 pub fn create_item(origin, collection_id: u64, properties: Vec<u8>) -> DispatchResult {223224 let sender = ensure_signed(origin)?;225 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");226227 let target_collection = <Collection<T>>::get(collection_id);228 ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");229 let is_owner = sender == target_collection.owner;230231 let no_perm_mes = "You do not have permissions to modify this collection";232 let exists = <AdminList<T>>::contains_key(collection_id);233234 if !is_owner235 {236 ensure!(exists, no_perm_mes);237 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);238 }239240 let new_balance = <Balance<T>>::get((collection_id, sender.clone())) + 1;241 <Balance<T>>::insert((collection_id, sender.clone()), new_balance);242243 244 let new_item = NftItemType {245 collection: collection_id,246 owner: sender,247 data: properties,248 };249250 let current_index = <ItemListIndex>::get(collection_id);251 <ItemListIndex>::insert(collection_id, current_index);252 <ItemList<T>>::insert((collection_id, current_index), new_item);253254 Ok(())255 }256257 #[weight = frame_support::weights::SimpleDispatchInfo::default()]258 pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {259260 let sender = ensure_signed(origin)?;261 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");262263 let target_collection = <Collection<T>>::get(collection_id);264 let is_owner = sender == target_collection.owner;265266 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");267 let item = <ItemList<T>>::get((collection_id, item_id));268269 if !is_owner270 {271 272 if item.owner != sender273 {274 let no_perm_mes = "You do not have permissions to modify this collection";275276 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);277 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);278 }279 }280 <ItemList<T>>::remove((collection_id, item_id));281282 283 let new_balance = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;284 <Balance<T>>::insert((collection_id, item.owner.clone()), new_balance);285286 Ok(())287 }288289 #[weight = frame_support::weights::SimpleDispatchInfo::default()]290 pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {291292 let sender = ensure_signed(origin)?;293 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");294295 let target_collection = <Collection<T>>::get(collection_id);296 let is_owner = sender == target_collection.owner;297298 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");299 let mut item = <ItemList<T>>::get((collection_id, item_id));300301 if !is_owner302 {303 304 if item.owner != sender305 {306 let no_perm_mes = "You do not have permissions to modify this collection";307308 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);309 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);310 }311 }312 <ItemList<T>>::remove((collection_id, item_id));313314 315 let balance_old_owner = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;316 <Balance<T>>::insert((collection_id, item.owner.clone()), balance_old_owner);317318 let balance_new_owner = <Balance<T>>::get((collection_id, new_owner.clone())) + 1;319 <Balance<T>>::insert((collection_id, new_owner.clone()), balance_new_owner);320321 322 item.owner = new_owner;323 <ItemList<T>>::insert((collection_id, item_id), item);324325 326 let itm: Vec<T::AccountId> = Vec::new();327 <ApprovedList<T>>::insert((collection_id, item_id), itm);328329 Ok(())330 }331332 #[weight = frame_support::weights::SimpleDispatchInfo::default()]333 pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult {334335 let sender = ensure_signed(origin)?;336 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");337338 let target_collection = <Collection<T>>::get(collection_id);339 let is_owner = sender == target_collection.owner;340341 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");342 let item = <ItemList<T>>::get((collection_id, item_id));343344 if !is_owner345 {346 347 if item.owner != sender348 {349 let no_perm_mes = "You do not have permissions to modify this collection";350351 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);352 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);353 }354 }355356 let list_exists = <ApprovedList<T>>::contains_key((collection_id, item_id));357 if list_exists {358359 let mut list = <ApprovedList<T>>::get((collection_id, item_id));360 let item_contains = list.contains(&approved.clone());361362 if !item_contains {363 list.push(approved.clone());364 }365 } else {366367 let mut itm = Vec::new();368 itm.push(approved.clone());369 <ApprovedList<T>>::insert((collection_id, item_id), itm);370 }371372 Ok(())373 }374375 #[weight = frame_support::weights::SimpleDispatchInfo::default()]376 pub fn transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {377378 let no_perm_mes = "You do not have permissions to modify this collection";379 ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);380 let list_itm = <ApprovedList<T>>::get((collection_id, item_id));381 ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);382383 Self::transfer(origin, collection_id, item_id, new_owner)?;384385 Ok(())386 }387388 #[weight = frame_support::weights::SimpleDispatchInfo::default()]389 pub fn safe_transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {390391 let no_perm_mes = "You do not have permissions to modify this collection";392 ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);393 let list_itm = <ApprovedList<T>>::get((collection_id, item_id));394 ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);395396 397398 Self::transfer(origin, collection_id, item_id, new_owner)?;399400 Ok(())401 }402 }403}