Skip to content

Commit

Permalink
Simplifications part 5
Browse files Browse the repository at this point in the history
  • Loading branch information
JuaniRios committed Jul 26, 2024
1 parent 5d4ca77 commit ca95c55
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 73 deletions.
2 changes: 1 addition & 1 deletion pallets/funding/src/functions/1_application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<T: Config> Pallet<T> {
evaluation_round_info: EvaluationRoundInfoOf::<T> {
total_bonded_usd: Zero::zero(),
total_bonded_plmc: Zero::zero(),
evaluators_outcome: EvaluatorsOutcome::Unchanged,
evaluators_outcome: None,
},
usd_bid_on_oversubscription: None,
funding_end_block: None,
Expand Down
13 changes: 3 additions & 10 deletions pallets/funding/src/functions/5_funding_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,24 +55,17 @@ impl<T: Config> Pallet<T> {

// * Update Storage *
DidWithActiveProjects::<T>::set(issuer_did, None);
let evaluator_outcome = match funding_ratio {
ratio if ratio <= Perquintill::from_percent(75u64) => EvaluatorsOutcome::Slashed,
ratio if ratio < Perquintill::from_percent(90u64) => EvaluatorsOutcome::Unchanged,
_ => {
let reward_info = Self::generate_evaluator_rewards_info(project_id)?;
EvaluatorsOutcome::Rewarded(reward_info)
},
};

project_details.evaluation_round_info.evaluators_outcome = evaluator_outcome;

let (next_status, duration, actual_weight) = if funding_ratio <= T::FundingSuccessThreshold::get() {
project_details.evaluation_round_info.evaluators_outcome = Some(EvaluatorsOutcome::Slashed);
(
ProjectStatus::FundingFailed,
1u32.into(),
WeightInfoOf::<T>::end_funding_automatically_rejected_evaluators_slashed(1),
)
} else {
let reward_info = Self::generate_evaluator_rewards_info(project_id)?;
project_details.evaluation_round_info.evaluators_outcome = Some(EvaluatorsOutcome::Rewarded(reward_info));
(
ProjectStatus::FundingSuccessful,
T::SuccessToSettlementTime::get(),
Expand Down
42 changes: 20 additions & 22 deletions pallets/funding/src/functions/6_settlement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,35 +98,32 @@ impl<T: Config> Pallet<T> {
Error::<T>::FundingSuccessSettlementNotStarted
);

// Based on the results of the funding round, the evaluator is either:
// 1. Slashed
// 2. Rewarded with CT tokens
// 3. Not slashed or Rewarded.
let (bond, reward): (BalanceOf<T>, BalanceOf<T>) =
match project_details.evaluation_round_info.evaluators_outcome {
EvaluatorsOutcome::Slashed => (Self::slash_evaluator(project_id, &evaluation)?, Zero::zero()),
EvaluatorsOutcome::Rewarded(info) => Self::reward_evaluator(project_id, &evaluation, &info)?,
EvaluatorsOutcome::Unchanged => (evaluation.current_plmc_bond, Zero::zero()),
};
let Some(EvaluatorsOutcome::Rewarded(reward_info)) = project_details.evaluation_round_info.evaluators_outcome
else {
return Err(Error::<T>::ImpossibleState.into());
};

let (plmc_bond, ct_reward): (BalanceOf<T>, BalanceOf<T>) =
Self::reward_evaluator(project_id, &evaluation, &reward_info)?;

// Release the held PLMC bond
T::NativeCurrency::release(
&HoldReason::Evaluation(project_id).into(),
&evaluation.evaluator,
bond,
plmc_bond,
Precision::Exact,
)?;

// Create Migration
if reward > Zero::zero() {
if ct_reward > Zero::zero() {
let multiplier = MultiplierOf::<T>::try_from(1u8).map_err(|_| Error::<T>::BadMath)?;
let duration = multiplier.calculate_vesting_duration::<T>();
Self::create_migration(
project_id,
&evaluation.evaluator,
evaluation.id,
ParticipationType::Evaluation,
reward,
ct_reward,
duration,
)?;
}
Expand All @@ -136,8 +133,8 @@ impl<T: Config> Pallet<T> {
project_id,
account: evaluation.evaluator,
id: evaluation.id,
ct_amount: reward,
slashed_plmc_amount: evaluation.current_plmc_bond.saturating_sub(bond),
ct_amount: ct_reward,
slashed_plmc_amount: evaluation.current_plmc_bond.saturating_sub(plmc_bond),
});

Ok(())
Expand All @@ -150,17 +147,18 @@ impl<T: Config> Pallet<T> {
Error::<T>::FundingFailedSettlementNotStarted
);

let bond = if matches!(project_details.evaluation_round_info.evaluators_outcome, EvaluatorsOutcome::Slashed) {
Self::slash_evaluator(project_id, &evaluation)?
} else {
evaluation.current_plmc_bond
};
ensure!(
project_details.evaluation_round_info.evaluators_outcome == Some(EvaluatorsOutcome::Slashed),
Error::<T>::ImpossibleState
);

let plmc_bond_released = Self::slash_evaluator(project_id, &evaluation)?;

// Release the held PLMC bond
T::NativeCurrency::release(
&HoldReason::Evaluation(project_id).into(),
&evaluation.evaluator,
bond,
plmc_bond_released,
Precision::Exact,
)?;

Expand All @@ -171,7 +169,7 @@ impl<T: Config> Pallet<T> {
account: evaluation.evaluator,
id: evaluation.id,
ct_amount: Zero::zero(),
slashed_plmc_amount: evaluation.current_plmc_bond.saturating_sub(bond),
slashed_plmc_amount: evaluation.current_plmc_bond.saturating_sub(plmc_bond_released),
});

Ok(())
Expand Down
9 changes: 3 additions & 6 deletions pallets/funding/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,21 @@ sp_api::decl_runtime_apis! {
}

impl<T: Config> Pallet<T> {
pub fn top_evaluations
(project_id: ProjectId, amount: u32) -> Vec<EvaluationInfoOf<T>> {
pub fn top_evaluations(project_id: ProjectId, amount: u32) -> Vec<EvaluationInfoOf<T>> {
Evaluations::<T>::iter_prefix_values((project_id,))
.sorted_by(|a, b| b.original_plmc_bond.cmp(&a.original_plmc_bond))
.take(amount as usize)
.collect_vec()
}

pub fn top_bids
(project_id: ProjectId, amount: u32) -> Vec<BidInfoOf<T>> {
pub fn top_bids(project_id: ProjectId, amount: u32) -> Vec<BidInfoOf<T>> {
Bids::<T>::iter_prefix_values((project_id,))
.sorted_by(|a, b| b.final_ct_amount.cmp(&a.final_ct_amount))
.take(amount as usize)
.collect_vec()
}

pub fn top_contributions
(project_id: ProjectId, amount: u32) -> Vec<ContributionInfoOf<T>> {
pub fn top_contributions(project_id: ProjectId, amount: u32) -> Vec<ContributionInfoOf<T>> {
Contributions::<T>::iter_prefix_values((project_id,))
.sorted_by(|a, b| b.ct_amount.cmp(&a.ct_amount))
.take(amount as usize)
Expand Down
36 changes: 12 additions & 24 deletions pallets/funding/src/tests/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use super::*;
use crate::runtime_api::{ExtrinsicHelpers, Leaderboards, ProjectInformation, UserInformation};

#[test]
fn top_evaluations
() {
fn top_evaluations() {
let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext())));
let evaluations = vec![
UserToUSDBalance::new(EVALUATOR_1, 500_000 * USD_UNIT),
Expand All @@ -16,21 +15,18 @@ fn top_evaluations

inst.execute(|| {
let block_hash = System::block_hash(System::block_number());
let top_1 = TestRuntime::top_evaluations
(&TestRuntime, block_hash, project_id, 1).unwrap();
let top_1 = TestRuntime::top_evaluations(&TestRuntime, block_hash, project_id, 1).unwrap();
let evaluator_4_evaluation = Evaluations::<TestRuntime>::get((project_id, EVALUATOR_4, 3)).unwrap();
assert!(top_1.len() == 1 && top_1[0] == evaluator_4_evaluation);

let top_4_evaluators = TestRuntime::top_evaluations
(&TestRuntime, block_hash, project_id, 4)
let top_4_evaluators = TestRuntime::top_evaluations(&TestRuntime, block_hash, project_id, 4)
.unwrap()
.into_iter()
.map(|evaluation| evaluation.evaluator)
.collect_vec();
assert_eq!(top_4_evaluators, vec![EVALUATOR_4, EVALUATOR_1, EVALUATOR_3, EVALUATOR_2]);

let top_6_evaluators = TestRuntime::top_evaluations
(&TestRuntime, block_hash, project_id, 6)
let top_6_evaluators = TestRuntime::top_evaluations(&TestRuntime, block_hash, project_id, 6)
.unwrap()
.into_iter()
.map(|evaluation| evaluation.evaluator)
Expand All @@ -40,8 +36,7 @@ fn top_evaluations
}

#[test]
fn top_bids
() {
fn top_bids() {
let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext())));
let bids = vec![
(BIDDER_1, 8000 * CT_UNIT).into(),
Expand All @@ -60,21 +55,18 @@ fn top_bids

inst.execute(|| {
let block_hash = System::block_hash(System::block_number());
let top_1 = TestRuntime::top_bids
(&TestRuntime, block_hash, project_id, 1).unwrap();
let top_1 = TestRuntime::top_bids(&TestRuntime, block_hash, project_id, 1).unwrap();
let bidder_4_evaluation = Bids::<TestRuntime>::get((project_id, BIDDER_4, 3)).unwrap();
assert!(top_1.len() == 1 && top_1[0] == bidder_4_evaluation);

let top_4_bidders = TestRuntime::top_bids
(&TestRuntime, block_hash, project_id, 4)
let top_4_bidders = TestRuntime::top_bids(&TestRuntime, block_hash, project_id, 4)
.unwrap()
.into_iter()
.map(|evaluation| evaluation.bidder)
.collect_vec();
assert_eq!(top_4_bidders, vec![BIDDER_4, BIDDER_1, BIDDER_3, BIDDER_2]);

let top_6_bidders = TestRuntime::top_bids
(&TestRuntime, block_hash, project_id, 6)
let top_6_bidders = TestRuntime::top_bids(&TestRuntime, block_hash, project_id, 6)
.unwrap()
.into_iter()
.map(|evaluation| evaluation.bidder)
Expand All @@ -84,8 +76,7 @@ fn top_bids
}

#[test]
fn top_contributions
() {
fn top_contributions() {
let mut inst = MockInstantiator::new(Some(RefCell::new(new_test_ext())));
let community_contributors =
vec![(BUYER_1, 8000 * CT_UNIT).into(), (BUYER_2, 501 * CT_UNIT).into(), (BUYER_3, 1200 * CT_UNIT).into()];
Expand All @@ -102,21 +93,18 @@ fn top_contributions

inst.execute(|| {
let block_hash = System::block_hash(System::block_number());
let top_1 = TestRuntime::top_contributions
(&TestRuntime, block_hash, project_id, 1).unwrap();
let top_1 = TestRuntime::top_contributions(&TestRuntime, block_hash, project_id, 1).unwrap();
let contributor_4_evaluation = Contributions::<TestRuntime>::get((project_id, BUYER_4, 3)).unwrap();
assert!(top_1.len() == 1 && top_1[0] == contributor_4_evaluation);

let top_4_contributors = TestRuntime::top_contributions
(&TestRuntime, block_hash, project_id, 4)
let top_4_contributors = TestRuntime::top_contributions(&TestRuntime, block_hash, project_id, 4)
.unwrap()
.into_iter()
.map(|evaluation| evaluation.contributor)
.collect_vec();
assert_eq!(top_4_contributors, vec![BUYER_4, BUYER_1, BUYER_3, BUYER_2]);

let top_6_contributors = TestRuntime::top_contributions
(&TestRuntime, block_hash, project_id, 6)
let top_6_contributors = TestRuntime::top_contributions(&TestRuntime, block_hash, project_id, 6)
.unwrap()
.into_iter()
.map(|evaluation| evaluation.contributor)
Expand Down
11 changes: 1 addition & 10 deletions pallets/funding/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -791,32 +791,23 @@ pub mod inner_types {
/// An enum representing all possible outcomes for a project.
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum ProjectOutcome {
/// 90%+ of the funding target was reached, so the project is successful.
FundingSuccessful,
/// 33%- of the funding target was reached, so the project failed.
FundingFailed,
}

#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct EvaluationRoundInfo<Balance> {
pub total_bonded_usd: Balance,
pub total_bonded_plmc: Balance,
pub evaluators_outcome: EvaluatorsOutcome<Balance>,
pub evaluators_outcome: Option<EvaluatorsOutcome<Balance>>,
}

#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum EvaluatorsOutcome<Balance> {
Unchanged,
Rewarded(RewardInfo<Balance>),
Slashed,
}

#[derive(Clone, Copy, Encode, Decode, Eq, PartialEq, PartialOrd, Ord, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub enum RewardOrSlash<Balance> {
Reward(Balance),
Slash(Balance),
}

#[derive(Default, Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug, TypeInfo, MaxEncodedLen)]
pub struct RewardInfo<Balance> {
// Total "Early Evaluators" rewards amount in Contribution Tokens
Expand Down

0 comments on commit ca95c55

Please sign in to comment.