From 6699784fdba4f9cb6e04be0739d3ec54745c112f Mon Sep 17 00:00:00 2001 From: Juan Ignacio Rios Date: Fri, 26 Apr 2024 16:29:04 +0200 Subject: [PATCH] fix auction tests --- pallets/funding/src/benchmarking.rs | 163 ++++++------ pallets/funding/src/functions.rs | 1 - .../src/instantiator/async_features.rs | 161 ++++-------- .../funding/src/instantiator/calculations.rs | 120 +++++---- .../src/instantiator/chain_interactions.rs | 241 +++++++++--------- pallets/funding/src/tests/1_application.rs | 8 +- pallets/funding/src/tests/2_evaluation.rs | 36 ++- pallets/funding/src/tests/3_auction.rs | 241 ++++++++---------- pallets/funding/src/tests/4_community.rs | 103 +++----- pallets/funding/src/tests/5_remainder.rs | 81 +++--- pallets/funding/src/tests/6_funding_end.rs | 4 +- pallets/funding/src/tests/7_settlement.rs | 31 +-- pallets/funding/src/tests/misc.rs | 21 +- pallets/funding/src/tests/mod.rs | 20 +- 14 files changed, 550 insertions(+), 681 deletions(-) diff --git a/pallets/funding/src/benchmarking.rs b/pallets/funding/src/benchmarking.rs index 77932cb98..6898b3446 100644 --- a/pallets/funding/src/benchmarking.rs +++ b/pallets/funding/src/benchmarking.rs @@ -130,8 +130,9 @@ where let auction_funding_target = default_project_metadata.minimum_price.saturating_mul_int( default_project_metadata.auction_round_allocation_percentage * default_project_metadata.total_allocation_size, ); + let inst = BenchInstantiator::::new(None); - BenchInstantiator::generate_bids_from_total_usd( + inst.generate_bids_from_total_usd( Percent::from_percent(95) * auction_funding_target, 10u128.into(), default_weights(), @@ -147,10 +148,11 @@ where ::Balance: From, T::Hash: From, { + let inst = BenchInstantiator::::new(None); let default_project = default_project::(account::>("issuer", 0, 0)); let total_ct_for_bids = default_project.auction_round_allocation_percentage * default_project.total_allocation_size; let total_usd_for_bids = default_project.minimum_price.checked_mul_int(total_ct_for_bids).unwrap(); - BenchInstantiator::::generate_bids_from_total_usd( + inst.generate_bids_from_total_usd( total_usd_for_bids, default_project.minimum_price, default_weights(), @@ -165,6 +167,7 @@ where ::Balance: From, T::Hash: From, { + let inst = BenchInstantiator::::new(None); let default_project_metadata = default_project::(account::>("issuer", 0, 0)); let funding_target = @@ -175,7 +178,7 @@ where let contributing_funding_target = funding_target - auction_funding_target; - BenchInstantiator::generate_contributions_from_total_usd( + inst.generate_contributions_from_total_usd( Percent::from_percent(85) * contributing_funding_target, 10u128.into(), default_weights(), @@ -190,6 +193,7 @@ where ::Balance: From, T::Hash: From, { + let inst = BenchInstantiator::::new(None); let default_project_metadata = default_project::(account::>("issuer", 0, 0)); let funding_target = @@ -200,7 +204,7 @@ where let contributing_funding_target = funding_target - auction_funding_target; - BenchInstantiator::generate_contributions_from_total_usd( + inst.generate_contributions_from_total_usd( Percent::from_percent(15) * contributing_funding_target, 10u128.into(), default_weights(), @@ -342,14 +346,14 @@ pub fn run_blocks_to_execute_next_transition( } #[benchmarks( - where - T: Config + frame_system::Config::RuntimeEvent> + pallet_balances::Config>, - ::RuntimeEvent: TryInto> + Parameter + Member, - ::Price: From, - ::Balance: From, - T::Hash: From, - ::AccountId: Into<<::RuntimeOrigin as OriginTrait>::AccountId> + sp_std::fmt::Debug, - ::Balance: Into>, +where +T: Config + frame_system::Config::RuntimeEvent> + pallet_balances::Config>, +::RuntimeEvent: TryInto> + Parameter + Member, +::Price: From, +::Balance: From, +T::Hash: From, +::AccountId: Into<<::RuntimeOrigin as OriginTrait>::AccountId> + sp_std::fmt::Debug, +::Balance: Into>, )] mod benchmarks { use super::*; @@ -368,7 +372,7 @@ mod benchmarks { // real benchmark starts at block 0, and we can't call `events()` at block 0 inst.advance_time(1u32.into()).unwrap(); - let ed = BenchInstantiator::::get_ed(); + let ed = inst.get_ed(); let issuer = account::>("issuer", 0, 0); whitelist_account!(issuer); @@ -623,7 +627,7 @@ mod benchmarks { let project_id = inst.create_evaluating_project(project_metadata.clone(), issuer.clone()); let evaluations = default_evaluations(); - let plmc_for_evaluating = BenchInstantiator::::calculate_evaluation_plmc_spent(evaluations.clone()); + let plmc_for_evaluating = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let existential_plmc: Vec> = plmc_for_evaluating.accounts().existential_deposits(); inst.mint_plmc_to(existential_plmc); @@ -686,10 +690,8 @@ mod benchmarks { let extrinsic_evaluation = UserToUSDBalance::new(test_evaluator.clone(), (1_000 * US_DOLLAR).into()); let existing_evaluations = vec![existing_evaluation; x as usize]; - let plmc_for_existing_evaluations = - BenchInstantiator::::calculate_evaluation_plmc_spent(existing_evaluations.clone()); - let plmc_for_extrinsic_evaluation = - BenchInstantiator::::calculate_evaluation_plmc_spent(vec![extrinsic_evaluation.clone()]); + let plmc_for_existing_evaluations = inst.calculate_evaluation_plmc_spent(existing_evaluations.clone()); + let plmc_for_extrinsic_evaluation = inst.calculate_evaluation_plmc_spent(vec![extrinsic_evaluation.clone()]); let existential_plmc: Vec> = plmc_for_extrinsic_evaluation.accounts().existential_deposits(); @@ -703,10 +705,8 @@ mod benchmarks { inst.evaluate_for_users(project_id, existing_evaluations).expect("All evaluations are accepted"); let extrinsic_plmc_bonded = plmc_for_extrinsic_evaluation[0].plmc_amount; - let total_expected_plmc_bonded = BenchInstantiator::::sum_balance_mappings(vec![ - plmc_for_existing_evaluations.clone(), - plmc_for_extrinsic_evaluation.clone(), - ]); + let total_expected_plmc_bonded = inst + .sum_balance_mappings(vec![plmc_for_existing_evaluations.clone(), plmc_for_extrinsic_evaluation.clone()]); let jwt = get_mock_jwt_with_cid( extrinsic_evaluation.account.clone(), @@ -801,7 +801,7 @@ mod benchmarks { (100_000_000 * ASSET_UNIT).try_into().unwrap_or_else(|_| panic!("Failed to create BalanceOf")); project_metadata.minimum_price = PriceOf::::checked_from_rational(100, 1).unwrap(); - let evaluations = BenchInstantiator::::generate_successful_evaluations( + let evaluations = inst.generate_successful_evaluations( project_metadata.clone(), default_evaluators::(), default_weights(), @@ -812,22 +812,18 @@ mod benchmarks { let existing_bid = BidParams::new(bidder.clone(), (50 * ASSET_UNIT).into(), 5u8, AcceptedFundingAsset::USDT); let existing_bids = vec![existing_bid; existing_bids_count as usize]; - let existing_bids_post_bucketing = BenchInstantiator::::get_actual_price_charged_for_bucketed_bids( + let existing_bids_post_bucketing = + inst.get_actual_price_charged_for_bucketed_bids(&existing_bids, project_metadata.clone(), None); + let plmc_for_existing_bids = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( &existing_bids, project_metadata.clone(), None, ); - let plmc_for_existing_bids = - BenchInstantiator::::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( - &existing_bids, - project_metadata.clone(), - None, - ); let existential_deposits: Vec> = vec![bidder.clone()].existential_deposits(); - let usdt_for_existing_bids: Vec> = - BenchInstantiator::::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + let usdt_for_existing_bids: Vec> = inst + .calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( &existing_bids, project_metadata.clone(), None, @@ -860,12 +856,12 @@ mod benchmarks { assert!(new_bidder.clone() != bidder.clone()); let bid_params = BidParams::new(new_bidder, current_bucket.amount_left, 1u8, AcceptedFundingAsset::USDT); maybe_filler_bid = Some(bid_params.clone()); - let plmc_for_new_bidder = BenchInstantiator::::calculate_auction_plmc_charged_with_given_price( + let plmc_for_new_bidder = inst.calculate_auction_plmc_charged_with_given_price( &vec![bid_params.clone()], current_bucket.current_price, ); let plmc_ed = plmc_for_new_bidder.accounts().existential_deposits(); - let usdt_for_new_bidder = BenchInstantiator::::calculate_auction_funding_asset_charged_with_given_price( + let usdt_for_new_bidder = inst.calculate_auction_funding_asset_charged_with_given_price( &vec![bid_params.clone()], current_bucket.current_price, ); @@ -886,21 +882,21 @@ mod benchmarks { let original_extrinsic_bid = extrinsic_bid.clone(); let current_bucket = Buckets::::get(project_id).unwrap(); // we need to call this after bidding `x` amount of times, to get the latest bucket from storage - let extrinsic_bids_post_bucketing = BenchInstantiator::::get_actual_price_charged_for_bucketed_bids( + let extrinsic_bids_post_bucketing = inst.get_actual_price_charged_for_bucketed_bids( &vec![extrinsic_bid.clone()], project_metadata.clone(), Some(current_bucket), ); assert_eq!(extrinsic_bids_post_bucketing.len(), (do_perform_bid_calls as usize).max(1usize)); - let plmc_for_extrinsic_bids: Vec> = - BenchInstantiator::::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + let plmc_for_extrinsic_bids: Vec> = inst + .calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( &vec![extrinsic_bid.clone()], project_metadata.clone(), Some(current_bucket), ); - let usdt_for_extrinsic_bids: Vec> = - BenchInstantiator::::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + let usdt_for_extrinsic_bids: Vec> = inst + .calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( &vec![extrinsic_bid], project_metadata.clone(), Some(current_bucket), @@ -909,12 +905,10 @@ mod benchmarks { inst.mint_foreign_asset_to(usdt_for_extrinsic_bids.clone()); let total_free_plmc = existential_deposits[0].plmc_amount; - let total_plmc_participation_bonded = BenchInstantiator::::sum_balance_mappings(vec![ - plmc_for_extrinsic_bids.clone(), - plmc_for_existing_bids.clone(), - ]); + let total_plmc_participation_bonded = + inst.sum_balance_mappings(vec![plmc_for_extrinsic_bids.clone(), plmc_for_existing_bids.clone()]); let total_free_usdt = Zero::zero(); - let total_escrow_usdt_locked = BenchInstantiator::::sum_foreign_mappings(vec![ + let total_escrow_usdt_locked = inst.sum_foreign_mappings(vec![ prev_total_escrow_usdt_locked.clone(), usdt_for_extrinsic_bids.clone(), usdt_for_existing_bids.clone(), @@ -1154,15 +1148,13 @@ mod benchmarks { let mut total_ct_sold: BalanceOf = existing_amount * (x as u128).into() + extrinsic_amount; let plmc_for_existing_contributions = - BenchInstantiator::::calculate_contributed_plmc_spent(existing_contributions.clone(), price); + inst.calculate_contributed_plmc_spent(existing_contributions.clone(), price); let plmc_for_extrinsic_contribution = - BenchInstantiator::::calculate_contributed_plmc_spent(vec![extrinsic_contribution.clone()], price); + inst.calculate_contributed_plmc_spent(vec![extrinsic_contribution.clone()], price); let usdt_for_existing_contributions = - BenchInstantiator::::calculate_contributed_funding_asset_spent(existing_contributions.clone(), price); - let usdt_for_extrinsic_contribution = BenchInstantiator::::calculate_contributed_funding_asset_spent( - vec![extrinsic_contribution.clone()], - price, - ); + inst.calculate_contributed_funding_asset_spent(existing_contributions.clone(), price); + let usdt_for_extrinsic_contribution = + inst.calculate_contributed_funding_asset_spent(vec![extrinsic_contribution.clone()], price); let existential_deposits: Vec> = plmc_for_extrinsic_contribution.accounts().existential_deposits(); @@ -1179,11 +1171,11 @@ mod benchmarks { // do "x" contributions for this user inst.contribute_for_users(project_id, existing_contributions).expect("All contributions are accepted"); - let mut total_plmc_bonded = BenchInstantiator::::sum_balance_mappings(vec![ + let mut total_plmc_bonded = inst.sum_balance_mappings(vec![ plmc_for_existing_contributions.clone(), plmc_for_extrinsic_contribution.clone(), ]); - let mut total_usdt_locked = BenchInstantiator::::sum_foreign_mappings(vec![ + let mut total_usdt_locked = inst.sum_foreign_mappings(vec![ prev_total_usdt_locked, usdt_for_existing_contributions.clone(), usdt_for_extrinsic_contribution.clone(), @@ -1424,7 +1416,7 @@ mod benchmarks { project_metadata.minimum_price.saturating_mul_int(project_metadata.total_allocation_size); let evaluations = default_evaluations::(); - let bids = BenchInstantiator::generate_bids_from_total_usd( + let bids = inst.generate_bids_from_total_usd( Percent::from_percent(30) * target_funding_amount, project_metadata.minimum_price, default_weights(), @@ -1432,7 +1424,7 @@ mod benchmarks { default_bidder_multipliers(), ); - let contributions = BenchInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( Percent::from_percent(40) * target_funding_amount, project_metadata.minimum_price, default_weights(), @@ -1557,14 +1549,14 @@ mod benchmarks { let target_funding_amount: BalanceOf = project_metadata.minimum_price.saturating_mul_int(project_metadata.total_allocation_size); - let bids = BenchInstantiator::generate_bids_from_total_usd( + let bids = inst.generate_bids_from_total_usd( Percent::from_percent(15) * target_funding_amount, 10u128.into(), default_weights(), default_bidders::(), default_bidder_multipliers(), ); - let contributions = BenchInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( Percent::from_percent(10) * target_funding_amount, project_metadata.minimum_price, default_weights(), @@ -1583,7 +1575,7 @@ mod benchmarks { let treasury_account = T::ProtocolGrowthTreasury::get(); let free_treasury_plmc = inst.get_free_plmc_balances_for(vec![treasury_account])[0].plmc_amount; - assert_eq!(free_treasury_plmc, BenchInstantiator::::get_ed()); + assert_eq!(free_treasury_plmc, inst.get_ed()); #[extrinsic_call] settle_failed_evaluation( @@ -1606,7 +1598,7 @@ mod benchmarks { let treasury_account = T::ProtocolGrowthTreasury::get(); let free_treasury_plmc = inst.get_free_plmc_balances_for(vec![treasury_account])[0].plmc_amount; - let ed = BenchInstantiator::::get_ed(); + let ed = inst.get_ed(); assert_eq!(free_treasury_plmc, slashed_amount + ed); // Events @@ -1683,7 +1675,7 @@ mod benchmarks { let target_funding_amount: BalanceOf = project_metadata.minimum_price.saturating_mul_int(project_metadata.total_allocation_size); - let bids: Vec> = BenchInstantiator::generate_bids_from_total_usd( + let bids: Vec> = inst.generate_bids_from_total_usd( Percent::from_percent(15) * target_funding_amount, 10u128.into(), default_weights(), @@ -1692,7 +1684,7 @@ mod benchmarks { ); let bidder = bids[0].bidder.clone(); whitelist_account!(bidder); - let contributions = BenchInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( Percent::from_percent(10) * target_funding_amount, project_metadata.minimum_price, default_weights(), @@ -1799,14 +1791,14 @@ mod benchmarks { let target_funding_amount: BalanceOf = project_metadata.minimum_price.saturating_mul_int(project_metadata.total_allocation_size); - let bids: Vec> = BenchInstantiator::generate_bids_from_total_usd( + let bids: Vec> = inst.generate_bids_from_total_usd( Percent::from_percent(15) * target_funding_amount, project_metadata.minimum_price, default_weights(), default_bidders::(), default_bidder_multipliers(), ); - let contributions: Vec> = BenchInstantiator::generate_contributions_from_total_usd( + let contributions: Vec> = inst.generate_contributions_from_total_usd( Percent::from_percent(10) * target_funding_amount, project_metadata.minimum_price, default_weights(), @@ -1875,7 +1867,7 @@ mod benchmarks { let project_id = inst.create_evaluating_project(project_metadata, issuer.clone()); let evaluations = default_evaluations(); - let plmc_for_evaluating = BenchInstantiator::::calculate_evaluation_plmc_spent(evaluations.clone()); + let plmc_for_evaluating = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let existential_plmc: Vec> = plmc_for_evaluating.accounts().existential_deposits(); inst.mint_plmc_to(existential_plmc); @@ -1936,7 +1928,7 @@ mod benchmarks { (Percent::from_percent(25) * evaluation_usd_target).into(), ), ]; - let plmc_for_evaluating = BenchInstantiator::::calculate_evaluation_plmc_spent(evaluations.clone()); + let plmc_for_evaluating = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let existential_plmc: Vec> = plmc_for_evaluating.accounts().existential_deposits(); inst.mint_plmc_to(existential_plmc); @@ -2092,15 +2084,14 @@ mod benchmarks { let all_bids = accepted_bids.iter().chain(rejected_bids.iter()).cloned().collect_vec(); - let plmc_needed_for_bids = - BenchInstantiator::::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( - &all_bids, - project_metadata.clone(), - None, - ); + let plmc_needed_for_bids = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + &all_bids, + project_metadata.clone(), + None, + ); let plmc_ed = all_bids.accounts().existential_deposits(); - let funding_asset_needed_for_bids = - BenchInstantiator::::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + let funding_asset_needed_for_bids = inst + .calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( &all_bids, project_metadata.clone(), None, @@ -2231,14 +2222,14 @@ mod benchmarks { let automatically_rejected_threshold = Percent::from_percent(33); - let bids: Vec> = BenchInstantiator::generate_bids_from_total_usd( + let bids: Vec> = inst.generate_bids_from_total_usd( (automatically_rejected_threshold * target_funding_amount) / 2.into(), project_metadata.minimum_price, default_weights(), default_bidders::(), default_bidder_multipliers(), ); - let contributions = BenchInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( (automatically_rejected_threshold * target_funding_amount) / 2.into(), project_metadata.minimum_price, default_weights(), @@ -2288,14 +2279,14 @@ mod benchmarks { let automatically_rejected_threshold = Percent::from_percent(75); - let bids: Vec> = BenchInstantiator::generate_bids_from_total_usd( + let bids: Vec> = inst.generate_bids_from_total_usd( (automatically_rejected_threshold * target_funding_amount) / 2.into(), project_metadata.minimum_price, default_weights(), default_bidders::(), default_bidder_multipliers(), ); - let contributions = BenchInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( (automatically_rejected_threshold * target_funding_amount) / 2.into(), project_metadata.minimum_price, default_weights(), @@ -2346,14 +2337,14 @@ mod benchmarks { let automatically_rejected_threshold = Percent::from_percent(89); - let bids: Vec> = BenchInstantiator::generate_bids_from_total_usd( + let bids: Vec> = inst.generate_bids_from_total_usd( (automatically_rejected_threshold * target_funding_amount) / 2.into(), project_metadata.minimum_price, default_weights(), default_bidders::(), default_bidder_multipliers(), ); - let contributions = BenchInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( (automatically_rejected_threshold * target_funding_amount) / 2.into(), project_metadata.minimum_price, default_weights(), @@ -2418,20 +2409,20 @@ mod benchmarks { evaluation_target_usd, )); - let plmc_needed_for_evaluating = BenchInstantiator::::calculate_evaluation_plmc_spent(evaluations.clone()); + let plmc_needed_for_evaluating = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let plmc_ed = evaluations.accounts().existential_deposits(); inst.mint_plmc_to(plmc_needed_for_evaluating); inst.mint_plmc_to(plmc_ed); - let bids: Vec> = BenchInstantiator::generate_bids_from_total_usd( + let bids: Vec> = inst.generate_bids_from_total_usd( (automatically_rejected_threshold * target_funding_amount) / 2.into(), project_metadata.minimum_price, default_weights(), default_bidders::(), default_bidder_multipliers(), ); - let contributions = BenchInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( (automatically_rejected_threshold * target_funding_amount) / 2.into(), project_metadata.minimum_price, default_weights(), @@ -2479,14 +2470,14 @@ mod benchmarks { project_metadata.minimum_price.saturating_mul_int(project_metadata.total_allocation_size); let manual_outcome_threshold = Percent::from_percent(50); - let bids: Vec> = BenchInstantiator::generate_bids_from_total_usd( + let bids: Vec> = inst.generate_bids_from_total_usd( (manual_outcome_threshold * target_funding_amount) / 2.into(), project_metadata.minimum_price, default_weights(), default_bidders::(), default_bidder_multipliers(), ); - let contributions = BenchInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( (manual_outcome_threshold * target_funding_amount) / 2.into(), project_metadata.minimum_price, default_weights(), @@ -2557,7 +2548,7 @@ mod benchmarks { let target_funding_amount: BalanceOf = project_metadata.minimum_price.saturating_mul_int(project_metadata.total_allocation_size); - let bids: Vec> = BenchInstantiator::generate_bids_from_total_usd( + let bids: Vec> = inst.generate_bids_from_total_usd( Percent::from_percent(15) * target_funding_amount, project_metadata.minimum_price, default_weights(), @@ -2565,7 +2556,7 @@ mod benchmarks { default_bidder_multipliers(), ); - let contributions = BenchInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( Percent::from_percent(10) * target_funding_amount, project_metadata.minimum_price, default_weights(), diff --git a/pallets/funding/src/functions.rs b/pallets/funding/src/functions.rs index e3207a4a6..056d9fc49 100644 --- a/pallets/funding/src/functions.rs +++ b/pallets/funding/src/functions.rs @@ -2044,7 +2044,6 @@ impl Pallet { multiplier: MultiplierOf, bonded_amount: BalanceOf, ) -> Result, BalanceOf>, DispatchError> { - // TODO: duration should depend on `_multiplier` and `_caller` credential let duration: BlockNumberFor = multiplier.calculate_vesting_duration::(); let duration_as_balance = T::BlockNumberToBalance::convert(duration); let amount_per_block = if duration_as_balance == Zero::zero() { diff --git a/pallets/funding/src/instantiator/async_features.rs b/pallets/funding/src/instantiator/async_features.rs index 2b70cfdb5..687e7e7c2 100644 --- a/pallets/funding/src/instantiator/async_features.rs +++ b/pallets/funding/src/instantiator/async_features.rs @@ -138,11 +138,9 @@ pub async fn async_create_new_project< let mut inst = instantiator.lock().await; let now = inst.current_block(); + let ed = inst.get_ed(); // One ED for the issuer, one for the escrow account - inst.mint_plmc_to(vec![UserToPLMCBalance::new( - issuer.clone(), - Instantiator::::get_ed() * 2u64.into(), - )]); + inst.mint_plmc_to(vec![UserToPLMCBalance::new(issuer.clone(), ed * 2u64.into())]); inst.execute(|| { crate::Pallet::::do_create_project( &issuer.clone(), @@ -233,15 +231,11 @@ pub async fn async_create_auctioning_project< let prev_supply = inst.get_plmc_total_supply(); let prev_plmc_balances = inst.get_free_plmc_balances_for(evaluators.clone()); - let plmc_eval_deposits: Vec> = - Instantiator::::calculate_evaluation_plmc_spent(evaluations.clone()); + let plmc_eval_deposits: Vec> = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let plmc_existential_deposits: Vec> = evaluators.existential_deposits(); let expected_remaining_plmc: Vec> = - Instantiator::::generic_map_operation( - vec![prev_plmc_balances, plmc_existential_deposits.clone()], - MergeOperation::Add, - ); + inst.generic_map_operation(vec![prev_plmc_balances, plmc_existential_deposits.clone()], MergeOperation::Add); inst.mint_plmc_to(plmc_eval_deposits.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); @@ -249,10 +243,7 @@ pub async fn async_create_auctioning_project< inst.evaluate_for_users(project_id, evaluations).unwrap(); let expected_evaluator_balances = - Instantiator::::sum_balance_mappings(vec![ - plmc_eval_deposits.clone(), - plmc_existential_deposits.clone(), - ]); + inst.sum_balance_mappings(vec![plmc_eval_deposits.clone(), plmc_existential_deposits.clone()]); let expected_total_supply = prev_supply + expected_evaluator_balances; @@ -264,18 +255,10 @@ pub async fn async_create_auctioning_project< inst = instantiator.lock().await; let plmc_for_bids = - Instantiator::::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( - &bids, - project_metadata.clone(), - None - ); + inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket(&bids, project_metadata.clone(), None); let plmc_existential_deposits: Vec> = bids.accounts().existential_deposits(); let usdt_for_bids = - Instantiator::::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &bids, - project_metadata, - None - ); + inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket(&bids, project_metadata, None); inst.mint_plmc_to(plmc_for_bids.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); @@ -362,14 +345,9 @@ pub async fn async_create_community_contributing_project< let asset_id = bids[0].asset.to_assethub_id(); let prev_plmc_balances = inst.get_free_plmc_balances_for(bidders.clone()); let prev_funding_asset_balances = inst.get_free_foreign_asset_balances_for(asset_id, bidders.clone()); - let plmc_evaluation_deposits: Vec> = - Instantiator::::calculate_evaluation_plmc_spent(evaluations); + let plmc_evaluation_deposits: Vec> = inst.calculate_evaluation_plmc_spent(evaluations); let plmc_bid_deposits: Vec> = - Instantiator::::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( - &bids, - project_metadata.clone(), - None - ); + inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket(&bids, project_metadata.clone(), None); let participation_usable_evaluation_deposits = plmc_evaluation_deposits .into_iter() .map(|mut x| { @@ -377,29 +355,24 @@ pub async fn async_create_community_contributing_project< x }) .collect::>>(); - let necessary_plmc_mint = Instantiator::::generic_map_operation( + let necessary_plmc_mint = inst.generic_map_operation( vec![plmc_bid_deposits.clone(), participation_usable_evaluation_deposits], MergeOperation::Subtract, ); let total_plmc_participation_locked = plmc_bid_deposits; let plmc_existential_deposits: Vec> = bidders.existential_deposits(); - let funding_asset_deposits = - Instantiator::::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &bids, - project_metadata.clone(), - None - ); - - let bidder_balances = Instantiator::::sum_balance_mappings(vec![ - necessary_plmc_mint.clone(), - plmc_existential_deposits.clone(), - ]); - - let expected_free_plmc_balances = Instantiator::::generic_map_operation( - vec![prev_plmc_balances, plmc_existential_deposits.clone()], - MergeOperation::Add, + let funding_asset_deposits = inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + &bids, + project_metadata.clone(), + None, ); + let bidder_balances = + inst.sum_balance_mappings(vec![necessary_plmc_mint.clone(), plmc_existential_deposits.clone()]); + + let expected_free_plmc_balances = + inst.generic_map_operation(vec![prev_plmc_balances, plmc_existential_deposits.clone()], MergeOperation::Add); + let prev_supply = inst.get_plmc_total_supply(); let post_supply = prev_supply + bidder_balances; @@ -426,7 +399,7 @@ pub async fn async_create_community_contributing_project< let mut inst = instantiator.lock().await; let _weighted_price = inst.get_project_details(project_id).weighted_average_price.unwrap(); - let accepted_bids = Instantiator::::filter_bids_after_auction( + let accepted_bids = inst.filter_bids_after_auction( bids, project_metadata.auction_round_allocation_percentage * project_metadata.total_allocation_size, ); @@ -519,45 +492,25 @@ pub async fn async_create_remainder_contributing_project< let prev_plmc_balances = inst.get_free_plmc_balances_for(contributors.clone()); let prev_funding_asset_balances = inst.get_free_foreign_asset_balances_for(asset_id, contributors.clone()); - let plmc_evaluation_deposits = - Instantiator::::calculate_evaluation_plmc_spent(evaluations); - let plmc_bid_deposits = - Instantiator::::calculate_auction_plmc_charged_with_given_price( - &accepted_bids, - ct_price, - ); + let plmc_evaluation_deposits = inst.calculate_evaluation_plmc_spent(evaluations); + let plmc_bid_deposits = inst.calculate_auction_plmc_charged_with_given_price(&accepted_bids, ct_price); - let plmc_contribution_deposits = - Instantiator::::calculate_contributed_plmc_spent( - contributions.clone(), - ct_price, - ); + let plmc_contribution_deposits = inst.calculate_contributed_plmc_spent(contributions.clone(), ct_price); - let necessary_plmc_mint = Instantiator::::generic_map_operation( + let necessary_plmc_mint = inst.generic_map_operation( vec![plmc_contribution_deposits.clone(), plmc_evaluation_deposits], MergeOperation::Subtract, ); let total_plmc_participation_locked = - Instantiator::::generic_map_operation( - vec![plmc_bid_deposits, plmc_contribution_deposits], - MergeOperation::Add, - ); + inst.generic_map_operation(vec![plmc_bid_deposits, plmc_contribution_deposits], MergeOperation::Add); let plmc_existential_deposits = contributors.existential_deposits(); - let funding_asset_deposits = - Instantiator::::calculate_contributed_funding_asset_spent( - contributions.clone(), - ct_price, - ); - let contributor_balances = Instantiator::::sum_balance_mappings(vec![ - necessary_plmc_mint.clone(), - plmc_existential_deposits.clone(), - ]); + let funding_asset_deposits = inst.calculate_contributed_funding_asset_spent(contributions.clone(), ct_price); + let contributor_balances = + inst.sum_balance_mappings(vec![necessary_plmc_mint.clone(), plmc_existential_deposits.clone()]); - let expected_free_plmc_balances = Instantiator::::generic_map_operation( - vec![prev_plmc_balances, plmc_existential_deposits.clone()], - MergeOperation::Add, - ); + let expected_free_plmc_balances = + inst.generic_map_operation(vec![prev_plmc_balances, plmc_existential_deposits.clone()], MergeOperation::Add); let prev_supply = inst.get_plmc_total_supply(); let post_supply = prev_supply + contributor_balances; @@ -658,50 +611,34 @@ pub async fn async_create_finished_project< let prev_plmc_balances = inst.get_free_plmc_balances_for(contributors.clone()); let prev_funding_asset_balances = inst.get_free_foreign_asset_balances_for(asset_id, contributors.clone()); - let plmc_evaluation_deposits = - Instantiator::::calculate_evaluation_plmc_spent(evaluations); - let plmc_bid_deposits = - Instantiator::::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( - &accepted_bids, - project_metadata.clone(), - None - ); + let plmc_evaluation_deposits = inst.calculate_evaluation_plmc_spent(evaluations); + let plmc_bid_deposits = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + &accepted_bids, + project_metadata.clone(), + None, + ); let plmc_community_contribution_deposits = - Instantiator::::calculate_contributed_plmc_spent( - community_contributions.clone(), - ct_price, - ); + inst.calculate_contributed_plmc_spent(community_contributions.clone(), ct_price); let plmc_remainder_contribution_deposits = - Instantiator::::calculate_contributed_plmc_spent( - remainder_contributions.clone(), - ct_price, - ); + inst.calculate_contributed_plmc_spent(remainder_contributions.clone(), ct_price); - let necessary_plmc_mint = Instantiator::::generic_map_operation( + let necessary_plmc_mint = inst.generic_map_operation( vec![plmc_remainder_contribution_deposits.clone(), plmc_evaluation_deposits], MergeOperation::Subtract, ); - let total_plmc_participation_locked = - Instantiator::::generic_map_operation( - vec![plmc_bid_deposits, plmc_community_contribution_deposits, plmc_remainder_contribution_deposits], - MergeOperation::Add, - ); + let total_plmc_participation_locked = inst.generic_map_operation( + vec![plmc_bid_deposits, plmc_community_contribution_deposits, plmc_remainder_contribution_deposits], + MergeOperation::Add, + ); let plmc_existential_deposits = contributors.existential_deposits(); let funding_asset_deposits = - Instantiator::::calculate_contributed_funding_asset_spent( - remainder_contributions.clone(), - ct_price, - ); + inst.calculate_contributed_funding_asset_spent(remainder_contributions.clone(), ct_price); - let contributor_balances = Instantiator::::sum_balance_mappings(vec![ - necessary_plmc_mint.clone(), - plmc_existential_deposits.clone(), - ]); + let contributor_balances = + inst.sum_balance_mappings(vec![necessary_plmc_mint.clone(), plmc_existential_deposits.clone()]); - let expected_free_plmc_balances = Instantiator::::generic_map_operation( - vec![prev_plmc_balances, plmc_existential_deposits.clone()], - MergeOperation::Add, - ); + let expected_free_plmc_balances = + inst.generic_map_operation(vec![prev_plmc_balances, plmc_existential_deposits.clone()], MergeOperation::Add); let prev_supply = inst.get_plmc_total_supply(); let post_supply = prev_supply + contributor_balances; diff --git a/pallets/funding/src/instantiator/calculations.rs b/pallets/funding/src/instantiator/calculations.rs index 72061473f..28d0fe256 100644 --- a/pallets/funding/src/instantiator/calculations.rs +++ b/pallets/funding/src/instantiator/calculations.rs @@ -7,17 +7,22 @@ impl< RuntimeEvent: From> + TryInto> + Parameter + Member + IsType<::RuntimeEvent>, > Instantiator { - pub fn get_ed() -> BalanceOf { + pub fn get_ed(&self) -> BalanceOf { T::ExistentialDeposit::get() } - pub fn get_ct_account_deposit() -> BalanceOf { + pub fn get_ct_account_deposit(&self) -> BalanceOf { ::ContributionTokenCurrency::deposit_required(One::one()) } - pub fn calculate_evaluation_plmc_spent(evaluations: Vec>) -> Vec> { - let plmc_usd_price = - T::PriceProvider::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap(); + pub fn calculate_evaluation_plmc_spent( + &mut self, + evaluations: Vec>, + ) -> Vec> { + let plmc_usd_price = self.execute(|| { + T::PriceProvider::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap() + }); + let mut output = Vec::new(); for eval in evaluations { let usd_bond = eval.usd_amount; @@ -28,6 +33,7 @@ impl< } pub fn get_actual_price_charged_for_bucketed_bids( + &self, bids: &Vec>, project_metadata: ProjectMetadataOf, maybe_bucket: Option>, @@ -59,11 +65,14 @@ impl< } pub fn calculate_auction_plmc_charged_with_given_price( + &mut self, bids: &Vec>, ct_price: PriceOf, ) -> Vec> { - let plmc_usd_price = - T::PriceProvider::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap(); + let plmc_usd_price = self.execute(|| { + T::PriceProvider::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap() + }); + let mut output = Vec::new(); for bid in bids { let usd_ticket_size = ct_price.saturating_mul_int(bid.amount); @@ -76,15 +85,17 @@ impl< // Make sure you give it all the bids made for the project. It doesn't require a ct_price, since it will simulate the bucket prices itself pub fn calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + &mut self, bids: &Vec>, project_metadata: ProjectMetadataOf, maybe_bucket: Option>, ) -> Vec> { let mut output = Vec::new(); - let plmc_usd_price = - T::PriceProvider::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap(); + let plmc_usd_price = self.execute(|| { + T::PriceProvider::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap() + }); - for (bid, price) in Self::get_actual_price_charged_for_bucketed_bids(bids, project_metadata, maybe_bucket) { + for (bid, price) in self.get_actual_price_charged_for_bucketed_bids(bids, project_metadata, maybe_bucket) { let usd_ticket_size = price.saturating_mul_int(bid.amount); let usd_bond = bid.multiplier.calculate_bonding_requirement::(usd_ticket_size).unwrap(); let plmc_bond = plmc_usd_price.reciprocal().unwrap().saturating_mul_int(usd_bond); @@ -96,13 +107,14 @@ impl< // WARNING: Only put bids that you are sure will be done before the random end of the closing auction pub fn calculate_auction_plmc_returned_from_all_bids_made( + &mut self, // bids in the order they were made bids: &Vec>, project_metadata: ProjectMetadataOf, weighted_average_price: PriceOf, ) -> Vec> { let mut output = Vec::new(); - let charged_bids = Self::get_actual_price_charged_for_bucketed_bids(bids, project_metadata.clone(), None); + let charged_bids = self.get_actual_price_charged_for_bucketed_bids(bids, project_metadata.clone(), None); let grouped_by_price_bids = charged_bids.clone().into_iter().group_by(|&(_, price)| price); let mut grouped_by_price_bids: Vec<(PriceOf, Vec>)> = grouped_by_price_bids .into_iter() @@ -110,8 +122,9 @@ impl< .collect(); grouped_by_price_bids.reverse(); - let plmc_usd_price = - T::PriceProvider::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap(); + let plmc_usd_price = self.execute(|| { + T::PriceProvider::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap() + }); let mut remaining_cts = project_metadata.auction_round_allocation_percentage * project_metadata.total_allocation_size; @@ -148,16 +161,14 @@ impl< } pub fn calculate_auction_plmc_spent_post_wap( + &mut self, bids: &Vec>, project_metadata: ProjectMetadataOf, weighted_average_price: PriceOf, ) -> Vec> { - let plmc_charged = Self::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( - bids, - project_metadata.clone(), - None, - ); - let plmc_returned = Self::calculate_auction_plmc_returned_from_all_bids_made( + let plmc_charged = + self.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket(bids, project_metadata.clone(), None); + let plmc_returned = self.calculate_auction_plmc_returned_from_all_bids_made( bids, project_metadata.clone(), weighted_average_price, @@ -167,16 +178,18 @@ impl< } pub fn calculate_auction_funding_asset_charged_with_given_price( + &mut self, bids: &Vec>, ct_price: PriceOf, ) -> Vec> { let mut output = Vec::new(); for bid in bids { let funding_asset_id = bid.asset.to_assethub_id(); - let funding_asset_decimals = T::FundingCurrency::decimals(funding_asset_id); - let funding_asset_usd_price = + let funding_asset_decimals = self.execute(|| T::FundingCurrency::decimals(funding_asset_id)); + let funding_asset_usd_price = self.execute(|| { T::PriceProvider::get_decimals_aware_price(funding_asset_id, USD_DECIMALS, funding_asset_decimals) - .unwrap(); + .unwrap() + }); let usd_ticket_size = ct_price.saturating_mul_int(bid.amount); let funding_asset_spent = funding_asset_usd_price.reciprocal().unwrap().saturating_mul_int(usd_ticket_size); output.push(UserToForeignAssets::new(bid.bidder.clone(), funding_asset_spent, bid.asset.to_assethub_id())); @@ -186,15 +199,16 @@ impl< // Make sure you give it all the bids made for the project. It doesn't require a ct_price, since it will simulate the bucket prices itself pub fn calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + &mut self, bids: &Vec>, project_metadata: ProjectMetadataOf, maybe_bucket: Option>, ) -> Vec> { let mut output = Vec::new(); - for (bid, price) in Self::get_actual_price_charged_for_bucketed_bids(bids, project_metadata, maybe_bucket) { + for (bid, price) in self.get_actual_price_charged_for_bucketed_bids(bids, project_metadata, maybe_bucket) { let funding_asset_id = bid.asset.to_assethub_id(); - let funding_asset_decimals = T::FundingCurrency::decimals(funding_asset_id); + let funding_asset_decimals = self.execute(|| T::FundingCurrency::decimals(funding_asset_id)); let funding_asset_usd_price = T::PriceProvider::get_decimals_aware_price(funding_asset_id, USD_DECIMALS, funding_asset_decimals) .ok_or(Error::::PriceNotFound) @@ -213,13 +227,14 @@ impl< // WARNING: Only put bids that you are sure will be done before the random end of the closing auction pub fn calculate_auction_funding_asset_returned_from_all_bids_made( + &mut self, // bids in the order they were made bids: &Vec>, project_metadata: ProjectMetadataOf, weighted_average_price: PriceOf, ) -> Vec> { let mut output = Vec::new(); - let charged_bids = Self::get_actual_price_charged_for_bucketed_bids(bids, project_metadata.clone(), None); + let charged_bids = self.get_actual_price_charged_for_bucketed_bids(bids, project_metadata.clone(), None); let grouped_by_price_bids = charged_bids.clone().into_iter().group_by(|&(_, price)| price); let mut grouped_by_price_bids: Vec<(PriceOf, Vec>)> = grouped_by_price_bids .into_iter() @@ -233,11 +248,12 @@ impl< for (price_charged, bids) in grouped_by_price_bids { for bid in bids { let funding_asset_id = bid.asset.to_assethub_id(); - let funding_asset_decimals = T::FundingCurrency::decimals(funding_asset_id); - let funding_asset_usd_price = + let funding_asset_decimals = self.execute(|| T::FundingCurrency::decimals(funding_asset_id)); + let funding_asset_usd_price = self.execute(|| { T::PriceProvider::get_decimals_aware_price(funding_asset_id, USD_DECIMALS, funding_asset_decimals) .ok_or(Error::::PriceNotFound) - .unwrap(); + .unwrap() + }); let charged_usd_ticket_size = price_charged.saturating_mul_int(bid.amount); let charged_usd_bond = bid.multiplier.calculate_bonding_requirement::(charged_usd_ticket_size).unwrap(); @@ -279,16 +295,17 @@ impl< } pub fn calculate_auction_funding_asset_spent_post_wap( + &mut self, bids: &Vec>, project_metadata: ProjectMetadataOf, weighted_average_price: PriceOf, ) -> Vec> { - let funding_asset_charged = Self::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + let funding_asset_charged = self.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( bids, project_metadata.clone(), None, ); - let funding_asset_returned = Self::calculate_auction_funding_asset_returned_from_all_bids_made( + let funding_asset_returned = self.calculate_auction_funding_asset_returned_from_all_bids_made( bids, project_metadata.clone(), weighted_average_price, @@ -298,7 +315,7 @@ impl< } /// Filters the bids that would be rejected after the auction ends. - pub fn filter_bids_after_auction(bids: Vec>, total_cts: BalanceOf) -> Vec> { + pub fn filter_bids_after_auction(&self, bids: Vec>, total_cts: BalanceOf) -> Vec> { let mut filtered_bids: Vec> = Vec::new(); let sorted_bids = bids; let mut total_cts_left = total_cts; @@ -320,11 +337,13 @@ impl< } pub fn calculate_contributed_plmc_spent( + &mut self, contributions: Vec>, token_usd_price: PriceOf, ) -> Vec> { - let plmc_usd_price = - T::PriceProvider::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap(); + let plmc_usd_price = self.execute(|| { + T::PriceProvider::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap() + }); let mut output = Vec::new(); for cont in contributions { @@ -337,14 +356,15 @@ impl< } pub fn calculate_total_plmc_locked_from_evaluations_and_remainder_contributions( + &mut self, evaluations: Vec>, contributions: Vec>, price: PriceOf, slashed: bool, ) -> Vec> { - let evaluation_locked_plmc_amounts = Self::calculate_evaluation_plmc_spent(evaluations); + let evaluation_locked_plmc_amounts = self.calculate_evaluation_plmc_spent(evaluations); // how much new plmc would be locked without considering evaluation bonds - let theoretical_contribution_locked_plmc_amounts = Self::calculate_contributed_plmc_spent(contributions, price); + let theoretical_contribution_locked_plmc_amounts = self.calculate_contributed_plmc_spent(contributions, price); let slash_percentage = ::EvaluatorSlash::get(); let slashable_min_deposits = evaluation_locked_plmc_amounts @@ -354,41 +374,43 @@ impl< plmc_amount: slash_percentage * *plmc_amount, }) .collect::>(); - let available_evaluation_locked_plmc_for_lock_transfer = Self::generic_map_operation( + let available_evaluation_locked_plmc_for_lock_transfer = self.generic_map_operation( vec![evaluation_locked_plmc_amounts.clone(), slashable_min_deposits.clone()], MergeOperation::Subtract, ); // how much new plmc was actually locked, considering already evaluation bonds used // first. - let actual_contribution_locked_plmc_amounts = Self::generic_map_operation( + let actual_contribution_locked_plmc_amounts = self.generic_map_operation( vec![theoretical_contribution_locked_plmc_amounts, available_evaluation_locked_plmc_for_lock_transfer], MergeOperation::Subtract, ); - let mut result = Self::generic_map_operation( + let mut result = self.generic_map_operation( vec![evaluation_locked_plmc_amounts, actual_contribution_locked_plmc_amounts], MergeOperation::Add, ); if slashed { - result = Self::generic_map_operation(vec![result, slashable_min_deposits], MergeOperation::Subtract); + result = self.generic_map_operation(vec![result, slashable_min_deposits], MergeOperation::Subtract); } result } pub fn calculate_contributed_funding_asset_spent( + &mut self, contributions: Vec>, token_usd_price: PriceOf, ) -> Vec> { let mut output = Vec::new(); for cont in contributions { let funding_asset_id = cont.asset.to_assethub_id(); - let funding_asset_decimals = T::FundingCurrency::decimals(funding_asset_id); - let funding_asset_usd_price = + let funding_asset_decimals = self.execute(|| T::FundingCurrency::decimals(funding_asset_id)); + let funding_asset_usd_price = self.execute(|| { T::PriceProvider::get_decimals_aware_price(funding_asset_id, USD_DECIMALS, funding_asset_decimals) .ok_or(Error::::PriceNotFound) - .unwrap(); + .unwrap() + }); let usd_ticket_size = token_usd_price.saturating_mul_int(cont.amount); let funding_asset_spent = funding_asset_usd_price.reciprocal().unwrap().saturating_mul_int(usd_ticket_size); output.push(UserToForeignAssets::new(cont.contributor, funding_asset_spent, cont.asset.to_assethub_id())); @@ -397,6 +419,7 @@ impl< } pub fn generic_map_merge_reduce( + &self, mappings: Vec>, key_extractor: impl Fn(&M) -> K, initial_state: S, @@ -431,6 +454,7 @@ impl< pub fn generic_map_operation< N: AccountMerge + Extend<::Inner> + IntoIterator::Inner>, >( + &self, mut mappings: Vec, ops: MergeOperation, ) -> N { @@ -445,7 +469,7 @@ impl< output.merge_accounts(ops) } - pub fn sum_balance_mappings(mut mappings: Vec>>) -> BalanceOf { + pub fn sum_balance_mappings(&self, mut mappings: Vec>>) -> BalanceOf { let mut output = mappings .swap_remove(0) .into_iter() @@ -457,7 +481,7 @@ impl< output } - pub fn sum_foreign_mappings(mut mappings: Vec>>) -> BalanceOf { + pub fn sum_foreign_mappings(&self, mut mappings: Vec>>) -> BalanceOf { let mut output = mappings .swap_remove(0) .into_iter() @@ -470,6 +494,7 @@ impl< } pub fn generate_successful_evaluations( + &self, project_metadata: ProjectMetadataOf, evaluators: Vec>, weights: Vec, @@ -487,6 +512,7 @@ impl< } pub fn generate_bids_from_total_usd( + &self, usd_amount: BalanceOf, min_price: PriceOf, weights: Vec, @@ -506,6 +532,7 @@ impl< } pub fn generate_bids_from_total_ct_percent( + &self, project_metadata: ProjectMetadataOf, percent_funding: u8, weights: Vec, @@ -526,6 +553,7 @@ impl< } pub fn generate_contributions_from_total_usd( + &self, usd_amount: BalanceOf, final_price: PriceOf, weights: Vec, @@ -543,6 +571,7 @@ impl< } pub fn generate_contributions_from_total_ct_percent( + &self, project_metadata: ProjectMetadataOf, percent_funding: u8, weights: Vec, @@ -562,7 +591,7 @@ impl< .collect() } - pub fn slash_evaluator_balances(mut balances: Vec>) -> Vec> { + pub fn slash_evaluator_balances(&self, mut balances: Vec>) -> Vec> { let slash_percentage = ::EvaluatorSlash::get(); for UserToPLMCBalance { account: _acc, plmc_amount: balance } in balances.iter_mut() { *balance -= slash_percentage * *balance; @@ -571,6 +600,7 @@ impl< } pub fn calculate_total_reward_for_evaluation( + &self, evaluation: EvaluationInfoOf, reward_info: RewardInfoOf, ) -> BalanceOf { diff --git a/pallets/funding/src/instantiator/chain_interactions.rs b/pallets/funding/src/instantiator/chain_interactions.rs index f18717164..80849b2f4 100644 --- a/pallets/funding/src/instantiator/chain_interactions.rs +++ b/pallets/funding/src/instantiator/chain_interactions.rs @@ -299,9 +299,9 @@ impl< ) { // just in case we forgot to merge accounts: let expected_free_plmc_balances = - Self::generic_map_operation(vec![expected_free_plmc_balances], MergeOperation::Add); + self.generic_map_operation(vec![expected_free_plmc_balances], MergeOperation::Add); let expected_reserved_plmc_balances = - Self::generic_map_operation(vec![expected_reserved_plmc_balances], MergeOperation::Add); + self.generic_map_operation(vec![expected_reserved_plmc_balances], MergeOperation::Add); let project_details = self.get_project_details(project_id); @@ -369,7 +369,7 @@ impl< pub fn create_new_project(&mut self, project_metadata: ProjectMetadataOf, issuer: AccountIdOf) -> ProjectId { let now = self.current_block(); // one ED for the issuer, one ED for the escrow account - self.mint_plmc_to(vec![UserToPLMCBalance::new(issuer.clone(), Self::get_ed() * 2u64.into())]); + self.mint_plmc_to(vec![UserToPLMCBalance::new(issuer.clone(), self.get_ed() * 2u64.into())]); self.execute(|| { crate::Pallet::::do_create_project( @@ -457,13 +457,11 @@ impl< let prev_supply = self.get_plmc_total_supply(); let prev_plmc_balances = self.get_free_plmc_balances_for(evaluators.clone()); - let plmc_eval_deposits: Vec> = Self::calculate_evaluation_plmc_spent(evaluations.clone()); + let plmc_eval_deposits: Vec> = self.calculate_evaluation_plmc_spent(evaluations.clone()); let plmc_existential_deposits: Vec> = evaluators.existential_deposits(); - let expected_remaining_plmc: Vec> = Self::generic_map_operation( - vec![prev_plmc_balances, plmc_existential_deposits.clone()], - MergeOperation::Add, - ); + let expected_remaining_plmc: Vec> = self + .generic_map_operation(vec![prev_plmc_balances, plmc_existential_deposits.clone()], MergeOperation::Add); self.mint_plmc_to(plmc_eval_deposits.clone()); self.mint_plmc_to(plmc_existential_deposits.clone()); @@ -471,7 +469,7 @@ impl< self.evaluate_for_users(project_id, evaluations).unwrap(); let expected_evaluator_balances = - Self::sum_balance_mappings(vec![plmc_eval_deposits.clone(), plmc_existential_deposits.clone()]); + self.sum_balance_mappings(vec![plmc_eval_deposits.clone(), plmc_existential_deposits.clone()]); let expected_total_supply = prev_supply + expected_evaluator_balances; @@ -549,13 +547,9 @@ impl< let asset_id = bids[0].asset.to_assethub_id(); let prev_plmc_balances = self.get_free_plmc_balances_for(bidders.clone()); let prev_funding_asset_balances = self.get_free_foreign_asset_balances_for(asset_id, bidders.clone()); - let plmc_evaluation_deposits: Vec> = Self::calculate_evaluation_plmc_spent(evaluations); - let plmc_bid_deposits: Vec> = - Self::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( - &bids, - project_metadata.clone(), - None, - ); + let plmc_evaluation_deposits: Vec> = self.calculate_evaluation_plmc_spent(evaluations); + let plmc_bid_deposits: Vec> = self + .calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket(&bids, project_metadata.clone(), None); let participation_usable_evaluation_deposits = plmc_evaluation_deposits .into_iter() .map(|mut x| { @@ -563,25 +557,23 @@ impl< x }) .collect::>>(); - let necessary_plmc_mint = Self::generic_map_operation( + let necessary_plmc_mint = self.generic_map_operation( vec![plmc_bid_deposits.clone(), participation_usable_evaluation_deposits], MergeOperation::Subtract, ); let total_plmc_participation_locked = plmc_bid_deposits; let plmc_existential_deposits: Vec> = bidders.existential_deposits(); - let funding_asset_deposits = Self::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + let funding_asset_deposits = self.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( &bids, project_metadata.clone(), None, ); let bidder_balances = - Self::sum_balance_mappings(vec![necessary_plmc_mint.clone(), plmc_existential_deposits.clone()]); + self.sum_balance_mappings(vec![necessary_plmc_mint.clone(), plmc_existential_deposits.clone()]); - let expected_free_plmc_balances = Self::generic_map_operation( - vec![prev_plmc_balances, plmc_existential_deposits.clone()], - MergeOperation::Add, - ); + let expected_free_plmc_balances = self + .generic_map_operation(vec![prev_plmc_balances, plmc_existential_deposits.clone()], MergeOperation::Add); let prev_supply = self.get_plmc_total_supply(); let post_supply = prev_supply + bidder_balances; @@ -699,35 +691,41 @@ impl< pub fn settle_project(&mut self, project_id: ProjectId) -> Result<(), DispatchError> { let details = self.get_project_details(project_id); - self.execute(|| match details.status { - ProjectStatus::FundingSuccessful => Self::settle_successful_project(project_id), - ProjectStatus::FundingFailed => Self::settle_failed_project(project_id), + match details.status { + ProjectStatus::FundingSuccessful => self.settle_successful_project(project_id), + ProjectStatus::FundingFailed => self.settle_failed_project(project_id), _ => panic!("Project should be in FundingSuccessful or FundingFailed status"), - }) + } } - fn settle_successful_project(project_id: ProjectId) -> Result<(), DispatchError> { - Evaluations::::iter_prefix((project_id,)) - .try_for_each(|(_, evaluation)| Pallet::::do_settle_successful_evaluation(evaluation, project_id))?; + fn settle_successful_project(&mut self, project_id: ProjectId) -> Result<(), DispatchError> { + self.execute(|| { + Evaluations::::iter_prefix((project_id,)) + .try_for_each(|(_, evaluation)| Pallet::::do_settle_successful_evaluation(evaluation, project_id))?; - Bids::::iter_prefix((project_id,)) - .try_for_each(|(_, bid)| Pallet::::do_settle_successful_bid(bid, project_id))?; + Bids::::iter_prefix((project_id,)) + .try_for_each(|(_, bid)| Pallet::::do_settle_successful_bid(bid, project_id))?; - Contributions::::iter_prefix((project_id,)) - .try_for_each(|(_, contribution)| Pallet::::do_settle_successful_contribution(contribution, project_id)) + Contributions::::iter_prefix((project_id,)).try_for_each(|(_, contribution)| { + Pallet::::do_settle_successful_contribution(contribution, project_id) + }) + }) } - fn settle_failed_project(project_id: ProjectId) -> Result<(), DispatchError> { - Evaluations::::iter_prefix((project_id,)) - .try_for_each(|(_, evaluation)| Pallet::::do_settle_failed_evaluation(evaluation, project_id))?; + fn settle_failed_project(&mut self, project_id: ProjectId) -> Result<(), DispatchError> { + self.execute(|| { + Evaluations::::iter_prefix((project_id,)) + .try_for_each(|(_, evaluation)| Pallet::::do_settle_failed_evaluation(evaluation, project_id))?; - Bids::::iter_prefix((project_id,)) - .try_for_each(|(_, bid)| Pallet::::do_settle_failed_bid(bid, project_id))?; + Bids::::iter_prefix((project_id,)) + .try_for_each(|(_, bid)| Pallet::::do_settle_failed_bid(bid, project_id))?; - Contributions::::iter_prefix((project_id,)) - .try_for_each(|(_, contribution)| Pallet::::do_settle_failed_contribution(contribution, project_id))?; + Contributions::::iter_prefix((project_id,)).try_for_each(|(_, contribution)| { + Pallet::::do_settle_failed_contribution(contribution, project_id) + })?; - Ok(()) + Ok(()) + }) } pub fn get_evaluations(&mut self, project_id: ProjectId) -> Vec> { @@ -753,42 +751,40 @@ impl< let details = self.get_project_details(project_id); assert!(matches!(details.status, ProjectStatus::FundingSuccessful | ProjectStatus::FundingFailed)); - self.execute(|| { - for evaluation in evaluations { - let reward_info = - ProjectsDetails::::get(project_id).unwrap().evaluation_round_info.evaluators_outcome; - let account = evaluation.evaluator.clone(); - assert_eq!(Evaluations::::iter_prefix_values((&project_id, &account)).count(), 0); - - let (amount, should_exist) = match percentage { - 0..=75 => { - assert!(matches!(reward_info, EvaluatorsOutcome::Slashed)); - (0u64.into(), false) - }, - 76..=89 => { - assert!(matches!(reward_info, EvaluatorsOutcome::Unchanged)); - (0u64.into(), false) - }, - 90..=100 => { - let reward = match reward_info { - EvaluatorsOutcome::Rewarded(info) => - Pallet::::calculate_evaluator_reward(&evaluation, &info), - _ => panic!("Evaluators should be rewarded"), - }; - (reward, true) - }, - _ => panic!("Percentage should be between 0 and 100"), - }; - Self::assert_migration( - project_id, - account, - amount, - evaluation.id, - ParticipationType::Evaluation, - should_exist, - ); - } - }); + for evaluation in evaluations { + let reward_info = self + .execute(|| ProjectsDetails::::get(project_id).unwrap().evaluation_round_info.evaluators_outcome); + let account = evaluation.evaluator.clone(); + assert_eq!(self.execute(|| { Evaluations::::iter_prefix_values((&project_id, &account)).count() }), 0); + + let (amount, should_exist) = match percentage { + 0..=75 => { + assert!(matches!(reward_info, EvaluatorsOutcome::Slashed)); + (0u64.into(), false) + }, + 76..=89 => { + assert!(matches!(reward_info, EvaluatorsOutcome::Unchanged)); + (0u64.into(), false) + }, + 90..=100 => { + let reward = match reward_info { + EvaluatorsOutcome::Rewarded(info) => + Pallet::::calculate_evaluator_reward(&evaluation, &info), + _ => panic!("Evaluators should be rewarded"), + }; + (reward, true) + }, + _ => panic!("Percentage should be between 0 and 100"), + }; + self.assert_migration( + project_id, + account, + amount, + evaluation.id, + ParticipationType::Evaluation, + should_exist, + ); + } } // Testing if a list of bids are settled correctly. @@ -798,14 +794,12 @@ impl< bids: Vec>, is_successful: bool, ) { - self.execute(|| { - for bid in bids { - let account = bid.bidder.clone(); - assert_eq!(Bids::::iter_prefix_values((&project_id, &account)).count(), 0); - let amount: BalanceOf = if is_successful { bid.final_ct_amount } else { 0u64.into() }; - Self::assert_migration(project_id, account, amount, bid.id, ParticipationType::Bid, is_successful); - } - }); + for bid in bids { + let account = bid.bidder.clone(); + assert_eq!(self.execute(|| { Bids::::iter_prefix_values((&project_id, &account)).count() }), 0); + let amount: BalanceOf = if is_successful { bid.final_ct_amount } else { 0u64.into() }; + self.assert_migration(project_id, account, amount, bid.id, ParticipationType::Bid, is_successful); + } } // Testing if a list of contributions are settled correctly. @@ -815,24 +809,23 @@ impl< contributions: Vec>, is_successful: bool, ) { - self.execute(|| { - for contribution in contributions { - let account = contribution.contributor.clone(); - assert_eq!(Bids::::iter_prefix_values((&project_id, &account)).count(), 0); - let amount: BalanceOf = if is_successful { contribution.ct_amount } else { 0u64.into() }; - Self::assert_migration( - project_id, - account, - amount, - contribution.id, - ParticipationType::Contribution, - is_successful, - ); - } - }); + for contribution in contributions { + let account = contribution.contributor.clone(); + assert_eq!(self.execute(|| { Bids::::iter_prefix_values((&project_id, &account)).count() }), 0); + let amount: BalanceOf = if is_successful { contribution.ct_amount } else { 0u64.into() }; + self.assert_migration( + project_id, + account, + amount, + contribution.id, + ParticipationType::Contribution, + is_successful, + ); + } } fn assert_migration( + &mut self, project_id: ProjectId, account: AccountIdOf, amount: BalanceOf, @@ -840,7 +833,7 @@ impl< participation_type: ParticipationType, should_exist: bool, ) { - let correct = match (should_exist, UserMigrations::::get(project_id, account.clone())) { + let correct = match (should_exist, self.execute(|| UserMigrations::::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| { @@ -890,27 +883,25 @@ impl< let prev_plmc_balances = self.get_free_plmc_balances_for(contributors.clone()); let prev_funding_asset_balances = self.get_free_foreign_asset_balances_for(asset_id, contributors.clone()); - let plmc_evaluation_deposits = Self::calculate_evaluation_plmc_spent(evaluations.clone()); - let plmc_bid_deposits = Self::calculate_auction_plmc_spent_post_wap(&bids, project_metadata.clone(), ct_price); - let plmc_contribution_deposits = Self::calculate_contributed_plmc_spent(contributions.clone(), ct_price); + let plmc_evaluation_deposits = self.calculate_evaluation_plmc_spent(evaluations.clone()); + let plmc_bid_deposits = self.calculate_auction_plmc_spent_post_wap(&bids, project_metadata.clone(), ct_price); + let plmc_contribution_deposits = self.calculate_contributed_plmc_spent(contributions.clone(), ct_price); - let reducible_evaluator_balances = Self::slash_evaluator_balances(plmc_evaluation_deposits.clone()); - let necessary_plmc_mint = Self::generic_map_operation( + let reducible_evaluator_balances = self.slash_evaluator_balances(plmc_evaluation_deposits.clone()); + let necessary_plmc_mint = self.generic_map_operation( vec![plmc_contribution_deposits.clone(), reducible_evaluator_balances], MergeOperation::Subtract, ); let total_plmc_participation_locked = - Self::generic_map_operation(vec![plmc_bid_deposits, plmc_contribution_deposits], MergeOperation::Add); + self.generic_map_operation(vec![plmc_bid_deposits, plmc_contribution_deposits], MergeOperation::Add); let plmc_existential_deposits = contributors.existential_deposits(); - let funding_asset_deposits = Self::calculate_contributed_funding_asset_spent(contributions.clone(), ct_price); + let funding_asset_deposits = self.calculate_contributed_funding_asset_spent(contributions.clone(), ct_price); let contributor_balances = - Self::sum_balance_mappings(vec![necessary_plmc_mint.clone(), plmc_existential_deposits.clone()]); + self.sum_balance_mappings(vec![necessary_plmc_mint.clone(), plmc_existential_deposits.clone()]); - let expected_free_plmc_balances = Self::generic_map_operation( - vec![prev_plmc_balances, plmc_existential_deposits.clone()], - MergeOperation::Add, - ); + let expected_free_plmc_balances = self + .generic_map_operation(vec![prev_plmc_balances, plmc_existential_deposits.clone()], MergeOperation::Add); let prev_supply = self.get_plmc_total_supply(); let post_supply = prev_supply + contributor_balances; @@ -969,32 +960,30 @@ impl< let prev_plmc_balances = self.get_free_plmc_balances_for(contributors.clone()); let prev_funding_asset_balances = self.get_free_foreign_asset_balances_for(asset_id, contributors.clone()); - let plmc_evaluation_deposits = Self::calculate_evaluation_plmc_spent(evaluations); - let plmc_bid_deposits = Self::calculate_auction_plmc_spent_post_wap(&bids, project_metadata.clone(), ct_price); + let plmc_evaluation_deposits = self.calculate_evaluation_plmc_spent(evaluations); + let plmc_bid_deposits = self.calculate_auction_plmc_spent_post_wap(&bids, project_metadata.clone(), ct_price); let plmc_community_contribution_deposits = - Self::calculate_contributed_plmc_spent(community_contributions.clone(), ct_price); + self.calculate_contributed_plmc_spent(community_contributions.clone(), ct_price); let plmc_remainder_contribution_deposits = - Self::calculate_contributed_plmc_spent(remainder_contributions.clone(), ct_price); + self.calculate_contributed_plmc_spent(remainder_contributions.clone(), ct_price); - let necessary_plmc_mint = Self::generic_map_operation( + let necessary_plmc_mint = self.generic_map_operation( vec![plmc_remainder_contribution_deposits.clone(), plmc_evaluation_deposits], MergeOperation::Subtract, ); - let total_plmc_participation_locked = Self::generic_map_operation( + let total_plmc_participation_locked = self.generic_map_operation( vec![plmc_bid_deposits, plmc_community_contribution_deposits, plmc_remainder_contribution_deposits], MergeOperation::Add, ); let plmc_existential_deposits = contributors.existential_deposits(); let funding_asset_deposits = - Self::calculate_contributed_funding_asset_spent(remainder_contributions.clone(), ct_price); + self.calculate_contributed_funding_asset_spent(remainder_contributions.clone(), ct_price); let contributor_balances = - Self::sum_balance_mappings(vec![necessary_plmc_mint.clone(), plmc_existential_deposits.clone()]); + self.sum_balance_mappings(vec![necessary_plmc_mint.clone(), plmc_existential_deposits.clone()]); - let expected_free_plmc_balances = Self::generic_map_operation( - vec![prev_plmc_balances, plmc_existential_deposits.clone()], - MergeOperation::Add, - ); + let expected_free_plmc_balances = self + .generic_map_operation(vec![prev_plmc_balances, plmc_existential_deposits.clone()], MergeOperation::Add); let prev_supply = self.get_plmc_total_supply(); let post_supply = prev_supply + contributor_balances; diff --git a/pallets/funding/src/tests/1_application.rs b/pallets/funding/src/tests/1_application.rs index 66a130421..db64ff3c6 100644 --- a/pallets/funding/src/tests/1_application.rs +++ b/pallets/funding/src/tests/1_application.rs @@ -293,7 +293,7 @@ mod create_project_extrinsic { fn did_cannot_have_2_active_projects() { let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); let project_metadata = default_project_metadata(ISSUER_1); - let ed = MockInstantiator::get_ed(); + let ed = inst.get_ed(); let issuer_mint: UserToPLMCBalance = (ISSUER_1, ed * 2).into(); // Create a first project let jwt = get_mock_jwt_with_cid( @@ -334,7 +334,7 @@ mod create_project_extrinsic { fn not_enough_plmc_for_escrow_ed() { let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); let project_metadata = default_project_metadata(ISSUER_1); - let ed = MockInstantiator::get_ed(); + let ed = inst.get_ed(); inst.mint_plmc_to(vec![UserToPLMCBalance::new(ISSUER_1, ed)]); let jwt = get_mock_jwt_with_cid( ISSUER_1, @@ -745,7 +745,7 @@ mod edit_project_extrinsic { #[test] fn called_by_different_issuer() { let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); - let ed = MockInstantiator::get_ed(); + let ed = inst.get_ed(); let issuer_1_mint: UserToPLMCBalance = (ISSUER_1, ed).into(); let issuer_2_mint: UserToPLMCBalance = (ISSUER_2, ed).into(); @@ -902,7 +902,7 @@ mod remove_project_extrinsic { fn can_create_after_remove() { let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); let project_metadata = default_project_metadata(ISSUER_1); - let ed = MockInstantiator::get_ed(); + let ed = inst.get_ed(); let issuer_mint: UserToPLMCBalance = (ISSUER_1, ed * 2).into(); // Create a first project let jwt = get_mock_jwt_with_cid( diff --git a/pallets/funding/src/tests/2_evaluation.rs b/pallets/funding/src/tests/2_evaluation.rs index 57fa28d9a..8a2649675 100644 --- a/pallets/funding/src/tests/2_evaluation.rs +++ b/pallets/funding/src/tests/2_evaluation.rs @@ -45,7 +45,7 @@ mod round_flow { let target_evaluation_usd = Percent::from_percent(10) * target_funding; let evaluations = vec![(EVALUATOR_1, target_evaluation_usd).into()]; - let evaluation_plmc = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()); + let evaluation_plmc = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let evaluation_existential = evaluation_plmc.accounts().existential_deposits(); inst.mint_plmc_to(evaluation_plmc); inst.mint_plmc_to(evaluation_existential); @@ -60,7 +60,7 @@ mod round_flow { // Increasing the price before the end doesn't make a project under the threshold succeed. let evaluations = vec![(EVALUATOR_1, target_evaluation_usd / 2).into()]; - let evaluation_plmc = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()); + let evaluation_plmc = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let evaluation_existential = evaluation_plmc.accounts().existential_deposits(); inst.mint_plmc_to(evaluation_plmc); inst.mint_plmc_to(evaluation_existential); @@ -82,7 +82,7 @@ mod round_flow { fn different_decimals_ct_works_as_expected() { // Setup some base values to compare different decimals let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); - let ed = MockInstantiator::get_ed(); + let ed = inst.get_ed(); let default_project_metadata = default_project_metadata(ISSUER_1); let original_decimal_aware_price = default_project_metadata.minimum_price; let original_price = ::PriceProvider::convert_back_to_normal_price( @@ -216,11 +216,10 @@ mod round_flow { let issuer = ISSUER_1; let project_metadata = default_project_metadata(issuer); let evaluations = default_failing_evaluations(); - let plmc_eval_deposits: Vec> = - MockInstantiator::calculate_evaluation_plmc_spent(evaluations); + let plmc_eval_deposits: Vec> = inst.calculate_evaluation_plmc_spent(evaluations); let plmc_existential_deposits = plmc_eval_deposits.accounts().existential_deposits(); - let expected_evaluator_balances = MockInstantiator::generic_map_operation( + let expected_evaluator_balances = inst.generic_map_operation( vec![plmc_eval_deposits.clone(), plmc_existential_deposits.clone()], MergeOperation::Add, ); @@ -497,7 +496,7 @@ mod evaluate_extrinsic { (EVALUATOR_2, 1000 * USD_UNIT).into(), (EVALUATOR_3, 20_000 * USD_UNIT).into(), ]; - let necessary_plmc = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()); + let necessary_plmc = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let plmc_existential_deposits = necessary_plmc.accounts().existential_deposits(); inst.mint_plmc_to(necessary_plmc); @@ -548,7 +547,7 @@ mod evaluate_extrinsic { let project_id = inst.create_evaluating_project(project_metadata.clone(), issuer); let evaluation = UserToUSDBalance::new(EVALUATOR_1, 500 * USD_UNIT); - let necessary_plmc = MockInstantiator::calculate_evaluation_plmc_spent(vec![evaluation.clone()]); + let necessary_plmc = inst.calculate_evaluation_plmc_spent(vec![evaluation.clone()]); let plmc_existential_deposits = necessary_plmc.accounts().existential_deposits(); inst.mint_plmc_to(necessary_plmc.clone()); @@ -577,7 +576,7 @@ mod evaluate_extrinsic { let project_metadata = default_project_metadata(ISSUER_1); let project_id = inst.create_evaluating_project(project_metadata.clone(), ISSUER_1); let evaluation = UserToUSDBalance::new(EVALUATOR_1, 500 * USD_UNIT); - let necessary_plmc = MockInstantiator::calculate_evaluation_plmc_spent(vec![evaluation.clone()]); + let necessary_plmc = inst.calculate_evaluation_plmc_spent(vec![evaluation.clone()]); let plmc_existential_deposits = necessary_plmc.accounts().existential_deposits(); inst.mint_plmc_to(necessary_plmc.clone()); inst.mint_plmc_to(plmc_existential_deposits); @@ -652,7 +651,8 @@ mod evaluate_extrinsic { let issuer = ISSUER_1; let project_metadata = default_project_metadata(issuer); let evaluations = default_evaluations(); - let insufficient_eval_deposits = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()) + let insufficient_eval_deposits = inst + .calculate_evaluation_plmc_spent(evaluations.clone()) .iter() .map(|UserToPLMCBalance { account, plmc_amount }| UserToPLMCBalance::new(*account, plmc_amount / 2)) .collect::>>(); @@ -674,7 +674,7 @@ mod evaluate_extrinsic { let issuer = ISSUER_1; let project_metadata = default_project_metadata(issuer); let evaluations = vec![UserToUSDBalance::new(EVALUATOR_1, 1000 * USD_UNIT)]; - let evaluating_plmc = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()); + let evaluating_plmc = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let mut plmc_insufficient_existential_deposit = evaluating_plmc.accounts().existential_deposits(); plmc_insufficient_existential_deposit[0].plmc_amount = @@ -700,7 +700,7 @@ mod evaluate_extrinsic { let project_id = inst.create_evaluating_project(project_metadata.clone(), ISSUER_1); - let plmc_for_evaluating = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()); + let plmc_for_evaluating = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let plmc_existential_deposits = evaluations.accounts().existential_deposits(); inst.mint_plmc_to(plmc_for_evaluating.clone()); @@ -708,8 +708,7 @@ mod evaluate_extrinsic { inst.evaluate_for_users(project_id, evaluations.clone()).unwrap(); - let plmc_for_failing_evaluating = - MockInstantiator::calculate_evaluation_plmc_spent(vec![failing_evaluation.clone()]); + let plmc_for_failing_evaluating = inst.calculate_evaluation_plmc_spent(vec![failing_evaluation.clone()]); let plmc_existential_deposits = plmc_for_failing_evaluating.accounts().existential_deposits(); inst.mint_plmc_to(plmc_for_failing_evaluating.clone()); @@ -732,7 +731,7 @@ mod evaluate_extrinsic { let project_id = inst.create_evaluating_project(project_metadata.clone(), ISSUER_1); - let plmc_for_evaluating = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()); + let plmc_for_evaluating = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let plmc_existential_deposits = evaluations.accounts().existential_deposits(); inst.mint_plmc_to(plmc_for_evaluating.clone()); @@ -740,8 +739,7 @@ mod evaluate_extrinsic { inst.evaluate_for_users(project_id, evaluations.clone()).unwrap(); - let plmc_for_failing_evaluating = - MockInstantiator::calculate_evaluation_plmc_spent(vec![failing_evaluation.clone()]); + let plmc_for_failing_evaluating = inst.calculate_evaluation_plmc_spent(vec![failing_evaluation.clone()]); let plmc_existential_deposits = plmc_for_failing_evaluating.accounts().existential_deposits(); inst.mint_plmc_to(plmc_for_failing_evaluating.clone()); @@ -761,7 +759,7 @@ mod evaluate_extrinsic { let project_id = inst.create_evaluating_project(project_metadata.clone(), issuer); let evaluation = UserToUSDBalance::new(EVALUATOR_1, 500 * USD_UNIT); - let necessary_plmc = MockInstantiator::calculate_evaluation_plmc_spent(vec![evaluation.clone()]); + let necessary_plmc = inst.calculate_evaluation_plmc_spent(vec![evaluation.clone()]); let plmc_existential_deposits = necessary_plmc.accounts().existential_deposits(); inst.mint_plmc_to(necessary_plmc.clone()); @@ -820,7 +818,7 @@ mod evaluate_extrinsic { let project_id = inst.create_evaluating_project(project_metadata.clone(), issuer); let evaluation = UserToUSDBalance::new(EVALUATOR_1, 500 * USD_UNIT); - let necessary_plmc = MockInstantiator::calculate_evaluation_plmc_spent(vec![evaluation.clone()]); + let necessary_plmc = inst.calculate_evaluation_plmc_spent(vec![evaluation.clone()]); let plmc_existential_deposits = necessary_plmc.accounts().existential_deposits(); inst.mint_plmc_to(necessary_plmc.clone()); diff --git a/pallets/funding/src/tests/3_auction.rs b/pallets/funding/src/tests/3_auction.rs index 1b02edea0..6135cacce 100644 --- a/pallets/funding/src/tests/3_auction.rs +++ b/pallets/funding/src/tests/3_auction.rs @@ -123,7 +123,7 @@ mod round_flow { let issuer = ISSUER_1; let project_metadata = default_project_metadata(issuer); let evaluations = default_evaluations(); - let mut bids: Vec> = MockInstantiator::generate_bids_from_total_usd( + let mut bids: Vec> = inst.generate_bids_from_total_usd( project_metadata.minimum_price.saturating_mul_int( project_metadata.auction_round_allocation_percentage * project_metadata.total_allocation_size, ), @@ -210,17 +210,16 @@ mod round_flow { let project_id = inst.create_auctioning_project(project_metadata.clone(), issuer, evaluations); - let plmc_fundings = MockInstantiator::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + let plmc_fundings = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + &bids, + project_metadata.clone(), + None, + ); + let usdt_fundings = inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( &bids, project_metadata.clone(), None, ); - let usdt_fundings = - MockInstantiator::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &bids, - project_metadata.clone(), - None, - ); let plmc_existential_amounts = plmc_fundings.accounts().existential_deposits(); @@ -231,8 +230,8 @@ mod round_flow { inst.bid_for_users(project_id, bids.clone()).unwrap(); inst.do_free_plmc_assertions(vec![ - UserToPLMCBalance::new(BIDDER_1, MockInstantiator::get_ed()), - UserToPLMCBalance::new(BIDDER_2, MockInstantiator::get_ed()), + UserToPLMCBalance::new(BIDDER_1, inst.get_ed()), + UserToPLMCBalance::new(BIDDER_2, inst.get_ed()), ]); inst.do_reserved_plmc_assertions(plmc_fundings.clone(), HoldReason::Participation(project_id).into()); inst.do_bid_transferred_foreign_asset_assertions(usdt_fundings.clone(), project_id); @@ -240,31 +239,21 @@ mod round_flow { inst.start_community_funding(project_id).unwrap(); let wap = inst.get_project_details(project_id).weighted_average_price.unwrap(); - let returned_auction_plmc = MockInstantiator::calculate_auction_plmc_returned_from_all_bids_made( - &bids, - project_metadata.clone(), - wap, - ); - let returned_funding_assets = MockInstantiator::calculate_auction_funding_asset_returned_from_all_bids_made( - &bids, - project_metadata, - wap, - ); + let returned_auction_plmc = + inst.calculate_auction_plmc_returned_from_all_bids_made(&bids, project_metadata.clone(), wap); + let returned_funding_assets = + inst.calculate_auction_funding_asset_returned_from_all_bids_made(&bids, project_metadata, wap); - let expected_free_plmc = MockInstantiator::generic_map_operation( + let expected_free_plmc = inst.generic_map_operation( vec![returned_auction_plmc.clone(), plmc_existential_amounts], MergeOperation::Add, ); let expected_free_funding_assets = - MockInstantiator::generic_map_operation(vec![returned_funding_assets.clone()], MergeOperation::Add); - let expected_reserved_plmc = MockInstantiator::generic_map_operation( - vec![plmc_fundings.clone(), returned_auction_plmc], - MergeOperation::Subtract, - ); - let expected_held_funding_assets = MockInstantiator::generic_map_operation( - vec![usdt_fundings.clone(), returned_funding_assets], - MergeOperation::Subtract, - ); + inst.generic_map_operation(vec![returned_funding_assets.clone()], MergeOperation::Add); + let expected_reserved_plmc = inst + .generic_map_operation(vec![plmc_fundings.clone(), returned_auction_plmc], MergeOperation::Subtract); + let expected_held_funding_assets = inst + .generic_map_operation(vec![usdt_fundings.clone(), returned_funding_assets], MergeOperation::Subtract); inst.do_free_plmc_assertions(expected_free_plmc); @@ -301,18 +290,17 @@ mod round_flow { let all_included_bids = vec![full_ct_bid_rejected.clone(), full_ct_bid_partially_accepted.clone(), oversubscription_bid]; - let necessary_plmc = MockInstantiator::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + let necessary_plmc = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( &all_bids, project_metadata.clone(), None, ); let plmc_existential_amounts = necessary_plmc.accounts().existential_deposits(); - let necessary_usdt = - MockInstantiator::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &all_bids, - project_metadata.clone(), - None, - ); + let necessary_usdt = inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + &all_bids, + project_metadata.clone(), + None, + ); inst.mint_plmc_to(necessary_plmc.clone()); inst.mint_plmc_to(plmc_existential_amounts.clone()); @@ -327,22 +315,22 @@ mod round_flow { inst.bid_for_users(project_id, vec![after_random_end_bid]).unwrap(); inst.do_free_plmc_assertions(vec![ - UserToPLMCBalance::new(BIDDER_1, MockInstantiator::get_ed()), - UserToPLMCBalance::new(BIDDER_2, MockInstantiator::get_ed()), - UserToPLMCBalance::new(BIDDER_3, MockInstantiator::get_ed()), - UserToPLMCBalance::new(BIDDER_4, MockInstantiator::get_ed()), + UserToPLMCBalance::new(BIDDER_1, inst.get_ed()), + UserToPLMCBalance::new(BIDDER_2, inst.get_ed()), + UserToPLMCBalance::new(BIDDER_3, inst.get_ed()), + UserToPLMCBalance::new(BIDDER_4, inst.get_ed()), ]); inst.do_reserved_plmc_assertions(necessary_plmc.clone(), HoldReason::Participation(project_id).into()); inst.do_bid_transferred_foreign_asset_assertions(necessary_usdt.clone(), project_id); inst.start_community_funding(project_id).unwrap(); let wap = inst.get_project_details(project_id).weighted_average_price.unwrap(); - let plmc_returned = MockInstantiator::calculate_auction_plmc_returned_from_all_bids_made( + let plmc_returned = inst.calculate_auction_plmc_returned_from_all_bids_made( &all_included_bids, project_metadata.clone(), wap, ); - let usdt_returned = MockInstantiator::calculate_auction_funding_asset_returned_from_all_bids_made( + let usdt_returned = inst.calculate_auction_funding_asset_returned_from_all_bids_made( &all_included_bids, project_metadata.clone(), wap, @@ -351,17 +339,17 @@ mod round_flow { let rejected_bid_necessary_plmc = &necessary_plmc[3]; let rejected_bid_necessary_usdt = &necessary_usdt[3]; - let expected_free = MockInstantiator::generic_map_operation( + let expected_free = inst.generic_map_operation( vec![plmc_returned.clone(), plmc_existential_amounts, vec![rejected_bid_necessary_plmc.clone()]], MergeOperation::Add, ); inst.do_free_plmc_assertions(expected_free); - let expected_reserved = MockInstantiator::generic_map_operation( + let expected_reserved = inst.generic_map_operation( vec![necessary_plmc.clone(), plmc_returned.clone(), vec![rejected_bid_necessary_plmc.clone()]], MergeOperation::Subtract, ); inst.do_reserved_plmc_assertions(expected_reserved, HoldReason::Participation(project_id).into()); - let expected_reserved = MockInstantiator::generic_map_operation( + let expected_reserved = inst.generic_map_operation( vec![necessary_usdt.clone(), usdt_returned.clone(), vec![rejected_bid_necessary_usdt.clone()]], MergeOperation::Subtract, ); @@ -400,18 +388,17 @@ mod round_flow { let bids = default_bids(); let project_id = inst.create_auctioning_project(project_metadata.clone(), issuer, evaluations); - let necessary_plmc = MockInstantiator::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + let necessary_plmc = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( &bids, project_metadata.clone(), None, ); let plmc_existential_amounts = necessary_plmc.accounts().existential_deposits(); - let necessary_usdt = - MockInstantiator::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &bids, - project_metadata.clone(), - None, - ); + let necessary_usdt = inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + &bids, + project_metadata.clone(), + None, + ); inst.mint_plmc_to(necessary_plmc.clone()); inst.mint_plmc_to(plmc_existential_amounts.clone()); @@ -546,7 +533,7 @@ mod start_auction_extrinsic { let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); let project_id = inst.create_evaluating_project(default_project_metadata(ISSUER_1), ISSUER_1); let evaluations = default_evaluations(); - let required_plmc = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()); + let required_plmc = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let ed_plmc = required_plmc.accounts().existential_deposits(); inst.mint_plmc_to(required_plmc); @@ -563,7 +550,7 @@ mod start_auction_extrinsic { let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); let project_id = inst.create_evaluating_project(default_project_metadata(ISSUER_1), ISSUER_1); let evaluations = default_evaluations(); - let required_plmc = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()); + let required_plmc = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let ed_plmc = required_plmc.accounts().existential_deposits(); inst.mint_plmc_to(required_plmc); inst.mint_plmc_to(ed_plmc); @@ -580,7 +567,7 @@ mod start_auction_extrinsic { let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); let project_id = inst.create_evaluating_project(default_project_metadata(ISSUER_1), ISSUER_1); let evaluations = default_evaluations(); - let required_plmc = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()); + let required_plmc = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let ed_plmc = required_plmc.accounts().existential_deposits(); inst.mint_plmc_to(required_plmc); inst.mint_plmc_to(ed_plmc); @@ -634,7 +621,7 @@ mod start_auction_extrinsic { let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); let project_id = inst.create_evaluating_project(default_project_metadata(ISSUER_1), ISSUER_1); let evaluations = default_evaluations(); - let required_plmc = MockInstantiator::calculate_evaluation_plmc_spent(evaluations.clone()); + let required_plmc = inst.calculate_evaluation_plmc_spent(evaluations.clone()); let ed_plmc = required_plmc.accounts().existential_deposits(); inst.mint_plmc_to(required_plmc); inst.mint_plmc_to(ed_plmc); @@ -676,20 +663,18 @@ mod bid_extrinsic { let project_id = inst.create_auctioning_project(project_metadata.clone(), issuer, evaluations); let already_bonded_plmc = - MockInstantiator::calculate_evaluation_plmc_spent(vec![(evaluator_bidder, evaluation_amount).into()]) - [0] - .plmc_amount; + inst.calculate_evaluation_plmc_spent(vec![(evaluator_bidder, evaluation_amount).into()])[0].plmc_amount; let usable_evaluation_plmc = already_bonded_plmc - ::EvaluatorSlash::get() * already_bonded_plmc; - let necessary_plmc_for_bid = MockInstantiator::calculate_auction_plmc_charged_with_given_price( + let necessary_plmc_for_bid = inst.calculate_auction_plmc_charged_with_given_price( &vec![evaluator_bid.clone()], project_metadata.minimum_price, )[0] .plmc_amount; - let necessary_usdt_for_bid = MockInstantiator::calculate_auction_funding_asset_charged_with_given_price( + let necessary_usdt_for_bid = inst.calculate_auction_funding_asset_charged_with_given_price( &vec![evaluator_bid.clone()], project_metadata.minimum_price, ); @@ -792,21 +777,19 @@ mod bid_extrinsic { let usdc_bid = BidParams::new(BIDDER_1, 10_000 * CT_UNIT, 1u8, AcceptedFundingAsset::USDC); let dot_bid = BidParams::new(BIDDER_1, 10_000 * CT_UNIT, 1u8, AcceptedFundingAsset::DOT); - let plmc_fundings = MockInstantiator::calculate_auction_plmc_charged_with_given_price( + let plmc_fundings = inst.calculate_auction_plmc_charged_with_given_price( &vec![usdt_bid.clone(), usdc_bid.clone(), dot_bid.clone()], project_metadata_all.minimum_price, ); let plmc_existential_deposits = plmc_fundings.accounts().existential_deposits(); - let plmc_all_mints = MockInstantiator::generic_map_operation( - vec![plmc_fundings, plmc_existential_deposits], - MergeOperation::Add, - ); + let plmc_all_mints = + inst.generic_map_operation(vec![plmc_fundings, plmc_existential_deposits], MergeOperation::Add); inst.mint_plmc_to(plmc_all_mints.clone()); inst.mint_plmc_to(plmc_all_mints.clone()); inst.mint_plmc_to(plmc_all_mints.clone()); - let usdt_fundings = MockInstantiator::calculate_auction_funding_asset_charged_with_given_price( + let usdt_fundings = inst.calculate_auction_funding_asset_charged_with_given_price( &vec![usdt_bid.clone(), usdc_bid.clone(), dot_bid.clone()], project_metadata_all.minimum_price, ); @@ -871,16 +854,12 @@ mod bid_extrinsic { multiplier, asset: AcceptedFundingAsset::USDT, }; - - let necessary_plmc = MockInstantiator::calculate_auction_plmc_charged_with_given_price( - &vec![bid.clone()], - inst.get_project_metadata(project_id).minimum_price, - ); + let min_price = inst.get_project_metadata(project_id).minimum_price; + let necessary_plmc = + inst.calculate_auction_plmc_charged_with_given_price(&vec![bid.clone()], min_price); let plmc_existential_amounts = necessary_plmc.accounts().existential_deposits(); - let necessary_usdt = MockInstantiator::calculate_auction_funding_asset_charged_with_given_price( - &vec![bid.clone()], - inst.get_project_metadata(project_id).minimum_price, - ); + let necessary_usdt = + inst.calculate_auction_funding_asset_charged_with_given_price(&vec![bid.clone()], min_price); inst.mint_plmc_to(necessary_plmc.clone()); inst.mint_plmc_to(plmc_existential_amounts.clone()); @@ -902,11 +881,8 @@ mod bid_extrinsic { fn multiplier_limits() { let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); let project_metadata = default_project_metadata(ISSUER_1); - let evaluations = MockInstantiator::generate_successful_evaluations( - project_metadata.clone(), - default_evaluators(), - default_weights(), - ); + let evaluations = + inst.generate_successful_evaluations(project_metadata.clone(), default_evaluators(), default_weights()); let project_id = inst.create_auctioning_project(project_metadata.clone(), ISSUER_1, evaluations); // Professional bids: 0x multiplier should fail assert_err!( @@ -955,7 +931,7 @@ mod bid_extrinsic { let project_id = inst.create_auctioning_project(project_metadata.clone(), ISSUER_1, evaluations); // bid that fills 80% of the first bucket - let bid_40_percent = MockInstantiator::generate_bids_from_total_ct_percent( + let bid_40_percent = inst.generate_bids_from_total_ct_percent( project_metadata.clone(), 40u8, vec![100], @@ -967,7 +943,7 @@ mod bid_extrinsic { // This bid fills last 20% of the first bucket, // and gets split into 3 more bids of 2 more full and one partially full buckets. // 10% + 5% + 5% + 3% = 23% - let bid_23_percent = MockInstantiator::generate_bids_from_total_ct_percent( + let bid_23_percent = inst.generate_bids_from_total_ct_percent( project_metadata.clone(), 23u8, vec![100], @@ -977,18 +953,17 @@ mod bid_extrinsic { let all_bids = vec![bid_40_percent[0].clone(), bid_23_percent[0].clone()]; - let necessary_plmc = MockInstantiator::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + let necessary_plmc = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( &all_bids, project_metadata.clone(), None, ); let ed_plmc = necessary_plmc.accounts().existential_deposits(); - let necessary_usdt = - MockInstantiator::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &all_bids, - project_metadata.clone(), - None, - ); + let necessary_usdt = inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + &all_bids, + project_metadata.clone(), + None, + ); inst.mint_plmc_to(necessary_plmc.clone()); inst.mint_plmc_to(ed_plmc.clone()); inst.mint_foreign_asset_to(necessary_usdt.clone()); @@ -1049,7 +1024,7 @@ mod bid_extrinsic { let project_id = inst.create_auctioning_project(project_metadata.clone(), issuer, evaluations); - let necessary_usdt_for_bid = MockInstantiator::calculate_auction_funding_asset_charged_with_given_price( + let necessary_usdt_for_bid = inst.calculate_auction_funding_asset_charged_with_given_price( &vec![evaluator_bid.clone()], project_metadata.minimum_price, ); @@ -1081,17 +1056,15 @@ mod bid_extrinsic { // Necessary Mints let already_bonded_plmc = - MockInstantiator::calculate_evaluation_plmc_spent(vec![(evaluator_bidder, evaluation_amount).into()]) - [0] - .plmc_amount; + inst.calculate_evaluation_plmc_spent(vec![(evaluator_bidder, evaluation_amount).into()])[0].plmc_amount; let usable_evaluation_plmc = already_bonded_plmc - ::EvaluatorSlash::get() * already_bonded_plmc; - let necessary_plmc_for_bid = MockInstantiator::calculate_auction_plmc_charged_with_given_price( + let necessary_plmc_for_bid = inst.calculate_auction_plmc_charged_with_given_price( &vec![evaluator_bid.clone()], project_metadata_2.minimum_price, )[0] .plmc_amount; - let necessary_usdt_for_bid = MockInstantiator::calculate_auction_funding_asset_charged_with_given_price( + let necessary_usdt_for_bid = inst.calculate_auction_funding_asset_charged_with_given_price( &vec![evaluator_bid.clone()], project_metadata_2.minimum_price, ); @@ -1153,23 +1126,18 @@ mod bid_extrinsic { project_metadata.mainnet_token_max_supply = 1_000_000_000 * CT_UNIT; project_metadata.total_allocation_size = 100_000_000 * CT_UNIT; - let evaluations = MockInstantiator::generate_successful_evaluations( - project_metadata.clone(), - vec![EVALUATOR_1], - vec![100u8], - ); + let evaluations = + inst.generate_successful_evaluations(project_metadata.clone(), vec![EVALUATOR_1], vec![100u8]); let max_bids_per_project: u32 = ::MaxBidsPerProject::get(); let bids = (0u32..max_bids_per_project - 1).map(|i| (i as u32 + 420u32, 5000 * CT_UNIT).into()).collect_vec(); let project_id = inst.create_auctioning_project(project_metadata.clone(), ISSUER_1, evaluations); - let plmc_for_bidding = MockInstantiator::calculate_auction_plmc_charged_with_given_price( - &bids.clone(), - project_metadata.minimum_price, - ); + let plmc_for_bidding = + inst.calculate_auction_plmc_charged_with_given_price(&bids.clone(), project_metadata.minimum_price); let plmc_existential_deposits = bids.accounts().existential_deposits(); - let usdt_for_bidding = MockInstantiator::calculate_auction_funding_asset_charged_with_given_price( + let usdt_for_bidding = inst.calculate_auction_funding_asset_charged_with_given_price( &bids.clone(), project_metadata.minimum_price, ); @@ -1185,20 +1153,18 @@ mod bid_extrinsic { // This bid should be split in 2, but the second one should fail, making the whole extrinsic fail and roll back storage let failing_bid = BidParams::::new(BIDDER_1, remaining_ct + 5000 * CT_UNIT, 1u8, AcceptedFundingAsset::USDT); - let plmc_for_failing_bid = - MockInstantiator::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( - &vec![failing_bid.clone()], - project_metadata.clone(), - Some(current_bucket), - ); + let plmc_for_failing_bid = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + &vec![failing_bid.clone()], + project_metadata.clone(), + Some(current_bucket), + ); let plmc_existential_deposits = plmc_for_failing_bid.accounts().existential_deposits(); - let usdt_for_bidding = - MockInstantiator::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &vec![failing_bid.clone()], - project_metadata.clone(), - Some(current_bucket), - ); + let usdt_for_bidding = inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + &vec![failing_bid.clone()], + project_metadata.clone(), + Some(current_bucket), + ); inst.mint_plmc_to(plmc_for_failing_bid.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); @@ -1266,22 +1232,17 @@ mod bid_extrinsic { project_metadata.mainnet_token_max_supply = 1_000_000_000 * CT_UNIT; project_metadata.total_allocation_size = 100_000_000 * CT_UNIT; - let evaluations = MockInstantiator::generate_successful_evaluations( - project_metadata.clone(), - vec![EVALUATOR_1], - vec![100u8], - ); + let evaluations = + inst.generate_successful_evaluations(project_metadata.clone(), vec![EVALUATOR_1], vec![100u8]); let max_bids_per_user: u32 = ::MaxBidsPerUser::get(); let bids = (0u32..max_bids_per_user - 1u32).map(|_| (BIDDER_1, 5000 * CT_UNIT).into()).collect_vec(); let project_id = inst.create_auctioning_project(project_metadata.clone(), ISSUER_1, evaluations); - let plmc_for_bidding = MockInstantiator::calculate_auction_plmc_charged_with_given_price( - &bids.clone(), - project_metadata.minimum_price, - ); + let plmc_for_bidding = + inst.calculate_auction_plmc_charged_with_given_price(&bids.clone(), project_metadata.minimum_price); let plmc_existential_deposits = bids.accounts().existential_deposits(); - let usdt_for_bidding = MockInstantiator::calculate_auction_funding_asset_charged_with_given_price( + let usdt_for_bidding = inst.calculate_auction_funding_asset_charged_with_given_price( &bids.clone(), project_metadata.minimum_price, ); @@ -1297,19 +1258,17 @@ mod bid_extrinsic { // This bid should be split in 2, but the second one should fail, making the whole extrinsic fail and roll back storage let failing_bid = BidParams::::new(BIDDER_1, remaining_ct + 5000 * CT_UNIT, 1u8, AcceptedFundingAsset::USDT); - let plmc_for_failing_bid = - MockInstantiator::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( - &vec![failing_bid.clone()], - project_metadata.clone(), - Some(current_bucket), - ); + let plmc_for_failing_bid = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + &vec![failing_bid.clone()], + project_metadata.clone(), + Some(current_bucket), + ); let plmc_existential_deposits = plmc_for_failing_bid.accounts().existential_deposits(); - let usdt_for_bidding = - MockInstantiator::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &vec![failing_bid.clone()], - project_metadata.clone(), - Some(current_bucket), - ); + let usdt_for_bidding = inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + &vec![failing_bid.clone()], + project_metadata.clone(), + Some(current_bucket), + ); inst.mint_plmc_to(plmc_for_failing_bid.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); inst.mint_foreign_asset_to(usdt_for_bidding.clone()); diff --git a/pallets/funding/src/tests/4_community.rs b/pallets/funding/src/tests/4_community.rs index 9a0edf719..cde9f0286 100644 --- a/pallets/funding/src/tests/4_community.rs +++ b/pallets/funding/src/tests/4_community.rs @@ -74,10 +74,10 @@ mod round_flow { let ct_price = inst.get_project_details(project_id).weighted_average_price.expect("CT Price should exist"); let contributions = vec![ContributionParams::new(BOB, remaining_ct, 1u8, AcceptedFundingAsset::USDT)]; - let plmc_fundings = MockInstantiator::calculate_contributed_plmc_spent(contributions.clone(), ct_price); + let plmc_fundings = inst.calculate_contributed_plmc_spent(contributions.clone(), ct_price); let plmc_existential_deposits = plmc_fundings.accounts().existential_deposits(); let foreign_asset_fundings = - MockInstantiator::calculate_contributed_funding_asset_spent(contributions.clone(), ct_price); + inst.calculate_contributed_funding_asset_spent(contributions.clone(), ct_price); inst.mint_plmc_to(plmc_fundings.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); @@ -133,7 +133,7 @@ mod round_flow { let contributions = vec![(BUYER_1, project_details.remaining_contribution_tokens).into()]; - let plmc_contribution_funding = MockInstantiator::calculate_contributed_plmc_spent( + let plmc_contribution_funding = inst.calculate_contributed_plmc_spent( contributions.clone(), project_details.weighted_average_price.unwrap(), ); @@ -141,7 +141,7 @@ mod round_flow { inst.mint_plmc_to(plmc_contribution_funding.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); - let foreign_asset_contribution_funding = MockInstantiator::calculate_contributed_funding_asset_spent( + let foreign_asset_contribution_funding = inst.calculate_contributed_funding_asset_spent( contributions.clone(), project_details.weighted_average_price.unwrap(), ); @@ -218,10 +218,8 @@ mod community_contribute_extrinsic { }); // Can partially use the usable evaluation bond (half in this case) - let contribution_usdt = MockInstantiator::calculate_contributed_funding_asset_spent( - vec![(BOB, usable_ct / 2).into()], - ct_price, - ); + let contribution_usdt = + inst.calculate_contributed_funding_asset_spent(vec![(BOB, usable_ct / 2).into()], ct_price); inst.mint_foreign_asset_to(contribution_usdt.clone()); inst.execute(|| { assert_ok!(Pallet::::community_contribute( @@ -241,7 +239,7 @@ mod community_contribute_extrinsic { // Can use the full evaluation bond let contribution_usdt = - MockInstantiator::calculate_contributed_funding_asset_spent(vec![(CARL, usable_ct).into()], ct_price); + inst.calculate_contributed_funding_asset_spent(vec![(CARL, usable_ct).into()], ct_price); inst.mint_foreign_asset_to(contribution_usdt.clone()); inst.execute(|| { assert_ok!(Pallet::::community_contribute( @@ -281,7 +279,7 @@ mod community_contribute_extrinsic { let slashable_bond = ::EvaluatorSlash::get() * evaluation_bond; let usable_bond = evaluation_bond - slashable_bond; - let bids_plmc = MockInstantiator::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + let bids_plmc = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( &all_bids, project_metadata.clone(), None, @@ -290,12 +288,11 @@ mod community_contribute_extrinsic { inst.mint_plmc_to(bids_plmc.clone()); inst.mint_plmc_to(bids_existential_deposits.clone()); - let bids_foreign = - MockInstantiator::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &all_bids, - project_metadata.clone(), - None, - ); + let bids_foreign = inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + &all_bids, + project_metadata.clone(), + None, + ); inst.mint_foreign_asset_to(bids_foreign.clone()); inst.bid_for_users(project_id, bids).unwrap(); @@ -316,8 +313,7 @@ mod community_contribute_extrinsic { let usable_ct = wap.reciprocal().unwrap().saturating_mul_int(usable_usd); let bob_contribution = (bob, 1337 * CT_UNIT).into(); - let contribution_usdt = - MockInstantiator::calculate_contributed_funding_asset_spent(vec![bob_contribution], wap); + let contribution_usdt = inst.calculate_contributed_funding_asset_spent(vec![bob_contribution], wap); inst.mint_foreign_asset_to(contribution_usdt.clone()); inst.execute(|| { assert_ok!(Pallet::::community_contribute( @@ -357,19 +353,17 @@ mod community_contribute_extrinsic { let wap = inst.get_project_details(project_id).weighted_average_price.unwrap(); - let plmc_fundings = MockInstantiator::calculate_contributed_plmc_spent( + let plmc_fundings = inst.calculate_contributed_plmc_spent( vec![usdt_contribution.clone(), usdc_contribution.clone(), dot_contribution.clone()], wap, ); let plmc_existential_deposits = plmc_fundings.accounts().existential_deposits(); - let plmc_all_mints = MockInstantiator::generic_map_operation( - vec![plmc_fundings, plmc_existential_deposits], - MergeOperation::Add, - ); + let plmc_all_mints = + inst.generic_map_operation(vec![plmc_fundings, plmc_existential_deposits], MergeOperation::Add); inst.mint_plmc_to(plmc_all_mints.clone()); - let asset_hub_fundings = MockInstantiator::calculate_contributed_funding_asset_spent( + let asset_hub_fundings = inst.calculate_contributed_funding_asset_spent( vec![usdt_contribution.clone(), usdc_contribution.clone(), dot_contribution.clone()], wap, ); @@ -407,11 +401,9 @@ mod community_contribute_extrinsic { asset: AcceptedFundingAsset::USDT, }; - let necessary_plmc = - MockInstantiator::calculate_contributed_plmc_spent(vec![contribution.clone()], wap); + let necessary_plmc = inst.calculate_contributed_plmc_spent(vec![contribution.clone()], wap); let plmc_existential_amounts = necessary_plmc.accounts().existential_deposits(); - let necessary_usdt = - MockInstantiator::calculate_contributed_funding_asset_spent(vec![contribution.clone()], wap); + let necessary_usdt = inst.calculate_contributed_funding_asset_spent(vec![contribution.clone()], wap); inst.mint_plmc_to(necessary_plmc.clone()); inst.mint_plmc_to(plmc_existential_amounts.clone()); @@ -446,12 +438,9 @@ mod community_contribute_extrinsic { institutional: TicketSize::new(None, None), phantom: Default::default(), }; - let evaluations = MockInstantiator::generate_successful_evaluations( - project_metadata.clone(), - default_evaluators(), - default_weights(), - ); - let bids = MockInstantiator::generate_bids_from_total_ct_percent( + let evaluations = + inst.generate_successful_evaluations(project_metadata.clone(), default_evaluators(), default_weights()); + let bids = inst.generate_bids_from_total_ct_percent( project_metadata.clone(), 50, default_weights(), @@ -588,7 +577,7 @@ mod community_contribute_extrinsic { let project_id = inst.create_auctioning_project(project_metadata.clone(), ISSUER_1, default_evaluations()); - let plmc_fundings = MockInstantiator::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + let plmc_fundings = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( &all_bids.clone(), project_metadata.clone(), None, @@ -597,12 +586,11 @@ mod community_contribute_extrinsic { inst.mint_plmc_to(plmc_fundings.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); - let foreign_funding = - MockInstantiator::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &all_bids.clone(), - project_metadata.clone(), - None, - ); + let foreign_funding = inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + &all_bids.clone(), + project_metadata.clone(), + None, + ); inst.mint_foreign_asset_to(foreign_funding.clone()); inst.bid_for_users(project_id, failing_bids_sold_out).unwrap(); @@ -724,11 +712,10 @@ mod community_contribute_extrinsic { .map(|_| ContributionParams::new(CONTRIBUTOR, token_amount, 1u8, AcceptedFundingAsset::USDT)) .collect(); - let plmc_funding = MockInstantiator::calculate_contributed_plmc_spent(contributions.clone(), token_price); + let plmc_funding = inst.calculate_contributed_plmc_spent(contributions.clone(), token_price); let plmc_existential_deposits = plmc_funding.accounts().existential_deposits(); - let foreign_funding = - MockInstantiator::calculate_contributed_funding_asset_spent(contributions.clone(), token_price); + let foreign_funding = inst.calculate_contributed_funding_asset_spent(contributions.clone(), token_price); inst.mint_plmc_to(plmc_funding.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); @@ -753,7 +740,7 @@ mod community_contribute_extrinsic { ) }); - assert_eq!(contributor_post_buy_plmc_balance, MockInstantiator::get_ed()); + assert_eq!(contributor_post_buy_plmc_balance, inst.get_ed()); assert_eq!(contributor_post_buy_foreign_asset_balance, 0); let plmc_bond_stored = inst.execute(|| { @@ -768,11 +755,8 @@ mod community_contribute_extrinsic { .sum::>() }); - assert_eq!(plmc_bond_stored, MockInstantiator::sum_balance_mappings(vec![plmc_funding.clone()])); - assert_eq!( - foreign_asset_contributions_stored, - MockInstantiator::sum_foreign_mappings(vec![foreign_funding.clone()]) - ); + assert_eq!(plmc_bond_stored, inst.sum_balance_mappings(vec![plmc_funding.clone()])); + assert_eq!(foreign_asset_contributions_stored, inst.sum_foreign_mappings(vec![foreign_funding.clone()])); } #[test] @@ -1162,14 +1146,13 @@ mod community_contribute_extrinsic { let wap = inst.get_project_details(project_id).weighted_average_price.unwrap(); // 1 unit less native asset than needed - let plmc_funding = MockInstantiator::calculate_contributed_plmc_spent(vec![contribution.clone()], wap); + let plmc_funding = inst.calculate_contributed_plmc_spent(vec![contribution.clone()], wap); let plmc_existential_deposits = plmc_funding.accounts().existential_deposits(); inst.mint_plmc_to(plmc_funding.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); inst.execute(|| Balances::burn_from(&BUYER_1, 1, Precision::BestEffort, Fortitude::Force)).unwrap(); - let foreign_funding = - MockInstantiator::calculate_contributed_funding_asset_spent(vec![contribution.clone()], wap); + let foreign_funding = inst.calculate_contributed_funding_asset_spent(vec![contribution.clone()], wap); inst.mint_foreign_asset_to(foreign_funding.clone()); inst.execute(|| { assert_noop!( @@ -1186,12 +1169,11 @@ mod community_contribute_extrinsic { }); // 1 unit less funding asset than needed - let plmc_funding = MockInstantiator::calculate_contributed_plmc_spent(vec![contribution.clone()], wap); + let plmc_funding = inst.calculate_contributed_plmc_spent(vec![contribution.clone()], wap); let plmc_existential_deposits = plmc_funding.accounts().existential_deposits(); inst.mint_plmc_to(plmc_funding.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); - let foreign_funding = - MockInstantiator::calculate_contributed_funding_asset_spent(vec![contribution.clone()], wap); + let foreign_funding = inst.calculate_contributed_funding_asset_spent(vec![contribution.clone()], wap); inst.execute(|| ForeignAssets::set_balance(AcceptedFundingAsset::USDT.to_assethub_id(), &BUYER_1, 0)); inst.mint_foreign_asset_to(foreign_funding.clone()); @@ -1373,17 +1355,14 @@ mod community_contribute_extrinsic { // Necessary Mints let already_bonded_plmc = - MockInstantiator::calculate_evaluation_plmc_spent(vec![ - (evaluator_contributor, evaluation_amount).into() - ])[0] + inst.calculate_evaluation_plmc_spent(vec![(evaluator_contributor, evaluation_amount).into()])[0] .plmc_amount; let usable_evaluation_plmc = already_bonded_plmc - ::EvaluatorSlash::get() * already_bonded_plmc; let necessary_plmc_for_contribution = - MockInstantiator::calculate_contributed_plmc_spent(vec![evaluator_contribution.clone()], wap)[0] - .plmc_amount; + inst.calculate_contributed_plmc_spent(vec![evaluator_contribution.clone()], wap)[0].plmc_amount; let necessary_usdt_for_contribution = - MockInstantiator::calculate_contributed_funding_asset_spent(vec![evaluator_contribution.clone()], wap); + inst.calculate_contributed_funding_asset_spent(vec![evaluator_contribution.clone()], wap); inst.mint_plmc_to(vec![UserToPLMCBalance::new( evaluator_contributor, necessary_plmc_for_contribution - usable_evaluation_plmc, diff --git a/pallets/funding/src/tests/5_remainder.rs b/pallets/funding/src/tests/5_remainder.rs index 20ba61c03..75ec2192d 100644 --- a/pallets/funding/src/tests/5_remainder.rs +++ b/pallets/funding/src/tests/5_remainder.rs @@ -38,10 +38,10 @@ mod round_flow { let ct_price = inst.get_project_details(project_id).weighted_average_price.expect("CT Price should exist"); let contributions = vec![ContributionParams::new(BOB, remaining_ct, 1u8, AcceptedFundingAsset::USDT)]; - let plmc_fundings = MockInstantiator::calculate_contributed_plmc_spent(contributions.clone(), ct_price); + let plmc_fundings = inst.calculate_contributed_plmc_spent(contributions.clone(), ct_price); let plmc_existential_deposits = contributions.accounts().existential_deposits(); let foreign_asset_fundings = - MockInstantiator::calculate_contributed_funding_asset_spent(contributions.clone(), ct_price); + inst.calculate_contributed_funding_asset_spent(contributions.clone(), ct_price); inst.mint_plmc_to(plmc_fundings.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); @@ -101,7 +101,7 @@ mod round_flow { let contributions = vec![(BUYER_1, project_details.remaining_contribution_tokens).into()]; - let plmc_contribution_funding = MockInstantiator::calculate_contributed_plmc_spent( + let plmc_contribution_funding = inst.calculate_contributed_plmc_spent( contributions.clone(), project_details.weighted_average_price.unwrap(), ); @@ -109,7 +109,7 @@ mod round_flow { inst.mint_plmc_to(plmc_contribution_funding.clone()); inst.mint_plmc_to(plmc_existential_deposits.clone()); - let foreign_asset_contribution_funding = MockInstantiator::calculate_contributed_funding_asset_spent( + let foreign_asset_contribution_funding = inst.calculate_contributed_funding_asset_spent( contributions.clone(), project_details.weighted_average_price.unwrap(), ); @@ -152,20 +152,15 @@ mod remaining_contribute_extrinsic { community_contributions, ); let ct_price = inst.get_project_details(project_id).weighted_average_price.unwrap(); - let already_bonded_plmc = MockInstantiator::calculate_evaluation_plmc_spent(vec![UserToUSDBalance::new( - evaluator_contributor, - evaluation_amount, - )])[0] + let already_bonded_plmc = inst + .calculate_evaluation_plmc_spent(vec![UserToUSDBalance::new(evaluator_contributor, evaluation_amount)])[0] .plmc_amount; let plmc_available_for_contribution = already_bonded_plmc - ::EvaluatorSlash::get() * already_bonded_plmc; let necessary_plmc_for_buy = - MockInstantiator::calculate_contributed_plmc_spent(vec![remainder_contribution.clone()], ct_price)[0] - .plmc_amount; - let necessary_usdt_for_buy = MockInstantiator::calculate_contributed_funding_asset_spent( - vec![remainder_contribution.clone()], - ct_price, - ); + inst.calculate_contributed_plmc_spent(vec![remainder_contribution.clone()], ct_price)[0].plmc_amount; + let necessary_usdt_for_buy = + inst.calculate_contributed_funding_asset_spent(vec![remainder_contribution.clone()], ct_price); inst.mint_plmc_to(vec![UserToPLMCBalance::new( evaluator_contributor, @@ -271,21 +266,19 @@ mod remaining_contribute_extrinsic { let wap = inst.get_project_details(project_id_all).weighted_average_price.unwrap(); - let plmc_fundings = MockInstantiator::calculate_contributed_plmc_spent( + let plmc_fundings = inst.calculate_contributed_plmc_spent( vec![usdt_contribution.clone(), usdc_contribution.clone(), dot_contribution.clone()], wap, ); let plmc_existential_deposits = plmc_fundings.accounts().existential_deposits(); - let plmc_all_mints = MockInstantiator::generic_map_operation( - vec![plmc_fundings, plmc_existential_deposits], - MergeOperation::Add, - ); + let plmc_all_mints = + inst.generic_map_operation(vec![plmc_fundings, plmc_existential_deposits], MergeOperation::Add); inst.mint_plmc_to(plmc_all_mints.clone()); inst.mint_plmc_to(plmc_all_mints.clone()); inst.mint_plmc_to(plmc_all_mints.clone()); - let usdt_fundings = MockInstantiator::calculate_contributed_funding_asset_spent( + let usdt_fundings = inst.calculate_contributed_funding_asset_spent( vec![usdt_contribution.clone(), usdc_contribution.clone(), dot_contribution.clone()], wap, ); @@ -353,12 +346,9 @@ mod remaining_contribute_extrinsic { funding_destination_account: ISSUER_1, policy_ipfs_cid: Some(ipfs_hash()), }; - let evaluations = MockInstantiator::generate_successful_evaluations( - project_metadata.clone(), - default_evaluators(), - default_weights(), - ); - let bids = MockInstantiator::generate_bids_from_total_ct_percent( + let evaluations = + inst.generate_successful_evaluations(project_metadata.clone(), default_evaluators(), default_weights()); + let bids = inst.generate_bids_from_total_ct_percent( project_metadata.clone(), 50, default_weights(), @@ -402,15 +392,15 @@ mod remaining_contribute_extrinsic { generate_did_from_account(BUYER_1), project_metadata.clone().policy_ipfs_cid.unwrap(), ); - let bidder_plmc = MockInstantiator::calculate_contributed_plmc_spent( + let bidder_plmc = inst.calculate_contributed_plmc_spent( vec![(BUYER_1, 1_000 * CT_UNIT, Multiplier::force_new(multiplier)).into()], wap, ); - let bidder_usdt = MockInstantiator::calculate_contributed_funding_asset_spent( + let bidder_usdt = inst.calculate_contributed_funding_asset_spent( vec![(BUYER_1, 1_000 * CT_UNIT, Multiplier::force_new(multiplier)).into()], wap, ); - let ed = MockInstantiator::get_ed(); + let ed = inst.get_ed(); inst.mint_plmc_to(vec![(BUYER_1, ed).into()]); inst.mint_plmc_to(bidder_plmc); inst.mint_foreign_asset_to(bidder_usdt); @@ -431,15 +421,15 @@ mod remaining_contribute_extrinsic { generate_did_from_account(BUYER_1), project_metadata.clone().policy_ipfs_cid.unwrap(), ); - let bidder_plmc = MockInstantiator::calculate_contributed_plmc_spent( + let bidder_plmc = inst.calculate_contributed_plmc_spent( vec![(BUYER_1, 1_000 * CT_UNIT, Multiplier::force_new(multiplier)).into()], wap, ); - let bidder_usdt = MockInstantiator::calculate_contributed_funding_asset_spent( + let bidder_usdt = inst.calculate_contributed_funding_asset_spent( vec![(BUYER_1, 1_000 * CT_UNIT, Multiplier::force_new(multiplier)).into()], wap, ); - let ed = MockInstantiator::get_ed(); + let ed = inst.get_ed(); inst.mint_plmc_to(vec![(BUYER_1, ed).into()]); inst.mint_plmc_to(bidder_plmc); inst.mint_foreign_asset_to(bidder_usdt); @@ -486,15 +476,15 @@ mod remaining_contribute_extrinsic { generate_did_from_account(BUYER_2), project_metadata.clone().policy_ipfs_cid.unwrap(), ); - let bidder_plmc = MockInstantiator::calculate_contributed_plmc_spent( + let bidder_plmc = inst.calculate_contributed_plmc_spent( vec![(BUYER_2, 1_000 * CT_UNIT, Multiplier::force_new(multiplier)).into()], wap, ); - let bidder_usdt = MockInstantiator::calculate_contributed_funding_asset_spent( + let bidder_usdt = inst.calculate_contributed_funding_asset_spent( vec![(BUYER_2, 1_000 * CT_UNIT, Multiplier::force_new(multiplier)).into()], wap, ); - let ed = MockInstantiator::get_ed(); + let ed = inst.get_ed(); inst.mint_plmc_to(vec![(BUYER_2, ed).into()]); inst.mint_plmc_to(bidder_plmc); inst.mint_foreign_asset_to(bidder_usdt); @@ -515,15 +505,15 @@ mod remaining_contribute_extrinsic { generate_did_from_account(BUYER_2), project_metadata.clone().policy_ipfs_cid.unwrap(), ); - let bidder_plmc = MockInstantiator::calculate_contributed_plmc_spent( + let bidder_plmc = inst.calculate_contributed_plmc_spent( vec![(BUYER_2, 1_000 * CT_UNIT, Multiplier::force_new(multiplier)).into()], wap, ); - let bidder_usdt = MockInstantiator::calculate_contributed_funding_asset_spent( + let bidder_usdt = inst.calculate_contributed_funding_asset_spent( vec![(BUYER_2, 1_000 * CT_UNIT, Multiplier::force_new(multiplier)).into()], wap, ); - let ed = MockInstantiator::get_ed(); + let ed = inst.get_ed(); inst.mint_plmc_to(vec![(BUYER_2, ed).into()]); inst.mint_plmc_to(bidder_plmc); inst.mint_foreign_asset_to(bidder_usdt); @@ -569,15 +559,15 @@ mod remaining_contribute_extrinsic { project_policy, ); let wap = inst.get_project_details(project_id).weighted_average_price.unwrap(); - let contributor_plmc = MockInstantiator::calculate_contributed_plmc_spent( + let contributor_plmc = inst.calculate_contributed_plmc_spent( vec![(BUYER_1, 1_000 * CT_UNIT, Multiplier::force_new(multiplier)).into()], wap, ); - let bidder_usdt = MockInstantiator::calculate_contributed_funding_asset_spent( + let bidder_usdt = inst.calculate_contributed_funding_asset_spent( vec![(BUYER_1, 1_000 * CT_UNIT, Multiplier::force_new(multiplier)).into()], wap, ); - let ed = MockInstantiator::get_ed(); + let ed = inst.get_ed(); inst.mint_plmc_to(vec![(BUYER_1, ed).into()]); inst.mint_plmc_to(contributor_plmc); inst.mint_foreign_asset_to(bidder_usdt); @@ -1009,17 +999,14 @@ mod remaining_contribute_extrinsic { // Necessary Mints let already_bonded_plmc = - MockInstantiator::calculate_evaluation_plmc_spent(vec![ - (evaluator_contributor, evaluation_amount).into() - ])[0] + inst.calculate_evaluation_plmc_spent(vec![(evaluator_contributor, evaluation_amount).into()])[0] .plmc_amount; let usable_evaluation_plmc = already_bonded_plmc - ::EvaluatorSlash::get() * already_bonded_plmc; let necessary_plmc_for_contribution = - MockInstantiator::calculate_contributed_plmc_spent(vec![evaluator_contribution.clone()], wap)[0] - .plmc_amount; + inst.calculate_contributed_plmc_spent(vec![evaluator_contribution.clone()], wap)[0].plmc_amount; let necessary_usdt_for_contribution = - MockInstantiator::calculate_contributed_funding_asset_spent(vec![evaluator_contribution.clone()], wap); + inst.calculate_contributed_funding_asset_spent(vec![evaluator_contribution.clone()], wap); inst.mint_plmc_to(vec![UserToPLMCBalance::new( evaluator_contributor, necessary_plmc_for_contribution - usable_evaluation_plmc, diff --git a/pallets/funding/src/tests/6_funding_end.rs b/pallets/funding/src/tests/6_funding_end.rs index 20f5fa632..d7a294d89 100644 --- a/pallets/funding/src/tests/6_funding_end.rs +++ b/pallets/funding/src/tests/6_funding_end.rs @@ -36,14 +36,14 @@ fn automatic_acceptance_on_manual_decision_after_time_delta() { let twenty_percent_funding_usd = Perquintill::from_percent(55) * (project_metadata.minimum_price.checked_mul_int(project_metadata.total_allocation_size).unwrap()); let evaluations = default_evaluations(); - let bids = MockInstantiator::generate_bids_from_total_usd( + let bids = inst.generate_bids_from_total_usd( Percent::from_percent(50u8) * twenty_percent_funding_usd, min_price, default_weights(), default_bidders(), default_multipliers(), ); - let contributions = MockInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( Percent::from_percent(50u8) * twenty_percent_funding_usd, min_price, default_weights(), diff --git a/pallets/funding/src/tests/7_settlement.rs b/pallets/funding/src/tests/7_settlement.rs index bbd99a85f..9e7b42f33 100644 --- a/pallets/funding/src/tests/7_settlement.rs +++ b/pallets/funding/src/tests/7_settlement.rs @@ -366,7 +366,7 @@ fn unsuccessful_bids_dont_get_vest_schedule() { let auction_token_allocation = project_metadata.auction_round_allocation_percentage * project_metadata.total_allocation_size; - let mut bids = MockInstantiator::generate_bids_from_total_usd( + let mut bids = inst.generate_bids_from_total_usd( Percent::from_percent(80) * project_metadata.minimum_price.saturating_mul_int(auction_token_allocation), project_metadata.minimum_price, vec![60, 40], @@ -385,21 +385,17 @@ fn unsuccessful_bids_dont_get_vest_schedule() { let project_id = inst.create_auctioning_project(project_metadata.clone(), issuer, evaluations); - let bidders_plmc = MockInstantiator::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( - &bids, - project_metadata.clone(), - None, - ); + let bidders_plmc = + inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket(&bids, project_metadata.clone(), None); let bidders_existential_deposits = bidders_plmc.accounts().existential_deposits(); inst.mint_plmc_to(bidders_plmc.clone()); inst.mint_plmc_to(bidders_existential_deposits); - let bidders_funding_assets = - MockInstantiator::calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( - &bids, - project_metadata.clone(), - None, - ); + let bidders_funding_assets = inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( + &bids, + project_metadata.clone(), + None, + ); inst.mint_foreign_asset_to(bidders_funding_assets); inst.bid_for_users(project_id, bids).unwrap(); @@ -407,14 +403,13 @@ fn unsuccessful_bids_dont_get_vest_schedule() { inst.start_community_funding(project_id).unwrap(); let final_price = inst.get_project_details(project_id).weighted_average_price.unwrap(); - let contributors_plmc = - MockInstantiator::calculate_contributed_plmc_spent(community_contributions.clone(), final_price); + let contributors_plmc = inst.calculate_contributed_plmc_spent(community_contributions.clone(), final_price); let contributors_existential_deposits = contributors_plmc.accounts().existential_deposits(); inst.mint_plmc_to(contributors_plmc.clone()); inst.mint_plmc_to(contributors_existential_deposits); let contributors_funding_assets = - MockInstantiator::calculate_contributed_funding_asset_spent(community_contributions.clone(), final_price); + inst.calculate_contributed_funding_asset_spent(community_contributions.clone(), final_price); inst.mint_foreign_asset_to(contributors_funding_assets); inst.contribute_for_users(project_id, community_contributions).unwrap(); @@ -424,10 +419,8 @@ fn unsuccessful_bids_dont_get_vest_schedule() { inst.advance_time(::SuccessToSettlementTime::get()).unwrap(); inst.settle_project(project_id).unwrap(); - let plmc_locked_for_accepted_bid = - MockInstantiator::calculate_auction_plmc_charged_with_given_price(&accepted_bid, final_price); - let plmc_locked_for_rejected_bid = - MockInstantiator::calculate_auction_plmc_charged_with_given_price(&rejected_bid, final_price); + let plmc_locked_for_accepted_bid = inst.calculate_auction_plmc_charged_with_given_price(&accepted_bid, final_price); + let plmc_locked_for_rejected_bid = inst.calculate_auction_plmc_charged_with_given_price(&rejected_bid, final_price); let UserToPLMCBalance { account: accepted_user, plmc_amount: accepted_plmc_amount } = plmc_locked_for_accepted_bid[0]; diff --git a/pallets/funding/src/tests/misc.rs b/pallets/funding/src/tests/misc.rs index 69e025bae..965db1c17 100644 --- a/pallets/funding/src/tests/misc.rs +++ b/pallets/funding/src/tests/misc.rs @@ -65,6 +65,7 @@ mod helper_functions { #[test] fn calculate_evaluation_plmc_spent() { + let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); const EVALUATOR_1: AccountIdOf = 1u32; const USD_AMOUNT_1: u128 = 150_000_0_000_000_000_u128; const EXPECTED_PLMC_AMOUNT_1: u128 = 17_857_1_428_571_428_u128; @@ -108,7 +109,7 @@ mod helper_functions { UserToPLMCBalance::new(EVALUATOR_5, EXPECTED_PLMC_AMOUNT_5), ]; - let result = MockInstantiator::calculate_evaluation_plmc_spent(evaluations); + let result = inst.calculate_evaluation_plmc_spent(evaluations); assert_eq!(result, expected_plmc_spent); } @@ -184,7 +185,7 @@ mod helper_functions { ]; let mut returned_plmc_mappings = - MockInstantiator::calculate_auction_plmc_returned_from_all_bids_made(&bids, project_metadata.clone(), wap); + inst.calculate_auction_plmc_returned_from_all_bids_made(&bids, project_metadata.clone(), wap); returned_plmc_mappings.sort_by(|b1, b2| b1.account.cmp(&b2.account)); let returned_plmc_balances = returned_plmc_mappings.into_iter().map(|map| map.plmc_amount).collect_vec(); @@ -196,6 +197,7 @@ mod helper_functions { #[test] fn calculate_contributed_plmc_spent() { + let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); const PLMC_PRICE: f64 = 8.4f64; const CT_PRICE: f64 = 16.32f64; @@ -250,10 +252,7 @@ mod helper_functions { UserToPLMCBalance::new(CONTRIBUTOR_5, EXPECTED_PLMC_AMOUNT_5), ]; - let result = MockInstantiator::calculate_contributed_plmc_spent( - contributions, - PriceOf::::from_float(CT_PRICE), - ); + let result = inst.calculate_contributed_plmc_spent(contributions, PriceOf::::from_float(CT_PRICE)); assert_eq!(result, expected_plmc_spent); } } @@ -383,6 +382,8 @@ mod async_tests { #[test] fn genesis_parallel_instantiaton() { let mut t = frame_system::GenesisConfig::::default().build_storage().unwrap(); + // only used to generate some values, and not for chain interactions + let inst = MockInstantiator::new(None); // only used to generate some values, and not for chain interactions let funding_percent = 93u64; @@ -391,21 +392,21 @@ mod async_tests { let twenty_percent_funding_usd = Perquintill::from_percent(funding_percent) * (project_metadata.minimum_price.checked_mul_int(project_metadata.total_allocation_size).unwrap()); let evaluations = default_evaluations(); - let bids = MockInstantiator::generate_bids_from_total_usd( + let bids = inst.generate_bids_from_total_usd( Percent::from_percent(50u8) * twenty_percent_funding_usd, min_price, default_weights(), default_bidders(), default_bidder_multipliers(), ); - let community_contributions = MockInstantiator::generate_contributions_from_total_usd( + let community_contributions = inst.generate_contributions_from_total_usd( Percent::from_percent(30u8) * twenty_percent_funding_usd, min_price, default_weights(), default_community_contributors(), default_community_contributor_multipliers(), ); - let remainder_contributions = MockInstantiator::generate_contributions_from_total_usd( + let remainder_contributions = inst.generate_contributions_from_total_usd( Percent::from_percent(20u8) * twenty_percent_funding_usd, min_price, default_weights(), @@ -597,7 +598,7 @@ mod bug_hunting { let max_insertion_attempts: u32 = ::MaxProjectsToUpdateInsertionAttempts::get(); let project_id = inst.create_evaluating_project(default_project_metadata(ISSUER_1), ISSUER_1); - let plmc_balances = MockInstantiator::calculate_evaluation_plmc_spent(default_evaluations()); + let plmc_balances = inst.calculate_evaluation_plmc_spent(default_evaluations()); let ed = plmc_balances.accounts().existential_deposits(); inst.mint_plmc_to(plmc_balances); inst.mint_plmc_to(ed); diff --git a/pallets/funding/src/tests/mod.rs b/pallets/funding/src/tests/mod.rs index dfa8e646c..d1e89d744 100644 --- a/pallets/funding/src/tests/mod.rs +++ b/pallets/funding/src/tests/mod.rs @@ -310,14 +310,14 @@ pub mod defaults { let usd_to_reach = Perquintill::from_percent(percent) * (project_metadata.minimum_price.checked_mul_int(project_metadata.total_allocation_size).unwrap()); let evaluations = default_evaluations(); - let bids = MockInstantiator::generate_bids_from_total_usd( + let bids = instantiator.generate_bids_from_total_usd( Percent::from_percent(50u8) * usd_to_reach, min_price, default_weights(), default_bidders(), default_multipliers(), ); - let contributions = MockInstantiator::generate_contributions_from_total_usd( + let contributions = instantiator.generate_contributions_from_total_usd( Percent::from_percent(50u8) * usd_to_reach, min_price, default_weights(), @@ -328,8 +328,10 @@ pub mod defaults { } pub fn default_bids_from_ct_percent(percent: u8) -> Vec> { + // Used only to generate values, not for chain interactions + let inst = MockInstantiator::new(None); let project_metadata = default_project_metadata(ISSUER_1); - MockInstantiator::generate_bids_from_total_ct_percent( + inst.generate_bids_from_total_ct_percent( project_metadata, percent, default_weights(), @@ -339,8 +341,10 @@ pub mod defaults { } pub fn default_community_contributions_from_ct_percent(percent: u8) -> Vec> { + // Used only to generate values, not for chain interactions + let inst = MockInstantiator::new(None); let project_metadata = default_project_metadata(ISSUER_1); - MockInstantiator::generate_contributions_from_total_ct_percent( + inst.generate_contributions_from_total_ct_percent( project_metadata, percent, default_weights(), @@ -350,8 +354,10 @@ pub mod defaults { } pub fn default_remainder_contributions_from_ct_percent(percent: u8) -> Vec> { + // Used only to generate values, not for chain interactions + let inst = MockInstantiator::new(None); let project_metadata = default_project_metadata(ISSUER_1); - MockInstantiator::generate_contributions_from_total_ct_percent( + inst.generate_contributions_from_total_ct_percent( project_metadata, percent, default_weights(), @@ -371,14 +377,14 @@ pub fn create_project_with_funding_percentage( let percentage_funded_usd = Perquintill::from_percent(percentage) * (project_metadata.minimum_price.checked_mul_int(project_metadata.total_allocation_size).unwrap()); let evaluations = default_evaluations(); - let bids = MockInstantiator::generate_bids_from_total_usd( + let bids = inst.generate_bids_from_total_usd( Percent::from_percent(50u8) * percentage_funded_usd, min_price, default_weights(), default_bidders(), default_multipliers(), ); - let contributions = MockInstantiator::generate_contributions_from_total_usd( + let contributions = inst.generate_contributions_from_total_usd( Percent::from_percent(50u8) * percentage_funded_usd, min_price, default_weights(),