Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Builder pattern for Prices
Browse files Browse the repository at this point in the history
lrazovic committed Sep 10, 2024

Verified

This commit was signed with the committer’s verified signature.
lrazovic Leonardo Razovic
1 parent b039a60 commit 74624f3
Showing 4 changed files with 86 additions and 40 deletions.
84 changes: 68 additions & 16 deletions integration-tests/src/constants.rs
Original file line number Diff line number Diff line change
@@ -64,6 +64,69 @@ fn get_from_seed<TPublic: Public>(seed: &str) -> <TPublic::Pair as Pair>::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<FixedU128>,
usdc: Option<FixedU128>,
usdt: Option<FixedU128>,
plmc: Option<FixedU128>,
}

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<FixedU128>,
usdc: Option<FixedU128>,
usdt: Option<FixedU128>,
plmc: Option<FixedU128>,
) {
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), <PolimecRuntime as orml_oracle::Config>::MaxFeedValues> =
vec![dot, usdc, usdt, plmc].try_into().expect("benchmarks can panic");
6 changes: 3 additions & 3 deletions integration-tests/src/tests/ct_migration.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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<AccountId>) {

#[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::<PolimecRuntime>::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);
16 changes: 8 additions & 8 deletions integration-tests/src/tests/e2e.rs
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

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);

20 changes: 7 additions & 13 deletions integration-tests/src/tests/evaluator_slash_sideffects.rs
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit 74624f3

Please sign in to comment.