git.delta.rocks / unique-network / refs/commits / a1ee59a87cde

difftreelog

Safe transfer update

str-mv2020-06-26parent: #fde4f4f.patch.diff
in: master

1 file changed

modifiedpallets/nft/src/lib.rsdiffbeforeafterboth
before · pallets/nft/src/lib.rs
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 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;6162        pub Collection get(collection): map hasher(identity) u64 => CollectionType<T::AccountId>;63        //pub Collection get(collection): map hasher(identity) u64 => CollectionType<T::AccountId>;6465        pub AdminList get(admin_list_collection): map hasher(identity) u64 => Vec<T::AccountId>;6667        /// Balance owner per collection map68        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        // pub ItemList get(item_id): map hasher(blake2_128_concat) (u64, u64) => NftItemType<T::AccountId>;7374        pub ItemListIndex get(item_index): map hasher(blake2_128_concat) u64 => u64;75        // pub ItemListIndex get(item_index): map hasher(blake2_128_concat) u64 => u64;76    }77}7879// The pallet's events80decl_event!(81    pub enum Event<T>82    where83        AccountId = <T as system::Trait>::AccountId,84    {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 = NextCollectionID::get()118                .checked_add(1)119                .expect("collection id error") - 1;120121            NextCollectionID::put(next_id);122123            // Create new collection124            let new_collection = CollectionType {125                owner: who,126                next_item_id: next_id,127                custom_data_size: custom_data_sz,128            };129130            // Add new collection to map131            <Collection<T>>::insert(next_id, new_collection);132133            Ok(())134        }135136        #[weight = frame_support::weights::SimpleDispatchInfo::default()]137        pub fn destroy_collection(origin, collection_id: u64) -> DispatchResult {138139            let sender = ensure_signed(origin)?;140            ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");141142            let owner = <Collection<T>>::get(collection_id).owner;143            ensure!(sender == owner, "You do not own this collection");144            <Collection<T>>::remove(collection_id);145146            Ok(())147        }148149        #[weight = frame_support::weights::SimpleDispatchInfo::default()]150        pub fn change_collection_owner(origin, collection_id: u64, new_owner: T::AccountId) -> DispatchResult {151152            let sender = ensure_signed(origin)?;153            ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");154155            let mut target_collection = <Collection<T>>::get(collection_id);156            ensure!(sender == target_collection.owner, "You do not own this collection");157158            target_collection.owner = new_owner;159            <Collection<T>>::insert(collection_id, target_collection);160161            Ok(())162        }163164        #[weight = frame_support::weights::SimpleDispatchInfo::default()]165        pub fn add_collection_admin(origin, collection_id: u64, new_admin_id: T::AccountId) -> DispatchResult {166167            let sender = ensure_signed(origin)?;168            ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");169170            let target_collection = <Collection<T>>::get(collection_id);171            let is_owner = sender == target_collection.owner;172173            let no_perm_mes = "You do not have permissions to modify this collection";174            let exists = <AdminList<T>>::contains_key(collection_id);175176            if !is_owner177            {178                 ensure!(exists, no_perm_mes);179                 ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);180            }181182            let mut admin_arr: Vec<T::AccountId> = Vec::new();183            if exists184            {185                admin_arr = <AdminList<T>>::get(collection_id);186                ensure!(!admin_arr.contains(&new_admin_id), "Account already has admin role");187            }188189            admin_arr.push(new_admin_id);190            <AdminList<T>>::insert(collection_id, admin_arr);191192            Ok(())193        }194195        #[weight = frame_support::weights::SimpleDispatchInfo::default()]196        pub fn remove_collection_admin(origin, collection_id: u64, account_id: T::AccountId) -> DispatchResult {197198            let sender = ensure_signed(origin)?;199            ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");200201            let target_collection = <Collection<T>>::get(collection_id);202            let is_owner = sender == target_collection.owner;203204            let no_perm_mes = "You do not have permissions to modify this collection";205            let exists = <AdminList<T>>::contains_key(collection_id);206207            if !is_owner208            {209                ensure!(exists, no_perm_mes);210                ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);211            }212213            if exists214            {215                let mut admin_arr = <AdminList<T>>::get(collection_id);216                admin_arr.retain(|i| *i != account_id);217                <AdminList<T>>::insert(collection_id, admin_arr);218            }219220            Ok(())221        }222223        #[weight = frame_support::weights::SimpleDispatchInfo::default()]224        pub fn create_item(origin, collection_id: u64, properties: Vec<u8>) -> DispatchResult {225226            let sender = ensure_signed(origin)?;227            ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");228229            let target_collection = <Collection<T>>::get(collection_id);230            ensure!(target_collection.custom_data_size >= properties.len() as u32, "Size of item is too large");231            let is_owner = sender == target_collection.owner;232233            let no_perm_mes = "You do not have permissions to modify this collection";234            let exists = <AdminList<T>>::contains_key(collection_id);235236            if !is_owner237            {238                ensure!(exists, no_perm_mes);239                ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);240            }241242            let new_balance = <Balance<T>>::get((collection_id, sender.clone())) + 1;243            <Balance<T>>::insert((collection_id, sender.clone()), new_balance);244245            // Create new item246            let new_item = NftItemType {247                collection: collection_id,248                owner: sender,249                data: properties,250            };251252            let current_index = <ItemListIndex>::get(collection_id) + 1;253            <ItemListIndex>::insert(collection_id, current_index);254            <ItemList<T>>::insert((collection_id, current_index), new_item);255256            Ok(())257        }258259        #[weight = frame_support::weights::SimpleDispatchInfo::default()]260        pub fn burn_item(origin, collection_id: u64, item_id: u64) -> DispatchResult {261262            let sender = ensure_signed(origin)?;263            ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");264265            let target_collection = <Collection<T>>::get(collection_id);266            let is_owner = sender == target_collection.owner;267268            ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");269            let item = <ItemList<T>>::get((collection_id, item_id));270271            if !is_owner272            {273                // check if item owner274                if item.owner != sender275                {276                    let no_perm_mes = "You do not have permissions to modify this collection";277278                    ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);279                    ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);280                }281            }282            <ItemList<T>>::remove((collection_id, item_id));283284            // update balance285            let new_balance = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;286            <Balance<T>>::insert((collection_id, item.owner.clone()), new_balance);287288            Ok(())289        }290291        #[weight = frame_support::weights::SimpleDispatchInfo::default()]292        pub fn transfer(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {293294            let sender = ensure_signed(origin)?;295            ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");296297            let target_collection = <Collection<T>>::get(collection_id);298            let is_owner = sender == target_collection.owner;299300            ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");301            let mut item = <ItemList<T>>::get((collection_id, item_id));302303            if !is_owner304            {305                // check if item owner306                if item.owner != sender307                {308                    let no_perm_mes = "You do not have permissions to modify this collection";309310                    ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);311                    ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);312                }313            }314            <ItemList<T>>::remove((collection_id, item_id));315316            // update balance317            let balance_old_owner = <Balance<T>>::get((collection_id, item.owner.clone())) - 1;318            <Balance<T>>::insert((collection_id, item.owner.clone()), balance_old_owner);319320            let balance_new_owner = <Balance<T>>::get((collection_id, new_owner.clone())) + 1;321            <Balance<T>>::insert((collection_id, new_owner.clone()), balance_new_owner);322323            // change owner324            item.owner = new_owner;325            <ItemList<T>>::insert((collection_id, item_id), item);326327            // reset approved list328            let itm: Vec<T::AccountId> = Vec::new();329            <ApprovedList<T>>::insert((collection_id, item_id), itm);330331            Ok(())332        }333334        #[weight = frame_support::weights::SimpleDispatchInfo::default()]335        pub fn approve(origin, approved: T::AccountId, collection_id: u64, item_id: u64) -> DispatchResult {336337            let sender = ensure_signed(origin)?;338            ensure!(<Collection<T>>::contains_key(collection_id), "This collection does not exist");339340            let target_collection = <Collection<T>>::get(collection_id);341            let is_owner = sender == target_collection.owner;342343            ensure!(<ItemList<T>>::contains_key((collection_id, item_id)), "Item does not exists");344            let item = <ItemList<T>>::get((collection_id, item_id));345346            if !is_owner347            {348                // check if item owner349                if item.owner != sender350                {351                    let no_perm_mes = "You do not have permissions to modify this collection";352353                    ensure!(<AdminList<T>>::contains_key(collection_id), no_perm_mes);354                    ensure!(<AdminList<T>>::get(collection_id).contains(&sender), no_perm_mes);355                }356            }357358            let list_exists = <ApprovedList<T>>::contains_key((collection_id, item_id));359            if list_exists {360361                let mut list = <ApprovedList<T>>::get((collection_id, item_id));362                let item_contains = list.contains(&approved.clone());363364                if !item_contains {365                    list.push(approved.clone());366                }367            } else {368369                let mut itm = Vec::new();370                itm.push(approved.clone());371                <ApprovedList<T>>::insert((collection_id, item_id), itm);372            }373374            Ok(())375        }376377        #[weight = frame_support::weights::SimpleDispatchInfo::default()]378        pub fn transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {379380            let no_perm_mes = "You do not have permissions to modify this collection";381            ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);382            let list_itm = <ApprovedList<T>>::get((collection_id, item_id));383            ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);384385            Self::transfer(origin, collection_id, item_id, new_owner)?;386387            Ok(())388        }389390        #[weight = frame_support::weights::SimpleDispatchInfo::default()]391        pub fn safe_transfer_from(origin, collection_id: u64, item_id: u64, new_owner: T::AccountId) -> DispatchResult {392393            let no_perm_mes = "You do not have permissions to modify this collection";394            ensure!(<ApprovedList<T>>::contains_key((collection_id, item_id)), no_perm_mes);395            let list_itm = <ApprovedList<T>>::get((collection_id, item_id));396            ensure!(list_itm.contains(&new_owner.clone()), no_perm_mes);397398            Self::transfer(origin, collection_id, item_id, new_owner)?;399400            Ok(())401        }402    }403}