Skip to content

Commit

Permalink
Comprehensive settlement tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed May 27, 2024
1 parent c6646cc commit ed27dcd
Show file tree
Hide file tree
Showing 4 changed files with 670 additions and 380 deletions.
2 changes: 0 additions & 2 deletions pallets/funding/src/functions/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,6 @@ impl<T: Config> Pallet<T> {
Ok((liquidity_pools_reward_pot, long_term_holder_reward_pot))
}



pub fn migrations_per_xcm_message_allowed() -> u32 {
const MAX_WEIGHT: Weight = Weight::from_parts(20_000_000_000, 1_000_000);

Expand Down
85 changes: 59 additions & 26 deletions pallets/funding/src/instantiator/chain_interactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use super::*;

// general chain interactions
impl<
T: Config + pallet_balances::Config<Balance = BalanceOf<T>>,
AllPalletsWithoutSystem: OnFinalize<BlockNumberFor<T>> + OnIdle<BlockNumberFor<T>> + OnInitialize<BlockNumberFor<T>>,
RuntimeEvent: From<Event<T>> + TryInto<Event<T>> + Parameter + Member + IsType<<T as frame_system::Config>::RuntimeEvent>,
> Instantiator<T, AllPalletsWithoutSystem, RuntimeEvent>
T: Config + pallet_balances::Config<Balance = BalanceOf<T>>,
AllPalletsWithoutSystem: OnFinalize<BlockNumberFor<T>> + OnIdle<BlockNumberFor<T>> + OnInitialize<BlockNumberFor<T>>,
RuntimeEvent: From<Event<T>> + TryInto<Event<T>> + Parameter + Member + IsType<<T as frame_system::Config>::RuntimeEvent>,
> Instantiator<T, AllPalletsWithoutSystem, RuntimeEvent>
{
pub fn new(ext: OptionalExternalities) -> Self {
Self { ext, nonce: RefCell::new(0u64), _marker: PhantomData }
Expand Down 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,16 @@ 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 +110,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 @@ -232,10 +258,10 @@ impl<

// assertions
impl<
T: Config + pallet_balances::Config<Balance = BalanceOf<T>>,
AllPalletsWithoutSystem: OnFinalize<BlockNumberFor<T>> + OnIdle<BlockNumberFor<T>> + OnInitialize<BlockNumberFor<T>>,
RuntimeEvent: From<Event<T>> + TryInto<Event<T>> + Parameter + Member + IsType<<T as frame_system::Config>::RuntimeEvent>,
> Instantiator<T, AllPalletsWithoutSystem, RuntimeEvent>
T: Config + pallet_balances::Config<Balance = BalanceOf<T>>,
AllPalletsWithoutSystem: OnFinalize<BlockNumberFor<T>> + OnIdle<BlockNumberFor<T>> + OnInitialize<BlockNumberFor<T>>,
RuntimeEvent: From<Event<T>> + TryInto<Event<T>> + Parameter + Member + IsType<<T as frame_system::Config>::RuntimeEvent>,
> Instantiator<T, AllPalletsWithoutSystem, RuntimeEvent>
{
pub fn test_ct_created_for(&mut self, project_id: ProjectId) {
self.execute(|| {
Expand Down Expand Up @@ -352,10 +378,10 @@ impl<

// project chain interactions
impl<
T: Config + pallet_balances::Config<Balance = BalanceOf<T>>,
AllPalletsWithoutSystem: OnFinalize<BlockNumberFor<T>> + OnIdle<BlockNumberFor<T>> + OnInitialize<BlockNumberFor<T>>,
RuntimeEvent: From<Event<T>> + TryInto<Event<T>> + Parameter + Member + IsType<<T as frame_system::Config>::RuntimeEvent>,
> Instantiator<T, AllPalletsWithoutSystem, RuntimeEvent>
T: Config + pallet_balances::Config<Balance = BalanceOf<T>>,
AllPalletsWithoutSystem: OnFinalize<BlockNumberFor<T>> + OnIdle<BlockNumberFor<T>> + OnInitialize<BlockNumberFor<T>>,
RuntimeEvent: From<Event<T>> + TryInto<Event<T>> + Parameter + Member + IsType<<T as frame_system::Config>::RuntimeEvent>,
> Instantiator<T, AllPalletsWithoutSystem, RuntimeEvent>
{
pub fn get_issuer(&mut self, project_id: ProjectId) -> AccountIdOf<T> {
self.execute(|| ProjectsDetails::<T>::get(project_id).unwrap().issuer_account)
Expand Down Expand Up @@ -392,7 +418,7 @@ impl<
project_metadata.clone(),
generate_did_from_account(issuer.clone()),
)
.unwrap();
.unwrap();
let last_project_metadata = ProjectsMetadata::<T>::iter().last().unwrap();
log::trace!("Last project metadata: {:?}", last_project_metadata);
});
Expand Down Expand Up @@ -775,17 +801,17 @@ impl<
AcceptedFundingAsset::DOT.to_assethub_id(),
vec![project_metadata.funding_destination_account.clone()],
)[0]
.asset_amount;
.asset_amount;
let total_stored_usdt = self.get_free_foreign_asset_balances_for(
AcceptedFundingAsset::USDT.to_assethub_id(),
vec![project_metadata.funding_destination_account.clone()],
)[0]
.asset_amount;
.asset_amount;
let total_stored_usdc = self.get_free_foreign_asset_balances_for(
AcceptedFundingAsset::USDC.to_assethub_id(),
vec![project_metadata.funding_destination_account.clone()],
)[0]
.asset_amount;
.asset_amount;

assert_eq!(total_expected_dot, total_stored_dot, "DOT amount is incorrect");
assert_eq!(total_expected_usdt, total_stored_usdt, "USDT amount is incorrect");
Expand Down Expand Up @@ -876,7 +902,7 @@ impl<
}
}

fn assert_migration(
pub(crate) fn assert_migration(
&mut self,
project_id: ProjectId,
account: AccountIdOf<T>,
Expand All @@ -885,25 +911,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
2 changes: 0 additions & 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 Down
Loading

0 comments on commit ed27dcd

Please sign in to comment.