Skip to content

Commit

Permalink
test-freeze-interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed May 27, 2024
1 parent 10a1837 commit a00d687
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 41 deletions.
20 changes: 5 additions & 15 deletions pallets/funding/src/instantiator/chain_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ impl<
}

pub fn get_free_plmc_balance_for(&mut self, user: AccountIdOf<T>) -> BalanceOf<T> {
self.execute(|| {
<T as Config>::NativeCurrency::balance(&user)
})
self.execute(|| <T as Config>::NativeCurrency::balance(&user))
}

pub fn get_reserved_plmc_balances_for(
Expand All @@ -67,10 +65,8 @@ impl<
&mut self,
user: AccountIdOf<T>,
lock_type: <T as Config>::RuntimeHoldReason,
) -> BalanceOf<T>{
self.execute(|| {
<T as Config>::NativeCurrency::balance_on_hold(&lock_type, &user)
})
) -> BalanceOf<T> {
self.execute(|| <T as Config>::NativeCurrency::balance_on_hold(&lock_type, &user))
}

pub fn get_free_foreign_asset_balances_for(
Expand Down Expand Up @@ -104,14 +100,8 @@ impl<
})
}

pub fn get_ct_asset_balance_for(
&mut self,
project_id: ProjectId,
user: AccountIdOf<T>,
) -> BalanceOf<T> {
self.execute(|| {
<T as Config>::ContributionTokenCurrency::balance(project_id, &user)
})
pub fn get_ct_asset_balance_for(&mut self, project_id: ProjectId, user: AccountIdOf<T>) -> BalanceOf<T> {
self.execute(|| <T as Config>::ContributionTokenCurrency::balance(project_id, &user))
}

pub fn get_all_free_plmc_balances(&mut self) -> Vec<UserToPLMCBalance<T>> {
Expand Down
115 changes: 115 additions & 0 deletions pallets/funding/src/tests/2_evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,10 @@ mod evaluate_extrinsic {
#[cfg(test)]
mod success {
use super::*;
use frame_support::traits::{fungible::Mutate, tokens::Preservation};
use frame_support::traits::fungible::InspectFreeze;
use pallet_balances::AccountData;
use sp_runtime::DispatchError::Token;

#[test]
fn all_investor_types() {
Expand Down Expand Up @@ -618,6 +622,117 @@ mod evaluate_extrinsic {
assert_eq!(stored_evaluation, &expected_evaluation_item);
});
}

#[test]
fn can_evaluate_with_frozen_tokens() {
let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext())));
let issuer = ISSUER_1;
let project_metadata = default_project_metadata(issuer);
let project_id = inst.create_evaluating_project(project_metadata.clone(), issuer);

let evaluation = UserToUSDBalance::new(EVALUATOR_4, 500 * USD_UNIT);
let plmc_required = inst.calculate_evaluation_plmc_spent(vec![evaluation.clone()]);
let frozen_amount = plmc_required[0].plmc_amount;
let plmc_existential_deposits = plmc_required.accounts().existential_deposits();

inst.mint_plmc_to(plmc_existential_deposits);
inst.mint_plmc_to(plmc_required.clone());

inst.execute(|| {
mock::Balances::set_freeze(&(), &EVALUATOR_4, plmc_required[0].plmc_amount).unwrap();
});

inst.execute(|| {
assert_noop!(
Balances::transfer_allow_death(
RuntimeOrigin::signed(EVALUATOR_4),
ISSUER_1,
frozen_amount,
),
TokenError::Frozen
);
});

inst.execute(|| {
assert_ok!(PolimecFunding::evaluate(
RuntimeOrigin::signed(EVALUATOR_4),
get_mock_jwt_with_cid(
EVALUATOR_4,
InvestorType::Retail,
generate_did_from_account(EVALUATOR_4),
project_metadata.clone().policy_ipfs_cid.unwrap()
),
project_id,
evaluation.usd_amount
));
});

let new_evaluations = default_evaluations();
let new_plmc_required = inst.calculate_evaluation_plmc_spent(new_evaluations.clone());
let new_plmc_existential_deposits = new_plmc_required.accounts().existential_deposits();
inst.mint_plmc_to(new_plmc_existential_deposits);
inst.mint_plmc_to(new_plmc_required.clone());
inst.evaluate_for_users(project_id, new_evaluations).unwrap();

inst.start_auction(project_id, ISSUER_1).unwrap();
inst.start_community_funding(project_id).unwrap();
inst.start_remainder_or_end_funding(project_id).unwrap();
inst.finish_funding(project_id).unwrap();

assert_eq!(inst.get_project_details(project_id).status, ProjectStatus::FundingFailed);

let free_balance = inst.get_free_plmc_balance_for(EVALUATOR_4);
let evaluation_held_balance = inst.get_reserved_plmc_balance_for(EVALUATOR_4, HoldReason::Evaluation(project_id).into());
let frozen_balance = inst.execute(|| {
mock::Balances::balance_frozen(&(), &EVALUATOR_4)
});
let account_data = inst.execute(|| {System::account(&EVALUATOR_4)}).data;

assert_eq!(free_balance, inst.get_ed());
assert_eq!(evaluation_held_balance, frozen_amount);
assert_eq!(frozen_balance, frozen_amount);
let expected_account_data = AccountData {
free: inst.get_ed(),
reserved: frozen_amount,
frozen: frozen_amount,
flags: Default::default(),
};
assert_eq!(account_data, expected_account_data);

let treasury_account = <TestRuntime as Config>::ProtocolGrowthTreasury::get();
let pre_slash_treasury_balance = inst.get_free_plmc_balance_for(treasury_account);
inst.execute(|| {
PolimecFunding::settle_failed_evaluation(
RuntimeOrigin::signed(EVALUATOR_4),
project_id,
EVALUATOR_4,
0
).unwrap();
});

let post_slash_treasury_balance = inst.get_free_plmc_balance_for(treasury_account);
let free_balance = inst.get_free_plmc_balance_for(EVALUATOR_4);
let evaluation_held_balance = inst.get_reserved_plmc_balance_for(EVALUATOR_4, HoldReason::Evaluation(project_id).into());
let frozen_balance = inst.execute(|| {
mock::Balances::balance_frozen(&(), &EVALUATOR_4)
});
let account_data = inst.execute(|| {System::account(&EVALUATOR_4)}).data;

let post_slash_evaluation_plmc = frozen_amount - (<TestRuntime as Config>::EvaluatorSlash::get() * frozen_amount);
assert_eq!(free_balance, inst.get_ed() + post_slash_evaluation_plmc);
assert_eq!(evaluation_held_balance, Zero::zero());
assert_eq!(frozen_balance, frozen_amount);
let expected_account_data = AccountData {
free: inst.get_ed() + post_slash_evaluation_plmc,
reserved: Zero::zero(),
frozen: frozen_amount,
flags: Default::default(),
};
assert_eq!(account_data, expected_account_data);

assert!(account_data.frozen > account_data.free);
assert_eq!(post_slash_treasury_balance, pre_slash_treasury_balance + <TestRuntime as Config>::EvaluatorSlash::get() * frozen_amount);
}
}

