Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added more tests and changes from nft minter document #26

Merged
merged 2 commits into from
Dec 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 16 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)]
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -142,6 +142,7 @@ pub trait DataNftMint:
attributes
}

// Endpoint used to burn Data NFT-FTs.
#[payable("*")]
#[endpoint(burn)]
fn burn_token(&self) {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -172,15 +173,16 @@ 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 address 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);
}

// 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<ManagedAddress>) {
require!(!whitelist.is_empty(), "Given whitelist is empty");
Expand All @@ -195,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<ManagedAddress>) {
require!(!whitelist.is_empty(), "Given whitelist is empty");
Expand All @@ -218,16 +220,17 @@ 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
#[only_owner]
// 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();
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);
}

// 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();
Expand All @@ -236,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) {
Expand Down
15 changes: 10 additions & 5 deletions src/requirements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
104 changes: 103 additions & 1 deletion tests/rust_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -1058,7 +1080,7 @@ fn white_list_test() {

b_wrapper
.execute_tx(
&owner_address,
&second_user_address,
&setup.contract_wrapper,
&rust_biguint!(0),
|sc| {
Expand Down Expand Up @@ -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");
}
1 change: 1 addition & 0 deletions wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ elrond_wasm_node::wasm_endpoints! {
getMintedPerAddress
getMintedTokens
getTokenId
getUserDataOut
getWhiteList
initializeContract
isWhiteListEnabled
Expand Down