From bf025eab2105faf1b676d56db3249abf7cfdb380 Mon Sep 17 00:00:00 2001 From: David Bucur Date: Tue, 6 Dec 2022 18:11:32 +0200 Subject: [PATCH 1/2] Added more tests and changed for #2 --- src/lib.rs | 8 ++-- src/requirements.rs | 15 ++++--- tests/rust_tests.rs | 104 +++++++++++++++++++++++++++++++++++++++++++- wasm/src/lib.rs | 1 + 4 files changed, 119 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 9a48015..5b72eef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,10 +172,11 @@ pub trait DataNftMint: self.anti_spam_tax(&token_id).set(tax); } - // Endpoint that will be used by the owner to change the whitelist enable value. - #[only_owner] + // Endpoint that will be used by the owner and privileged addresses to change the whitelist enable value. #[endpoint(setWhiteListEnabled)] fn set_white_list_enabled(&self, is_enabled: bool) { + let caller = self.blockchain().get_caller(); + self.require_is_privileged(&caller); self.whitelist_enable_toggle_event(&is_enabled); self.white_list_enabled().set(is_enabled); } @@ -219,9 +220,10 @@ pub trait DataNftMint: } // Endpoint that will be used by the owner to set min and max royalties - #[only_owner] #[endpoint(setRoyaltiesLimits)] fn set_royalties_limits(&self, min_royalties: BigUint, max_royalties: BigUint) { + let caller = self.blockchain().get_caller(); + self.require_is_privileged(&caller); self.set_royalties_limits_event(&min_royalties, &max_royalties); self.min_royalties().set(min_royalties); self.max_royalties().set(max_royalties); diff --git a/src/requirements.rs b/src/requirements.rs index e8d7da0..1f3513b 100644 --- a/src/requirements.rs +++ b/src/requirements.rs @@ -58,11 +58,16 @@ pub trait RequirementsModule: crate::storage::StorageModule { // Checks whether address is privileged fn require_is_privileged(&self, address: &ManagedAddress) { - require!( - &self.blockchain().get_owner_address() == address - || &self.administrator().get() == address, - "Address is not privileged" - ); + if &self.blockchain().get_owner_address() != address { + require!( + !&self.administrator().is_empty(), + "Address is not privileged" + ); + require!( + &self.administrator().get() == address, + "Address is not privileged" + ); + } } // Checks wheter the uris are valid diff --git a/tests/rust_tests.rs b/tests/rust_tests.rs index 228aab2..85f67e1 100644 --- a/tests/rust_tests.rs +++ b/tests/rust_tests.rs @@ -1016,6 +1016,28 @@ fn white_list_test() { ) .assert_ok(); + b_wrapper + .execute_tx( + &owner_address, + &setup.contract_wrapper, + &rust_biguint!(0), + |sc| { + sc.set_white_list_enabled(true); + }, + ) + .assert_ok(); + + b_wrapper + .execute_tx( + &second_user_address, + &setup.contract_wrapper, + &rust_biguint!(0), + |sc| { + sc.set_white_list_enabled(true); + }, + ) + .assert_ok(); + b_wrapper .execute_query(&setup.contract_wrapper, |sc| { let whitelist = MultiValueEncoded::new(); @@ -1058,7 +1080,7 @@ fn white_list_test() { b_wrapper .execute_tx( - &owner_address, + &second_user_address, &setup.contract_wrapper, &rust_biguint!(0), |sc| { @@ -1238,3 +1260,83 @@ fn url_validation_test() { }) .assert_ok(); } + +#[test] // Tests wheter an user cannont interact with functions that require privileges +fn privileges_test() { + let mut setup = setup_contract(datanftmint::contract_obj); + let b_wrapper = &mut setup.blockchain_wrapper; + let user_address = &setup.first_user_address; + + b_wrapper + .execute_tx( + &user_address, + &setup.contract_wrapper, + &rust_biguint!(0u64), + |sc| { + sc.set_is_paused(false); + }, + ) + .assert_user_error("Address is not privileged"); + + b_wrapper + .execute_tx( + &user_address, + &setup.contract_wrapper, + &rust_biguint!(0), + |sc| sc.set_anti_spam_tax(managed_token_id_wrapped!(TOKEN_ID), managed_biguint!(200)), + ) + .assert_user_error("Address is not privileged"); + + b_wrapper + .execute_tx( + &user_address, + &setup.contract_wrapper, + &rust_biguint!(0u64), + |sc| sc.set_white_list_enabled(false), + ) + .assert_user_error("Address is not privileged"); + + b_wrapper + .execute_tx( + &user_address, + &setup.contract_wrapper, + &rust_biguint!(0u64), + |sc| { + let mut args = MultiValueEncoded::new(); + args.push(managed_address!(user_address)); + sc.set_whitelist_spots(args); + }, + ) + .assert_user_error("Address is not privileged"); + + b_wrapper + .execute_tx( + &user_address, + &setup.contract_wrapper, + &rust_biguint!(0u64), + |sc| { + let mut args = MultiValueEncoded::new(); + args.push(managed_address!(user_address)); + sc.remove_whitelist_spots(args); + }, + ) + .assert_user_error("Address is not privileged"); + + b_wrapper + .execute_tx( + &user_address, + &setup.contract_wrapper, + &rust_biguint!(0u64), + |sc| sc.set_royalties_limits(managed_biguint!(200), managed_biguint!(200)), + ) + .assert_user_error("Address is not privileged"); + + b_wrapper + .execute_tx( + &user_address, + &setup.contract_wrapper, + &rust_biguint!(0u64), + |sc| sc.set_max_supply(managed_biguint!(200)), + ) + .assert_user_error("Address is not privileged"); +} diff --git a/wasm/src/lib.rs b/wasm/src/lib.rs index 008174f..27b5a7d 100644 --- a/wasm/src/lib.rs +++ b/wasm/src/lib.rs @@ -19,6 +19,7 @@ elrond_wasm_node::wasm_endpoints! { getMintedPerAddress getMintedTokens getTokenId + getUserDataOut getWhiteList initializeContract isWhiteListEnabled From 434a0d45d48000200e4e28855011a4c9b793d160 Mon Sep 17 00:00:00 2001 From: David Bucur Date: Tue, 6 Dec 2022 18:57:28 +0200 Subject: [PATCH 2/2] Added code comments #2 --- src/lib.rs | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5b72eef..fb19826 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,7 +20,7 @@ pub trait DataNftMint: + nft_mint_utils::NftMintUtils + views::ViewsModule { - // When the smart contract is deployed or upgraded, minting is automatically paused and sale is set to private. + // When the smart contract is deployed or upgraded, minting is automatically paused, whitelisting is enabled and default values are set #[init] fn init(&self) { self.is_paused().set(true); @@ -35,7 +35,7 @@ pub trait DataNftMint: self.max_supply().set(&BigUint::from(20u64)); } - // Endpoint used by the owner in the first place to initialize the contract with all the data needed for the token creation to begin. + // Endpoint used by the owner in the first place to initialize the contract with all the data needed for the SFT token creation #[only_owner] #[payable("EGLD")] #[endpoint(initializeContract)] @@ -74,7 +74,7 @@ pub trait DataNftMint: ) } - // Public endpoint used to mint and buy SFTs. + // Public endpoint used to mint Data NFT-FTs. #[payable("*")] #[endpoint(mint)] fn mint_token( @@ -142,6 +142,7 @@ pub trait DataNftMint: attributes } + // Endpoint used to burn Data NFT-FTs. #[payable("*")] #[endpoint(burn)] fn burn_token(&self) { @@ -154,7 +155,7 @@ pub trait DataNftMint: .nft_burn(payment.token_nonce, &payment.amount) } - // Endpoint that will be used by privileged addresses to change the mint pause value. + // Endpoint that will be used by privileged address to change the contract pause value. #[endpoint(setIsPaused)] fn set_is_paused(&self, is_paused: bool) { let caller = self.blockchain().get_caller(); @@ -163,7 +164,7 @@ pub trait DataNftMint: self.is_paused().set(is_paused); } - // Endpoint that will be used by privileged addresses to set public sale prices. + // Endpoint that will be used by privileged address to set the anti spam tax for a specific token identifier. #[endpoint(setAntiSpamTax)] fn set_anti_spam_tax(&self, token_id: EgldOrEsdtTokenIdentifier, tax: BigUint) { let caller = self.blockchain().get_caller(); @@ -172,7 +173,7 @@ pub trait DataNftMint: self.anti_spam_tax(&token_id).set(tax); } - // Endpoint that will be used by the owner and privileged addresses to change the whitelist enable value. + // Endpoint that will be used by the owner and privileged address to change the whitelist enable value. #[endpoint(setWhiteListEnabled)] fn set_white_list_enabled(&self, is_enabled: bool) { let caller = self.blockchain().get_caller(); @@ -181,7 +182,7 @@ pub trait DataNftMint: self.white_list_enabled().set(is_enabled); } - // Endpoint that will be used by privileged addresses to set whitelist spots. + // Endpoint that will be used by the owner and privileged address to set whitelist spots. #[endpoint(setWhiteListSpots)] fn set_whitelist_spots(&self, whitelist: MultiValueEncoded) { require!(!whitelist.is_empty(), "Given whitelist is empty"); @@ -196,7 +197,7 @@ pub trait DataNftMint: } } - // Endpoint that will be used by privileged addresses to unset whitelist spots. + // Endpoint that will be used by the owner privileged address to unset whitelist spots. #[endpoint(removeWhiteListSpots)] fn remove_whitelist_spots(&self, whitelist: MultiValueEncoded) { require!(!whitelist.is_empty(), "Given whitelist is empty"); @@ -219,7 +220,7 @@ pub trait DataNftMint: self.mint_time_limit().set(mint_time_limit); } - // Endpoint that will be used by the owner to set min and max royalties + // Endpoint that will be used by the owner and privileged address to set min and max royalties #[endpoint(setRoyaltiesLimits)] fn set_royalties_limits(&self, min_royalties: BigUint, max_royalties: BigUint) { let caller = self.blockchain().get_caller(); @@ -229,7 +230,7 @@ pub trait DataNftMint: self.max_royalties().set(max_royalties); } - // Endpoint that will be used by privileged addresses to set max supply. + // Endpoint that will be used by the owner and privileged address to set max supply. #[endpoint(setMaxSupply)] fn set_max_supply(&self, max_supply: BigUint) { let caller = self.blockchain().get_caller(); @@ -238,7 +239,7 @@ pub trait DataNftMint: self.max_supply().set(max_supply); } - // Endpoint that will be used by the owner to change the administrator + // Endpoint that will be used by the owner to change the administrator (privileged) address. #[only_owner] #[endpoint(setAdministrator)] fn set_administrator(&self, administrator: ManagedAddress) {