Skip to content

Commit

Permalink
Fix existing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Jul 26, 2024
1 parent ca95c55 commit 1e58a5f
Show file tree
Hide file tree
Showing 22 changed files with 2,395 additions and 4,522 deletions.
41 changes: 15 additions & 26 deletions pallets/funding/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,16 +302,6 @@ pub fn fill_projects_to_update<T: Config>(
}
}

pub fn run_blocks_to_execute_next_transition<T: Config>(
project_id: ProjectId,
update_type: UpdateType,
inst: &mut BenchInstantiator<T>,
) {
let update_block = inst.get_update_block(project_id, &update_type).unwrap();
frame_system::Pallet::<T>::set_block_number(update_block - 1u32.into());
inst.advance_time(One::one()).unwrap();
}

#[benchmarks(
where
T: Config + frame_system::Config<RuntimeEvent = <T as Config>::RuntimeEvent> + pallet_balances::Config<Balance = BalanceOf<T>>,
Expand Down Expand Up @@ -877,7 +867,7 @@ mod benchmarks {
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 = inst.sum_foreign_mappings(vec![
let total_escrow_usdt_locked = inst.sum_foreign_asset_mappings(vec![
prev_total_escrow_usdt_locked.clone(),
usdt_for_extrinsic_bids.clone(),
usdt_for_existing_bids.clone(),
Expand Down Expand Up @@ -1146,7 +1136,7 @@ mod benchmarks {
plmc_for_existing_contributions.clone(),
plmc_for_extrinsic_contribution.clone(),
]);
let mut total_usdt_locked = inst.sum_foreign_mappings(vec![
let mut total_usdt_locked = inst.sum_foreign_asset_mappings(vec![
prev_total_usdt_locked,
usdt_for_existing_contributions.clone(),
usdt_for_extrinsic_contribution.clone(),
Expand Down Expand Up @@ -1432,8 +1422,7 @@ mod benchmarks {

// * validity checks *
// Storage
let maybe_transition =
inst.get_update_block(project_id, &UpdateType::ProjectDecision(FundingOutcomeDecision::AcceptFunding));
let maybe_transition = inst.go_to_next_state(project_id);
assert!(maybe_transition.is_some());
}

Expand Down Expand Up @@ -2088,7 +2077,7 @@ mod benchmarks {

inst.bid_for_users(project_id, accepted_bids).unwrap();

let transition_block = inst.get_update_block(project_id, &UpdateType::AuctionClosingStart).unwrap();
let transition_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(transition_block);
let auction_closing_end_block =
inst.get_project_details(project_id).phase_transition_points.auction_closing.end().unwrap();
Expand Down Expand Up @@ -2714,7 +2703,7 @@ mod benchmarks {
vec![],
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down Expand Up @@ -2764,7 +2753,7 @@ mod benchmarks {
vec![],
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down Expand Up @@ -2835,7 +2824,7 @@ mod benchmarks {
participant_contributions,
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down Expand Up @@ -2881,7 +2870,7 @@ mod benchmarks {
vec![],
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down Expand Up @@ -2940,7 +2929,7 @@ mod benchmarks {
vec![],
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down Expand Up @@ -3003,7 +2992,7 @@ mod benchmarks {
vec![],
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down Expand Up @@ -3084,7 +3073,7 @@ mod benchmarks {
vec![],
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down Expand Up @@ -3206,7 +3195,7 @@ mod benchmarks {
participant_contributions,
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down Expand Up @@ -3301,7 +3290,7 @@ mod benchmarks {
participant_contributions,
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down Expand Up @@ -3377,7 +3366,7 @@ mod benchmarks {
vec![],
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down Expand Up @@ -3427,7 +3416,7 @@ mod benchmarks {
vec![],
);

let settlement_block = inst.get_update_block(project_id, &UpdateType::StartSettlement).unwrap();
let settlement_block = inst.go_to_next_state(project_id).unwrap();
inst.jump_to_block(settlement_block);

inst.settle_project(project_id).unwrap();
Expand Down
10 changes: 6 additions & 4 deletions pallets/funding/src/functions/3_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<T: Config> Pallet<T> {
project_id,
project_details,
ProjectStatus::AuctionInitializePeriod,
ProjectStatus::Auction,
ProjectStatus::AuctionRound,
T::AuctionOpeningDuration::get(),
skip_round_end_check,
)
Expand All @@ -60,6 +60,8 @@ impl<T: Config> Pallet<T> {
project_metadata.auction_round_allocation_percentage * project_metadata.total_allocation_size,
weighted_token_price,
);
let updated_project_details =
ProjectsDetails::<T>::get(project_id).ok_or(Error::<T>::ProjectDetailsNotFound)?;

match calculation_result {
Err(e) => return Err(DispatchErrorWithPostInfo { post_info: ().into(), error: e }),
Expand All @@ -68,8 +70,8 @@ impl<T: Config> Pallet<T> {
// * Transition Round *
Self::transition_project(
project_id,
project_details,
ProjectStatus::Auction,
updated_project_details,
ProjectStatus::AuctionRound,
ProjectStatus::CommunityRound(now.saturating_add(T::CommunityFundingDuration::get())),
T::CommunityFundingDuration::get() + T::RemainderFundingDuration::get(),
false,
Expand Down Expand Up @@ -151,7 +153,7 @@ impl<T: Config> Pallet<T> {

ensure!(ct_amount > Zero::zero(), Error::<T>::TooLow);
ensure!(did != project_details.issuer_did, Error::<T>::ParticipationToOwnProject);
ensure!(matches!(project_details.status, ProjectStatus::Auction), Error::<T>::IncorrectRound);
ensure!(matches!(project_details.status, ProjectStatus::AuctionRound), Error::<T>::IncorrectRound);
ensure!(
project_metadata.participation_currencies.contains(&funding_asset),
Error::<T>::FundingAssetNotAccepted
Expand Down
1 change: 1 addition & 0 deletions pallets/funding/src/functions/5_funding_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ impl<T: Config> Pallet<T> {
let round_end = now.saturating_add(duration).saturating_sub(One::one());
project_details.round_duration.update(Some(now), Some(round_end));
project_details.status = next_status;
ProjectsDetails::<T>::insert(project_id, project_details);

Ok(PostDispatchInfo { actual_weight: Some(actual_weight), pays_fee: Pays::Yes })
}
Expand Down
2 changes: 2 additions & 0 deletions pallets/funding/src/functions/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ impl<T: Config> Pallet<T> {
if let Some(info) = maybe_info {
info.remaining_contribution_tokens.saturating_reduce(bid_token_amount_sum);
info.funding_amount_reached_usd.saturating_accrue(total_auction_allocation_usd);
info.weighted_average_price = Some(wap);

Ok(())
} else {
Err(Error::<T>::ProjectDetailsNotFound.into())
Expand Down
37 changes: 20 additions & 17 deletions pallets/funding/src/instantiator/calculations.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use itertools::GroupBy;
use polimec_common::USD_DECIMALS;

impl<
Expand Down Expand Up @@ -242,7 +243,6 @@ impl<
output.merge_accounts(MergeOperation::Add)
}

// 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
Expand Down Expand Up @@ -272,10 +272,9 @@ impl<
.unwrap()
});
let charged_usd_ticket_size = price_charged.saturating_mul_int(bid.amount);
let charged_usd_bond =
bid.multiplier.calculate_bonding_requirement::<T>(charged_usd_ticket_size).unwrap();

let charged_funding_asset =
funding_asset_usd_price.reciprocal().unwrap().saturating_mul_int(charged_usd_bond);
funding_asset_usd_price.reciprocal().unwrap().saturating_mul_int(charged_usd_ticket_size);

if remaining_cts <= Zero::zero() {
output.push(UserToForeignAssets::new(
Expand All @@ -293,10 +292,8 @@ impl<
if weighted_average_price > price_charged { price_charged } else { weighted_average_price };

let actual_usd_ticket_size = final_price.saturating_mul_int(bought_cts);
let actual_usd_bond =
bid.multiplier.calculate_bonding_requirement::<T>(actual_usd_ticket_size).unwrap();
let actual_funding_asset_spent =
funding_asset_usd_price.reciprocal().unwrap().saturating_mul_int(actual_usd_bond);
funding_asset_usd_price.reciprocal().unwrap().saturating_mul_int(actual_usd_ticket_size);

let returned_foreign_asset = charged_funding_asset - actual_funding_asset_spent;

Expand Down Expand Up @@ -508,14 +505,22 @@ impl<
output
}

pub fn sum_foreign_mappings(&self, mut mappings: Vec<Vec<UserToForeignAssets<T>>>) -> BalanceOf<T> {
let mut output = mappings
.swap_remove(0)
.into_iter()
.map(|user_to_asset| user_to_asset.asset_amount)
.fold(Zero::zero(), |a, b| a + b);
for map in mappings {
output += map.into_iter().map(|user_to_asset| user_to_asset.asset_amount).fold(Zero::zero(), |a, b| a + b);
pub fn sum_foreign_asset_mappings(
&self,
mut mappings: Vec<Vec<UserToForeignAssets<T>>>,
) -> Vec<(AssetIdOf<T>, BalanceOf<T>)> {
let flattened_list = mappings.into_iter().flatten().collect_vec();

let ordered_list = flattened_list.into_iter().sorted_by(|a, b| a.asset_id.cmp(&b.asset_id)).collect_vec();

let asset_lists: GroupBy<AssetIdOf<T>, _, fn(&UserToForeignAssets<T>) -> AssetIdOf<T>> =
ordered_list.into_iter().group_by(|item| item.asset_id);

let mut output = Vec::new();

for (asset_id, asset_list) in &asset_lists {
let sum = asset_list.fold(Zero::zero(), |acc, item| acc + item.asset_amount);
output.push((asset_id, sum));
}
output
}
Expand Down Expand Up @@ -705,7 +710,6 @@ impl<

// Fill remaining buckets till we pass by the wap
loop {
dbg!(&bucket.current_price);
let wap = self.dry_run_wap(bucket.clone(), auction_allocation);

if wap < target_wap {
Expand Down Expand Up @@ -740,7 +744,6 @@ impl<

new_wap = self.dry_run_wap(bucket.clone(), auction_allocation);
}
dbg!(&bucket);
bucket
}

Expand Down
Loading

0 comments on commit 1e58a5f

Please sign in to comment.