Skip to content

Commit

Permalink
fix funding asset ed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Oct 10, 2024
1 parent 85088f5 commit 5f44c41
Show file tree
Hide file tree
Showing 14 changed files with 494 additions and 439 deletions.
6 changes: 2 additions & 4 deletions integration-tests/src/tests/evaluator_slash_sideffects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ fn evaluator_slash_reduces_vesting_schedules() {
));

let alice_evaluation = UserToUSDBalance::<PolimecRuntime>::new(alice.clone(), 35_000 * USD_UNIT);
let alice_plmc_evaluated =
inst.calculate_evaluation_plmc_spent(vec![alice_evaluation.clone()], false)[0].plmc_amount;
let alice_plmc_evaluated = inst.calculate_evaluation_plmc_spent(vec![alice_evaluation.clone()])[0].plmc_amount;
let alice_slashed = slash_percent * alice_plmc_evaluated;

const BOB_EVALUATION: u128 = 60_000;
Expand All @@ -94,8 +93,7 @@ fn evaluator_slash_reduces_vesting_schedules() {
vesting_info_5
));
let bob_evaluation = UserToUSDBalance::<PolimecRuntime>::new(bob.clone(), BOB_EVALUATION * USD_UNIT);
let bob_plmc_evaluated =
inst.calculate_evaluation_plmc_spent(vec![bob_evaluation.clone()], false)[0].plmc_amount;
let bob_plmc_evaluated = inst.calculate_evaluation_plmc_spent(vec![bob_evaluation.clone()])[0].plmc_amount;
let bob_slashed = slash_percent * bob_plmc_evaluated;

// Set metadata so 50k USD succeeds the evaluation round
Expand Down
7 changes: 1 addition & 6 deletions integration-tests/src/tests/otm_edge_cases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,7 @@ fn after_otm_fee_user_goes_under_ed_reverts() {
pallet_funding::Error::<PolimecRuntime>::ParticipantNotEnoughFunds
);

inst.mint_funding_asset_to(vec![(
bobert.clone(),
usdt_ed,
AcceptedFundingAsset::USDT.id(),
)
.into()]);
inst.mint_funding_asset_to(vec![(bobert.clone(), usdt_ed, AcceptedFundingAsset::USDT.id()).into()]);

