From b3fda41c65ef3d5198749d60622f0184af244734 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Thu, 06 May 2021 13:35:08 +0000 Subject: [PATCH] feat: add/remove from white list chain extension --- --- a/pallets/nft/src/lib.rs +++ b/pallets/nft/src/lib.rs @@ -842,10 +842,14 @@ let sender = ensure_signed(origin)?; let collection = Self::get_collection(collection_id)?; - Self::check_owner_or_admin_permissions(&collection, sender)?; - >::insert(collection_id, address, true); - + Self::toggle_white_list_internal( + &sender, + &collection, + &address, + true, + )?; + Ok(()) } @@ -867,9 +871,13 @@ let sender = ensure_signed(origin)?; let collection = Self::get_collection(collection_id)?; - Self::check_owner_or_admin_permissions(&collection, sender)?; - >::remove(collection_id, address); + Self::toggle_white_list_internal( + &sender, + &collection, + &address, + false, + )?; Ok(()) } @@ -1851,6 +1859,23 @@ Ok(()) } + pub fn toggle_white_list_internal( + sender: &T::AccountId, + collection: &CollectionHandle, + address: &T::AccountId, + whitelisted: bool, + ) -> DispatchResult { + Self::check_owner_or_admin_permissions(&collection, sender.clone())?; + + if whitelisted { + >::insert(collection.id, address, true); + } else { + >::remove(collection.id, address); + } + + Ok(()) + } + fn is_correct_transfer(collection: &CollectionHandle, recipient: &T::AccountId) -> DispatchResult { let collection_id = collection.id; --- a/runtime/src/chain_extension.rs +++ b/runtime/src/chain_extension.rs @@ -67,6 +67,13 @@ pub data: Vec, } +#[derive(Debug, PartialEq, Encode, Decode)] +pub struct NFTExtToggleWhiteList { + pub collection_id: u32, + pub address: ::AccountId, + pub whitelisted: bool, +} + /// The chain Extension of NFT pallet pub struct NFTExtension; @@ -175,6 +182,21 @@ )?; Ok(RetVal::Converging(func_id)) }, + 6 => { + // Toggle whitelist + let mut env = env.buf_in_buf_out(); + let input: NFTExtToggleWhiteList = env.read_as()?; + + let collection = pallet_nft::Module::::get_collection(input.collection_id)?; + + pallet_nft::Module::::toggle_white_list_internal( + &env.ext().address().clone(), + &collection, + &input.address, + input.whitelisted, + )?; + Ok(RetVal::Converging(func_id)) + } _ => { panic!("Passed unknown func_id to test chain extension: {}", func_id); } -- gitstuff