Skip to content

Commit

Permalink
Comprehensive settlement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Jun 4, 2024
1 parent 9497c32 commit c87ef41
Show file tree
Hide file tree
Showing 3 changed files with 1,370 additions and 407 deletions.
47 changes: 37 additions & 10 deletions pallets/funding/src/instantiator/chain_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ impl<
})
}

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

pub fn get_reserved_plmc_balances_for(
&mut self,
user_keys: Vec<AccountIdOf<T>>,
Expand All @@ -57,6 +61,14 @@ impl<
})
}

pub fn get_reserved_plmc_balance_for(
&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))
}

pub fn get_free_foreign_asset_balances_for(
&mut self,
asset_id: AssetIdOf<T>,
Expand All @@ -73,6 +85,10 @@ impl<
})
}

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

pub fn get_ct_asset_balances_for(
&mut self,
project_id: ProjectId,
Expand All @@ -88,6 +104,10 @@ 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_all_free_plmc_balances(&mut self) -> Vec<UserToPLMCBalance<T>> {
let user_keys = self.execute(|| frame_system::Account::<T>::iter_keys().collect());
self.get_free_plmc_balances_for(user_keys)
Expand Down Expand Up @@ -876,7 +896,7 @@ impl<
}
}

fn assert_migration(
pub(crate) fn assert_migration(
&mut self,
project_id: ProjectId,
account: AccountIdOf<T>,
Expand All @@ -885,25 +905,32 @@ impl<
participation_type: ParticipationType,
should_exist: bool,
) {
let correct = match (should_exist, self.execute(|| UserMigrations::<T>::get(project_id, account.clone()))) {
match (should_exist, self.execute(|| UserMigrations::<T>::get(project_id, account.clone()))) {
// User has migrations, so we need to check if any matches our criteria
(_, Some((_, migrations))) => {
let maybe_migration = migrations.into_iter().find(|migration| {
let user = T::AccountId32Conversion::convert(account.clone());
matches!(migration.origin, MigrationOrigin { user: m_user, id: m_id, participation_type: m_participation_type } if m_user == user && m_id == id && m_participation_type == participation_type)
});
let user = T::AccountId32Conversion::convert(account.clone());
matches!(migration.origin, MigrationOrigin { user: m_user, id: m_id, participation_type: m_participation_type } if m_user == user && m_id == id && m_participation_type == participation_type)
});
match maybe_migration {
// Migration exists so we check if the amount is correct and if it should exist
Some(migration) => migration.info.contribution_token_amount == amount.into() && should_exist,
Some(migration) => {
assert!(should_exist);
assert_close_enough!(
migration.info.contribution_token_amount,
amount.into(),
Perquintill::from_percent(99u64)
);
},

// Migration doesn't exist so we check if it should not exist
None => !should_exist,
None => assert!(should_exist),
}
},
// User does not have any migrations, so the migration should not exist
(false, None) => true,
(true, None) => false,
(false, None) => (),
(true, None) => panic!("No migration should have been found"),
};
assert!(correct);
}

pub fn create_remainder_contributing_project(
Expand Down
3 changes: 1 addition & 2 deletions pallets/funding/src/tests/6_funding_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ mod round_flow {
let fee_2 = FEE_2 * 4_000_000 * USD_UNIT;
let fee_3 = FEE_3 * 4_500_000 * USD_UNIT;

let x = PolimecFunding::calculate_fees(USD_REACHED);
dbg!(x);
let total_fee = Perquintill::from_rational(fee_1 + fee_2 + fee_3, USD_REACHED);

let total_ct_fee =
Expand All @@ -66,6 +64,7 @@ mod round_flow {
early_evaluator_total_bonded_usd: EARLY_EVALUATOR_TOTAL_USD_BONDED,
normal_evaluator_total_bonded_usd: NORMAL_EVALUATOR_TOTAL_USD_BONDED,
};
dbg!(&expected_reward_info);
assert_eq!(
inst.get_project_details(project_id).evaluation_round_info.evaluators_outcome,
EvaluatorsOutcome::Rewarded(expected_reward_info)
Expand Down
Loading

0 comments on commit c87ef41

Please sign in to comment.