difftreelog
NFTPAR-56
in: master
3 files changed
pallets/nft/src/lib.rsdiffbeforeafterboth1#![cfg_attr(not(feature = "std"), no_std)]23use codec::{Decode, Encode};4/// A FRAME pallet template with necessary imports56/// Feel free to remove or edit this file as needed.7/// If you change the name of this file, make sure to update its references in runtime/src/lib.rs8/// If you remove this file, you can remove those references910/// For more guidance on Substrate FRAME, see the example pallet11/// https://github.com/paritytech/substrate/blob/master/frame/example/src/lib.rs12use 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}4445/// The pallet's configuration trait.46pub trait Trait: system::Trait {47 // Add other types and constants required to configure this pallet.4849 /// The overarching event type.50 type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;51}5253// This pallet's storage items.54decl_storage! {55 // It is important to update your storage name so that your pallet's56 // storage items are isolated from other pallets.57 trait Store for Module<T: Trait> as Nft {5859 /// Next available collection ID60 pub NextCollectionID get(fn next_collection_id): u64;61 pub Collection get(fn collection): map hasher(identity) u64 => CollectionType<T::AccountId>;62 pub AdminList get(fn admin_list_collection): map hasher(identity) u64 => Vec<T::AccountId>;6364 /// Balance owner per collection map65 pub Balance get(fn balance_count): map hasher(blake2_128_concat) (u64, T::AccountId) => u64;66 pub ApprovedList get(fn approved): map hasher(blake2_128_concat) (u64, u64) => Vec<T::AccountId>;6768 pub ItemList get(fn item_id): map hasher(blake2_128_concat) (u64, u64) => NftItemType<T::AccountId>;69 pub ItemListIndex get(fn item_index): map hasher(blake2_128_concat) u64 => u64;7071 pub AddressTokens get(fn address_tokens): map hasher(blake2_128_concat) (u64, T::AccountId) => Vec<u64>;7273 }74}7576// The pallet's events77decl_event!(78 pub enum Event<T>79 where80 AccountId = <T as system::Trait>::AccountId,81 {82 Created(u32, AccountId),83 }84);8586// The pallet's dispatchable functions.87decl_module! {88 /// The module declaration.89 pub struct Module<T: Trait> for enum Call where origin: T::Origin {9091 // Initializing events92 // this is needed only if you are using events in your pallet93 fn deposit_event() = default;9495 // Initializing events96 // this is needed only if you are using events in your module97 // fn deposit_event<T>() = default;9899 // Create collection of NFT with given parameters100 //101 // @param customDataSz size of custom data in each collection item102 // returns collection ID103104 // Create collection of NFT with given parameters105 //106 // @param customDataSz size of custom data in each collection item107 // returns collection ID108 #[weight = 0]109 pub fn create_collection(origin, custom_data_sz: u32) -> DispatchResult {110 // Anyone can create a collection111 let who = ensure_signed(origin)?;112113 // Generate next collection ID114 let next_id = NextCollectionID::get();115116 NextCollectionID::put(next_id);117118 // Create new collection119 let new_collection = CollectionType {120 owner: who,121 next_item_id: next_id,122 custom_data_size: custom_data_sz,123 };124125 // Add new collection to map126 <Collection<T>>::insert(next_id, new_collection);127128 Ok(())129 }130131 #[weight = 0]132 pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {133134 let sender = ensure_signed(origin)?;135 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");136137 let owner = <Collection<T>>::get(collection_id).owner;138 ensure!(sender == owner, "You do not own this collection");139 <Collection<T>>::remove(collection_id);140141 Ok(())142 }143144 #[weight = 0]145 pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {146147 let sender = ensure_signed(origin)?;148 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");149150 let mut target_collection = <Collection<T>>::get(collection_id);151 ensure!(sender == target_collection.owner, "You do not own this collection");152153 target_collection.owner = new_owner;154 <Collection<T>>::insert(collection_id, target_collection);155156 Ok(())157 }158159 #[weight = 0]160 pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {161162 let sender = ensure_signed(origin)?;163 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");164165 let target_collection = <Collection<T>>::get(collection_id);166 let is_owner = sender == target_collection.owner;167168 let no_perm_mes = "You do not have permissions to modify this collection";169 let exists = <AdminList<T>>::contains_key(collection_id);170171 if !is_owner172 {173 ensure!(exists, no_perm_mes);174 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);175 }176177 let mut admin_arr: Vec<T::AccountId> = Vec::new();178 if exists179 {180 admin_arr = <AdminList<T>>::get(collection_id);181 ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role");182 }183184 admin_arr.push(new_admin_id);185 <AdminList<T>>::insert(collection_id, admin_arr);186187 Ok(())188 }189190 #[weight = 0]191 pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {192193 let sender = ensure_signed(origin)?;194 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");195196 let target_collection = <Collection<T>>::get(collection_id);197 let is_owner = sender == target_collection.owner;198199 let no_perm_mes = "You do not have permissions to modify this collection";200 let exists = <AdminList<T>>::contains_key(collection_id);201202 if !is_owner203 {204 ensure!(exists, no_perm_mes);205 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);206 }207208 if exists209 {210 let mut admin_arr = <AdminList<T>>::get(collection_id);211 admin_arr.retain(|i| *i != account_id);212 <AdminList<T>>::insert(collection_id, admin_arr);213 }214215 Ok(())216 }217218 #[weight = 0]219 pub fn create_item(origin, collection_id: u64, properties: Vec<u8>) -> DispatchResult {220221 let sender = ensure_signed(origin)?;222 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");223224 let target_collection = <Collection<T>>::get(collection_id);225 ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");226 let is_owner = sender == target_collection.owner;227228 let no_perm_mes = "You do not have permissions to modify this collection";229 let exists = <AdminList<T>>::contains_key(collection_id);230231 if !is_owner232 {233 ensure!(exists, no_perm_mes);234 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);235 }236237 let new_balance = <Balance<T>>::get((collection_id, sender.clone())) + 1;238 <Balance<T>>::insert((collection_id, sender.clone()), new_balance);239240 // Create new item241 let new_item = NftItemType {242 collection: collection_id,243 owner: sender,244 data: properties,245 };246247248 let current_index = <ItemListIndex>::get(collection_id);249250 Self::add_token_index(collection_id, current_index, new_item.owner.clone())?;251252 <ItemListIndex>::insert(collection_id, current_index);253 <ItemList<T>>::insert((collection_id, current_index), new_item);254255 Ok(())256 }257258 #[weight = 0]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_owner271 {272 // check if item owner273 if item.owner != sender274 {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 Self::remove_token_index(collection_id, item_id, item.owner.clone())?;284285 // update balance286 let new_balance = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;287 <Balance<T>>::insert((collection_id, item.owner.clone()), new_balance);288289 Ok(())290 }291292 #[weight = 0]293 pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {294295 let sender = ensure_signed(origin)?;296 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");297298 let target_collection = <Collection<T>>::get(collection_id);299 let is_owner = sender == target_collection.owner;300301 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");302 let mut item = <ItemList<T>>::get((collection_id, item_id));303304 if !is_owner305 {306 // check if item owner307 if item.owner != sender308 {309 let no_perm_mes = "You do not have permissions to modify this collection";310311 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);312 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);313 }314 }315 <ItemList<T>>::remove((collection_id, item_id));316317 // update balance318 let balance_old_owner = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;319 <Balance<T>>::insert((collection_id, item.owner.clone()), balance_old_owner);320321 let balance_new_owner = <Balance<T>>::get((collection_id, new_owner.clone())) + 1;322 <Balance<T>>::insert((collection_id, new_owner.clone()), balance_new_owner);323324 // change owner325 let old_owner = item.owner.clone();326 item.owner = new_owner.clone();327 <ItemList<T>>::insert((collection_id, item_id), item);328329 // update index collection330 Self::move_token_index(collection_id, item_id, old_owner, new_owner.clone())?;331332 // reset approved list333 let itm: Vec<T::AccountId> = Vec::new();334 <ApprovedList<T>>::insert((collection_id, item_id), itm);335336 Ok(())337 }338339 #[weight = 0]340 pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult {341342 let sender = ensure_signed(origin)?;343 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");344345 let target_collection = <Collection<T>>::get(collection_id);346 let is_owner = sender == target_collection.owner;347348 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");349 let item = <ItemList<T>>::get((collection_id, item_id));350351 if !is_owner352 {353 // check if item owner354 if item.owner != sender355 {356 let no_perm_mes = "You do not have permissions to modify this collection";357358 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);359 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);360 }361 }362363 let list_exists = <ApprovedList<T>>::contains_key((collection_id, item_id));364 if list_exists {365366 let mut list = <ApprovedList<T>>::get((collection_id, item_id));367 let item_contains = list.contains(&approved.clone());368369 if !item_contains {370 list.push(approved.clone());371 }372 } else {373374 let mut itm = Vec::new();375 itm.push(approved.clone());376 <ApprovedList<T>>::insert((collection_id, item_id), itm);377 }378379 Ok(())380 }381382 #[weight = 0]383 pub fn transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {384385 let no_perm_mes = "You do not have permissions to modify this collection";386 ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);387 let list_itm = <ApprovedList<T>>::get((collection_id, item_id));388 ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);389390 Self::transfer(origin, collection_id, item_id, new_owner)?;391392 Ok(())393 }394395 #[weight = 0]396 pub fn safe_transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {397398 let no_perm_mes = "You do not have permissions to modify this collection";399 ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);400 let list_itm = <ApprovedList<T>>::get((collection_id, item_id));401 ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);402403 // on_nft_received call404405 Self::transfer(origin, collection_id, item_id, new_owner)?;406407 Ok(())408 }409 }410}411412413impl<T: Trait> Module<T> {414 fn add_token_index(collection_id: u64, item_index: u64, owner: T::AccountId) -> DispatchResult {415 416 let list_exists = <AddressTokens<T>>::contains_key((collection_id, owner.clone()));417 if list_exists {418419 let mut list = <AddressTokens<T>>::get((collection_id, owner.clone()));420 let item_contains = list.contains(&item_index.clone());421422 if !item_contains {423 list.push(item_index.clone());424 }425426 <AddressTokens<T>>::insert((collection_id, owner.clone()), list);427428 } else {429430 let mut itm = Vec::new();431 itm.push(item_index.clone());432 <AddressTokens<T>>::insert((collection_id, owner), itm);433 }434435 Ok(())436 }437438 fn remove_token_index(collection_id: u64, item_index: u64, owner: T::AccountId) -> DispatchResult {439 440 let list_exists = <AddressTokens<T>>::contains_key((collection_id, owner.clone()));441 if list_exists {442443 let mut list = <AddressTokens<T>>::get((collection_id, owner.clone()));444 let item_contains = list.contains(&item_index.clone());445446 if item_contains {447 list.retain(|&item| item != item_index);448 <AddressTokens<T>>::insert((collection_id, owner), list);449 }450 }451452 Ok(())453 }454455 fn move_token_index(collection_id: u64, item_index: u64, old_owner: T::AccountId, new_owner: T::AccountId) -> DispatchResult {456 457 Self::remove_token_index(collection_id, item_index, old_owner)?;458 Self::add_token_index(collection_id, item_index, new_owner)?;459 460 Ok(())461 }462}1#![cfg_attr(not(feature = "std"), no_std)]23use codec::{Decode, Encode};4/// A FRAME pallet template with necessary imports56/// Feel free to remove or edit this file as needed.7/// If you change the name of this file, make sure to update its references in runtime/src/lib.rs8/// If you remove this file, you can remove those references910/// For more guidance on Substrate FRAME, see the example pallet11/// https://github.com/paritytech/substrate/blob/master/frame/example/src/lib.rs12use 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>, // 64 include null escape char28 pub description: Vec<u16>, // 256 include null escape char29 pub token_trefix: Vec<u8>, // 16 include null escape char30 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}4748/// The pallet's configuration trait.49pub trait Trait: system::Trait {50 // Add other types and constants required to configure this pallet.5152 /// The overarching event type.53 type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;54}5556// This pallet's storage items.57decl_storage! {58 // It is important to update your storage name so that your pallet's59 // storage items are isolated from other pallets.60 trait Store for Module<T: Trait> as Nft {6162 /// Next available collection ID63 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 /// Balance owner per collection map68 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>;7576 }77}7879// The pallet's events80decl_event!(81 pub enum Event<T>82 where83 AccountId = <T as system::Trait>::AccountId,84 {85 Created(u64, AccountId),86 ItemCreated(u64),87 ItemDestroyed(u64, u64),88 }89);9091// The pallet's dispatchable functions.92decl_module! {93 /// The module declaration.94 pub struct Module<T: Trait> for enum Call where origin: T::Origin {9596 // Initializing events97 // this is needed only if you are using events in your pallet98 fn deposit_event() = default;99100 // Create collection of NFT with given parameters101 //102 // @param customDataSz size of custom data in each collection item103 // returns collection ID104105 // Create collection of NFT with given parameters106 //107 // @param customDataSz size of custom data in each collection item108 // returns collection ID109 #[weight = 0]110 pub fn create_collection( origin, 111 collection_name: Vec<u16>, 112 collection_description: Vec<u16>, 113 token_prefix: Vec<u8>, 114 custom_data_sz: u32) -> DispatchResult {115116 // Anyone can create a collection117 let who = ensure_signed(origin)?;118119 // check params 120 let mut name = collection_name.to_vec();121 name.push(0);122 ensure!(name.len() <= 64, "Collection name can not be longer than 63 char");123124 let mut description = collection_description.to_vec();125 description.push(0);126 ensure!(name.len() <= 256, "Collection description can not be longer than 255 char");127128 let mut token_trefix = token_prefix.to_vec();129 token_trefix.push(0);130 ensure!(token_trefix.len() <= 16, "Token prefix can not be longer than 15 char");131132 // Generate next collection ID133 let next_id = NextCollectionID::get();134135 NextCollectionID::put(next_id);136137 // Create new collection138 let new_collection = CollectionType {139 owner: who.clone(),140 name: name,141 description: description,142 token_trefix: token_trefix,143 next_item_id: next_id,144 custom_data_size: custom_data_sz,145 };146147 // Add new collection to map148 <Collection<T>>::insert(next_id, new_collection);149150 // call event151 Self::deposit_event(RawEvent::Created(next_id, who.clone()));152153 Ok(())154 }155156 #[weight = 0]157 pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {158159 let sender = ensure_signed(origin)?;160 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");161162 let owner = <Collection<T>>::get(collection_id).owner;163 ensure!(sender == owner, "You do not own this collection");164 <Collection<T>>::remove(collection_id);165166 Ok(())167 }168169 #[weight = 0]170 pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {171172 let sender = ensure_signed(origin)?;173 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");174175 let mut target_collection = <Collection<T>>::get(collection_id);176 ensure!(sender == target_collection.owner, "You do not own this collection");177178 target_collection.owner = new_owner;179 <Collection<T>>::insert(collection_id, target_collection);180181 Ok(())182 }183184 #[weight = 0]185 pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {186187 let sender = ensure_signed(origin)?;188 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");189190 let target_collection = <Collection<T>>::get(collection_id);191 let is_owner = sender == target_collection.owner;192193 let no_perm_mes = "You do not have permissions to modify this collection";194 let exists = <AdminList<T>>::contains_key(collection_id);195196 if !is_owner197 {198 ensure!(exists, no_perm_mes);199 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);200 }201202 let mut admin_arr: Vec<T::AccountId> = Vec::new();203 if exists204 {205 admin_arr = <AdminList<T>>::get(collection_id);206 ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role");207 }208209 admin_arr.push(new_admin_id);210 <AdminList<T>>::insert(collection_id, admin_arr);211212 Ok(())213 }214215 #[weight = 0]216 pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {217218 let sender = ensure_signed(origin)?;219 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");220221 let target_collection = <Collection<T>>::get(collection_id);222 let is_owner = sender == target_collection.owner;223224 let no_perm_mes = "You do not have permissions to modify this collection";225 let exists = <AdminList<T>>::contains_key(collection_id);226227 if !is_owner228 {229 ensure!(exists, no_perm_mes);230 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);231 }232233 if exists234 {235 let mut admin_arr = <AdminList<T>>::get(collection_id);236 admin_arr.retain(|i| *i != account_id);237 <AdminList<T>>::insert(collection_id, admin_arr);238 }239240 Ok(())241 }242243 #[weight = 0]244 pub fn create_item(origin, collection_id: u64, properties: Vec<u8>) -> DispatchResult {245246 let sender = ensure_signed(origin)?;247 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");248249 let target_collection = <Collection<T>>::get(collection_id);250 ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");251 let is_owner = sender == target_collection.owner;252253 let no_perm_mes = "You do not have permissions to modify this collection";254 let exists = <AdminList<T>>::contains_key(collection_id);255256 if !is_owner257 {258 ensure!(exists, no_perm_mes);259 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);260 }261262 let new_balance = <Balance<T>>::get((collection_id, sender.clone())) + 1;263 <Balance<T>>::insert((collection_id, sender.clone()), new_balance);264265 // Create new item266 let new_item = NftItemType {267 collection: collection_id,268 owner: sender,269 data: properties,270 };271272273 let current_index = <ItemListIndex>::get(collection_id);274275 Self::add_token_index(collection_id, current_index, new_item.owner.clone())?;276277 <ItemListIndex>::insert(collection_id, current_index);278 <ItemList<T>>::insert((collection_id, current_index), new_item);279280 // call event281 Self::deposit_event(RawEvent::ItemCreated(collection_id));282283 Ok(())284 }285286 #[weight = 0]287 pub fn burn_item(origin, collection_id: u64, item_id: u64) -> 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 item = <ItemList<T>>::get((collection_id, item_id));297298 if !is_owner299 {300 // check if item owner301 if item.owner != sender302 {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 Self::remove_token_index(collection_id, item_id, item.owner.clone())?;312313 // update balance314 let new_balance = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;315 <Balance<T>>::insert((collection_id, item.owner.clone()), new_balance);316317 // call event318 Self::deposit_event(RawEvent::ItemDestroyed(collection_id, item_id));319320 Ok(())321 }322323 #[weight = 0]324 pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {325326 let sender = ensure_signed(origin)?;327 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");328329 let target_collection = <Collection<T>>::get(collection_id);330 let is_owner = sender == target_collection.owner;331332 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");333 let mut item = <ItemList<T>>::get((collection_id, item_id));334335 if !is_owner336 {337 // check if item owner338 if item.owner != sender339 {340 let no_perm_mes = "You do not have permissions to modify this collection";341342 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);343 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);344 }345 }346 <ItemList<T>>::remove((collection_id, item_id));347348 // update balance349 let balance_old_owner = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;350 <Balance<T>>::insert((collection_id, item.owner.clone()), balance_old_owner);351352 let balance_new_owner = <Balance<T>>::get((collection_id, new_owner.clone())) + 1;353 <Balance<T>>::insert((collection_id, new_owner.clone()), balance_new_owner);354355 // change owner356 let old_owner = item.owner.clone();357 item.owner = new_owner.clone();358 <ItemList<T>>::insert((collection_id, item_id), item);359360 // update index collection361 Self::move_token_index(collection_id, item_id, old_owner, new_owner.clone())?;362363 // reset approved list364 let itm: Vec<T::AccountId> = Vec::new();365 <ApprovedList<T>>::insert((collection_id, item_id), itm);366367 Ok(())368 }369370 #[weight = 0]371 pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult {372373 let sender = ensure_signed(origin)?;374 ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");375376 let target_collection = <Collection<T>>::get(collection_id);377 let is_owner = sender == target_collection.owner;378379 ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");380 let item = <ItemList<T>>::get((collection_id, item_id));381382 if !is_owner383 {384 // check if item owner385 if item.owner != sender386 {387 let no_perm_mes = "You do not have permissions to modify this collection";388389 ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);390 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);391 }392 }393394 let list_exists = <ApprovedList<T>>::contains_key((collection_id, item_id));395 if list_exists {396397 let mut list = <ApprovedList<T>>::get((collection_id, item_id));398 let item_contains = list.contains(&approved.clone());399400 if !item_contains {401 list.push(approved.clone());402 }403 } else {404405 let mut itm = Vec::new();406 itm.push(approved.clone());407 <ApprovedList<T>>::insert((collection_id, item_id), itm);408 }409410 Ok(())411 }412413 #[weight = 0]414 pub fn transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {415416 let no_perm_mes = "You do not have permissions to modify this collection";417 ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);418 let list_itm = <ApprovedList<T>>::get((collection_id, item_id));419 ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);420421 Self::transfer(origin, collection_id, item_id, new_owner)?;422423 Ok(())424 }425426 #[weight = 0]427 pub fn safe_transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {428429 let no_perm_mes = "You do not have permissions to modify this collection";430 ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);431 let list_itm = <ApprovedList<T>>::get((collection_id, item_id));432 ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);433434 // on_nft_received call435436 Self::transfer(origin, collection_id, item_id, new_owner)?;437438 Ok(())439 }440 }441}442443444impl<T: Trait> Module<T> {445 fn add_token_index(collection_id: u64, item_index: u64, owner: T::AccountId) -> DispatchResult {446 447 let list_exists = <AddressTokens<T>>::contains_key((collection_id, owner.clone()));448 if list_exists {449450 let mut list = <AddressTokens<T>>::get((collection_id, owner.clone()));451 let item_contains = list.contains(&item_index.clone());452453 if !item_contains {454 list.push(item_index.clone());455 }456457 <AddressTokens<T>>::insert((collection_id, owner.clone()), list);458459 } else {460461 let mut itm = Vec::new();462 itm.push(item_index.clone());463 <AddressTokens<T>>::insert((collection_id, owner), itm);464 }465466 Ok(())467 }468469 fn remove_token_index(collection_id: u64, item_index: u64, owner: T::AccountId) -> DispatchResult {470 471 let list_exists = <AddressTokens<T>>::contains_key((collection_id, owner.clone()));472 if list_exists {473474 let mut list = <AddressTokens<T>>::get((collection_id, owner.clone()));475 let item_contains = list.contains(&item_index.clone());476477 if item_contains {478 list.retain(|&item| item != item_index);479 <AddressTokens<T>>::insert((collection_id, owner), list);480 }481 }482483 Ok(())484 }485486 fn move_token_index(collection_id: u64, item_index: u64, old_owner: T::AccountId, new_owner: T::AccountId) -> DispatchResult {487 488 Self::remove_token_index(collection_id, item_index, old_owner)?;489 Self::add_token_index(collection_id, item_index, new_owner)?;490 491 Ok(())492 }493}pallets/nft/src/mock.rsdiffbeforeafterboth--- a/pallets/nft/src/mock.rs
+++ b/pallets/nft/src/mock.rs
@@ -30,7 +30,7 @@
pub const MaximumBlockWeight: Weight = 1024;
pub const MaximumBlockLength: u32 = 2 * 1024;
pub const AvailableBlockRatio: Perbill = Perbill::from_percent(75);
- pub MaximumExtrinsicWeight: Weight = 10 * WEIGHT_PER_SECOND;
+ pub const MaximumExtrinsicWeight: Weight = 10 * WEIGHT_PER_SECOND;
}
impl system::Trait for Test {
pallets/nft/src/tests.rsdiffbeforeafterboth--- a/pallets/nft/src/tests.rs
+++ b/pallets/nft/src/tests.rs
@@ -5,9 +5,14 @@
#[test]
fn create_collection_test() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_eq!(TemplateModule::collection(1).owner, 1);
});
}
@@ -15,10 +20,15 @@
#[test]
fn change_collection_owner() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_ok!(TemplateModule::change_collection_owner(
origin1.clone(),
1,
@@ -31,10 +41,14 @@
#[test]
fn destroy_collection() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
let size = 1024;
let origin1 = Origin::signed(1);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_ok!(TemplateModule::destroy_collection(origin1.clone(), 1));
});
}
@@ -42,11 +56,16 @@
#[test]
fn create_item() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), 1, 2));
assert_ok!(TemplateModule::create_item(
origin2.clone(),
@@ -62,11 +81,16 @@
#[test]
fn burn_item() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_ok!(TemplateModule::add_collection_admin(origin1.clone(), 1, 2));
assert_ok!(TemplateModule::create_item(
origin2.clone(),
@@ -91,14 +115,19 @@
#[test]
fn add_collection_admin() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
let origin3 = Origin::signed(3);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin2.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin3.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin2.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin3.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_eq!(TemplateModule::collection(1).owner, 1);
assert_eq!(TemplateModule::collection(2).owner, 2);
@@ -116,14 +145,19 @@
#[test]
fn remove_collection_admin() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
let origin3 = Origin::signed(3);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin2.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin3.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin2.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin3.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_eq!(TemplateModule::collection(1).owner, 1);
assert_eq!(TemplateModule::collection(2).owner, 2);
@@ -149,14 +183,19 @@
#[test]
fn balance_of() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
let origin3 = Origin::signed(3);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin2.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin3.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin2.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin3.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_eq!(TemplateModule::collection(1).owner, 1);
assert_eq!(TemplateModule::collection(2).owner, 2);
@@ -181,14 +220,19 @@
#[test]
fn transfer() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
let origin3 = Origin::signed(3);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin2.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin3.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin2.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin3.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_eq!(TemplateModule::collection(1).owner, 1);
assert_eq!(TemplateModule::collection(2).owner, 2);
@@ -214,14 +258,19 @@
#[test]
fn approve() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
let origin3 = Origin::signed(3);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin2.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin3.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin2.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin3.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_eq!(TemplateModule::collection(1).owner, 1);
assert_eq!(TemplateModule::collection(2).owner, 2);
@@ -243,14 +292,19 @@
#[test]
fn get_approved() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
let origin3 = Origin::signed(3);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin2.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin3.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin2.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin3.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_eq!(TemplateModule::collection(1).owner, 1);
assert_eq!(TemplateModule::collection(2).owner, 2);
@@ -272,14 +326,19 @@
#[test]
fn transfer_from() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
let origin3 = Origin::signed(3);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin2.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin3.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin2.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin3.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_eq!(TemplateModule::collection(1).owner, 1);
assert_eq!(TemplateModule::collection(2).owner, 2);
@@ -301,14 +360,19 @@
#[test]
fn index_list() {
new_test_ext().execute_with(|| {
+
+ let col_name1: Vec<u16> = "Test1\0".encode_utf16().collect::<Vec<u16>>();
+ let col_desc1: Vec<u16> = "TestDescription1\0".encode_utf16().collect::<Vec<u16>>();
+ let token_prefix1: Vec<u8> = b"token_prefix1\0".to_vec();
+
let size = 1024;
let origin1 = Origin::signed(1);
let origin2 = Origin::signed(2);
let origin3 = Origin::signed(3);
- assert_ok!(TemplateModule::create_collection(origin1.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin2.clone(), size));
- assert_ok!(TemplateModule::create_collection(origin3.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin1.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin2.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
+ assert_ok!(TemplateModule::create_collection(origin3.clone(), col_name1.clone(), col_desc1.clone(), token_prefix1.clone(), size));
assert_eq!(TemplateModule::collection(1).owner, 1);
assert_eq!(TemplateModule::collection(2).owner, 2);