assert_ok!(PolimecFunding::contribute(
PolimecOrigin::signed(bobert.clone()),
Expand Down
50 changes: 25 additions & 25 deletions pallets/funding/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -503,9 +503,8 @@ mod benchmarks {
let extrinsic_evaluation = UserToUSDBalance::new(test_evaluator.clone(), (1_000 * USD_UNIT).into());
let existing_evaluations = vec![existing_evaluation; x as usize];

let plmc_for_existing_evaluations = inst.calculate_evaluation_plmc_spent(existing_evaluations.clone(), false);
let plmc_for_extrinsic_evaluation =
inst.calculate_evaluation_plmc_spent(vec![extrinsic_evaluation.clone()], false);
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<UserToPLMCBalance<T>> =
plmc_for_extrinsic_evaluation.accounts().existential_deposits();

Expand Down Expand Up @@ -609,8 +608,9 @@ mod benchmarks {
(Percent::from_percent(25) * evaluation_usd_target).into(),
),
];
let plmc_for_evaluating = inst.calculate_evaluation_plmc_spent(evaluations.clone(), true);
let plmc_for_evaluating = inst.calculate_evaluation_plmc_spent(evaluations.clone());

inst.mint_plmc_ed_if_required(plmc_for_evaluating.accounts());
inst.mint_plmc_to(plmc_for_evaluating);

inst.advance_time(One::one());
Expand Down Expand Up @@ -681,11 +681,8 @@ mod benchmarks {
&existing_bids,
project_metadata.clone(),
None,
false,
);

let existential_deposits: Vec<UserToPLMCBalance<T>> = vec![bidder.clone()].existential_deposits();

let usdt_for_existing_bids: Vec<UserToFundingAsset<T>> = inst
.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket(
&existing_bids,
Expand All @@ -694,10 +691,11 @@ mod benchmarks {
);
let escrow_account = Pallet::<T>::fund_account_id(project_id);
let prev_total_escrow_usdt_locked =
inst.get_free_funding_asset_balances_for(usdt_id(), vec![escrow_account.clone()]);
inst.get_free_funding_asset_balances_for(vec![(escrow_account.clone(), usdt_id())]);

inst.mint_plmc_ed_if_required(plmc_for_existing_bids.accounts());
inst.mint_plmc_to(plmc_for_existing_bids.clone());
inst.mint_plmc_to(existential_deposits.clone());
inst.mint_funding_asset_ed_if_required(usdt_for_existing_bids.to_account_asset_map());
inst.mint_funding_asset_to(usdt_for_existing_bids.clone());

// do "x" contributions for this user
Expand All @@ -716,7 +714,7 @@ mod benchmarks {
// first lets bring the bucket to almost its limit with another bidder:
assert!(new_bidder.clone() != bidder.clone());
let bid_params = BidParams::new(
new_bidder,
new_bidder.clone(),
current_bucket.amount_left,
ParticipationMode::Classic(1u8),
AcceptedFundingAsset::USDT,
Expand All @@ -725,16 +723,16 @@ mod benchmarks {
let plmc_for_new_bidder = inst.calculate_auction_plmc_charged_with_given_price(
&vec![bid_params.clone()],
current_bucket.current_price,
false,
);
let plmc_ed = plmc_for_new_bidder.accounts().existential_deposits();
let usdt_for_new_bidder = inst.calculate_auction_funding_asset_charged_with_given_price(
&vec![bid_params.clone()],
current_bucket.current_price,
);

inst.mint_plmc_ed_if_required(vec![(new_bidder.clone())]);
inst.mint_plmc_to(plmc_for_new_bidder);
inst.mint_plmc_to(plmc_ed);

inst.mint_funding_asset_ed_if_required(vec![(new_bidder, AcceptedFundingAsset::USDT.id())]);
inst.mint_funding_asset_to(usdt_for_new_bidder.clone());

inst.bid_for_users(project_id, vec![bid_params]).unwrap();
Expand Down Expand Up @@ -763,21 +761,22 @@ mod benchmarks {
&vec![extrinsic_bid.clone()],
project_metadata.clone(),
Some(current_bucket),
false,
);
let usdt_for_extrinsic_bids: Vec<UserToFundingAsset<T>> = inst
.calculate_auction_funding_asset_charged_from_all_bids_made_or_with_bucket(
&vec![extrinsic_bid],
project_metadata.clone(),
Some(current_bucket),
);
inst.mint_plmc_ed_if_required(plmc_for_extrinsic_bids.accounts());
inst.mint_plmc_to(plmc_for_extrinsic_bids.clone());
inst.mint_funding_asset_ed_if_required(usdt_for_extrinsic_bids.to_account_asset_map());
inst.mint_funding_asset_to(usdt_for_extrinsic_bids.clone());

let total_free_plmc = existential_deposits[0].plmc_amount;
let total_free_plmc = inst.get_ed();
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_free_usdt = inst.get_funding_asset_ed(AcceptedFundingAsset::USDT.id());
let total_escrow_usdt_locked = inst.sum_funding_asset_mappings(vec![
prev_total_escrow_usdt_locked.clone(),
usdt_for_extrinsic_bids.clone(),
Expand Down Expand Up @@ -861,11 +860,10 @@ mod benchmarks {
assert_eq!(free_plmc, total_free_plmc);

let escrow_account = Pallet::<T>::fund_account_id(project_id);
let locked_usdt =
inst.get_free_funding_asset_balances_for(usdt_id(), vec![escrow_account.clone()])[0].asset_amount;
let locked_usdt = inst.get_free_funding_asset_balance_for(usdt_id(), escrow_account.clone());
assert_eq!(locked_usdt, total_escrow_usdt_locked);

let free_usdt = inst.get_free_funding_asset_balances_for(usdt_id(), vec![bidder])[0].asset_amount;
let free_usdt = inst.get_free_funding_asset_balance_for(usdt_id(), bidder);
assert_eq!(free_usdt, total_free_usdt);

// Events
Expand Down Expand Up @@ -978,16 +976,16 @@ mod benchmarks {
&all_bids,
project_metadata.clone(),
None,
true,
);
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,
);

inst.mint_plmc_ed_if_required(plmc_needed_for_bids.accounts());
inst.mint_plmc_to(plmc_needed_for_bids);
inst.mint_funding_asset_ed_if_required(funding_asset_needed_for_bids.to_account_asset_map());
inst.mint_funding_asset_to(funding_asset_needed_for_bids);

inst.bid_for_users(project_id, all_bids).unwrap();
Expand Down Expand Up @@ -1063,14 +1061,16 @@ mod benchmarks {
x as usize + 1
];

let plmc = inst.calculate_contributed_plmc_spent(contributions.clone(), price, false);
let plmc = inst.calculate_contributed_plmc_spent(contributions.clone(), price);
let usdt = inst.calculate_contributed_funding_asset_spent(contributions.clone(), price);

let escrow_account = Pallet::<T>::fund_account_id(project_id);
let prev_total_usdt_locked = inst.get_free_funding_asset_balances_for(usdt_id(), vec![escrow_account.clone()]);
let prev_total_usdt_locked =
inst.get_free_funding_asset_balances_for(vec![(escrow_account.clone(), usdt_id())]);

inst.mint_plmc_ed_if_required(plmc.accounts());
inst.mint_plmc_to(plmc.clone());
inst.mint_plmc_to(plmc.accounts().existential_deposits());
inst.mint_funding_asset_ed_if_required(usdt.to_account_asset_map());
inst.mint_funding_asset_to(usdt.clone());

// do "x" contributions for this user
Expand All @@ -1080,7 +1080,7 @@ mod benchmarks {
let total_usdt_locked = inst.sum_funding_asset_mappings(vec![prev_total_usdt_locked, usdt.clone()])[0].1;

let total_free_plmc = inst.get_ed();
let total_free_usdt = Zero::zero();
let total_free_usdt = inst.get_funding_asset_ed(AcceptedFundingAsset::USDT.id());

let jwt = get_mock_jwt_with_cid(
contributor.clone(),
Expand Down
35 changes: 9 additions & 26 deletions pallets/funding/src/instantiator/calculations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ impl<
pub fn calculate_evaluation_plmc_spent(
&mut self,
evaluations: Vec<UserToUSDBalance<T>>,
with_ed: bool,
) -> Vec<UserToPLMCBalance<T>> {
let plmc_usd_price = self.execute(|| {
<PriceProviderOf<T>>::get_decimals_aware_price(PLMC_FOREIGN_ID, USD_DECIMALS, PLMC_DECIMALS).unwrap()
Expand All @@ -42,10 +41,8 @@ impl<
let mut output = Vec::new();
for eval in evaluations {
let usd_bond = eval.usd_amount;
let mut plmc_bond = plmc_usd_price.reciprocal().unwrap().saturating_mul_int(usd_bond);
if with_ed {
plmc_bond = plmc_bond.saturating_add(self.get_ed());
}
let plmc_bond = plmc_usd_price.reciprocal().unwrap().saturating_mul_int(usd_bond);

output.push(UserToPLMCBalance::new(eval.account, plmc_bond));
}
output
Expand Down Expand Up @@ -83,7 +80,6 @@ impl<
&mut self,
bids: &Vec<BidParams<T>>,
ct_price: PriceOf<T>,
with_ed: bool,
) -> Vec<UserToPLMCBalance<T>> {
let mut output = Vec::new();
for bid in bids {
Expand All @@ -92,9 +88,7 @@ impl<
if let ParticipationMode::Classic(multiplier) = bid.mode {
self.add_required_plmc_to(&mut plmc_required, usd_ticket_size, multiplier)
}
if with_ed {
plmc_required = plmc_required.saturating_add(self.get_ed());
}

output.push(UserToPLMCBalance::new(bid.bidder.clone(), plmc_required));
}
output
Expand All @@ -106,7 +100,6 @@ impl<
bids: &Vec<BidParams<T>>,
project_metadata: ProjectMetadataOf<T>,
maybe_bucket: Option<BucketOf<T>>,
with_ed: bool,
) -> Vec<UserToPLMCBalance<T>> {
let mut output = Vec::new();

Expand All @@ -116,9 +109,7 @@ impl<
if let ParticipationMode::Classic(multiplier) = bid.mode {
self.add_required_plmc_to(&mut plmc_required, usd_ticket_size, multiplier)
}
if with_ed {
plmc_required = plmc_required.saturating_add(self.get_ed());
}

output.push(UserToPLMCBalance::<T>::new(bid.bidder.clone(), plmc_required));
}

Expand Down Expand Up @@ -184,12 +175,8 @@ impl<
project_metadata: ProjectMetadataOf<T>,
weighted_average_price: PriceOf<T>,
) -> Vec<UserToPLMCBalance<T>> {
let plmc_charged = self.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket(
bids,
project_metadata.clone(),
None,
false,
);
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(),
Expand Down Expand Up @@ -341,7 +328,6 @@ impl<
&mut self,
contributions: Vec<ContributionParams<T>>,
token_usd_price: PriceOf<T>,
with_ed: bool,
) -> Vec<UserToPLMCBalance<T>> {
let mut output = Vec::new();
for cont in contributions {
Expand All @@ -351,9 +337,6 @@ impl<
self.add_required_plmc_to(&mut plmc_bond, usd_ticket_size, multiplier);
}

if with_ed {
plmc_bond = plmc_bond.saturating_add(self.get_ed());
}
output.push(UserToPLMCBalance::new(cont.contributor, plmc_bond));
}
output
Expand All @@ -367,10 +350,9 @@ impl<
slashed: bool,
with_ed: bool,
) -> Vec<UserToPLMCBalance<T>> {
let evaluation_locked_plmc_amounts = self.calculate_evaluation_plmc_spent(evaluations, false);
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, false);
let theoretical_contribution_locked_plmc_amounts = self.calculate_contributed_plmc_spent(contributions, price);

let slash_percentage = <T as Config>::EvaluatorSlash::get();
let slashable_min_deposits = evaluation_locked_plmc_amounts
Expand Down Expand Up @@ -420,6 +402,7 @@ impl<
if cont.mode == ParticipationMode::OTM {
self.add_otm_fee_to(&mut funding_asset_spent, usd_ticket_size, cont.asset);
}

output.push(UserToFundingAsset::new(cont.contributor, funding_asset_spent, cont.asset.id()));
}
output
Expand Down
Loading

0 comments on commit 5f44c41

Please sign in to comment.