Skip to content

Commit

Permalink
fix community tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Apr 30, 2024
1 parent 9b80e26 commit 9456760
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 19 deletions.
3 changes: 2 additions & 1 deletion pallets/funding/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ use frame_support::{
ensure,
pallet_prelude::*,
traits::{
fungible::{Mutate, MutateHold as FungibleMutateHold},
fungible::{
Mutate, MutateHold as FungibleMutateHold},
fungibles::{
metadata::{Inspect as MetadataInspect, Mutate as MetadataMutate},
Create, Inspect as FungibleInspect, Mutate as FungiblesMutate,
Expand Down
16 changes: 15 additions & 1 deletion pallets/funding/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,21 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
10,
),
],
metadata: vec![],
metadata: vec![
(
AcceptedFundingAsset::USDT.to_assethub_id(),
"USDT".as_bytes().to_vec(),
"USDT".as_bytes().to_vec(),
6,
),
(
AcceptedFundingAsset::USDC.to_assethub_id(),
"USDC".as_bytes().to_vec(),
"USDC".as_bytes().to_vec(),
6,
),
(AcceptedFundingAsset::DOT.to_assethub_id(), "DOT".as_bytes().to_vec(), "DOT".as_bytes().to_vec(), 10),
],
accounts: vec![],
},
..Default::default()
Expand Down
11 changes: 7 additions & 4 deletions pallets/funding/src/tests/3_auction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ mod round_flow {
#[cfg(test)]
mod success {
use super::*;
use frame_support::traits::fungibles::metadata::Inspect;
use sp_core::bounded_vec;
use std::ops::Not;

Expand Down Expand Up @@ -476,10 +477,12 @@ mod round_flow {

let usdt_fundings = accounts
.iter()
.map(|acc| UserToForeignAssets {
account: acc.clone(),
asset_amount: USDT_UNIT * 1_000_000,
asset_id: fundings.next().unwrap().to_assethub_id(),
.map(|acc| {
let accepted_asset = fundings.next().unwrap();
let asset_id = accepted_asset.to_assethub_id();
let asset_decimals = inst.execute(|| <TestRuntime as Config>::FundingCurrency::decimals(asset_id));
let asset_unit = 10u128.checked_pow(asset_decimals.into()).unwrap();
UserToForeignAssets { account: acc.clone(), asset_amount: asset_unit * 1_000_000, asset_id }
})
.collect_vec();
inst.mint_plmc_to(plmc_fundings);
Expand Down
35 changes: 22 additions & 13 deletions pallets/funding/src/tests/4_community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,19 +182,23 @@ mod community_contribute_extrinsic {
evaluations,
default_bids(),
);
let ct_price = inst.get_project_details(project_id).weighted_average_price.unwrap();
let plmc_price = <TestRuntime as Config>::PriceProvider::get_decimals_aware_price(
PLMC_FOREIGN_ID,
USD_DECIMALS,
PLMC_DECIMALS,
)
.unwrap();

let evaluation_bond =
let evaluation_plmc_bond =
inst.execute(|| Balances::balance_on_hold(&HoldReason::Evaluation(project_id).into(), &BOB));
let slashable_bond = <TestRuntime as Config>::EvaluatorSlash::get() * evaluation_bond;
let usable_bond = evaluation_bond - slashable_bond;
let slashable_plmc = <TestRuntime as Config>::EvaluatorSlash::get() * evaluation_plmc_bond;
let usable_plmc = evaluation_plmc_bond - slashable_plmc;

let plmc_price = <TestRuntime as Config>::PriceProvider::get_price(PLMC_FOREIGN_ID).unwrap();
let ct_price = inst.get_project_details(project_id).weighted_average_price.unwrap();
let usable_usd = plmc_price.checked_mul_int(usable_plmc).unwrap();
let slashable_usd = plmc_price.checked_mul_int(slashable_plmc).unwrap();

let usable_usd = plmc_price.saturating_mul_int(usable_bond);
let usable_ct = ct_price.reciprocal().unwrap().saturating_mul_int(usable_usd);

let slashable_usd = plmc_price.saturating_mul_int(slashable_bond);
let slashable_ct = ct_price.reciprocal().unwrap().saturating_mul_int(slashable_usd);

// Can't contribute with only the evaluation bond
Expand Down Expand Up @@ -274,10 +278,10 @@ mod community_contribute_extrinsic {

let project_id = inst.create_auctioning_project(default_project_metadata(ISSUER_2), ISSUER_2, evaluations);

let evaluation_bond =
let evaluation_plmc_bond =
inst.execute(|| Balances::balance_on_hold(&HoldReason::Evaluation(project_id).into(), &bob));
let slashable_bond = <TestRuntime as Config>::EvaluatorSlash::get() * evaluation_bond;
let usable_bond = evaluation_bond - slashable_bond;
let slashable_plmc_bond = <TestRuntime as Config>::EvaluatorSlash::get() * evaluation_plmc_bond;
let usable_plmc_bond = evaluation_plmc_bond - slashable_plmc_bond;

let bids_plmc = inst.calculate_auction_plmc_charged_from_all_bids_made_or_with_bucket(
&all_bids,
Expand Down Expand Up @@ -306,10 +310,15 @@ mod community_contribute_extrinsic {
inst.start_community_funding(project_id).unwrap();
assert_eq!(inst.get_project_details(project_id).status, ProjectStatus::CommunityRound);

let plmc_price = <TestRuntime as Config>::PriceProvider::get_price(PLMC_FOREIGN_ID).unwrap();
let plmc_price = <TestRuntime as Config>::PriceProvider::get_decimals_aware_price(
PLMC_FOREIGN_ID,
USD_DECIMALS,
PLMC_DECIMALS,
)
.unwrap();
let wap = inst.get_project_details(project_id).weighted_average_price.unwrap();

let usable_usd = plmc_price.saturating_mul_int(usable_bond);
let usable_usd = plmc_price.saturating_mul_int(usable_plmc_bond);
let usable_ct = wap.reciprocal().unwrap().saturating_mul_int(usable_usd);

let bob_contribution = (bob, 1337 * CT_UNIT).into();
Expand Down

0 comments on commit 9456760

Please sign in to comment.