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 name: Vec<u16>, 28 pub description: Vec<u16>, 29 pub token_prefix: Vec<u8>, 30 pub custom_data_size: u32,31}3233#[derive(Encode, Decode, Default, Clone, PartialEq)]34#[cfg_attr(feature = "std", derive(Debug))]35pub struct CollectionAdminsType<AccountId> {36 pub admin: AccountId,37 pub collection_id: u64,38}3940#[derive(Encode, Decode, Default, Clone, PartialEq)]41#[cfg_attr(feature = "std", derive(Debug))]42pub struct NftItemType<AccountId> {43 pub collection: u64,44 pub owner: AccountId,45 pub data: Vec<u8>,46}474849pub trait Trait: system::Trait {50 5152 53 type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;54}555657decl_storage! {58 59 60 trait Store for Module<T: Trait> as Nft {6162 63 pub NextCollectionID get(fn next_collection_id): u64;64 pub Collection get(fn collection): map hasher(identity) u64 => CollectionType<T::AccountId>;65 pub AdminList get(fn admin_list_collection): map hasher(identity) u64 => Vec<T::AccountId>;6667 68 pub Balance get(fn balance_count): map hasher(blake2_128_concat) (u64, T::AccountId) => u64;69 pub ApprovedList get(fn approved): map hasher(blake2_128_concat) (u64, u64) => Vec<T::AccountId>;7071 pub ItemList get(fn item_id): map hasher(blake2_128_concat) (u64, u64) => NftItemType<T::AccountId>;72 pub ItemListIndex get(fn item_index): map hasher(blake2_128_concat) u64 => u64;7374 pub AddressTokens get(fn address_tokens): map hasher(blake2_128_concat) (u64, T::AccountId) => Vec<u64>;75 }76}777879decl_event!(80 pub enum Event<T>81 where82 AccountId = <T as system::Trait>::AccountId,83 {84 Created(u64, AccountId),85 ItemCreated(u64),86 ItemDestroyed(u64, u64),87 }88);899091decl_module! {92 93 pub struct Module<T: Trait> for enum Call where origin: T::Origin {9495 96 97 fn deposit_event() = default;9899 100 101 102103 104 105 106 107108 109 110 111 112 #[weight = frame_support::weights::SimpleDispatchInfo::default()]113 pub fn create_collection( origin, 114 collection_name: Vec<u16>, 115 collection_description: Vec<u16>, 116 token_prefix: Vec<u8>, 117 custom_data_sz: u32) -> DispatchResult {118119 120 let who = ensure_signed(origin)?;121122 123 let mut name = collection_name.to_vec();124 name.push(0);125 ensure!(name.len() <= 64, "Collection name can not be longer than 63 char");126127 let mut description = collection_description.to_vec();128 description.push(0);129 ensure!(name.len() <= 256, "Collection description can not be longer than 255 char");130131 let mut prefix = token_prefix.to_vec();132 prefix.push(0);133 ensure!(prefix.len() <= 16, "Token prefix can not be longer than 15 char");134135 136 let next_id = NextCollectionID::get()137 .checked_add(1)138 .expect("collection id error");139140 NextCollectionID::put(next_id);141142 143 let new_collection = CollectionType {144 owner: who.clone(),145 name: name,146 description: description,147 token_prefix: prefix,148 next_item_id: next_id,149 custom_data_size: custom_data_sz,150 };151152 153 <Collection<T>>::insert(next_id, new_collection);154155 156 Self::deposit_event(RawEvent::Created(next_id, who.clone()));157158 Ok(())159 }160161 #[weight = frame_support::weights::SimpleDispatchInfo::default()]162 pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {163164 let sender = ensure_signed(origin)?;165 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");166167 let owner = <Collection<T>>::get(collection_id).owner;168 ensure!(sender == owner, "You do not own this collection");169 <Collection<T>>::remove(collection_id);170171 Ok(())172 }173174 #[weight = frame_support::weights::SimpleDispatchInfo::default()]175 pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {176177 let sender = ensure_signed(origin)?;178 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");179180 let mut target_collection = <Collection<T>>::get(collection_id);181 ensure!(sender == target_collection.owner, "You do not own this collection");182183 target_collection.owner = new_owner;184 <Collection<T>>::insert(collection_id, target_collection);185186 Ok(())187 }188189 #[weight = frame_support::weights::SimpleDispatchInfo::default()]190 pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {191192 let sender = ensure_signed(origin)?;193 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");194195 let target_collection = <Collection<T>>::get(collection_id);196 let is_owner = sender == target_collection.owner;197198 let no_perm_mes = "You do not have permissions to modify this collection";199 let exists = <AdminList<T>>::contains_key(collection_id);200201 if !is_owner202 {203 ensure!(exists, no_perm_mes);204 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);205 }206207 let mut admin_arr: Vec<T::AccountId> = Vec::new();208 if exists209 {210 admin_arr = <AdminList<T>>::get(collection_id);211 ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role");212 }213214 admin_arr.push(new_admin_id);215 <AdminList<T>>::insert(collection_id, admin_arr);216217 Ok(())218 }219220 #[weight = frame_support::weights::SimpleDispatchInfo::default()]221 pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> 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 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_owner233 {234 ensure!(exists, no_perm_mes);235 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);236 }237238 if exists239 {240 let mut admin_arr = <AdminList<T>>::get(collection_id);241 admin_arr.retain(|i| *i != account_id);242 <AdminList<T>>::insert(collection_id, admin_arr);243 }244245 Ok(())246 }247248 #[weight = frame_support::weights::SimpleDispatchInfo::default()]249 pub fn create_item(origin, collection_id: u64, properties: Vec<u8>) -> DispatchResult {250251 let sender = ensure_signed(origin)?;252 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");253254 let target_collection = <Collection<T>>::get(collection_id);255 ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");256 let is_owner = sender == target_collection.owner;257258 let no_perm_mes = "You do not have permissions to modify this collection";259 let exists = <AdminList<T>>::contains_key(collection_id);260261 if !is_owner262 {263 ensure!(exists, no_perm_mes);264 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);265 }266267 let new_balance = <Balance<T>>::get((collection_id, sender.clone())) + 1;268 <Balance<T>>::insert((collection_id, sender.clone()), new_balance);269270 271 let new_item = NftItemType {272 collection: collection_id,273 owner: sender,274 data: properties,275 };276277278 let current_index = <ItemListIndex>::get(collection_id)279 .checked_add(1)280 .expect("Item list index id error");281282 Self::add_token_index(collection_id, current_index, new_item.owner.clone())?;283284 <ItemListIndex>::insert(collection_id, current_index);285 <ItemList<T>>::insert((collection_id, current_index), new_item);286287 288 Self::deposit_event(RawEvent::ItemCreated(collection_id));289290 Ok(())291 }292293 #[weight = frame_support::weights::SimpleDispatchInfo::default()]294 pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {295296 let sender = ensure_signed(origin)?;297 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");298299 let target_collection = <Collection<T>>::get(collection_id);300 let is_owner = sender == target_collection.owner;301302 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");303 let item = <ItemList<T>>::get((collection_id, item_id));304305 if !is_owner306 {307 308 if item.owner != sender309 {310 let no_perm_mes = "You do not have permissions to modify this collection";311312 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);313 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);314 }315 }316 <ItemList<T>>::remove((collection_id, item_id));317318 Self::remove_token_index(collection_id, item_id, item.owner.clone())?;319320 321 let new_balance = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;322 <Balance<T>>::insert((collection_id, item.owner.clone()), new_balance);323324 325 Self::deposit_event(RawEvent::ItemDestroyed(collection_id, item_id));326327 Ok(())328 }329330 #[weight = frame_support::weights::SimpleDispatchInfo::default()]331 pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {332333 let sender = ensure_signed(origin)?;334 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");335336 let target_collection = <Collection<T>>::get(collection_id);337 let is_owner = sender == target_collection.owner;338339 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");340 let mut item = <ItemList<T>>::get((collection_id, item_id));341342 if !is_owner343 {344 345 if item.owner != sender346 {347 let no_perm_mes = "You do not have permissions to modify this collection";348349 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);350 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);351 }352 }353 <ItemList<T>>::remove((collection_id, item_id));354355 356 let balance_old_owner = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;357 <Balance<T>>::insert((collection_id, item.owner.clone()), balance_old_owner);358359 let balance_new_owner = <Balance<T>>::get((collection_id, new_owner.clone())) + 1;360 <Balance<T>>::insert((collection_id, new_owner.clone()), balance_new_owner);361362 363 let old_owner = item.owner.clone();364 item.owner = new_owner.clone();365 <ItemList<T>>::insert((collection_id, item_id), item);366367 368 Self::move_token_index(collection_id, item_id, old_owner, new_owner.clone())?;369370 371 let itm: Vec<T::AccountId> = Vec::new();372 <ApprovedList<T>>::insert((collection_id, item_id), itm);373374 Ok(())375 }376377 #[weight = frame_support::weights::SimpleDispatchInfo::default()]378 pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult {379380 let sender = ensure_signed(origin)?;381 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");382383 let target_collection = <Collection<T>>::get(collection_id);384 let is_owner = sender == target_collection.owner;385386 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");387 let item = <ItemList<T>>::get((collection_id, item_id));388389 if !is_owner390 {391 392 if item.owner != sender393 {394 let no_perm_mes = "You do not have permissions to modify this collection";395396 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);397 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);398 }399 }400401 let list_exists = <ApprovedList<T>>::contains_key((collection_id, item_id));402 if list_exists {403404 let mut list = <ApprovedList<T>>::get((collection_id, item_id));405 let item_contains = list.contains(&approved.clone());406407 if !item_contains {408 list.push(approved.clone());409 }410 } else {411412 let mut itm = Vec::new();413 itm.push(approved.clone());414 <ApprovedList<T>>::insert((collection_id, item_id), itm);415 }416417 Ok(())418 }419420 #[weight = frame_support::weights::SimpleDispatchInfo::default()]421 pub fn transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {422423 let no_perm_mes = "You do not have permissions to modify this collection";424 ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);425 let list_itm = <ApprovedList<T>>::get((collection_id, item_id));426 ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);427428 Self::transfer(origin, collection_id, item_id, new_owner)?;429430 Ok(())431 }432433 #[weight = frame_support::weights::SimpleDispatchInfo::default()]434 pub fn safe_transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {435436 let no_perm_mes = "You do not have permissions to modify this collection";437 ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);438 let list_itm = <ApprovedList<T>>::get((collection_id, item_id));439 ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);440441 442443 Self::transfer(origin, collection_id, item_id, new_owner)?;444445 Ok(())446 }447 }448}449450451impl<T: Trait> Module<T> {452 fn add_token_index(collection_id: u64, item_index: u64, owner: T::AccountId) -> DispatchResult {453 454 let list_exists = <AddressTokens<T>>::contains_key((collection_id, owner.clone()));455 if list_exists {456457 let mut list = <AddressTokens<T>>::get((collection_id, owner.clone()));458 let item_contains = list.contains(&item_index.clone());459460 if !item_contains {461 list.push(item_index.clone());462 }463464 <AddressTokens<T>>::insert((collection_id, owner.clone()), list);465466 } else {467468 let mut itm = Vec::new();469 itm.push(item_index.clone());470 <AddressTokens<T>>::insert((collection_id, owner), itm);471 }472473 Ok(())474 }475476 fn remove_token_index(collection_id: u64, item_index: u64, owner: T::AccountId) -> DispatchResult {477 478 let list_exists = <AddressTokens<T>>::contains_key((collection_id, owner.clone()));479 if list_exists {480481 let mut list = <AddressTokens<T>>::get((collection_id, owner.clone()));482 let item_contains = list.contains(&item_index.clone());483484 if item_contains {485 list.retain(|&item| item != item_index);486 <AddressTokens<T>>::insert((collection_id, owner), list);487 }488 }489490 Ok(())491 }492493 fn move_token_index(collection_id: u64, item_index: u64, old_owner: T::AccountId, new_owner: T::AccountId) -> DispatchResult {494 495 Self::remove_token_index(collection_id, item_index, old_owner)?;496 Self::add_token_index(collection_id, item_index, new_owner)?;497 498 Ok(())499 }500}