Skip to content

Commit

Permalink
test: initiate bond feature test && other endpoints coverage
Browse files Browse the repository at this point in the history
Refs: #11
  • Loading branch information
bucurdavid committed Apr 2, 2024
1 parent ada07a6 commit 06f5741
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 14 deletions.
28 changes: 23 additions & 5 deletions src/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ pub trait AdminModule:
self.add_to_blacklist_event(&compensation_id, &addresses);

for address in addresses.into_iter() {
require!(!self.compensation_blacklist(compensation_id).contains(&address), ERR_ALREADY_IN_STORAGE);
require!(
!self
.compensation_blacklist(compensation_id)
.contains(&address),
ERR_ALREADY_IN_STORAGE
);
self.compensation_blacklist(compensation_id).insert(address);
}
}
Expand All @@ -73,7 +78,11 @@ pub trait AdminModule:
self.remove_from_blacklist_event(&compensation_id, &addresses);

for address in addresses.into_iter() {
require!(self.compensation_blacklist(compensation_id).contains(&address), ERR_NOT_IN_STORAGE);
require!(
self.compensation_blacklist(compensation_id)
.contains(&address),
ERR_NOT_IN_STORAGE
);
self.compensation_blacklist(compensation_id)
.swap_remove(&address);
}
Expand Down Expand Up @@ -201,7 +210,10 @@ pub trait AdminModule:
only_privileged!(self, ERR_NOT_PRIVILEGED);
self.set_accepted_callers_event(&callers);
for caller in callers.into_iter() {
require!(!self.accepted_callers().contains(&caller), ERR_ALREADY_IN_STORAGE);
require!(
!self.accepted_callers().contains(&caller),
ERR_ALREADY_IN_STORAGE
);
self.accepted_callers().insert(caller);
}
}
Expand All @@ -211,7 +223,10 @@ pub trait AdminModule:
only_privileged!(self, ERR_NOT_PRIVILEGED);
self.remove_accepted_callers_event(&callers);
for caller in callers.into_iter() {
require!(self.accepted_callers().contains(&caller), ERR_NOT_IN_STORAGE);
require!(
self.accepted_callers().contains(&caller),
ERR_NOT_IN_STORAGE
);
self.accepted_callers().swap_remove(&caller);
}
}
Expand All @@ -233,7 +248,10 @@ pub trait AdminModule:
for input in args.into_iter() {
let (lock_period, bond) = input.into_tuple();

require!(!self.lock_periods().contains(&lock_period), ERR_ALREADY_IN_STORAGE);
require!(
!self.lock_periods().contains(&lock_period),
ERR_ALREADY_IN_STORAGE
);

self.set_period_and_bond_event(&lock_period, &bond);
self.lock_periods().insert(lock_period);
Expand Down
10 changes: 5 additions & 5 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ pub trait ConfigModule: storage::StorageModule + events::EventsModule {
self.is_contract_owner(address) || self.is_admin(address)
}

#[inline]
fn is_state_active(&self, state: State) -> bool {
state == State::Active
}

fn contract_is_ready(&self) -> bool {
let mut is_ready = true;

Expand All @@ -76,11 +81,6 @@ pub trait ConfigModule: storage::StorageModule + events::EventsModule {
is_ready
}

#[inline]
fn is_state_active(&self, state: State) -> bool {
state == State::Active
}

#[view(getContractState)]
#[storage_mapper("contract_state")]
fn contract_state(&self) -> SingleValueMapper<State>;
Expand Down
22 changes: 22 additions & 0 deletions tests/bonding_state/bonding_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,28 @@ impl ContractState {
self
}

pub fn initiate_bond_for_address(
&mut self,
caller: &str,
address: Address,
token_identifier: &[u8],
nonce: u64,
expect: Option<TxExpect>,
) -> &mut Self {
let tx_expect = expect.unwrap_or(TxExpect::ok());
self.world.sc_call(
ScCallStep::new()
.from(caller)
.call(self.contract.initiate_bond_for_address(
managed_address!(&address),
managed_token_id!(token_identifier),
nonce,
))
.expect(tx_expect),
);
self
}

pub fn pause_contract(&mut self, caller: &str, expect: Option<TxExpect>) -> &mut Self {
let tx_expect = expect.unwrap_or(TxExpect::ok());
self.world.sc_call(
Expand Down
54 changes: 51 additions & 3 deletions tests/endpoints/bond.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use multiversx_sc_scenario::{
};

use crate::bonding_state::bonding_state::{
ContractState, ANOTHER_TOKEN_IDENTIFIER_EXPR, BONDING_CONTRACT_ADDRESS_EXPR,
DATA_NFT_IDENTIFIER, FIRST_USER_ADDRESS_EXPR, ITHEUM_TOKEN_IDENTIFIER,
ITHEUM_TOKEN_IDENTIFIER_EXPR, MINTER_CONTRACT_ADDRESS_EXPR,
ContractState, ADMIN_BONDING_CONTRACT_ADDRESS_EXPR, ANOTHER_TOKEN_IDENTIFIER_EXPR,
BONDING_CONTRACT_ADDRESS_EXPR, DATA_NFT_IDENTIFIER, FIRST_USER_ADDRESS_EXPR,
ITHEUM_TOKEN_IDENTIFIER, ITHEUM_TOKEN_IDENTIFIER_EXPR, MINTER_CONTRACT_ADDRESS_EXPR,
OWNER_BONDING_CONTRACT_ADDRESS_EXPR, SECOND_USER_ADDRESS_EXPR,
};

Expand Down Expand Up @@ -212,3 +212,51 @@ fn bond() {
}),
);
}

#[test]
fn initiate_bond_for_another_address() {
let mut state = ContractState::new();
let first_user_address = state.first_user_address.clone();
let admin = state.admin.clone();

state
.default_deploy_and_set(10u64, 100u64)
.remove_accepted_caller(OWNER_BONDING_CONTRACT_ADDRESS_EXPR, admin.clone(), None)
.set_accepted_caller(
OWNER_BONDING_CONTRACT_ADDRESS_EXPR,
AddressValue::from(MINTER_CONTRACT_ADDRESS_EXPR).to_address(),
None,
)
.unpause_contract(ADMIN_BONDING_CONTRACT_ADDRESS_EXPR, None);

state.bond(
FIRST_USER_ADDRESS_EXPR,
first_user_address.clone(),
DATA_NFT_IDENTIFIER,
1u64,
10u64,
(ITHEUM_TOKEN_IDENTIFIER_EXPR, 0u64, 100u64),
Some(TxExpect::user_error(
"str:Endpoint callable only by accepted callers",
)),
);

state.initiate_bond_for_address(
ADMIN_BONDING_CONTRACT_ADDRESS_EXPR,
first_user_address.clone(),
DATA_NFT_IDENTIFIER,
1u64,
None,
);

// user can bond for himself (implementation for older mints that don't have bonds)
state.bond(
FIRST_USER_ADDRESS_EXPR,
first_user_address.clone(),
DATA_NFT_IDENTIFIER,
1u64,
10u64,
(ITHEUM_TOKEN_IDENTIFIER_EXPR, 0u64, 100u64),
None,
);
}
74 changes: 73 additions & 1 deletion tests/endpoints/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use multiversx_sc_scenario::{
use crate::bonding_state::bonding_state::{
ContractState, BONDING_CONTRACT_ADDRESS_EXPR, DATA_NFT_IDENTIFIER, FIRST_USER_ADDRESS_EXPR,
ITHEUM_TOKEN_IDENTIFIER, ITHEUM_TOKEN_IDENTIFIER_EXPR, MINTER_CONTRACT_ADDRESS_EXPR,
OWNER_BONDING_CONTRACT_ADDRESS_EXPR,
OWNER_BONDING_CONTRACT_ADDRESS_EXPR, SECOND_USER_ADDRESS_EXPR,
};

#[test]
Expand Down Expand Up @@ -69,6 +69,13 @@ fn withdraw_with_withdraw_penalty_test() {
None,
);

state.withdraw(
SECOND_USER_ADDRESS_EXPR,
DATA_NFT_IDENTIFIER,
1u64,
Some(TxExpect::user_error("str:Bond not found")),
);

state.withdraw(FIRST_USER_ADDRESS_EXPR, DATA_NFT_IDENTIFIER, 1u64, None);

state
Expand Down Expand Up @@ -111,6 +118,7 @@ fn withdraw_with_withdraw_penalty_test() {
fn withdraw_after_penalty_was_enforced_test() {
let mut state = ContractState::new();
let first_user_address = state.first_user_address.clone();
let second_user_address = state.second_user_address.clone();
let admin = state.admin.clone();

state
Expand Down Expand Up @@ -159,6 +167,70 @@ fn withdraw_after_penalty_was_enforced_test() {
"str:Penalties exceed withdrawal amount",
)),
);

state.world.transfer_step(
// mocks the mint call in minter and transfers the bond amount
TransferStep::new()
.from(SECOND_USER_ADDRESS_EXPR)
.to(MINTER_CONTRACT_ADDRESS_EXPR)
.esdt_transfer(ITHEUM_TOKEN_IDENTIFIER, 0u64, 100u64),
);

state.bond(
MINTER_CONTRACT_ADDRESS_EXPR,
second_user_address.clone(),
DATA_NFT_IDENTIFIER,
2u64,
10u64,
(ITHEUM_TOKEN_IDENTIFIER_EXPR, 0u64, 100u64),
None,
);

state.sanction(
OWNER_BONDING_CONTRACT_ADDRESS_EXPR,
DATA_NFT_IDENTIFIER,
2u64,
PenaltyType::Minimum,
OptionalValue::None,
None,
);

state
.world
.set_state_step(SetStateStep::new().block_timestamp(11u64));

state.withdraw(SECOND_USER_ADDRESS_EXPR, DATA_NFT_IDENTIFIER, 2u64, None);

state
.world
.check_state_step(CheckStateStep::new().put_account(
SECOND_USER_ADDRESS_EXPR,
CheckAccount::new().esdt_balance(ITHEUM_TOKEN_IDENTIFIER_EXPR, "95"), // 5% penalty
));

state
.world
.check_state_step(CheckStateStep::new().put_account(
BONDING_CONTRACT_ADDRESS_EXPR,
CheckAccount::new().esdt_balance(ITHEUM_TOKEN_IDENTIFIER_EXPR, "105"),
));

// after unbound period
state.withdraw(FIRST_USER_ADDRESS_EXPR, DATA_NFT_IDENTIFIER, 1u64, None);

state
.world
.check_state_step(CheckStateStep::new().put_account(
FIRST_USER_ADDRESS_EXPR,
CheckAccount::new().esdt_balance(ITHEUM_TOKEN_IDENTIFIER_EXPR, "70"), //30 % penalty
));

state
.world
.check_state_step(CheckStateStep::new().put_account(
BONDING_CONTRACT_ADDRESS_EXPR,
CheckAccount::new().esdt_balance(ITHEUM_TOKEN_IDENTIFIER_EXPR, "35"), // 30% first user penalty + 5% second user penalty
));
}

#[test]
Expand Down

0 comments on commit 06f5741

Please sign in to comment.