Skip to content

Commit

Permalink
test different ct decimals on evaluating
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Apr 26, 2024
1 parent 71c42d4 commit 41dfe8a
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 14 deletions.
152 changes: 152 additions & 0 deletions pallets/funding/src/tests/2_evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,158 @@ mod round_flow {
let project_status = inst.get_project_details(project_id).status;
assert_eq!(project_status, ProjectStatus::FundingFailed);
}

#[test]
fn different_decimals_ct_still_works_as_expected() {
let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext())));
let ed = MockInstantiator::get_ed();
let default_project_metadata = default_project_metadata(ISSUER_1);
let original_decimal_aware_price = default_project_metadata.minimum_price;
let original_price = <TestRuntime as Config>::PriceProvider::convert_back_to_normal_price(
original_decimal_aware_price,
USD_DECIMALS,
default_project_metadata.token_information.decimals,
)
.unwrap();

// Right PLMC conversion is stored. We move comma 4 places to the left since PLMC has 4 more decimals than USD.
let stored_plmc_price =
inst.execute(|| <TestRuntime as Config>::PriceProvider::get_price(PLMC_FOREIGN_ID).unwrap());
assert_eq!(stored_plmc_price, FixedU128::from_float(8.4));
let usable_plmc_price = inst.execute(|| {
<TestRuntime as Config>::PriceProvider::get_decimals_aware_price(
PLMC_FOREIGN_ID,
USD_DECIMALS,
PLMC_DECIMALS,
)
.unwrap()
});
assert_eq!(usable_plmc_price, FixedU128::from_float(0.00084));

let evaluation_threshold = inst.execute(|| <TestRuntime as Config>::EvaluationSuccessThreshold::get());

let min_evaluation_amount_usd = <TestRuntime as Config>::MinUsdPerEvaluation::get();
let min_evaluation_amount_plmc =
usable_plmc_price.reciprocal().unwrap().checked_mul_int(min_evaluation_amount_usd).unwrap();

// Check the evaluation threshold is calculated correctly for 2 decimals less than USD
let mut project_metadata_4_decimals = default_project_metadata.clone();
project_metadata_4_decimals.token_information.decimals = 4;
project_metadata_4_decimals.minimum_price =
<TestRuntime as Config>::PriceProvider::calculate_decimals_aware_price(
original_price,
USD_DECIMALS,
project_metadata_4_decimals.token_information.decimals,
)
.unwrap();
// 10MM funding target
project_metadata_4_decimals.total_allocation_size = 1_000_000 * 10u128.pow(4u32);
project_metadata_4_decimals.mainnet_token_max_supply = project_metadata_4_decimals.total_allocation_size;

assert_eq!(project_metadata_4_decimals.minimum_price, FixedU128::from_float(1000.0));
let project_id_4_decimals = inst.create_evaluating_project(project_metadata_4_decimals.clone(), ISSUER_1);

let evaluation_threshold_ct_4 = evaluation_threshold * project_metadata_4_decimals.total_allocation_size;
let evaluation_threshold_usd_4 =
project_metadata_4_decimals.minimum_price.saturating_mul_int(evaluation_threshold_ct_4);
let evaluation_threshold_plmc_4 =
usable_plmc_price.reciprocal().unwrap().checked_mul_int(evaluation_threshold_usd_4).unwrap();

// Check a minimum evaluation goes through
inst.mint_plmc_to(vec![UserToPLMCBalance::new(EVALUATOR_1, min_evaluation_amount_plmc + ed)]);
assert_ok!(inst.execute(|| PolimecFunding::evaluate(
RuntimeOrigin::signed(EVALUATOR_1),
get_mock_jwt_with_cid(
EVALUATOR_1,
InvestorType::Retail,
generate_did_from_account(EVALUATOR_1),
project_metadata_4_decimals.clone().policy_ipfs_cid.unwrap()
),
project_id_4_decimals,
min_evaluation_amount_usd
)));

// Check that once we bonded the minimum amount, evaluation succeeds
inst.mint_plmc_to(vec![UserToPLMCBalance::new(
EVALUATOR_2,
evaluation_threshold_plmc_4 + ed - min_evaluation_amount_plmc,
)]);
assert_ok!(inst.execute(|| PolimecFunding::evaluate(
RuntimeOrigin::signed(EVALUATOR_2),
get_mock_jwt_with_cid(
EVALUATOR_2,
InvestorType::Retail,
generate_did_from_account(EVALUATOR_2),
project_metadata_4_decimals.clone().policy_ipfs_cid.unwrap()
),
project_id_4_decimals,
evaluation_threshold_usd_4 - min_evaluation_amount_usd
)));

inst.start_auction(project_id_4_decimals, ISSUER_1).unwrap();


// Check the evaluation threshold is calculated correctly for 12 decimals more than USD
let mut project_metadata_18_decimals = default_project_metadata.clone();
project_metadata_18_decimals.token_information.decimals = 18;
project_metadata_18_decimals.minimum_price =
<TestRuntime as Config>::PriceProvider::calculate_decimals_aware_price(
original_price,
USD_DECIMALS,
project_metadata_18_decimals.token_information.decimals,
)
.unwrap();
// 10MM funding target
project_metadata_18_decimals.total_allocation_size = 1_000_000 * 10u128.pow(18u32);
project_metadata_18_decimals.mainnet_token_max_supply = project_metadata_18_decimals.total_allocation_size;

