From 10a690749c4259ffc438e56878bffd01fcc1a974 Mon Sep 17 00:00:00 2001 From: Leonardo Razovic <4128940+lrazovic@users.noreply.github.com> Date: Tue, 10 Sep 2024 15:45:10 +0200 Subject: [PATCH] Builder pattern for Prices --- integration-tests/src/constants.rs | 84 +++++++++++++++---- integration-tests/src/tests/ct_migration.rs | 6 +- integration-tests/src/tests/e2e.rs | 16 ++-- .../src/tests/evaluator_slash_sideffects.rs | 20 ++--- 4 files changed, 86 insertions(+), 40 deletions(-) diff --git a/integration-tests/src/constants.rs b/integration-tests/src/constants.rs index fcf5dcd36..f57e21e64 100644 --- a/integration-tests/src/constants.rs +++ b/integration-tests/src/constants.rs @@ -64,6 +64,69 @@ fn get_from_seed(seed: &str) -> ::Public TPublic::Pair::from_string(&format!("//{}", seed), None).expect("static values are valid; qed").public() } +pub struct Prices { + pub dot: FixedU128, + pub usdc: FixedU128, + pub usdt: FixedU128, + pub plmc: FixedU128, +} + +// PricesBuilder for optional fields before building Prices +#[derive(Clone, Copy)] +pub struct PricesBuilder { + dot: Option, + usdc: Option, + usdt: Option, + plmc: Option, +} + +impl PricesBuilder { + // Initialize a new builder with None for each field + pub fn new() -> Self { + Self { dot: None, usdc: None, usdt: None, plmc: None } + } + + pub fn default() -> Prices { + Prices { + dot: FixedU128::from_rational(69, 1), + usdc: FixedU128::from_rational(1, 1), + usdt: FixedU128::from_rational(1, 1), + plmc: FixedU128::from_rational(840, 100), + } + } + + // Setters that take FixedU128 and return &mut self for chaining + pub fn dot(&mut self, price: FixedU128) -> &mut Self { + self.dot = Some(price); + self + } + + pub fn usdc(&mut self, price: FixedU128) -> &mut Self { + self.usdc = Some(price); + self + } + + pub fn usdt(&mut self, price: FixedU128) -> &mut Self { + self.usdt = Some(price); + self + } + + pub fn plmc(&mut self, price: FixedU128) -> &mut Self { + self.plmc = Some(price); + self + } + + // Build Prices using provided values or default values + pub fn build(self) -> Prices { + Prices { + dot: self.dot.unwrap_or(FixedU128::from_rational(69, 1)), // Default DOT price + usdc: self.usdc.unwrap_or(FixedU128::from_rational(1, 1)), // Default USDC price + usdt: self.usdt.unwrap_or(FixedU128::from_rational(1, 1)), // Default USDT price + plmc: self.plmc.unwrap_or(FixedU128::from_rational(840, 100)), // Default PLMC price + } + } +} + pub mod accounts { use super::*; pub const ALICE: &str = "Alice"; @@ -377,23 +440,12 @@ pub mod polimec { const GENESIS_PARACHAIN_BOND_RESERVE_PERCENT: Percent = Percent::from_percent(0); const GENESIS_NUM_SELECTED_CANDIDATES: u32 = 5; - #[allow(unused)] - pub fn set_prices( - dot: Option, - usdc: Option, - usdt: Option, - plmc: Option, - ) { + pub fn set_prices(prices: Prices) { PolimecNet::execute_with(|| { - let dot_price = dot.unwrap_or(FixedU128::from_rational(69, 1)); - let usdc_price = usdc.unwrap_or(FixedU128::from_rational(1, 1)); - let usdt_price = usdt.unwrap_or(FixedU128::from_rational(1, 1)); - let plmc_price = plmc.unwrap_or(FixedU128::from_rational(840, 100)); - - let dot = (AcceptedFundingAsset::DOT.id(), dot_price); - let usdc = (AcceptedFundingAsset::USDC.id(), usdc_price); - let usdt = (AcceptedFundingAsset::USDT.id(), usdt_price); - let plmc = (pallet_funding::PLMC_FOREIGN_ID, plmc_price); + let dot = (AcceptedFundingAsset::DOT.id(), prices.dot); + let usdc = (AcceptedFundingAsset::USDC.id(), prices.usdc); + let usdt = (AcceptedFundingAsset::USDT.id(), prices.usdt); + let plmc = (pallet_funding::PLMC_FOREIGN_ID, prices.plmc); let values: BoundedVec<(u32, FixedU128), ::MaxFeedValues> = vec![dot, usdc, usdt, plmc].try_into().expect("benchmarks can panic"); diff --git a/integration-tests/src/tests/ct_migration.rs b/integration-tests/src/tests/ct_migration.rs index 11640d48b..95d1e66c4 100644 --- a/integration-tests/src/tests/ct_migration.rs +++ b/integration-tests/src/tests/ct_migration.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::*; +use crate::{constants::PricesBuilder, *}; use frame_support::traits::{fungible::Mutate, fungibles::Inspect}; use itertools::Itertools; use pallet_funding::{assert_close_enough, types::*, ProjectId, WeightInfo}; @@ -190,7 +190,7 @@ fn create_settled_project() -> (ProjectId, Vec) { #[test] fn full_pallet_migration_test() { - polimec::set_prices(None, None, None, None); + polimec::set_prices(PricesBuilder::default()); let (project_id, participants) = create_settled_project(); let _project_status = PolimecNet::execute_with(|| pallet_funding::ProjectsDetails::::get(project_id).unwrap().status); @@ -296,7 +296,7 @@ fn create_project_with_unsettled_participation(participation_type: Participation #[test] fn cannot_start_pallet_migration_with_unsettled_participations() { - polimec::set_prices(None, None, None, None); + polimec::set_prices(PricesBuilder::default()); let tup_1 = create_project_with_unsettled_participation(ParticipationType::Evaluation); let tup_2 = create_project_with_unsettled_participation(ParticipationType::Bid); diff --git a/integration-tests/src/tests/e2e.rs b/integration-tests/src/tests/e2e.rs index 6ee1e4198..d77c229c6 100644 --- a/integration-tests/src/tests/e2e.rs +++ b/integration-tests/src/tests/e2e.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . -use crate::{tests::defaults::*, *}; +use crate::{constants::PricesBuilder, tests::defaults::*, *}; use frame_support::{ traits::{ fungible::Mutate, @@ -271,7 +271,7 @@ fn excel_ct_amounts() -> UserToCTBalance { #[test] fn evaluation_round_completed() { - polimec::set_prices(None, None, None, None); + polimec::set_prices(PricesBuilder::default()); let mut inst = IntegrationInstantiator::new(None); @@ -286,7 +286,7 @@ fn evaluation_round_completed() { #[test] fn auction_round_completed() { - polimec::set_prices(None, None, None, None); + polimec::set_prices(PricesBuilder::default()); let mut inst = IntegrationInstantiator::new(None); @@ -326,7 +326,7 @@ fn auction_round_completed() { #[test] fn community_round_completed() { - polimec::set_prices(None, None, None, None); + polimec::set_prices(PricesBuilder::default()); let mut inst = IntegrationInstantiator::new(None); @@ -353,7 +353,7 @@ fn community_round_completed() { #[test] fn remainder_round_completed() { - polimec::set_prices(None, None, None, None); + polimec::set_prices(PricesBuilder::default()); let mut inst = IntegrationInstantiator::new(None); @@ -386,7 +386,7 @@ fn remainder_round_completed() { #[test] fn funds_raised() { - polimec::set_prices(None, None, None, None); + polimec::set_prices(PricesBuilder::default()); let mut inst = IntegrationInstantiator::new(None); @@ -418,7 +418,7 @@ fn funds_raised() { #[test] fn ct_minted() { - polimec::set_prices(None, None, None, None); + polimec::set_prices(PricesBuilder::default()); let mut inst = IntegrationInstantiator::new(None); @@ -445,7 +445,7 @@ fn ct_minted() { #[test] fn ct_migrated() { - polimec::set_prices(None, None, None, None); + polimec::set_prices(PricesBuilder::default()); let mut inst = IntegrationInstantiator::new(None); diff --git a/integration-tests/src/tests/evaluator_slash_sideffects.rs b/integration-tests/src/tests/evaluator_slash_sideffects.rs index ab291160c..bf9633842 100644 --- a/integration-tests/src/tests/evaluator_slash_sideffects.rs +++ b/integration-tests/src/tests/evaluator_slash_sideffects.rs @@ -1,28 +1,22 @@ -use crate::{tests::defaults::*, *}; -use frame_support::{ - traits::{ - fungible::Mutate, - }, -}; +use crate::{constants::PricesBuilder, tests::defaults::*, *}; +use frame_support::traits::fungible::Mutate; use frame_system::{pallet_prelude::BlockNumberFor, Account}; use macros::generate_accounts; use pallet_balances::AccountData; use pallet_funding::*; use pallet_vesting::VestingInfo; -use polimec_common::{USD_UNIT}; +use polimec_common::USD_UNIT; use polimec_runtime::PLMC; use sp_arithmetic::Perquintill; -use sp_runtime::{ - FixedU128, - MultiAddress::Id, -}; +use sp_runtime::{FixedU128, MultiAddress::Id}; generate_accounts!(STASH, ALICE, BOB, CHARLIE, DAVE, ISSUER); #[test] fn evaluator_slash_reduces_vesting_schedules() { - // set plmc price to 1 usd - polimec::set_prices(None, None, None, Some(FixedU128::from_float(1.0))); + // Set PLMC price to 1 USD + let prices = PricesBuilder::new().plmc(FixedU128::from_float(1.0)).build(); + polimec::set_prices(prices); let mut inst = IntegrationInstantiator::new(None); let alice: PolimecAccountId = ALICE.into();