#[cfg(test)]
Expand Down
31 changes: 5 additions & 26 deletions pallets/funding/src/tests/7_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ mod settle_successful_evaluation_extrinsic {
let prev_ct_balances = inst.get_ct_asset_balances_for(project_id, vec![ISSUER_1, ISSUER_2, ISSUER_3]);
assert!(prev_ct_balances.iter().all(|x| *x == Zero::zero()));

let evaluation_locked_plmc = inst.get_reserved_plmc_balance_for(EVALUATOR_1, HoldReason::Evaluation(project_id).into());
let evaluation_locked_plmc =
inst.get_reserved_plmc_balance_for(EVALUATOR_1, HoldReason::Evaluation(project_id).into());
let free_plmc = inst.get_free_plmc_balance_for(EVALUATOR_1);
assert_ok!(inst.execute(|| PolimecFunding::settle_successful_evaluation(
RuntimeOrigin::signed(EVALUATOR_1),
Expand All @@ -132,14 +133,7 @@ mod settle_successful_evaluation_extrinsic {
assert_eq!(eval_1_ct_rewarded, EVAL_1_REWARD);
assert_eq!(inst.get_reserved_plmc_balance_for(EVALUATOR_1, HoldReason::Evaluation(project_id).into()), 0);
assert_eq!(inst.get_free_plmc_balance_for(EVALUATOR_1), free_plmc + evaluation_locked_plmc);
inst.assert_migration(
project_id,
EVALUATOR_1,
EVAL_1_REWARD,
0,
ParticipationType::Evaluation,
true,
);
inst.assert_migration(project_id, EVALUATOR_1, EVAL_1_REWARD, 0, ParticipationType::Evaluation, true);

assert_ok!(inst.execute(|| PolimecFunding::settle_successful_evaluation(
RuntimeOrigin::signed(EVALUATOR_2),
Expand All @@ -152,15 +146,7 @@ mod settle_successful_evaluation_extrinsic {
assert_eq!(eval_1_ct_rewarded, EVAL_2_REWARD);
assert_eq!(inst.get_reserved_plmc_balance_for(EVALUATOR_2, HoldReason::Evaluation(project_id).into()), 0);
assert_eq!(inst.get_free_plmc_balance_for(EVALUATOR_2), free_plmc + evaluation_locked_plmc);
inst.assert_migration(
project_id,
EVALUATOR_2,
EVAL_2_REWARD,
0,
ParticipationType::Evaluation,
true,
);

inst.assert_migration(project_id, EVALUATOR_2, EVAL_2_REWARD, 0, ParticipationType::Evaluation, true);

assert_ok!(inst.execute(|| PolimecFunding::settle_successful_evaluation(
RuntimeOrigin::signed(EVALUATOR_3),
Expand All @@ -173,14 +159,7 @@ mod settle_successful_evaluation_extrinsic {
assert_eq!(eval_1_ct_rewarded, EVAL_3_REWARD);
assert_eq!(inst.get_reserved_plmc_balance_for(EVALUATOR_3, HoldReason::Evaluation(project_id).into()), 0);
assert_eq!(inst.get_free_plmc_balance_for(EVALUATOR_3), free_plmc + evaluation_locked_plmc);
inst.assert_migration(
project_id,
EVALUATOR_3,
EVAL_3_REWARD,
0,
ParticipationType::Evaluation,
true,
);
inst.assert_migration(project_id, EVALUATOR_3, EVAL_3_REWARD, 0, ParticipationType::Evaluation, true);
}
}

Expand Down

0 comments on commit a00d687

Please sign in to comment.