assert_eq!(project_metadata_18_decimals.minimum_price, FixedU128::from_float(0.00000000001));
let project_id_18_decimals = inst.create_evaluating_project(project_metadata_18_decimals.clone(), ISSUER_2);

let evaluation_threshold_ct_18 = evaluation_threshold * project_metadata_18_decimals.total_allocation_size;
let evaluation_threshold_usd_18 =
project_metadata_18_decimals.minimum_price.saturating_mul_int(evaluation_threshold_ct_18);
let evaluation_threshold_plmc_18 =
usable_plmc_price.reciprocal().unwrap().checked_mul_int(evaluation_threshold_usd_18).unwrap();

// Check a minimum evaluation goes through
inst.mint_plmc_to(vec![UserToPLMCBalance::new(EVALUATOR_1, min_evaluation_amount_plmc + ed)]);
assert_ok!(inst.execute(|| PolimecFunding::evaluate(
RuntimeOrigin::signed(EVALUATOR_1),
get_mock_jwt_with_cid(
EVALUATOR_1,
InvestorType::Retail,
generate_did_from_account(EVALUATOR_1),
project_metadata_18_decimals.clone().policy_ipfs_cid.unwrap()
),
project_id_18_decimals,
min_evaluation_amount_usd
)));

// Check that once we bonded the minimum amount, evaluation succeeds
inst.mint_plmc_to(vec![UserToPLMCBalance::new(
EVALUATOR_2,
evaluation_threshold_plmc_18 + ed - min_evaluation_amount_plmc,
)]);
assert_ok!(inst.execute(|| PolimecFunding::evaluate(
RuntimeOrigin::signed(EVALUATOR_2),
get_mock_jwt_with_cid(
EVALUATOR_2,
InvestorType::Retail,
generate_did_from_account(EVALUATOR_2),
project_metadata_18_decimals.clone().policy_ipfs_cid.unwrap()
),
project_id_18_decimals,
evaluation_threshold_usd_18 - min_evaluation_amount_usd
)));

inst.start_auction(project_id_18_decimals, ISSUER_2).unwrap();

// Even though both projects have different decimals, they both should have the same usd and plmc evaluation
// thresholds since all used the base price of 10
assert_eq!(evaluation_threshold_usd_4, evaluation_threshold_usd_18);
assert_eq!(evaluation_threshold_plmc_4, evaluation_threshold_plmc_18);
}
}

#[cfg(test)]
Expand Down
38 changes: 26 additions & 12 deletions pallets/funding/src/tests/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,22 +31,36 @@ mod helper_functions {
// Test with an asset with less decimals than USD
let original_price = FixedU128::from_float(1.85);
let asset_decimals = 4;
let decimal_aware = <TestRuntime as Config>::PriceProvider::calculate_decimals_aware_price(original_price, USD_DECIMALS, asset_decimals).unwrap();
let converted_back = <TestRuntime as Config>::PriceProvider::convert_back_to_normal_price(decimal_aware, USD_DECIMALS, asset_decimals).unwrap();
assert_eq!(
converted_back,
original_price
);
let decimal_aware = <TestRuntime as Config>::PriceProvider::calculate_decimals_aware_price(
original_price,
USD_DECIMALS,
asset_decimals,
)
.unwrap();
let converted_back = <TestRuntime as Config>::PriceProvider::convert_back_to_normal_price(
decimal_aware,
USD_DECIMALS,
asset_decimals,
)
.unwrap();
assert_eq!(converted_back, original_price);

// Test with an asset with more decimals than USD
let original_price = FixedU128::from_float(1.85);
let asset_decimals = 12;
let decimal_aware = <TestRuntime as Config>::PriceProvider::calculate_decimals_aware_price(original_price, USD_DECIMALS, asset_decimals).unwrap();
let converted_back = <TestRuntime as Config>::PriceProvider::convert_back_to_normal_price(decimal_aware, USD_DECIMALS, asset_decimals).unwrap();
assert_eq!(
converted_back,
original_price
);
let decimal_aware = <TestRuntime as Config>::PriceProvider::calculate_decimals_aware_price(
original_price,
USD_DECIMALS,
asset_decimals,
)
.unwrap();
let converted_back = <TestRuntime as Config>::PriceProvider::convert_back_to_normal_price(
decimal_aware,
USD_DECIMALS,
asset_decimals,
)
.unwrap();
assert_eq!(converted_back, original_price);
}

#[test]
Expand Down
6 changes: 4 additions & 2 deletions pallets/funding/src/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
use crate::{BalanceOf, Config, ProjectId};
use frame_support::weights::Weight;
use frame_system::pallet_prelude::BlockNumberFor;
use sp_arithmetic::FixedPointNumber;
use sp_arithmetic::traits::{CheckedDiv, CheckedMul};
use sp_arithmetic::{
traits::{CheckedDiv, CheckedMul},
FixedPointNumber,
};
use sp_runtime::DispatchError;

pub trait BondingRequirementCalculation {
Expand Down

0 comments on commit 41dfe8a

Please sign in to comment.