From 3981b5a4b05fa75d552791291ac85ef4a5d9e169 Mon Sep 17 00:00:00 2001 From: Juan Ignacio Rios Date: Fri, 26 Apr 2024 17:15:48 +0200 Subject: [PATCH] part 6 --- pallets/funding/src/benchmarking.rs | 110 +++++++++--------- .../funding/src/instantiator/calculations.rs | 5 +- pallets/funding/src/lib.rs | 1 + pallets/funding/src/tests/2_evaluation.rs | 1 - pallets/funding/src/tests/3_auction.rs | 92 +++++++++++---- 5 files changed, 128 insertions(+), 81 deletions(-) diff --git a/pallets/funding/src/benchmarking.rs b/pallets/funding/src/benchmarking.rs index 77932cb98..43826e859 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(), @@ -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); @@ -687,9 +691,9 @@ mod benchmarks { let existing_evaluations = vec![existing_evaluation; x as usize]; let plmc_for_existing_evaluations = - BenchInstantiator::::calculate_evaluation_plmc_spent(existing_evaluations.clone()); + inst.calculate_evaluation_plmc_spent(existing_evaluations.clone()); let plmc_for_extrinsic_evaluation = - BenchInstantiator::::calculate_evaluation_plmc_spent(vec![extrinsic_evaluation.clone()]); + inst.calculate_evaluation_plmc_spent(vec![extrinsic_evaluation.clone()]); let existential_plmc: Vec> = plmc_for_extrinsic_evaluation.accounts().existential_deposits(); @@ -703,7 +707,7 @@ 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![ + let total_expected_plmc_bonded = inst.sum_balance_mappings(vec![ plmc_for_existing_evaluations.clone(), plmc_for_extrinsic_evaluation.clone(), ]); @@ -801,7 +805,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,13 +816,13 @@ 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 = - BenchInstantiator::::calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( + inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket( &existing_bids, project_metadata.clone(), None, @@ -827,7 +831,7 @@ mod benchmarks { 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( + inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( &existing_bids, project_metadata.clone(), None, @@ -860,12 +864,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,7 +890,7 @@ 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), @@ -894,13 +898,13 @@ mod benchmarks { 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( + 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( + inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( &vec![extrinsic_bid], project_metadata.clone(), Some(current_bucket), @@ -909,12 +913,12 @@ 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![ + 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,12 +1158,12 @@ 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( + 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, ); @@ -1179,11 +1183,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 +1428,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 +1436,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 +1561,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 +1587,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 +1610,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 +1687,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 +1696,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 +1803,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 +1879,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 +1940,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); @@ -2093,14 +2097,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( + 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( + inst.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket( &all_bids, project_metadata.clone(), None, @@ -2231,14 +2235,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 +2292,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 +2350,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 +2422,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 +2483,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 +2561,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 +2569,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/instantiator/calculations.rs b/pallets/funding/src/instantiator/calculations.rs index 28d0fe256..a08efd679 100644 --- a/pallets/funding/src/instantiator/calculations.rs +++ b/pallets/funding/src/instantiator/calculations.rs @@ -209,10 +209,11 @@ impl< 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 = self.execute(|| T::FundingCurrency::decimals(funding_asset_id)); - let funding_asset_usd_price = + 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 = 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( diff --git a/pallets/funding/src/lib.rs b/pallets/funding/src/lib.rs index 50974f558..e3b10e0b7 100644 --- a/pallets/funding/src/lib.rs +++ b/pallets/funding/src/lib.rs @@ -163,6 +163,7 @@ pub type MultiplierOf = ::Multiplier; pub type BalanceOf = ::Balance; pub type PriceOf = ::Price; +pub type PriceProviderOf = ::PriceProvider; pub type StringLimitOf = ::StringLimit; pub type AssetIdOf = <::FundingCurrency as fungibles::Inspect<::AccountId>>::AssetId; diff --git a/pallets/funding/src/tests/2_evaluation.rs b/pallets/funding/src/tests/2_evaluation.rs index 0d29af4e0..f8cd8b8d2 100644 --- a/pallets/funding/src/tests/2_evaluation.rs +++ b/pallets/funding/src/tests/2_evaluation.rs @@ -7,7 +7,6 @@ mod round_flow { #[cfg(test)] mod success { use super::*; - use futures::StreamExt; use std::collections::HashSet; #[test] diff --git a/pallets/funding/src/tests/3_auction.rs b/pallets/funding/src/tests/3_auction.rs index 6135cacce..dd1092898 100644 --- a/pallets/funding/src/tests/3_auction.rs +++ b/pallets/funding/src/tests/3_auction.rs @@ -52,6 +52,12 @@ mod round_flow { let bounded_name = bounded_name(); let bounded_symbol = bounded_symbol(); let metadata_hash = ipfs_hash(); + let normalized_price = PriceOf::::from_float(10.0); + let decimal_aware_price = PriceProviderOf::::calculate_decimals_aware_price( + normalized_price, + USD_DECIMALS, + CT_DECIMALS, + ).unwrap(); let project_metadata = ProjectMetadata { token_information: CurrencyMetadata { name: bounded_name, @@ -61,7 +67,7 @@ mod round_flow { mainnet_token_max_supply: 8_000_000 * CT_UNIT, total_allocation_size: 100_000 * CT_UNIT, auction_round_allocation_percentage: Percent::from_percent(50u8), - minimum_price: PriceOf::::from_float(10.0), + minimum_price: decimal_aware_price, bidding_ticket_sizes: BiddingTicketSizes { professional: TicketSize::new(Some(5000 * USD_UNIT), None), institutional: TicketSize::new(Some(5000 * USD_UNIT), None), @@ -110,11 +116,15 @@ mod round_flow { inst.start_community_funding(project_id).unwrap(); let token_price = - inst.get_project_details(project_id).weighted_average_price.unwrap().saturating_mul_int(CT_UNIT); - - let desired_price = PriceOf::::from_float(11.1818f64).saturating_mul_int(CT_UNIT); - - assert_close_enough!(token_price, desired_price, Perquintill::from_float(0.99)); + inst.get_project_details(project_id).weighted_average_price.unwrap(); + let normalized_wap = PriceProviderOf::::convert_back_to_normal_price( + token_price, + USD_DECIMALS, + CT_DECIMALS, + ).unwrap(); + let desired_price = PriceOf::::from_float(11.1818f64); + + assert_close_enough!(normalized_wap.saturating_mul_int(CT_UNIT), desired_price.saturating_mul_int(CT_UNIT), Perquintill::from_float(0.99)); } #[test] @@ -136,11 +146,16 @@ mod round_flow { let second_bucket_bid = (BIDDER_6, 500 * CT_UNIT).into(); bids.push(second_bucket_bid); - let project_id = inst.create_community_contributing_project(project_metadata, issuer, evaluations, bids); + let project_id = inst.create_community_contributing_project(project_metadata.clone(), issuer, evaluations, bids); let bidder_5_bid = inst.execute(|| Bids::::iter_prefix_values((project_id, BIDDER_6)).next().unwrap()); let wabgp = inst.get_project_details(project_id).weighted_average_price.unwrap(); - assert_eq!(bidder_5_bid.original_ct_usd_price.to_float(), 11.0); + let price_normalized = ::PriceProvider::convert_back_to_normal_price( + bidder_5_bid.original_ct_usd_price, + USD_DECIMALS, + project_metadata.token_information.decimals + ).unwrap(); + assert_eq!(price_normalized.to_float(), 11.0); assert_eq!(bidder_5_bid.final_ct_usd_price, wabgp); } @@ -459,7 +474,7 @@ mod round_flow { .iter() .map(|acc| UserToForeignAssets { account: acc.clone(), - asset_amount: CT_UNIT * 1_000_000, + asset_amount: USDT_UNIT * 1_000_000, asset_id: fundings.next().unwrap().to_assethub_id(), }) .collect_vec(); @@ -482,11 +497,16 @@ mod round_flow { inst.start_community_funding(project_id).unwrap(); let token_price = - inst.get_project_details(project_id).weighted_average_price.unwrap().saturating_mul_int(CT_UNIT); + inst.get_project_details(project_id).weighted_average_price.unwrap(); + let normalized_wap = PriceProviderOf::::convert_back_to_normal_price( + token_price, + USD_DECIMALS, + CT_DECIMALS, + ).unwrap(); - let desired_price = PriceOf::::from_float(11.1818f64).saturating_mul_int(CT_UNIT); + let desired_price = PriceOf::::from_float(11.1818f64); - assert_close_enough!(token_price, desired_price, Perquintill::from_float(0.99)); + assert_close_enough!(normalized_wap.saturating_mul_int(USD_UNIT), desired_price.saturating_mul_int(USD_UNIT), Perquintill::from_float(0.99)); } } @@ -924,7 +944,12 @@ mod bid_extrinsic { let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext()))); let mut project_metadata = default_project_metadata(ISSUER_1); - project_metadata.minimum_price = PriceOf::::from_float(1.0); + project_metadata.minimum_price = PriceProviderOf::::calculate_decimals_aware_price( + PriceOf::::from_float(1.0), + USD_DECIMALS, + project_metadata.clone().token_information.decimals, + + ).unwrap(); project_metadata.auction_round_allocation_percentage = Percent::from_percent(50u8); let evaluations = default_evaluations(); @@ -978,32 +1003,40 @@ mod bid_extrinsic { // 40% + 10% + 5% + 5% + 3% = 5 total bids assert_eq!(stored_bids.len(), 5); - assert_eq!(stored_bids[1].original_ct_usd_price, PriceOf::::from_float(1.0)); + let normalize_price = |decimal_aware_price| { + PriceProviderOf::::convert_back_to_normal_price( + decimal_aware_price, + USD_DECIMALS, + project_metadata.clone().token_information.decimals, + ) + .unwrap() + }; + assert_eq!(normalize_price(stored_bids[1].original_ct_usd_price), PriceOf::::from_float(1.0)); assert_eq!( stored_bids[1].original_ct_amount, Percent::from_percent(10) * project_metadata.total_allocation_size ); - assert_eq!(stored_bids[2].original_ct_usd_price, PriceOf::::from_rational(11, 10)); + assert_eq!(normalize_price(stored_bids[2].original_ct_usd_price), PriceOf::::from_rational(11, 10)); assert_eq!( stored_bids[2].original_ct_amount, Percent::from_percent(5) * project_metadata.total_allocation_size ); - assert_eq!(stored_bids[3].original_ct_usd_price, PriceOf::::from_float(1.2)); + assert_eq!(normalize_price(stored_bids[3].original_ct_usd_price), PriceOf::::from_float(1.2)); assert_eq!( stored_bids[3].original_ct_amount, Percent::from_percent(5) * project_metadata.total_allocation_size ); - assert_eq!(stored_bids[4].original_ct_usd_price, PriceOf::::from_float(1.3)); + assert_eq!(normalize_price(stored_bids[4].original_ct_usd_price), PriceOf::::from_float(1.3)); assert_eq!( stored_bids[4].original_ct_amount, Percent::from_percent(3) * project_metadata.total_allocation_size ); let current_bucket = inst.execute(|| Buckets::::get(project_id)).unwrap(); - assert_eq!(current_bucket.current_price, PriceOf::::from_float(1.3)); + assert_eq!(normalize_price(current_bucket.current_price), PriceOf::::from_float(1.3)); assert_eq!(current_bucket.amount_left, Percent::from_percent(2) * project_metadata.total_allocation_size); - assert_eq!(current_bucket.delta_price, PriceOf::::from_float(0.1)); + assert_eq!(normalize_price(current_bucket.delta_price), PriceOf::::from_float(0.1)); } } @@ -1394,7 +1427,11 @@ mod bid_extrinsic { institutional: TicketSize::new(Some(20_000 * USD_UNIT), None), phantom: Default::default(), }; - project_metadata.minimum_price = PriceOf::::from_float(1.0); + project_metadata.minimum_price = PriceProviderOf::::calculate_decimals_aware_price( + PriceOf::::from_float(1.0), + USD_DECIMALS, + project_metadata.clone().token_information.decimals, + ).unwrap(); let evaluations = default_evaluations(); @@ -1406,9 +1443,9 @@ mod bid_extrinsic { (BIDDER_3, 200_000 * PLMC).into(), ]); inst.mint_foreign_asset_to(vec![ - (BIDDER_1, 200_000 * CT_UNIT).into(), - (BIDDER_2, 200_000 * CT_UNIT).into(), - (BIDDER_3, 200_000 * CT_UNIT).into(), + (BIDDER_1, 200_000 * USDT_UNIT).into(), + (BIDDER_2, 200_000 * USDT_UNIT).into(), + (BIDDER_3, 200_000 * USDT_UNIT).into(), ]); // First bucket is covered by one bidder @@ -1416,7 +1453,12 @@ mod bid_extrinsic { inst.bid_for_users(project_id, vec![big_bid.clone()]).unwrap(); // A bid at the min price of 1 should require a min of 8k CT, but with a new price of 1.1, we can now bid with less - let smallest_ct_amount_at_8k_usd = PriceOf::::from_float(1.1) + let bucket_increase_price = PriceProviderOf::::calculate_decimals_aware_price( + PriceOf::::from_float(1.1), + USD_DECIMALS, + project_metadata.clone().token_information.decimals, + ).unwrap(); + let smallest_ct_amount_at_8k_usd = bucket_increase_price .reciprocal() .unwrap() .checked_mul_int(8000 * USD_UNIT) @@ -1435,7 +1477,7 @@ mod bid_extrinsic { project_metadata.clone().policy_ipfs_cid.unwrap(), )); }); - let smallest_ct_amount_at_20k_usd = PriceOf::::from_float(1.1) + let smallest_ct_amount_at_20k_usd = bucket_increase_price .reciprocal() .unwrap() .checked_mul_int(20_000 * USD_UNIT)